From c8a09d9ab65dc682bb0843251af214fa1081479c Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Fri, 8 Oct 2021 15:02:22 +0200 Subject: [PATCH] Remove Discord Activity route --- app/Controllers/Http/StatsController.ts | 59 ++++++++++++++++++++++++- start/routes/api.ts | 14 +++++- start/routes/home.ts | 3 +- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/app/Controllers/Http/StatsController.ts b/app/Controllers/Http/StatsController.ts index c1d426b..4926eb1 100644 --- a/app/Controllers/Http/StatsController.ts +++ b/app/Controllers/Http/StatsController.ts @@ -1,10 +1,67 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import { + fetchDailyStatistics, + fetchMonthlyStatistics, + fetchStatistics, + fetchWeeklyStatistics +} from "App/Tasks/DevelopmentHoursTask"; +import CommandsRun from "App/Models/CommandsRun"; +import {DateTime} from "luxon"; +import BuildsRun from "App/Models/BuildsRun"; export default class StatsController { public async index ({ response }: HttpContextContract) { + const daily = await fetchDailyStatistics() + const weekly = await fetchWeeklyStatistics() + const monthly = await fetchMonthlyStatistics() + const total = await fetchStatistics() + return response.status(200).send({ - message: 'Stats is under maintenance! Come back later.' + daily, + weekly, + monthly, + total + }) + } + + public async incrementCommandCount({ response }: HttpContextContract) { + const current_date = DateTime.fromJSDate(new Date(new Date().setUTCMinutes(0, 0, 0))) + const current_commands = await CommandsRun.firstOrCreate( + { + date: current_date + }, + { + date: current_date, + commands: 0 + } + ) + + current_commands.commands++ + await current_commands.save() + + return response.status(200).send({ + message: 'Commands Count successfully incremented !' + }) + } + + public async incrementBuildCount({ response }: HttpContextContract) { + const current_date = DateTime.fromJSDate(new Date(new Date().setUTCMinutes(0, 0, 0))) + const current_builds = await BuildsRun.firstOrCreate( + { + date: current_date + }, + { + date: current_date, + builds: 0 + } + ) + + current_builds.builds++ + await current_builds.save() + + return response.status(200).send({ + message: 'Builds Count successfully incremented !' }) } diff --git a/start/routes/api.ts b/start/routes/api.ts index 0aec16d..37eaf58 100644 --- a/start/routes/api.ts +++ b/start/routes/api.ts @@ -1,9 +1,9 @@ import Route from "@ioc:Adonis/Core/Route"; import Application from "@ioc:Adonis/Core/Application"; -Route.get('/discord', 'ProfileController.discord') Route.get('/me', 'ProfileController.me') Route.get('/stats', 'StatsController.index') +Route.get('/states', 'StatesController.index') Route.resource('/locations', 'LocationsController').only(['index', 'store']) Route.group(() => { @@ -13,7 +13,17 @@ Route.group(() => { Route.resource('/files', 'FilesController').only(['index', 'store', 'destroy']) -}).middleware('auth') + Route.group(() => { + Route.post('/sleeping', 'StatesController.setSleeping') + Route.post('/developing', 'StatesController.setDeveloping') + }).prefix('states') + + Route.group(() => { + Route.post('/commands', 'StatsController.incrementCommandCount') + Route.post('/docker', 'StatsController.incrementBuildCount') + }).prefix('stats') + +}).middleware('auth:web,api') Route.get('/files/:filename', async ({response, params}) => { response.download(Application.makePath('storage', params.filename)) diff --git a/start/routes/home.ts b/start/routes/home.ts index 3d4546d..0efea04 100644 --- a/start/routes/home.ts +++ b/start/routes/home.ts @@ -10,10 +10,11 @@ Route.get('/', async ({response}: HttpContextContract) => { domain: BASE_URL, version: Env.get('API_VERSION'), source: `${BASE_URL}/source`, - healthCheck: `${BASE_URL}/health`, + health: `${BASE_URL}/health`, routes: { profile: `${BASE_URL}/me`, stats: `${BASE_URL}/stats`, + states: `${BASE_URL}/states`, locations: `${BASE_URL}/locations` } })