Added stats endpoint with some basic stats (more to come) - to be tested
ref #56
This commit is contained in:
parent
bdd4f705be
commit
1b7424f750
28
src/controllers/StatsController.ts
Normal file
28
src/controllers/StatsController.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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 { 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 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_users": users.length,
|
||||||
|
"total_scans": scans.length,
|
||||||
|
"total_distance": runners.reduce((sum, current) => sum + current.distance, 0),
|
||||||
|
"total_donation_amount": donations.reduce((sum, current) => sum + current.amount, 0),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user