Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 8653803

Browse files
committed
sonatype wip
1 parent 6853954 commit 8653803

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

metrics-collector/src/index.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function main() {
6666

6767
const metricDate = new Date(metricDateStr);
6868
const initialLoadFromDate = initialLoadFrom
69-
? new Date(initialLoadFrom)
69+
? new Date(`${initialLoadFrom}T00:00:00.000Z`)
7070
: undefined;
7171

7272
const collectNpm = argv["collect-npm"];
@@ -132,10 +132,15 @@ async function initialLoad(
132132
!skipLastSavedState && (await getLastSavedState(metricName));
133133
const date = lastSavedState || initialLoadFromDate;
134134

135-
if (monthlyInterval) {
136-
// Change the date to the first day of the month
137-
date.setDate(0);
138-
}
135+
console.info(
136+
`Initial load from ${initialLoadFromDate} to ${initialLoadToDate} with date ${date}`
137+
);
138+
139+
// if (monthlyInterval) {
140+
// // Change the date to the first day of the month
141+
// date.setDate(0);
142+
// }
143+
// console.info(`Date after setting to first day of the month: ${date}`);
139144

140145
while (date <= initialLoadToDate) {
141146
const dateStr = date.toISOString().split("T")[0];

metrics-collector/src/post-metric.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fetchWithRetry } from "./utils";
2+
13
const metricsServiceAppUrl = process.env.METRICS_SERVICE_APP_HOST_URL;
24

35
interface Labels {
@@ -24,7 +26,7 @@ export const postMetric = async (
2426
timestamp: (timestamp || new Date()).toISOString(),
2527
};
2628

27-
const response = await fetch(`${metricsServiceAppUrl}/metrics`, {
29+
const response = await fetchWithRetry(`${metricsServiceAppUrl}/metrics`, {
2830
method: "POST",
2931
headers: {
3032
"Content-Type": "application/json",

metrics-collector/src/sonatype-metrics.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import { createObjectCsvWriter } from "csv-writer";
4-
import { readJsonFile, writeJsonFile } from "./utils";
4+
import { fetchWithRetry, readJsonFile, writeJsonFile } from "./utils";
55
import { postMetric } from "./post-metric";
66

77
// Define the group id to collect metrics for
@@ -23,17 +23,33 @@ export async function collectSonatypeMetrics(metricDate: string) {
2323
const artifacts = await getArtifacts(projectId, groupId);
2424

2525
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+
);
3044

3145
await postSonatypeMavenMetrics({
3246
artifact,
3347
metricDate: new Date(metricDate),
3448
rawDownloads: rawDownloads.total,
3549
uniqueIPs: uniqueIPs.total,
3650
});
51+
52+
await new Promise((resolve) => setTimeout(resolve, 5000)); // to avoid Sonatype rate limit
3753
}
3854
}
3955

@@ -49,14 +65,14 @@ async function postSonatypeMavenMetrics(metric: {
4965
};
5066

5167
await postMetric(
52-
"sonatype_central_stats_downloads",
68+
"sonatype_central_stats_downloads_last_month",
5369
metric.rawDownloads,
5470
labels,
5571
metric.metricDate
5672
);
5773

5874
await postMetric(
59-
"sonatype_central_stats_unique_ips_downloads",
75+
"sonatype_central_stats_unique_ips_downloads_last_month",
6076
metric.uniqueIPs,
6177
labels,
6278
metric.metricDate
@@ -132,11 +148,14 @@ const initAuth = () => {
132148

133149
async function getProjectId(groupId: string): Promise<string> {
134150
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+
);
140159

141160
const data = await response.json();
142161
const project = data.data.find((project: any) => project.name === groupId);
@@ -154,7 +173,7 @@ async function getArtifacts(
154173
groupId: string
155174
): Promise<string[]> {
156175
try {
157-
const response = await fetch(
176+
const response = await fetchWithRetry(
158177
`${sonatypeCentralStatsUrl}/coord/${projectId}?g=${groupId}`,
159178
{
160179
method: "GET",
@@ -186,7 +205,7 @@ async function getArtifactStats(
186205
);
187206

188207
try {
189-
const response = await fetch(
208+
const response = await fetchWithRetry(
190209
`${sonatypeCentralStatsUrl}/timeline?p=${projectId}&g=${groupId}&a=${artifactId}&t=${type}&from=${from}&nom=1`,
191210
{
192211
method: "GET",
@@ -258,6 +277,7 @@ function getLastMonthDate() {
258277

259278
// function to convert YYYY-MM-DD to YYYYMM
260279
function convertDateToLastYearMonth(date: string) {
280+
console.info(`Converting date ${date} to last year month`);
261281
const lastMonth = new Date(date);
262282
// reduce 1 month, JS will automatically adjust the year if needed
263283
lastMonth.setMonth(lastMonth.getMonth() - 1);

0 commit comments

Comments
 (0)