Compare commits
4 Commits
0.0.5
...
63b8176bdf
| Author | SHA1 | Date | |
|---|---|---|---|
| 63b8176bdf | |||
| 6a762f570d | |||
| 1b7424f750 | |||
| bdd4f705be |
34
src/controllers/StatsController.ts
Normal file
34
src/controllers/StatsController.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Get, JsonController } from 'routing-controllers';
|
||||
import { OpenAPI } from 'routing-controllers-openapi';
|
||||
import { getConnection } from 'typeorm';
|
||||
import { Donation } from '../models/entities/Donation';
|
||||
import { Runner } from '../models/entities/Runner';
|
||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
||||
import { Scan } from '../models/entities/Scan';
|
||||
import { User } from '../models/entities/User';
|
||||
|
||||
@JsonController('/stats')
|
||||
export class StatsController {
|
||||
|
||||
@Get()
|
||||
@OpenAPI({ description: "A very basic stats endpoint providing basic counters for a dashboard or simmilar" })
|
||||
async get() {
|
||||
let connection = getConnection();
|
||||
let runners = await connection.getRepository(Runner).find({ relations: ["scans"] });
|
||||
let teams = await connection.getRepository(RunnerTeam).find();
|
||||
let orgs = await connection.getRepository(RunnerOrganisation).find();
|
||||
let users = await connection.getRepository(User).find();
|
||||
let scans = await connection.getRepository(Scan).find();
|
||||
let donations = await connection.getRepository(Donation).find({ relations: ["runner", "runner.scans"] });
|
||||
return {
|
||||
"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),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -31,5 +31,5 @@ export abstract class Donation {
|
||||
* The donation's amount in cents (or whatever your currency's smallest unit is.).
|
||||
* The exact implementation may differ for each type of donation.
|
||||
*/
|
||||
abstract amount: number | Promise<number>;
|
||||
abstract amount: number;
|
||||
}
|
||||
Reference in New Issue
Block a user