This commit is contained in:
2020-12-19 21:16:44 +01:00
parent 48fa997811
commit 7478e85bae
11 changed files with 149 additions and 44 deletions

View File

@@ -0,0 +1,31 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Location from "App/Models/Location";
import StoreValidator from "App/Validators/locations/StoreValidator";
export default class LocationsController {
public async get ({ response }: HttpContextContract) {
const location = await Location.query().orderBy('since', 'desc').firstOrFail()
return response.status(200).send({
place: location.place,
left: location.left,
since: location.since
})
}
public async history ({ response }: HttpContextContract) {
const locations = await Location.query().orderBy('since', 'desc')
return response.status(200).send({
locations
})
}
public async set ({ request, response }: HttpContextContract) {
const data = await request.validate(StoreValidator)
await Location.create(data)
return response.status(200).send({
message: 'Location successfully added !'
})
}
}

View File

@@ -1,31 +1,44 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import {getTotalStats, getWeeklyStats, getMonthlyStats, getOtherStats} from 'App/Helpers/StatsHelper'
import DockerBuild from "App/Models/DockerBuild";
import DockerCommand from "App/Models/DockerCommand";
export default class StatsController {
public async get ({response}: HttpContextContract) {
return response.status(200).send({
daily: this.getDailyStats(),
weekly: this.getWeeklyStats(),
monthly: this.getMontlyStats()
weekly: getWeeklyStats(),
monthly: getMonthlyStats(),
total: getTotalStats(),
other : getOtherStats()
})
}
getDailyStats() {
return {
development_hours: 0
public async incrementBuild () {
const date = new Date()
const last_entry = await DockerBuild.findBy('created_at', date)
if (last_entry) {
last_entry.builds = last_entry.builds ++
await last_entry.save()
} else {
await DockerBuild.create({
builds: BigInt(1)
})
}
}
getWeeklyStats() {
return {
development_hours: 0
}
}
public async incrementCommand () {
const date = new Date()
const last_entry = await DockerCommand.findBy('created_at', date)
getMontlyStats() {
return {
development_hours: 0
if (last_entry) {
last_entry.commands = last_entry.commands ++
await last_entry.save()
} else {
await DockerCommand.create({
commands: BigInt(1)
})
}
}

View File

@@ -0,0 +1,59 @@
import DockerCommand from "App/Models/DockerCommand";
import axios from "axios";
import DockerBuild from "App/Models/DockerBuild";
async function getWeeklyStats() {
const commands = await DockerCommand.query().where('created_at', '>', new Date().getTime() - 1000 * 60 * 60 * 24 * 7)
const {data} = await axios.get('https://wakatime.com/api/v1/users/arthurdanjou/stats/last_7_days')
const builds = await DockerBuild.query().where('created_at', '>', new Date().getMonth() - 1)
return {
development_hours: data.data.total_seconds / 60 / 60,
docker_commands_run: commands.length,
docker_build_count: builds.length,
best_project: data.data.projects[0]
}
}
async function getMonthlyStats() {
const commands = await DockerCommand.query().where('created_at', '>', new Date().getMonth() - 1)
const {data} = await axios.get('https://wakatime.com/api/v1/users/arthurdanjou/stats/last_30_days')
const builds = await DockerBuild.query().where('created_at', '>', new Date().getMonth() - 1)
return {
development_hours: data.data.total_seconds / 60 / 60,
docker_commands_run: commands.length,
docker_build_count: builds.length,
best_project: data.data.projects[0]
}
}
async function getTotalStats() {
const commands = await DockerCommand.query()
const {data} = await axios.get('https://wakatime.com/api/v1/users/arthurdanjou/all_time_since_today')
const builds = await DockerBuild.query()
return {
development_hours: data.data.seconds,
docker_commands_run: commands.length,
docker_build_count: builds.length
}
}
async function getOtherStats() {
const {data} = await axios.get('https://wakatime.com/api/v1/users/arthurdanjou/stats/last_year')
return {
daily_average: data.data.daily_average / 60 / 60,
editors: [
'WebStorm',
'Intellij Idea',
'PyCharm',
'GoLang',
'DataGrip'
],
operating_systems: 'Windows'
}
}
export {getMonthlyStats, getTotalStats, getWeeklyStats, getOtherStats}

View File

@@ -6,7 +6,7 @@ export default class DockerBuild extends BaseModel {
public id: number
@column()
public build: bigint
public builds: bigint
@column.dateTime({ autoCreate: true })
public createdAt: DateTime

View File

@@ -6,7 +6,7 @@ export default class DockerCommand extends BaseModel {
public id: number
@column()
public command: string
public commands: bigint
@column.dateTime({ autoCreate: true })
public createdAt: DateTime

View File

@@ -9,7 +9,7 @@ export default class Location extends BaseModel {
public since: DateTime
@column()
public location: string
public place: string
@column()
public left: string

View File

@@ -1,19 +0,0 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import {schema} from '@ioc:Adonis/Core/Validator'
export default class UpdateValidator {
constructor (private ctx: HttpContextContract) {
}
public schema = schema.create({
place: schema.string.optional(),
since: schema.date.optional(),
left: schema.string.optional(),
})
public cacheKey = this.ctx.routeKey
public messages = {
required: 'Le champ {{field}} doit être valide !'
}
}

View File

@@ -13,5 +13,7 @@ export default class StateValidator {
})
public messages = {}
public messages = {
required: 'Le champ {{field}} doit être valide !',
}
}

View File

@@ -26,6 +26,7 @@
"@adonisjs/redis": "^5.0.8",
"@adonisjs/repl": "^1.0.0",
"@adonisjs/session": "^4.0.5",
"axios": "^0.21.0",
"luxon": "^1.25.0",
"mysql": "^2.18.1",
"phc-argon2": "^1.0.11",

View File

@@ -5,6 +5,11 @@ import HealthCheck from "@ioc:Adonis/Core/HealthCheck";
const BASE_URL = "https://api.arthurdanjou.fr"
Route.get('/health', async ({ response }) => {
const report = await HealthCheck.getReport()
return report.healthy ? response.ok(report) : response.badRequest(report)
})
Route.get('/', async ({response}: HttpContextContract) => {
response.status(200).send({
domain: "api.arthurdanjou.fr",
@@ -14,6 +19,7 @@ Route.get('/', async ({response}: HttpContextContract) => {
stats_data: `${BASE_URL}/stats`,
state_data: `${BASE_URL}/state`,
locations_data: `${BASE_URL}/location`,
locations_history: `${BASE_URL}/location/history`,
health: `${BASE_URL}/health`
}
})
@@ -23,7 +29,6 @@ Route.get('/', async ({response}: HttpContextContract) => {
TODO
Stats : Daily, weekly, monthly (Docker Build & commands, Dev hours)
Location: get Last + Add location + View history
Deezer Songs:
@@ -32,10 +37,8 @@ Tasks: kernel : setTimeout or cron
Deezer songs: 1min
*/
Route.get('/health', async ({ response }) => {
const report = await HealthCheck.getReport()
return report.healthy ? response.ok(report) : response.badRequest(report)
})
Route.get('/location', 'LocationsController.get')
Route.get('/location/history', 'LocationsController.history')
Route.get('/stats', 'StatsController.get')
Route.get('/state', 'StatesController.get')
Route.resource('users', 'UsersController').only(['index', 'show'])
@@ -54,8 +57,11 @@ Route.group(() => {
Route.resource('users', 'UsersController').only(['store', 'update', 'destroy'])
Route.resource('posts', 'PostsController').only(['store', 'update', 'destroy'])
Route.resource('subscribers', 'SubscribersController').only(['store', 'update', 'destroy'])
Route.resource('files', 'FileController').only(['store', 'update', 'destroy'])
Route.resource('files', 'FileController').only(['store', 'destroy'])
Route.post('/state', 'StatesController.set')
Route.post('/stats/build', 'StatesController.incrementBuild')
Route.post('/stats/command', 'StatesController.incrementCommand')
Route.post('/location', 'StatesController.set')
}).middleware('auth')

View File

@@ -772,6 +772,13 @@ atomic-sleep@^1.0.0:
resolved "https://registry.yarnpkg.com/atomic-sleep/-/atomic-sleep-1.0.0.tgz#eb85b77a601fc932cfe432c5acd364a9e2c9075b"
integrity sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==
axios@^0.21.0:
version "0.21.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.0.tgz#26df088803a2350dff2c27f96fef99fe49442aca"
integrity sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==
dependencies:
follow-redirects "^1.10.0"
babel-code-frame@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
@@ -1612,6 +1619,11 @@ flatstr@^1.0.12:
resolved "https://registry.yarnpkg.com/flatstr/-/flatstr-1.0.12.tgz#c2ba6a08173edbb6c9640e3055b95e287ceb5931"
integrity sha512-4zPxDyhCyiN2wIAtSLI6gc82/EjqZc1onI4Mz/l0pWrAlsSfYH/2ZIcU+e3oA2wDwbzIWNKwa23F8rh6+DRWkw==
follow-redirects@^1.10.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.1.tgz#5f69b813376cee4fd0474a3aba835df04ab763b7"
integrity sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==
for-in@^1.0.1, for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"