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

Commit

Permalink
init postmetric
Browse files Browse the repository at this point in the history
  • Loading branch information
leordev committed Jun 1, 2024
1 parent f035f6c commit 911492a
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 18 deletions.
8 changes: 6 additions & 2 deletions metrics-collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"faker": "^6.6.6",
"pg": "^8.11.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/yargs": "^17.0.32"
}
}
}
122 changes: 122 additions & 0 deletions metrics-collector/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions metrics-collector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
import * as dotenv from "dotenv";
dotenv.config();

import yargs from "yargs";
import { hideBin } from 'yargs/helpers';

import { collectGhMetrics } from "./gh-metrics";
import { collectNpmMetrics } from "./npm-metrics";
import { collectSonatypeMetrics } from "./sonatype-metrics";

const isLocalPersistence = process.env.PERSIST_LOCAL_FILES === "true";

interface Arguments {
'collect-gh': boolean;
'collect-npm': boolean;
'collect-sonatype': boolean;
}

const argv = yargs(hideBin(process.argv))
.options({
'collect-gh': { type: 'boolean', description: 'Collect GitHub metrics', default: false },
'collect-npm': { type: 'boolean', description: 'Collect npm metrics', default: false },
'collect-sonatype': { type: 'boolean', description: 'Collect Sonatype metrics', default: false }
})
.argv as Arguments;

async function main() {
try {
await collectGhMetrics(isLocalPersistence);
await collectNpmMetrics(isLocalPersistence);
await collectSonatypeMetrics(isLocalPersistence);
const collectGh = argv['collect-gh'];
const collectNpm = argv['collect-npm'];
const collectSonatype = argv['collect-sonatype'];

const noArgs = !collectGh && !collectNpm && !collectSonatype;

if (collectGh || noArgs) {
await collectGhMetrics(isLocalPersistence);
}
if (collectNpm || noArgs) {
await collectNpmMetrics(isLocalPersistence);
}
if (collectSonatype || noArgs) {
await collectSonatypeMetrics(isLocalPersistence);
}
} catch (error) {
console.error(error);
}
Expand Down
39 changes: 37 additions & 2 deletions metrics-collector/src/npm-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import { createObjectCsvWriter } from "csv-writer";
import { readJsonFile, writeJsonFile } from "./utils";
import { postMetric } from "./post-metric";

// Define the npm packages to collect metrics for
const npmPackages = [
Expand All @@ -18,7 +19,10 @@ const npmPackages = [
const dataFilePath = path.join(process.cwd(), "npm_metrics.json");
const csvFilePath = path.join(process.cwd(), "npm_metrics.csv");

async function collectNpmMetrics(isLocalPersistence: boolean = false) {
async function collectNpmMetrics(
isLocalPersistence: boolean = false,
metricDate?: string
) {
const timestamp = new Date().toISOString();

const metrics = [];
Expand All @@ -28,12 +32,18 @@ async function collectNpmMetrics(isLocalPersistence: boolean = false) {
getNpmDownloadCount(pkg, true),
getNpmDownloadCount(pkg),
]);
let metricDateDownloads;
if (metricDate) {
metricDateDownloads = await getNpmDownloadCount(pkg, false, metricDate);
}
metrics.push({
pkg,
timestamp,
publishedAt: totalDownloads.start,
totalDownloads: totalDownloads.downloads,
lastMonthDownloads: lastMonthDownloads.downloads,
metricDate,
metricDateDownloads: metricDateDownloads?.downloads,
});
}

Expand All @@ -52,19 +62,44 @@ async function collectNpmMetrics(isLocalPersistence: boolean = false) {
console.log(
"NPM metrics have been successfully saved to npm_metrics.json and npm_metrics.csv"
);
} else {
await postNpmMetrics(metrics);
}

return metrics;
}

async function postNpmMetrics(metrics: any) {
for (const metric of metrics) {
const labels = {
package: metric.pkg,
};

if (metric.metricDate) {
const metricDate = new Date(metric.metricDate);
await postMetric(
"npm_downloads",
metric.metricDateDownloads || 0,
labels,
metricDate
);
}

await postMetric("npm_total_downloads", metric.totalDownloads, labels);
}
}

async function getNpmDownloadCount(
packageName: string,
onlyLastMonth?: boolean
onlyLastMonth?: boolean,
singleDay?: string
): Promise<{ downloads: number; start: string }> {
try {
let url = `https://api.npmjs.org/downloads/point`;
if (onlyLastMonth) {
url += `/last-month/${packageName}`;
} else if (singleDay) {
url += `/${singleDay}/${packageName}`;
} else {
url += `/1970-01-01:2100-01-01/${packageName}`;
}
Expand Down
Loading

0 comments on commit 911492a

Please sign in to comment.