parent
d7791756dc
commit
53a01ad977
@ -8,6 +8,7 @@ import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
|||||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
||||||
import { Scan } from '../models/entities/Scan';
|
import { Scan } from '../models/entities/Scan';
|
||||||
import { User } from '../models/entities/User';
|
import { User } from '../models/entities/User';
|
||||||
|
import { ResponseStats } from '../models/responses/ResponseStats';
|
||||||
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
|
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
|
||||||
import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
|
import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
|
||||||
|
|
||||||
@ -15,25 +16,17 @@ import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
|
|||||||
export class StatsController {
|
export class StatsController {
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
|
@ResponseSchema(ResponseStats)
|
||||||
@OpenAPI({ description: "A very basic stats endpoint providing basic counters for a dashboard or simmilar" })
|
@OpenAPI({ description: "A very basic stats endpoint providing basic counters for a dashboard or simmilar" })
|
||||||
async get() {
|
async get() {
|
||||||
let connection = getConnection();
|
let connection = getConnection();
|
||||||
let runners = await connection.getRepository(Runner).find({ relations: ["scans"] });
|
let runners = await connection.getRepository(Runner).find({ relations: ['scans', 'scans.track'] });
|
||||||
let teams = await connection.getRepository(RunnerTeam).find();
|
let teams = await connection.getRepository(RunnerTeam).find();
|
||||||
let orgs = await connection.getRepository(RunnerOrganisation).find();
|
let orgs = await connection.getRepository(RunnerOrganisation).find();
|
||||||
let users = await connection.getRepository(User).find();
|
let users = await connection.getRepository(User).find();
|
||||||
let scans = await connection.getRepository(Scan).find();
|
let scans = await connection.getRepository(Scan).find();
|
||||||
let donations = await connection.getRepository(Donation).find({ relations: ["runner", "runner.scans"] });
|
let donations = await connection.getRepository(Donation).find({ relations: ['runner', 'runner.scans', 'runner.scans.track'] });
|
||||||
return {
|
return new ResponseStats(runners, teams, orgs, users, scans, donations)
|
||||||
"total_runners": runners.length,
|
|
||||||
"total_teams": teams.length,
|
|
||||||
"total_orgs": orgs.length,
|
|
||||||
"total_users": users.length,
|
|
||||||
"total_scans": scans.filter(scan => { scan.valid === true }).length,
|
|
||||||
"total_distance": runners.reduce((sum, current) => sum + current.distance, 0),
|
|
||||||
"total_donation_amount": donations.reduce((sum, current) => sum + current.amount, 0),
|
|
||||||
"average_distance": runners.reduce((sum, current) => sum + current.distance, 0) / runners.length,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/runners/distance")
|
@Get("/runners/distance")
|
||||||
@ -41,7 +34,7 @@ export class StatsController {
|
|||||||
@ResponseSchema(ResponseStatsRunner, { isArray: true })
|
@ResponseSchema(ResponseStatsRunner, { isArray: true })
|
||||||
@OpenAPI({ description: "Returns the top ten runners by distance.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten runners by distance.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopRunnersByDistance() {
|
async getTopRunnersByDistance() {
|
||||||
let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations'] });
|
let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations', 'scans.track'] });
|
||||||
let topRunners = runners.sort((runner1, runner2) => runner1.distance - runner2.distance).slice(0, 9);
|
let topRunners = runners.sort((runner1, runner2) => runner1.distance - runner2.distance).slice(0, 9);
|
||||||
let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
|
let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
|
||||||
topRunners.forEach(runner => {
|
topRunners.forEach(runner => {
|
||||||
@ -55,7 +48,7 @@ export class StatsController {
|
|||||||
@ResponseSchema(ResponseStatsRunner, { isArray: true })
|
@ResponseSchema(ResponseStatsRunner, { isArray: true })
|
||||||
@OpenAPI({ description: "Returns the top ten runners by donations.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten runners by donations.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopRunnersByDonations() {
|
async getTopRunnersByDonations() {
|
||||||
let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations'] });
|
let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations', 'scans.track'] });
|
||||||
let topRunners = runners.sort((runner1, runner2) => runner1.distanceDonationAmount - runner2.distanceDonationAmount).slice(0, 9);
|
let topRunners = runners.sort((runner1, runner2) => runner1.distanceDonationAmount - runner2.distanceDonationAmount).slice(0, 9);
|
||||||
let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
|
let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
|
||||||
topRunners.forEach(runner => {
|
topRunners.forEach(runner => {
|
||||||
@ -69,7 +62,7 @@ export class StatsController {
|
|||||||
@ResponseSchema(ResponseStatsTeam, { isArray: true })
|
@ResponseSchema(ResponseStatsTeam, { isArray: true })
|
||||||
@OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopTeamsByDistance() {
|
async getTopTeamsByDistance() {
|
||||||
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations'] });
|
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track'] });
|
||||||
let topTeams = teams.sort((team1, team2) => team1.distance - team2.distance).slice(0, 9);
|
let topTeams = teams.sort((team1, team2) => team1.distance - team2.distance).slice(0, 9);
|
||||||
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
||||||
topTeams.forEach(team => {
|
topTeams.forEach(team => {
|
||||||
@ -83,7 +76,7 @@ export class StatsController {
|
|||||||
@ResponseSchema(ResponseStatsTeam, { isArray: true })
|
@ResponseSchema(ResponseStatsTeam, { isArray: true })
|
||||||
@OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopTeamsByDonations() {
|
async getTopTeamsByDonations() {
|
||||||
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations'] });
|
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track'] });
|
||||||
let topTeams = teams.sort((team1, team2) => team1.distanceDonationAmount - team2.distanceDonationAmount).slice(0, 9);
|
let topTeams = teams.sort((team1, team2) => team1.distanceDonationAmount - team2.distanceDonationAmount).slice(0, 9);
|
||||||
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
||||||
topTeams.forEach(team => {
|
topTeams.forEach(team => {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user