diff --git a/reports/constants.ts b/reports/constants.ts index b034e4b..6573e88 100644 --- a/reports/constants.ts +++ b/reports/constants.ts @@ -31,9 +31,10 @@ async function getApiKey (): Promise { } export const apiKey = await getApiKey() +console.log(`apiKey: `, apiKey); /** - * 90 days of data + * 90 days of data is the default */ export const daysOfDataInMs = 1000 * 60 * 60 * 24 * 90 diff --git a/reports/downloadDashboardData.ts b/reports/downloadDashboardData.ts index dfbf6aa..7c976c3 100644 --- a/reports/downloadDashboardData.ts +++ b/reports/downloadDashboardData.ts @@ -30,7 +30,7 @@ export interface DashboardData { /** * Print out a CSV of the number of unique users per day for each app */ -export async function downloadDashboardData ({ writeFiles = false }: { writeFiles?: boolean } = {}): Promise { +export async function downloadDashboardData ({ writeFiles = false, daysOfDataMs = daysOfDataInMs }: { writeFiles?: boolean, daysOfDataMs?: number } = {}): Promise { const todayEpoch = Date.now() const dailyArray: googleSheetData = [] const weeklyArray: googleSheetData = [] @@ -42,7 +42,7 @@ export async function downloadDashboardData ({ writeFiles = false }: { writeFile /** * @see https://api.count.ly/reference/oanalyticssessions */ - response = await doCountlyFetch({ path: '/o/active_users', appId, extraParams: `period=[${todayEpoch - daysOfDataInMs}, ${todayEpoch}]` }) + response = await doCountlyFetch({ path: '/o/active_users', appId, extraParams: `period=[${todayEpoch - daysOfDataMs}, ${todayEpoch}]` }) // eslint-disable-next-line no-console console.log(`${appName} calculating? `, response.calculating) if (response.calculating) { diff --git a/reports/package.json b/reports/package.json index 78c4cc3..3c8303b 100644 --- a/reports/package.json +++ b/reports/package.json @@ -11,7 +11,9 @@ "scripts": { "build": "tsc", "get-csv": "node dist/scripts/get-csv.js", - "update-dashboards": "node dist/index.js" + "update-dashboards": "node dist/index.js", + "download-data": "node dist/scripts/download-data.js", + "report:mom": "node dist/scripts/report-mom.js" }, "dependencies": { "@googleapis/sheets": "^4.0.1", diff --git a/reports/scripts/download-data.ts b/reports/scripts/download-data.ts new file mode 100644 index 0000000..ca7a80b --- /dev/null +++ b/reports/scripts/download-data.ts @@ -0,0 +1,12 @@ +import { downloadDashboardData } from '../downloadDashboardData.js' + +const daysInput = parseInt(process.argv[2], 10) +if (isNaN(daysInput)) { + throw new Error('daysInput must be a number') +} +const daysOfDataMs = daysInput * 1000 * 60 * 60 * 24 +const downloadOptions: Parameters[0] = { + daysOfDataMs, + writeFiles: true +} +await downloadDashboardData(downloadOptions) diff --git a/reports/scripts/report-mom.ts b/reports/scripts/report-mom.ts new file mode 100644 index 0000000..811be02 --- /dev/null +++ b/reports/scripts/report-mom.ts @@ -0,0 +1,69 @@ +import { readFile } from 'fs/promises'; + +/** + * Must run `npm run download-data ` before running this script + * + * Reads the data output by `downloadDashboardData` and outputs them to the console + * This script accepts a single argument, the month and year to filter by, in the format `YYYY-MM` + * For example, to filter by May 2023, run `npm report-mom.js 2023-05` + * + * This script is used to get the values for the MoM spreadsheet at: + * https://docs.google.com/spreadsheets/d/1xq36kjThObEaRKzb3VRtXEs9RgM-bfgfjGbi1vbPUiE/edit#gid=2037430789 + */ + +const filterMonth = process.argv[2] +if (filterMonth == null) { + throw new Error('filterMonth must be provided. use the format YYYY-MM') +} +['monthly'].forEach(async (period) => { + const data = await readFile(`./output/activeUsers-${period}.json`, { encoding: 'utf-8' }) + /** + * the JSON is in the format of an array of arrays, where + * the first array is the headers in the format of [labelForAppNames, ...dateStrings] + * the rest of the arrays are the data in the format of [appName, ...values] + */ + const parsedData = JSON.parse(data) + // Find all of the headers in the first row that start with the filterMonth + const [headers, ...rows] = parsedData + const headersAndIndex = headers.reduce((acc: Record, header: string, index: number) => { + if (header.startsWith(filterMonth)) { + acc[header] = index + acc.latestDateIndex = index + acc.latestDateFound = header + } + return acc + }, {}) + + console.log(`Getting values from day ${headersAndIndex.latestDateFound} at index ${headersAndIndex.latestDateIndex} for period ${period}`) + + const monthLabel = new Date(`${filterMonth}-10`).toLocaleString('en-US', { month: 'long' }) + const filterMonthLabel = `${monthLabel} (${headersAndIndex.latestDateFound})` + + const { latestDateIndex } = headersAndIndex + const filteredData = rows.reduce((acc: Record[], row: [string, ...number[]]) => { + const [appName, ...rest] = row + const value = rest[latestDateIndex] + + acc.push({ 'App Name': appName, [filterMonthLabel]: value }) + return acc + }, []) + + console.log(`\n${period} MoM data for ${filterMonthLabel}:`) + // need to sort the filteredData so that it appears in the order of + const sortedAppNames = [ + 'ipfs-companion', + 'ipfs-desktop', + 'public-gateway-checker', + 'ipfs-webui-kubo', + 'ipfs-webui', + 'cid-utils-website', + 'explore.ipld.io', + 'pinning-service-compliance', + 'starmap.site', + 'ipfs-check', + 'ipfs-dag-builder-vis', + 'pl-diagnose' + ] + const sortedData = filteredData.sort((a: any, b: any) => sortedAppNames.indexOf(a['App Name']) - sortedAppNames.indexOf(b['App Name'])) + console.table(sortedData, ['App Name', filterMonthLabel]) +}) diff --git a/reports/tsconfig.json b/reports/tsconfig.json index c1d2a5e..6cfa0c4 100644 --- a/reports/tsconfig.json +++ b/reports/tsconfig.json @@ -16,6 +16,7 @@ ], "exclude": [ "node_modules", + "dist" ], "ts-node": { "extends": "../tsconfig.json",