@datagouv/components-next 1.0.2-dev.182 → 1.0.2-dev.183

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { g as Ke } from "./main-8mcMUsT7.js";
1
+ import { g as Ke } from "./main-OEOVgq_i.js";
2
2
  import We from "vue";
3
3
  function Fe($, K) {
4
4
  for (var V = 0; V < K.length; V++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datagouv/components-next",
3
- "version": "1.0.2-dev.182",
3
+ "version": "1.0.2-dev.183",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "engines": {
@@ -31,49 +31,73 @@ export type ReuseMetrics = {
31
31
  visitsTotal: number
32
32
  }
33
33
 
34
+ type MonthlyVisitsRow = { metric_month: string, monthly_visit: number }
35
+ type MonthlyDatasetRow = MonthlyVisitsRow & { monthly_download_resource: number }
36
+ type MonthlyOrganizationRow = {
37
+ metric_month: string
38
+ monthly_download_resource: number
39
+ monthly_visit_dataservice: number
40
+ monthly_visit_dataset: number
41
+ monthly_visit_reuse: number
42
+ }
43
+
44
+ type TotalVisitsRow = { visit: number }
45
+ type TotalDatasetRow = TotalVisitsRow & { download_resource: number }
46
+ type TotalOrganizationRow = {
47
+ download_resource: number
48
+ visit_dataservice: number
49
+ visit_dataset: number
50
+ visit_reuse: number
51
+ }
52
+
53
+ /**
54
+ * The metrics API is a separate service that answers over the network, so a call fails in
55
+ * three ways: the request itself, an error status, or a body that isn't the expected page
56
+ * of rows. Rejecting on the last two turns them into the same failure as the first, which
57
+ * callers already hand to `useAsyncData` — reading `page.data` straight would instead throw
58
+ * a `TypeError` a few lines later, naming a symptom rather than the service.
59
+ */
60
+ async function fetchMetrics<Row>(url: string): Promise<Array<Row>> {
61
+ const response = await fetch(url)
62
+ if (!response.ok) throw new Error(`The metrics API answered ${response.status} for ${url}`)
63
+
64
+ const page: { data?: Array<Row> } = await response.json()
65
+ if (!Array.isArray(page?.data)) throw new Error(`The metrics API answered an unexpected body for ${url}`)
66
+
67
+ return page.data
68
+ }
69
+
34
70
  /**
35
71
  * There is only one metrics API endpoint to get these 3 values.
36
72
  * The rest of the metrics aren't stored yet at the organization level
37
73
  */
38
74
  export async function getOrganizationMetrics(oid: string, metricsApi: string): Promise<OrganizationMetrics> {
39
75
  // Fetching last 12 months
40
- const response = await fetch(`${metricsApi}/api/organizations/data/?organization_id__exact=${oid}&metric_month__sort=desc&page_size=12`)
41
- const page = await response.json()
76
+ const rows = await fetchMetrics<MonthlyOrganizationRow>(`${metricsApi}/api/organizations/data/?organization_id__exact=${oid}&metric_month__sort=desc&page_size=12`)
42
77
 
43
78
  const dataservicesViews: Record<string, number> = {}
44
79
  const datasetsViews: Record<string, number> = {}
45
80
  const downloads: Record<string, number> = {}
46
81
  const reusesViews: Record<string, number> = {}
47
82
 
48
- for (const { metric_month, monthly_download_resource, monthly_visit_dataservice, monthly_visit_dataset, monthly_visit_reuse } of page.data) {
83
+ for (const { metric_month, monthly_download_resource, monthly_visit_dataservice, monthly_visit_dataset, monthly_visit_reuse } of rows) {
49
84
  dataservicesViews[metric_month] = monthly_visit_dataservice
50
85
  datasetsViews[metric_month] = monthly_visit_dataset
51
86
  downloads[metric_month] = monthly_download_resource
52
87
  reusesViews[metric_month] = monthly_visit_reuse
53
88
  }
54
89
  // Fetching totals
55
- const totalResponse = await fetch(`${metricsApi}/api/organizations_total/data/?organization_id__exact=${oid}`)
56
- const totalPage = await totalResponse.json()
57
-
58
- let dataservicesViewsTotal = 0
59
- let datasetsViewsTotal = 0
60
- let downloadsTotal = 0
61
- let reusesViewsTotal = 0
62
- if (page.data[0]) {
63
- dataservicesViewsTotal = totalPage.data[0].visit_dataservice
64
- datasetsViewsTotal = totalPage.data[0].visit_dataset
65
- downloadsTotal = totalPage.data[0].download_resource
66
- reusesViewsTotal = totalPage.data[0].visit_reuse
67
- }
90
+ const total = (await fetchMetrics<TotalOrganizationRow>(`${metricsApi}/api/organizations_total/data/?organization_id__exact=${oid}`))[0]
91
+
68
92
  return {
69
93
  downloads,
70
- downloadsTotal,
94
+ downloadsTotal: total?.download_resource ?? 0,
71
95
  reusesViews,
72
- reusesViewsTotal,
96
+ reusesViewsTotal: total?.visit_reuse ?? 0,
73
97
  dataservicesViews,
74
- dataservicesViewsTotal,
98
+ dataservicesViewsTotal: total?.visit_dataservice ?? 0,
75
99
  datasetsViews,
76
- datasetsViewsTotal,
100
+ datasetsViewsTotal: total?.visit_dataset ?? 0,
77
101
  }
78
102
  }
79
103
 
@@ -89,33 +113,24 @@ export function createOrganizationMetricsUrl(datasetsViews: Record<string, numbe
89
113
 
90
114
  export async function getDatasetMetrics(datasetId: string, metricsApi: string): Promise<DatasetMetrics> {
91
115
  // Fetching last 12 months
92
- const response = await fetch(`${metricsApi}/api/datasets/data/?dataset_id__exact=${datasetId}&metric_month__sort=desc&page_size=12`)
93
- const page = await response.json()
116
+ const rows = await fetchMetrics<MonthlyDatasetRow>(`${metricsApi}/api/datasets/data/?dataset_id__exact=${datasetId}&metric_month__sort=desc&page_size=12`)
94
117
 
95
118
  const visits: Record<string, number> = {}
96
119
  const downloads: Record<string, number> = {}
97
120
 
98
- for (const { metric_month, monthly_visit, monthly_download_resource } of page.data) {
121
+ for (const { metric_month, monthly_visit, monthly_download_resource } of rows) {
99
122
  visits[metric_month] = monthly_visit
100
123
  downloads[metric_month] = monthly_download_resource
101
124
  }
102
125
 
103
126
  // Fetching totals
104
- const totalResponse = await fetch(`${metricsApi}/api/datasets_total/data/?dataset_id__exact=${datasetId}`)
105
- const totalPage = await totalResponse.json()
106
-
107
- let visitsTotal = 0
108
- let downloadsTotal = 0
109
- if (totalPage.data[0]) {
110
- visitsTotal = totalPage.data[0].visit
111
- downloadsTotal = totalPage.data[0].download_resource
112
- }
127
+ const total = (await fetchMetrics<TotalDatasetRow>(`${metricsApi}/api/datasets_total/data/?dataset_id__exact=${datasetId}`))[0]
113
128
 
114
129
  return {
115
130
  visits,
116
- visitsTotal,
131
+ visitsTotal: total?.visit ?? 0,
117
132
  downloads,
118
- downloadsTotal,
133
+ downloadsTotal: total?.download_resource ?? 0,
119
134
  }
120
135
  }
121
136
 
@@ -151,52 +166,38 @@ export async function createDatasetsForOrganizationMetricsUrl(organizationId: st
151
166
 
152
167
  export async function getDataserviceMetrics(dataserviceId: string, metricsApi: string): Promise<DataserviceMetrics> {
153
168
  // Fetching last 12 months
154
- const response = await fetch(`${metricsApi}/api/dataservices/data/?dataservice_id__exact=${dataserviceId}&metric_month__sort=desc&page_size=12`)
155
- const page = await response.json()
169
+ const rows = await fetchMetrics<MonthlyVisitsRow>(`${metricsApi}/api/dataservices/data/?dataservice_id__exact=${dataserviceId}&metric_month__sort=desc&page_size=12`)
156
170
 
157
171
  const visits: Record<string, number> = {}
158
172
 
159
- for (const { metric_month, monthly_visit } of page.data) {
173
+ for (const { metric_month, monthly_visit } of rows) {
160
174
  visits[metric_month] = monthly_visit
161
175
  }
162
176
 
163
177
  // Fetching totals
164
- const totalResponse = await fetch(`${metricsApi}/api/dataservices_total/data/?dataservice_id__exact=${dataserviceId}`)
165
- const totalPage = await totalResponse.json()
166
-
167
- let visitsTotal = 0
168
- if (totalPage.data[0]) {
169
- visitsTotal = totalPage.data[0].visit
170
- }
178
+ const total = (await fetchMetrics<TotalVisitsRow>(`${metricsApi}/api/dataservices_total/data/?dataservice_id__exact=${dataserviceId}`))[0]
171
179
 
172
180
  return {
173
181
  visits,
174
- visitsTotal,
182
+ visitsTotal: total?.visit ?? 0,
175
183
  }
176
184
  }
177
185
 
178
186
  export async function getReuseMetrics(reuseId: string, metricsApi: string): Promise<ReuseMetrics> {
179
187
  // Fetching last 12 months
180
- const response = await fetch(`${metricsApi}/api/reuses/data/?reuse_id__exact=${reuseId}&metric_month__sort=desc&page_size=12`)
181
- const page = await response.json()
188
+ const rows = await fetchMetrics<MonthlyVisitsRow>(`${metricsApi}/api/reuses/data/?reuse_id__exact=${reuseId}&metric_month__sort=desc&page_size=12`)
182
189
 
183
190
  const visits: Record<string, number> = {}
184
191
 
185
- for (const { metric_month, monthly_visit } of page.data) {
192
+ for (const { metric_month, monthly_visit } of rows) {
186
193
  visits[metric_month] = monthly_visit
187
194
  }
188
195
 
189
196
  // Fetching totals
190
- const totalResponse = await fetch(`${metricsApi}/api/reuses_total/data/?reuse_id__exact=${reuseId}`)
191
- const totalPage = await totalResponse.json()
192
-
193
- let visitsTotal = 0
194
- if (totalPage.data[0]) {
195
- visitsTotal = totalPage.data[0].visit
196
- }
197
+ const total = (await fetchMetrics<TotalVisitsRow>(`${metricsApi}/api/reuses_total/data/?reuse_id__exact=${reuseId}`))[0]
197
198
 
198
199
  return {
199
200
  visits,
200
- visitsTotal,
201
+ visitsTotal: total?.visit ?? 0,
201
202
  }
202
203
  }