Faster stats (not including donations)
This commit is contained in:
parent
5f17e7f783
commit
b2ac70e0ae
@ -22,13 +22,17 @@ export class StatsController {
|
|||||||
@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', 'scans.track'] });
|
let runners = await connection.getRepository(Runner).count();
|
||||||
let teams = await connection.getRepository(RunnerTeam).find();
|
let teams = await connection.getRepository(RunnerTeam).count();
|
||||||
let orgs = await connection.getRepository(RunnerOrganization).find();
|
let orgs = await connection.getRepository(RunnerOrganization).count();
|
||||||
let users = await connection.getRepository(User).find();
|
let users = await connection.getRepository(User).count();
|
||||||
let scans = await connection.getRepository(Scan).find();
|
let scans = await connection.getRepository(Scan).count({ where: { valid: true } });
|
||||||
|
let distance_query = await connection.getRepository(Scan).createQueryBuilder('scan')
|
||||||
|
.leftJoinAndSelect("scan.track", "track").where("scan.valid = TRUE")
|
||||||
|
.select("SUM(track.distance)", "sum_track").addSelect("SUM(_distance)", "sum_distance")
|
||||||
|
.getRawOne();
|
||||||
let donations = await connection.getRepository(Donation).find({ relations: ['runner', 'runner.scans', 'runner.scans.track'] });
|
let donations = await connection.getRepository(Donation).find({ relations: ['runner', 'runner.scans', 'runner.scans.track'] });
|
||||||
return new ResponseStats(runners, teams, orgs, users, scans, donations)
|
return new ResponseStats(runners, teams, orgs, users, scans, donations, distance_query.sum_track + distance_query.sum_distance)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/runners/distance")
|
@Get("/runners/distance")
|
||||||
|
@ -2,11 +2,6 @@ import {
|
|||||||
IsInt
|
IsInt
|
||||||
} from "class-validator";
|
} from "class-validator";
|
||||||
import { Donation } from '../entities/Donation';
|
import { Donation } from '../entities/Donation';
|
||||||
import { Runner } from '../entities/Runner';
|
|
||||||
import { RunnerOrganization } from '../entities/RunnerOrganization';
|
|
||||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
|
||||||
import { Scan } from '../entities/Scan';
|
|
||||||
import { User } from '../entities/User';
|
|
||||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||||
import { IResponse } from './IResponse';
|
import { IResponse } from './IResponse';
|
||||||
|
|
||||||
@ -78,13 +73,13 @@ export class ResponseStats implements IResponse {
|
|||||||
* @param scans Array containing all scans - no relations have to be resolved.
|
* @param scans Array containing all scans - no relations have to be resolved.
|
||||||
* @param donations Array containing all donations - the following relations have to be resolved: runner, runner.scans, runner.scans.track
|
* @param donations Array containing all donations - the following relations have to be resolved: runner, runner.scans, runner.scans.track
|
||||||
*/
|
*/
|
||||||
public constructor(runners: Runner[], teams: RunnerTeam[], orgs: RunnerOrganization[], users: User[], scans: Scan[], donations: Donation[]) {
|
public constructor(runners: number, teams: number, orgs: number, users: number, scans: number, donations: Donation[], distance: number) {
|
||||||
this.total_runners = runners.length;
|
this.total_runners = runners;
|
||||||
this.total_teams = teams.length;
|
this.total_teams = teams;
|
||||||
this.total_orgs = orgs.length;
|
this.total_orgs = orgs;
|
||||||
this.total_users = users.length;
|
this.total_users = users;
|
||||||
this.total_scans = scans.filter(scan => { scan.valid === true }).length;
|
this.total_scans = scans;
|
||||||
this.total_distance = runners.reduce((sum, current) => sum + current.distance, 0);
|
this.total_distance = distance;
|
||||||
this.total_donation = donations.reduce((sum, current) => sum + current.amount, 0);
|
this.total_donation = donations.reduce((sum, current) => sum + current.amount, 0);
|
||||||
this.average_distance = this.total_distance / this.total_runners;
|
this.average_distance = this.total_distance / this.total_runners;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user