Skip to content

Commit

Permalink
slightly optimize contribution stats using sets
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Nov 7, 2024
1 parent 1318ed2 commit 1fb31b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/multer": "^1.4.12",
"@types/node": "^22.8.4",
"@types/node": "^22.9.0",
"@types/response-time": "^2.3.8",
"@types/statuses": "^2.0.5",
"@types/swagger-ui-express": "^4.1.7",
Expand All @@ -45,7 +45,7 @@
"body-parser": "^1.20.3",
"cloudflare": "^3.5.0",
"cors": "^2.8.5",
"discord-api-types": "^0.37.102",
"discord-api-types": "^0.37.103",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"firestorm-db": "^1.13.0",
Expand Down
28 changes: 10 additions & 18 deletions src/v2/service/contribution.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export default class ContributionService {

async getStats(): Promise<ContributionStats> {
const cs = await this.getRaw();
let total_authors = 0;
let total_contributions = 0;

const authors = {};
// set ensures unique values much more quickly than a check object/array iteration
const authors = new Set();

let total_last_week = 0;
let total_last_month = 0;
Expand All @@ -41,18 +40,11 @@ export default class ContributionService {
const aggregate: PackRecord = {} as PackRecord;

Object.values(cs).forEach((cur) => {
total_contributions += 1;
cur.authors.forEach((a) => authors.add(a));

cur.authors.forEach((a) => {
if (!authors[a]) {
authors[a] = true;
total_authors++;
}
});

const { pack, date: timestamp } = cur;
const { pack, date } = cur;
//! Group data by the start of date if time dont coincide
const start_of_day = startOfDay(timestamp).getTime();
const start_of_day = startOfDay(date).getTime();

aggregate[pack] ||= {};
aggregate[pack][start_of_day] ||= {
Expand All @@ -61,9 +53,9 @@ export default class ContributionService {
};
aggregate[pack][start_of_day].count++;

if (timestamp >= last_week) total_last_week += 1;
if (timestamp >= last_month) total_last_month += 1;
if (timestamp >= last_day) total_last_day += 1;
if (date >= last_week) total_last_week += 1;
if (date >= last_month) total_last_month += 1;
if (date >= last_day) total_last_day += 1;
});

const finalActivity = {} as PackData;
Expand All @@ -79,8 +71,8 @@ export default class ContributionService {
});

return {
total_authors,
total_contributions,
total_authors: authors.size,
total_contributions: Object.keys(cs).length,
total_last_day,
total_last_week,
total_last_month,
Expand Down
14 changes: 7 additions & 7 deletions src/v2/tools/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export const WEEK_LENGTH = 604800000;
export function lastWeek() {
return new Date(+new Date() - WEEK_LENGTH);
return new Date(new Date().getTime() - WEEK_LENGTH);
}

export const MONTH_LENGTH = 2629800000;
export function lastMonth() {
return new Date(+new Date() - MONTH_LENGTH);
return new Date(new Date().getTime() - MONTH_LENGTH);
}

export const DAY_LENGTH = 86400000;
export function lastDay() {
return new Date(new Date().getTime() - DAY_LENGTH);
}

export function startOfDay(date: Date | number) {
const result = new Date(date);
result.setHours(0, 0, 0, 0);
return result;
}

export const DAY_LENGTH = 86400000;
export function lastDay() {
return new Date(+new Date() - DAY_LENGTH);
}

0 comments on commit 1fb31b3

Please sign in to comment.