Skip to content

Commit

Permalink
feat: create script for generating final MoM values for a month
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Dec 6, 2023
1 parent cd35522 commit d876052
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
3 changes: 2 additions & 1 deletion reports/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ async function getApiKey (): Promise<string> {
}

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

Expand Down
4 changes: 2 additions & 2 deletions reports/downloadDashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DashboardData> {
export async function downloadDashboardData ({ writeFiles = false, daysOfDataMs = daysOfDataInMs }: { writeFiles?: boolean, daysOfDataMs?: number } = {}): Promise<DashboardData> {
const todayEpoch = Date.now()
const dailyArray: googleSheetData = []
const weeklyArray: googleSheetData = []
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions reports/scripts/download-data.ts
Original file line number Diff line number Diff line change
@@ -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<typeof downloadDashboardData>[0] = {
daysOfDataMs,
writeFiles: true
}
await downloadDashboardData(downloadOptions)
69 changes: 69 additions & 0 deletions reports/scripts/report-mom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { readFile } from 'fs/promises';

/**
* Must run `npm run download-data <daysToDownload>` 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<string, number|string>, 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<string, any>[], 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])
})
1 change: 1 addition & 0 deletions reports/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"exclude": [
"node_modules",
"dist"
],
"ts-node": {
"extends": "../tsconfig.json",
Expand Down

0 comments on commit d876052

Please sign in to comment.