Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2eda50

Browse files
committedAug 30, 2024·
completes sonatype
1 parent 8653803 commit a2eda50

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed
 

‎metrics-collector/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@octokit/types": "^13.5.0",
1919
"@types/node": "^20.12.12",
2020
"csv-writer": "^1.6.0",
21+
"date-fns": "^3.6.0",
2122
"dotenv": "^16.4.5",
2223
"express": "^4.19.2",
2324
"faker": "^6.6.6",

‎metrics-collector/pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎metrics-collector/src/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ async function initialLoad(
136136
`Initial load from ${initialLoadFromDate} to ${initialLoadToDate} with date ${date}`
137137
);
138138

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}`);
144-
145139
while (date <= initialLoadToDate) {
146140
const dateStr = date.toISOString().split("T")[0];
147141
console.log(`\n\n>>> Collecting metric ${metricName} for date: ${dateStr}`);

‎metrics-collector/src/sonatype-metrics.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import { createObjectCsvWriter } from "csv-writer";
4+
import { parse, format, subMonths } from "date-fns";
45
import { fetchWithRetry, readJsonFile, writeJsonFile } from "./utils";
56
import { postMetric } from "./post-metric";
67

@@ -27,24 +28,28 @@ export async function collectSonatypeMetrics(metricDate: string) {
2728
continue; // TODO: add parameterized filter
2829
}
2930

31+
const reportPeriod = getLastMonthPeriod(metricDate);
32+
const reportPeriodWithoutHyphen = reportPeriod.replace("-", "");
33+
3034
const rawDownloads = await getArtifactStats(
3135
projectId,
3236
groupId,
3337
artifact,
3438
"raw",
35-
metricDate
39+
reportPeriodWithoutHyphen
3640
);
3741
const uniqueIPs = await getArtifactStats(
3842
projectId,
3943
groupId,
4044
artifact,
4145
"ip",
42-
metricDate
46+
reportPeriodWithoutHyphen
4347
);
4448

4549
await postSonatypeMavenMetrics({
4650
artifact,
4751
metricDate: new Date(metricDate),
52+
reportPeriod,
4853
rawDownloads: rawDownloads.total,
4954
uniqueIPs: uniqueIPs.total,
5055
});
@@ -56,12 +61,14 @@ export async function collectSonatypeMetrics(metricDate: string) {
5661
async function postSonatypeMavenMetrics(metric: {
5762
artifact: string;
5863
metricDate: Date;
64+
reportPeriod: string;
5965
rawDownloads: number;
6066
uniqueIPs: number;
6167
}) {
6268
console.info("posting sonatype metric", { metric });
6369
const labels = {
6470
artifact: metric.artifact,
71+
reportPeriod: metric.reportPeriod,
6572
};
6673

6774
await postMetric(
@@ -195,11 +202,9 @@ async function getArtifactStats(
195202
groupId: string,
196203
artifactId: string,
197204
type: string,
198-
fromDate?: string
205+
reportPeriod?: string
199206
): Promise<{ total: number }> {
200-
const from = fromDate
201-
? convertDateToLastYearMonth(fromDate)
202-
: getLastMonthDate();
207+
const from = reportPeriod ?? getLastMonthDate();
203208
console.info(
204209
`Fetching ${type} stats for artifact ${artifactId} from ${from}...`
205210
);
@@ -275,12 +280,8 @@ function getLastMonthDate() {
275280
return `${lastMonthYear}${String(lastMonth).padStart(2, "0")}`;
276281
}
277282

278-
// function to convert YYYY-MM-DD to YYYYMM
279-
function convertDateToLastYearMonth(date: string) {
280-
console.info(`Converting date ${date} to last year month`);
281-
const lastMonth = new Date(date);
282-
// reduce 1 month, JS will automatically adjust the year if needed
283-
lastMonth.setMonth(lastMonth.getMonth() - 1);
284-
const [year, month] = lastMonth.toISOString().split("T")[0].split("-");
285-
return `${year}${month}`;
283+
function getLastMonthPeriod(date: string): string {
284+
const parsedDate = parse(date, "yyyy-MM-dd", new Date());
285+
const previousMonth = subMonths(parsedDate, 1);
286+
return format(previousMonth, "yyyy-MM");
286287
}

‎metrics-collector/src/utils.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from "fs";
2-
import * as path from "path";
2+
import { subDays, format } from "date-fns";
33

44
// Read JSON data from the file
55
export function readJsonFile(filePath: string): any {
@@ -16,9 +16,8 @@ export function writeJsonFile(filePath: string, data: any): void {
1616
}
1717

1818
export const getYesterdayDate = () => {
19-
const yesterday = new Date();
20-
yesterday.setDate(yesterday.getDate() - 1);
21-
return yesterday.toISOString().split("T")[0];
19+
const yesterday = subDays(new Date(), 1);
20+
return format(yesterday, "yyyy-MM-dd");
2221
};
2322

2423
interface FetchWithRetryOptions {
@@ -32,7 +31,7 @@ export async function fetchWithRetry(
3231
options: RequestInit & FetchWithRetryOptions = {}
3332
): Promise<Response> {
3433
const {
35-
maxRetries = 3,
34+
maxRetries = 9,
3635
retryDelay = 1000,
3736
timeout = 10000,
3837
...fetchOptions

0 commit comments

Comments
 (0)
This repository has been archived.