Added stats response

ref #56
This commit is contained in:
2020-12-30 16:31:18 +01:00
parent d7791756dc
commit 53a01ad977
2 changed files with 83 additions and 16 deletions

View 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;
}
}