Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leordev committed Aug 28, 2024
1 parent b1ac406 commit 1f8c546
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
19 changes: 13 additions & 6 deletions metrics-collector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,19 @@ async function main() {

const collectGh = argv["collect-gh"];
if (collectGh) {
console.info(`\n\n============\n\n>>> Collecting metrics for GitHub...`);
await collectGhMetrics();
}

const localCollection = !collectGh && !collectNpm && !collectSonatype;
if (localCollection) {
await saveNpmMetrics();
await saveSonatypeMetrics();
console.info(
`\n\n============\n\n>>> Collecting local metrics...`
);
// await saveNpmMetrics();
// await saveSonatypeMetrics();
await collectGhMetrics(true);
}

console.log("Data collection completed successfully");
process.exit(0);
}

async function initialLoad(
Expand Down Expand Up @@ -136,4 +137,10 @@ async function initialLoad(
}
}

main();
main().then(() => {
console.log("Data collection completed successfully");
process.exit(0);
}).catch((error) => {
console.error("Data collection failed", error);
process.exit(1);
});
6 changes: 4 additions & 2 deletions metrics-collector/src/npm-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const dataFilePath = path.join(process.cwd(), "npm_metrics.json");
const csvFilePath = path.join(process.cwd(), "npm_metrics.csv");

// Push collected metrics to the metrics service
export async function collectNpmMetrics(metricDate: string) {
export const collectNpmMetrics = async (metricDate: string) => {
for (const pkg of npmPackages) {
const { downloads: totalDownloads } = await getNpmDownloadCount(
pkg,
Expand All @@ -41,7 +41,9 @@ export async function collectNpmMetrics(metricDate: string) {
{ begin: metricDate, end: metricDate }
);

postNpmMetrics({
console.info(`\n\n============\n\n>>> Collected metrics for ${pkg}...`);

await postNpmMetrics({
pkg,
metricDate: new Date(metricDate),
totalDownloads: totalDownloads,
Expand Down
32 changes: 14 additions & 18 deletions metrics-collector/src/post-metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,21 @@ export const postMetric = async (
timestamp: (timestamp || new Date()).toISOString(),
};

try {
const response = await fetch(`${metricsServiceAppUrl}/metrics`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const response = await fetch(`${metricsServiceAppUrl}/metrics`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});

if (!response.ok) {
if (response.body) {
const error = await response.json();
console.error("Errored response body:", { error });
}
throw new Error(`Error posting metric: ${response.statusText}`);
if (!response.ok) {
if (response.body) {
const error = await response.json();
console.error("Errored response body:", { error });
}

console.log("Metric posted successfully:", JSON.stringify(payload));
} catch (error) {
console.error("Error posting metric:", error, JSON.stringify(error));
throw new Error(`Error posting metric: ${response.statusText}`);
}

console.info("Metric posted successfully:", JSON.stringify(payload));
};

0 comments on commit 1f8c546

Please sign in to comment.