|
1 |
| -import { Stat, StatOutput, StatQueryParams } from '@tee/common' |
| 1 | +import { Stat, StatOutput, StatQueryParams, StatsPeriodicity } from './types' |
2 | 2 |
|
3 | 3 | export default class StatisticsFeatures {
|
4 | 4 | async fetchNorthStarStats(params: StatQueryParams): Promise<StatOutput> {
|
5 | 5 | const METABASE_URL = 'http://tee-metabase.osc-fr1.scalingo.io/public/question/6969b2ab-ec49-44a0-9db8-3e2f9afbcf29.json'
|
6 | 6 |
|
7 |
| - const periodicity = params.periodicity || 'month' |
8 |
| - |
9 |
| - const today = new Date() |
10 |
| - const since = params.since ? new Date(params.since) : new Date(today.getFullYear(), 0, 1) |
11 |
| - const to = params.to ? new Date(params.to) : new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1) |
12 |
| - |
13 | 7 | const response = await fetch(METABASE_URL)
|
14 | 8 | if (!response.ok) {
|
15 | 9 | throw new Error('Stats API: Failed to fetch Metabase data')
|
16 | 10 | }
|
17 | 11 |
|
18 |
| - const rawData: { week_start_date: string; total_2_3: number }[] = await response.json() |
19 |
| - const filtered = rawData |
20 |
| - .map((row) => ({ |
21 |
| - date: this.toLocalDateOnly(new Date(row.week_start_date)), // week_start_date is a metabase variable that is poorly named and that is simply the statistic date |
22 |
| - value: row.total_2_3 |
| 12 | + const data: { week_start_date: string; total_2_3: number }[] = await response.json() |
| 13 | + if (data.length === 0) { |
| 14 | + return { |
| 15 | + description: `Entreprises bénéficiaires`, |
| 16 | + stats: [] |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + const periodicity = params.periodicity || StatsPeriodicity.Month |
| 21 | + const today = new Date() |
| 22 | + const since = params.since ?? new Date(today.getFullYear(), 0, 1) |
| 23 | + const to = params.to ?? new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1) |
| 24 | + |
| 25 | + const filtered = data |
| 26 | + .map((datum) => ({ |
| 27 | + date: new Date(datum.week_start_date), // week_start_date is a metabase variable that is poorly named and that is simply the statistic date |
| 28 | + value: datum.total_2_3 |
23 | 29 | }))
|
24 |
| - .filter((row) => row.date >= since && row.date <= to) |
| 30 | + .filter((datum) => { |
| 31 | + return datum.date >= since && datum.date <= to |
| 32 | + }) |
25 | 33 |
|
26 |
| - const grouped: Record<string, { date: Date; value: number }> = {} |
| 34 | + const statsByPeriodicity: Record<string, Stat> = {} |
27 | 35 |
|
28 | 36 | filtered.forEach(({ date, value }) => {
|
29 |
| - const d = new Date(date) |
30 |
| - if (d < since || d > to) return |
31 |
| - |
32 | 37 | let keyStatDate = today
|
33 | 38 |
|
34 | 39 | switch (periodicity) {
|
35 |
| - case 'day': |
36 |
| - keyStatDate = new Date(d.getFullYear(), d.getMonth(), d.getDate()) |
| 40 | + case StatsPeriodicity.Day: |
| 41 | + // Set to the start of the day |
| 42 | + keyStatDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0)) |
37 | 43 | break
|
38 |
| - case 'week': { |
39 |
| - const weekStart = new Date(d.getFullYear(), d.getMonth(), d.getDate() - ((d.getDay() + 6) % 7)) |
40 |
| - keyStatDate = weekStart |
| 44 | + case StatsPeriodicity.Week: { |
| 45 | + // Set to the start of the week (Monday) |
| 46 | + keyStatDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate() - ((date.getDay() + 6) % 7), 0, 0, 0, 0)) |
41 | 47 | break
|
42 | 48 | }
|
43 |
| - case 'month': |
44 |
| - keyStatDate = new Date(d.getFullYear(), d.getMonth(), 1) |
| 49 | + case StatsPeriodicity.Month: |
| 50 | + // Set to the start of the month |
| 51 | + keyStatDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0)) |
45 | 52 | break
|
46 |
| - case 'year': |
47 |
| - keyStatDate = new Date(d.getFullYear(), 0, 1) |
| 53 | + case StatsPeriodicity.Year: |
| 54 | + // Set to the start of the year |
| 55 | + keyStatDate = new Date(Date.UTC(date.getFullYear(), 0, 1, 0, 0, 0, 0)) |
48 | 56 | break
|
49 | 57 | }
|
50 | 58 |
|
51 |
| - keyStatDate.setHours(keyStatDate.getHours() + 15) // To compensate for UTC hours and locales that can cause issues |
52 |
| - const key = keyStatDate.toISOString().slice(0, 10) |
53 |
| - if (!grouped[key]) { |
54 |
| - grouped[key] = { date: keyStatDate, value: 0 } |
| 59 | + const key = keyStatDate.toISOString() |
| 60 | + if (!statsByPeriodicity[key]) { |
| 61 | + statsByPeriodicity[key] = { date: keyStatDate.toISOString(), value: 0 } |
55 | 62 | }
|
56 |
| - grouped[key].value += value |
| 63 | + statsByPeriodicity[key].value += value |
57 | 64 | })
|
58 | 65 |
|
59 |
| - const stats: Stat[] = Object.values(grouped) |
60 |
| - .sort((a, b) => a.date.getTime() - b.date.getTime()) |
| 66 | + const stats: Stat[] = Object.values(statsByPeriodicity) |
| 67 | + .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) |
61 | 68 | .map((stat) => ({
|
62 | 69 | ...stat,
|
63 |
| - date: stat.date.toISOString().split('T')[0] |
| 70 | + date: stat.date.split('T')[0] |
64 | 71 | }))
|
65 | 72 |
|
66 | 73 | return {
|
|
0 commit comments