lts-stats-api/app/controllers/sessionsController.js

157 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-01-06 00:58:55 +01:00
const moment = require('moment');
const { getUserFromReferenceTable } = require('../db/mongo/mongoPilots');
2023-01-15 18:42:26 +01:00
const { getSessions, getSessionTracks, updateSessionTracks, insertOneSessionTracks } = require('../db/mongo/mongoSessions');
2023-01-06 19:12:43 +01:00
const { RedisClient } = require('../db/redis/redis');
2023-01-15 18:42:26 +01:00
const { getHistoricalSessions, getIvaoSessionTracks, getIvaoPilotsNow, getIvaoLatestSessionFlightPlan, getIvaoSessionLatestTrack } = require('../requests/ivao/session');
const { getAirTime } = require('./trackerAnalizer');
2023-01-06 00:58:55 +01:00
async function getTodaySessionsFromIvao(callsign, incompletes) {
const from = moment().utc().startOf('day').format();
const to = moment().utc().endOf('day').format();
const params = {
callsign,
from,
to,
};
const data = await getHistoricalSessions(params);
if (!incompletes) {
return data.filter(d => d.isCompleted);
}
return data;
}
2023-01-06 19:12:43 +01:00
async function checkUsername(user, key, usersList) {
2023-01-06 00:58:55 +01:00
const u = {...user };
if (!u.name) {
2023-01-06 19:12:43 +01:00
const ref1 = await getUserFromReferenceTable(key) || {};
const ref2 = usersList.find(d => key === d.vid) || {};
let ref;
if (ref2.name) {
ref = ref2;
if (!ref1.name) {
// TODO: update mongo
}
} else if (ref1.name) {
ref = ref1
}
2023-01-06 00:58:55 +01:00
if (ref) {
u.name = ref.name;
} else {
console.log(`Couldn't find info for ${key}`);
}
}
return u;
}
2023-01-06 19:12:43 +01:00
async function getWhitelist() {
const redisUsers = await RedisClient.getPair('users_whitelist');
return redisUsers.sort((a, b) => b.flightTime - a.flightTime);
}
2023-01-15 18:42:26 +01:00
async function getSessionCalculatedTime(sessionId) {
let tracks = await getSessionTracks(sessionId);
if (tracks && !Number.isInteger(tracks.calculatedTime)) {
tracks.calculatedTime = getAirTime(tracks);
await updateSessionTracks(tracks);
} else if (!tracks) {
const t = await getIvaoSessionTracks(sessionId);
tracks = {
sessionId,
tracks: t,
calculatedTime: getAirTime(t)
};
await insertOneSessionTracks(tracks);
}
return tracks.calculatedTime;
}
2023-01-06 00:58:55 +01:00
async function getList(callsign) {
2023-01-15 18:42:26 +01:00
const from = moment().startOf('month').format('YYYY-MM-DD');
const to = moment().subtract(1, 'day').endOf('day').format('YYYY-MM-DD');
2023-01-06 00:58:55 +01:00
const todayData = await getTodaySessionsFromIvao(callsign);
const monthData = await getSessions(from, to);
const allData = [...todayData, ...monthData];
2023-01-06 19:12:43 +01:00
const redisUsers = await RedisClient.getPair('users');
2023-01-06 00:58:55 +01:00
2023-01-15 18:42:26 +01:00
const totalsByUserId = {};
2023-01-06 00:58:55 +01:00
2023-01-15 18:42:26 +01:00
for (let index = 0; index < allData.length; index++) {
const session = allData[index];
const userId = session.userId;
const flightPlan = session.flightPlans[session.flightPlans.length - 1];
const date = moment(session.completedAt);
const calculated = await getSessionCalculatedTime(session.id);
if (!totalsByUserId[userId]) {
totalsByUserId[userId] = {
2023-01-06 00:58:55 +01:00
time: 0,
2023-01-15 18:42:26 +01:00
flights: 0,
sessionsTime: 0,
2023-01-06 00:58:55 +01:00
};
}
2023-01-15 18:42:26 +01:00
totalsByUserId[userId].time += calculated;
totalsByUserId[userId].sessionsTime += session.time || 0;
totalsByUserId[userId].flights++;
2023-01-06 00:58:55 +01:00
2023-01-15 18:42:26 +01:00
if (!totalsByUserId[userId].lastFlight || date.isAfter(totalsByUserId[userId].lastFlightDate)) {
totalsByUserId[userId].lastFlight = {...flightPlan };
totalsByUserId[userId].lastFlightDate = date;
totalsByUserId[userId].lastCallsign = session.callsign;
2023-01-06 00:58:55 +01:00
2023-01-15 18:42:26 +01:00
delete totalsByUserId[userId].lastFlight.id;
if (session.user.firstName) {
totalsByUserId[userId].name = `${session.user.firstName} ${session.user.lastName || ''}`;
2023-01-06 00:58:55 +01:00
}
2023-01-15 18:42:26 +01:00
totalsByUserId[userId].division = session.user.divisionId;
2023-01-06 00:58:55 +01:00
}
2023-01-15 18:42:26 +01:00
}
2023-01-06 00:58:55 +01:00
const array = [];
for (const key in totalsByUserId) {
if (Object.hasOwnProperty.call(totalsByUserId, key)) {
2023-01-06 19:12:43 +01:00
const user = await checkUsername(totalsByUserId[key], key, redisUsers);
2023-01-06 00:58:55 +01:00
user.vid = key;
array.push(user);
}
}
2023-01-15 18:42:26 +01:00
return array.filter(d => d.time > 0);
}
2023-01-06 00:58:55 +01:00
2023-01-15 18:42:26 +01:00
async function getLatestSessions() {
return await getIvaoPilotsNow();
}
async function getLatestsFlightPlans() {
const sessionsNow = await getLatestSessions();
const response = [];
for (let index = 0; index < sessionsNow.length; index++) {
const session = sessionsNow[index];
const sessionFlightplan = await getIvaoLatestSessionFlightPlan(session.id);
const track = await getIvaoSessionLatestTrack(session.id);
const fplan = {};
fplan.sessionId = session.id;
fplan.callsign = session.callsign;
fplan.arrival = sessionFlightplan.arrival;
fplan.departure = sessionFlightplan.departure;
fplan.departureTime = sessionFlightplan.departureTime;
fplan.eet = sessionFlightplan.eet;
fplan.eta = track.groundSpeed === 0 ? 0 : Math.round((track.arrivalDistance / track.groundSpeed) * 3600);
fplan.arrivalDistance = track.arrivalDistance;
fplan.groundSpeed = track.groundSpeed;
response.push(fplan);
}
return response;
2023-01-06 00:58:55 +01:00
}
module.exports = {
getList,
2023-01-06 19:12:43 +01:00
getWhitelist,
2023-01-15 18:42:26 +01:00
getLatestSessions,
getLatestsFlightPlans,
2023-01-06 00:58:55 +01:00
};