74
src/models/responses/ResponseStats.ts
Normal file
74
src/models/responses/ResponseStats.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
IsInt
|
||||
} from "class-validator";
|
||||
import { Donation } from '../entities/Donation';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { Scan } from '../entities/Scan';
|
||||
import { User } from '../entities/User';
|
||||
|
||||
/**
|
||||
* Defines the stats response.
|
||||
* The stats response calculates some basic stats for a dashboard or public display.
|
||||
*/
|
||||
export class ResponseStats {
|
||||
/**
|
||||
* The amount of runners registered in the system.
|
||||
*/
|
||||
@IsInt()
|
||||
total_runners: number;
|
||||
|
||||
/**
|
||||
* The amount of teams registered in the system.
|
||||
*/
|
||||
@IsInt()
|
||||
total_teams: number;
|
||||
|
||||
/**
|
||||
* The amount of organisations registered in the system.
|
||||
*/
|
||||
@IsInt()
|
||||
total_orgs: number;
|
||||
|
||||
/**
|
||||
* The amount of users registered in the system.
|
||||
*/
|
||||
@IsInt()
|
||||
total_users: number;
|
||||
|
||||
/**
|
||||
* The amount of valid scans registered in the system.
|
||||
*/
|
||||
@IsInt()
|
||||
total_scans: number;
|
||||
|
||||
/**
|
||||
* The total distance that all runners ran.
|
||||
*/
|
||||
@IsInt()
|
||||
total_distance: number;
|
||||
|
||||
/**
|
||||
* The total donation amount.
|
||||
*/
|
||||
@IsInt()
|
||||
total_donation: number;
|
||||
|
||||
/**
|
||||
* The average distance per runner.
|
||||
*/
|
||||
@IsInt()
|
||||
average_distance: number;
|
||||
|
||||
public constructor(runners: Runner[], teams: RunnerTeam[], orgs: RunnerOrganisation[], users: User[], scans: Scan[], donations: Donation[]) {
|
||||
this.total_runners = runners.length;
|
||||
this.total_teams = teams.length;
|
||||
this.total_orgs = orgs.length;
|
||||
this.total_users = users.length;
|
||||
this.total_scans = scans.filter(scan => { scan.valid === true }).length;
|
||||
this.total_distance = runners.reduce((sum, current) => sum + current.distance, 0);
|
||||
this.total_donation = donations.reduce((sum, current) => sum + current.amount, 0);
|
||||
this.average_distance = this.total_distance / this.total_runners;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user