diff --git a/app/Controllers/Http/StatsController.ts b/app/Controllers/Http/StatsController.ts index 4926eb1..c4fafc1 100644 --- a/app/Controllers/Http/StatsController.ts +++ b/app/Controllers/Http/StatsController.ts @@ -1,13 +1,13 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import CommandsRun from "App/Models/CommandsRun"; +import {DateTime} from "luxon"; +import BuildsRun from "App/Models/BuildsRun"; 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"; +} from "App/Utils/StatsUtils"; export default class StatsController { diff --git a/app/Utils/StatsUtils.ts b/app/Utils/StatsUtils.ts new file mode 100644 index 0000000..8c31242 --- /dev/null +++ b/app/Utils/StatsUtils.ts @@ -0,0 +1,143 @@ +import DevelopmentHour from "App/Models/DevelopmentHour"; +import CommandsRun from "App/Models/CommandsRun"; +import BuildsRun from "App/Models/BuildsRun"; + +interface Stats { + start: Date; + end: Date; + + development_seconds: number; + commands_ran: number; + builds_ran: number; +} + +export async function getDevelopmentHours(start: Date, end: Date): Promise { + const development_seconds = await DevelopmentHour + .query() + .where('date', '>=', start) + .where('date', '<=', end) + .orderBy('date', 'desc') + + if (!development_seconds || development_seconds.length <= 0) { + return 0 + } + + const reduced = development_seconds.reduce((a: DevelopmentHour, b: DevelopmentHour) => { + return { + ...a, + seconds: a.seconds + b.seconds + } + }) + + return Number(reduced.seconds) +} + +export async function getCommandsRan(start: Date, end: Date): Promise { + const commands_run = await CommandsRun + .query() + .where('date', '>=', start) + .where('date', '<=', end) + .orderBy('date', 'desc') + + if (!commands_run || commands_run.length <= 0) { + return 0 + } + + const reduced = commands_run.reduce((a: CommandsRun, b: CommandsRun) => { + return { + ...a, + commands: a.commands + b.commands + } + }) + + return Number(reduced.commands) +} + +export async function getBuildsRan(start: Date, end: Date): Promise { + const builds_run = await BuildsRun + .query() + .where('date', '>=', start) + .where('date', '<=', end) + .orderBy('date', 'desc') + + if (!builds_run || builds_run.length <= 0) { + return 0 + } + + const reduced = builds_run.reduce((a: BuildsRun, b: BuildsRun) => { + return { + ...a, + builds: a.builds + b.builds + } + }) + + return Number(reduced.builds) +} + +export async function fetchStatistics(): Promise { + const start = new Date("01-01-2000"); + const end = new Date(); + + const development_seconds = await getDevelopmentHours(start, end) + const commands_ran = await getCommandsRan(start, end) + const builds_ran = await getBuildsRan(start, end) + + return { + start, + end, + development_seconds, + commands_ran, + builds_ran + } +} + +export async function fetchMonthlyStatistics(): Promise { + const start = new Date(new Date().setMonth(new Date().getMonth() - 1)); + const end = new Date(); + + const development_seconds = await getDevelopmentHours(start, end) + const commands_ran = await getCommandsRan(start, end) + const builds_ran = await getBuildsRan(start, end) + + return { + start, + end, + development_seconds, + commands_ran, + builds_ran + } +} + +export async function fetchWeeklyStatistics(): Promise { + const start = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000); + const end = new Date(); + + const development_seconds = await getDevelopmentHours(start, end) + const commands_ran = await getCommandsRan(start, end) + const builds_ran = await getBuildsRan(start, end) + + return { + start, + end, + development_seconds, + commands_ran, + builds_ran + } +} + +export async function fetchDailyStatistics(): Promise { + const start = new Date(); + const end = new Date(); + + const development_seconds = await getDevelopmentHours(start, end) + const commands_ran = await getCommandsRan(start, end) + const builds_ran = await getBuildsRan(start, end) + + return { + start, + end, + development_seconds, + commands_ran, + builds_ran + } +}