1
1
import * as fs from "fs" ;
2
2
import * as path from "path" ;
3
3
import { createObjectCsvWriter } from "csv-writer" ;
4
- import { readJsonFile , writeJsonFile } from "./utils" ;
4
+ import { fetchWithRetry , readJsonFile , writeJsonFile } from "./utils" ;
5
5
import { postMetric } from "./post-metric" ;
6
6
7
7
// Define the group id to collect metrics for
@@ -23,17 +23,33 @@ export async function collectSonatypeMetrics(metricDate: string) {
23
23
const artifacts = await getArtifacts ( projectId , groupId ) ;
24
24
25
25
for ( const artifact of artifacts ) {
26
- const [ rawDownloads , uniqueIPs ] = await Promise . all ( [
27
- getArtifactStats ( projectId , groupId , artifact , "raw" , metricDate ) ,
28
- getArtifactStats ( projectId , groupId , artifact , "ip" , metricDate ) ,
29
- ] ) ;
26
+ if ( ! [ "tbdex" , "web5" ] . find ( ( a ) => artifact . includes ( a ) ) ) {
27
+ continue ; // TODO: add parameterized filter
28
+ }
29
+
30
+ const rawDownloads = await getArtifactStats (
31
+ projectId ,
32
+ groupId ,
33
+ artifact ,
34
+ "raw" ,
35
+ metricDate
36
+ ) ;
37
+ const uniqueIPs = await getArtifactStats (
38
+ projectId ,
39
+ groupId ,
40
+ artifact ,
41
+ "ip" ,
42
+ metricDate
43
+ ) ;
30
44
31
45
await postSonatypeMavenMetrics ( {
32
46
artifact,
33
47
metricDate : new Date ( metricDate ) ,
34
48
rawDownloads : rawDownloads . total ,
35
49
uniqueIPs : uniqueIPs . total ,
36
50
} ) ;
51
+
52
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ; // to avoid Sonatype rate limit
37
53
}
38
54
}
39
55
@@ -49,14 +65,14 @@ async function postSonatypeMavenMetrics(metric: {
49
65
} ;
50
66
51
67
await postMetric (
52
- "sonatype_central_stats_downloads " ,
68
+ "sonatype_central_stats_downloads_last_month " ,
53
69
metric . rawDownloads ,
54
70
labels ,
55
71
metric . metricDate
56
72
) ;
57
73
58
74
await postMetric (
59
- "sonatype_central_stats_unique_ips_downloads " ,
75
+ "sonatype_central_stats_unique_ips_downloads_last_month " ,
60
76
metric . uniqueIPs ,
61
77
labels ,
62
78
metric . metricDate
@@ -132,11 +148,14 @@ const initAuth = () => {
132
148
133
149
async function getProjectId ( groupId : string ) : Promise < string > {
134
150
try {
135
- const response = await fetch ( `${ sonatypeCentralStatsUrl } /projects` , {
136
- method : "GET" ,
137
- credentials : "include" ,
138
- headers : requestHeaders ,
139
- } ) ;
151
+ const response = await fetchWithRetry (
152
+ `${ sonatypeCentralStatsUrl } /projects` ,
153
+ {
154
+ method : "GET" ,
155
+ credentials : "include" ,
156
+ headers : requestHeaders ,
157
+ }
158
+ ) ;
140
159
141
160
const data = await response . json ( ) ;
142
161
const project = data . data . find ( ( project : any ) => project . name === groupId ) ;
@@ -154,7 +173,7 @@ async function getArtifacts(
154
173
groupId : string
155
174
) : Promise < string [ ] > {
156
175
try {
157
- const response = await fetch (
176
+ const response = await fetchWithRetry (
158
177
`${ sonatypeCentralStatsUrl } /coord/${ projectId } ?g=${ groupId } ` ,
159
178
{
160
179
method : "GET" ,
@@ -186,7 +205,7 @@ async function getArtifactStats(
186
205
) ;
187
206
188
207
try {
189
- const response = await fetch (
208
+ const response = await fetchWithRetry (
190
209
`${ sonatypeCentralStatsUrl } /timeline?p=${ projectId } &g=${ groupId } &a=${ artifactId } &t=${ type } &from=${ from } &nom=1` ,
191
210
{
192
211
method : "GET" ,
@@ -258,6 +277,7 @@ function getLastMonthDate() {
258
277
259
278
// function to convert YYYY-MM-DD to YYYYMM
260
279
function convertDateToLastYearMonth ( date : string ) {
280
+ console . info ( `Converting date ${ date } to last year month` ) ;
261
281
const lastMonth = new Date ( date ) ;
262
282
// reduce 1 month, JS will automatically adjust the year if needed
263
283
lastMonth . setMonth ( lastMonth . getMonth ( ) - 1 ) ;
0 commit comments