Skip to content

Commit

Permalink
add /voyagesByCrew route
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Moschkin committed Dec 6, 2023
1 parent 2b4a99b commit 4bf034e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/controllers/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ router.post('/telemetry', async (req: Request, res: Response, next) => {
});


router.get('/voyagesByCrew', async (req: Request, res: Response, next) => {
req.query.days ??= "31";
if (!req.query || !req.query.days || !req.query.crew) {
res.status(400).send('Whaat?');
return;
}

try {
let cstr = req.query.crew.toString();
let dstr = req.query.days.toString();
if (!cstr?.length) {
res.status(400).send('Whaat?');
return;
}
let crew = cstr.split(",");
let days = Number.parseInt(dstr);

let apiResult = await DataCoreAPI.getVoyages(crew, days);
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
next(e);
}
});

// Export the express.Router() instance to be used by server.ts
export const ApiController: Router = router;
12 changes: 11 additions & 1 deletion app/logic/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sign, verify } from 'jsonwebtoken';
import { Logger, LogData } from './logger';
import { uploadProfile, loadProfileCache, loginUser, getDBIDbyDiscord } from './profiletools';
import { loadCommentsDB, saveCommentDB } from './commenttools';
import { recordTelemetryDB, getTelemetryDB } from './telemetry';
import { recordTelemetryDB, getTelemetryDB, voyageRawByDays } from './telemetry';
import { getSTTToken } from './stttools';

require('dotenv').config();
Expand Down Expand Up @@ -303,6 +303,16 @@ export class ApiClass {
Body: result
}
}

async getVoyages(crew: string[], days: number) {
if (days <= 0 || days > 31) days = 31;
Logger.info('Get voyages', { crew, days });
let result = await voyageRawByDays(days, crew)
return {
Status: 200,
Body: result
}
}
}

export let DataCoreAPI = new ApiClass();
40 changes: 40 additions & 0 deletions app/logic/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,46 @@ export async function createStats(force?: boolean) {
setTimeout(() => createStats(), 1000 * 60 * 30);
}

export async function voyageRawByDays(days: number, crewMatch?: string[]) {
let endDate = new Date();
let startDate = new Date();
startDate.setDate(startDate.getDate() - days);

return voyageRawByRange(startDate, endDate, crewMatch);
}

export async function voyageRawByRange(startDate?: Date, endDate?: Date, crewMatch?: string[]) {
endDate ??= new Date();
if (!startDate) {
startDate = new Date(endDate.getTime());
startDate.setDate(startDate.getDate() - 7);
}

const where = {
voyageDate: [
{ [Op.gte]: startDate },
{ [Op.lte]: endDate }
],
crew: undefined as any
};

if (crewMatch) {
where.crew = { [Op.or]: [] as any[] };
for (let crew of crewMatch) {
where.crew[Op.or].push({ [Op.like]: `%"${crew}"%`})
}
}
else {
delete where.crew;
}

let results = await Voyage.findAll({
where
});

return results;
}

async function getVoyageStats() {
const one80DaysAgo = new Date(Date.now() - 180 * 24 * 60 * 60 * 1000);

Expand Down

0 comments on commit 4bf034e

Please sign in to comment.