Skip to content

Commit

Permalink
fix sonatype
Browse files Browse the repository at this point in the history
  • Loading branch information
leordev committed Sep 3, 2024
1 parent 5a73a4c commit 64a717f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions metrics-collector/src/sonatype-metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import { createObjectCsvWriter } from "csv-writer";
import { parse, format, subMonths } from "date-fns";
import { parse, format, subMonths, addHours } from "date-fns";
import { fetchWithRetry, readJsonFile, writeJsonFile } from "./utils";
import { postMetric } from "./post-metric";

Expand All @@ -18,6 +18,9 @@ const sonatypeCentralStatsUrl =
"https://s01.oss.sonatype.org/service/local/stats";

export async function collectSonatypeMetrics(metricDate: Date) {
const reportPeriod = getLastMonthPeriod(metricDate);
const reportPeriodWithoutHyphen = reportPeriod.replace("-", "");
console.info({ reportPeriod, reportPeriodWithoutHyphen });
initAuth();

const projectId = await getProjectId(groupId);
Expand All @@ -28,9 +31,6 @@ export async function collectSonatypeMetrics(metricDate: Date) {
continue; // TODO: add parameterized filter
}

const reportPeriod = getLastMonthPeriod(metricDate);
const reportPeriodWithoutHyphen = reportPeriod.replace("-", "");

const rawDownloads = await getArtifactStats(
projectId,
groupId,
Expand Down Expand Up @@ -281,7 +281,8 @@ function getLastMonthDate() {
}

function getLastMonthPeriod(date: Date): string {
const parsedDate = parse(date.toISOString(), "yyyy-MM-dd", new Date());
const previousMonth = subMonths(parsedDate, 1);
const midDayDate = addHours(date, 12); // avoid timezone issues
const previousMonth = subMonths(midDayDate, 1);
console.info({ date, midDayDate, previousMonth });
return format(previousMonth, "yyyy-MM");
}

0 comments on commit 64a717f

Please sign in to comment.