progress
This commit is contained in:
50
app/controllers/adminController.js
Normal file
50
app/controllers/adminController.js
Normal file
@ -0,0 +1,50 @@
|
||||
const moment = require('moment');
|
||||
const { insertSessions, insertSessionTracks } = require("../db/mongo/mongoSessions");
|
||||
const { getHistoricalSessions, getIvaoSessionTracks } = require("../requests/ivao/session");
|
||||
|
||||
async function initSessionsData(opts) {
|
||||
const { callsign, userId, from, clear = false } = opts;
|
||||
let to = opts.to;
|
||||
if (!to) {
|
||||
to = moment().utc().subtract(1, 'day').endOf('day').format();
|
||||
}
|
||||
const data = await getHistoricalSessions({
|
||||
callsign,
|
||||
userId,
|
||||
from,
|
||||
to
|
||||
});
|
||||
await insertSessions(data, clear);
|
||||
|
||||
console.log(`${data.length} sessions inserted.`);
|
||||
await initSessionsTracks(data.map(d => d.id), clear);
|
||||
}
|
||||
|
||||
async function initSessionsTracks(sessions, clear) {
|
||||
const array = [];
|
||||
const batchSize = 50;
|
||||
|
||||
for (let index = 0; index < sessions.length; index++) {
|
||||
const sessionId = sessions[index];
|
||||
const tracks = await getIvaoSessionTracks(sessionId);
|
||||
array.push({
|
||||
sessionId,
|
||||
tracks,
|
||||
});
|
||||
|
||||
if (index < sessions.length - 1 && (index + 1) % batchSize === 0) {
|
||||
await pause(5000);
|
||||
}
|
||||
}
|
||||
await insertSessionTracks(array, clear);
|
||||
|
||||
console.log(`${array.length} tracks inserted.`)
|
||||
}
|
||||
|
||||
async function pause(millis) {
|
||||
await new Promise((resolve) => setTimeout(resolve, millis));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initSessionsData,
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
const moment = require('moment');
|
||||
const { getUserFromReferenceTable } = require('../db/mongo/mongoPilots');
|
||||
const { getSessions } = require('../db/mongo/mongoSessions');
|
||||
const { getSessions, getSessionTracks, updateSessionTracks, insertOneSessionTracks } = require('../db/mongo/mongoSessions');
|
||||
const { RedisClient } = require('../db/redis/redis');
|
||||
const { getHistoricalSessions } = require('../requests/ivao/session');
|
||||
const { getHistoricalSessions, getIvaoSessionTracks, getIvaoPilotsNow, getIvaoLatestSessionFlightPlan, getIvaoSessionLatestTrack } = require('../requests/ivao/session');
|
||||
const { getAirTime } = require('./trackerAnalizer');
|
||||
|
||||
async function getTodaySessionsFromIvao(callsign, incompletes) {
|
||||
const from = moment().utc().startOf('day').format();
|
||||
@ -50,43 +51,64 @@ async function getWhitelist() {
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
async function getList(callsign) {
|
||||
const from = moment().utc().startOf('month');
|
||||
const to = moment().subtract(1, 'day').utc().endOf('day');
|
||||
const from = moment().startOf('month').format('YYYY-MM-DD');
|
||||
const to = moment().subtract(1, 'day').endOf('day').format('YYYY-MM-DD');
|
||||
const todayData = await getTodaySessionsFromIvao(callsign);
|
||||
const monthData = await getSessions(from, to);
|
||||
const allData = [...todayData, ...monthData];
|
||||
const redisUsers = await RedisClient.getPair('users');
|
||||
|
||||
const totalsByUserId = allData.reduce((acc, d) => {
|
||||
const userId = d.userId;
|
||||
const flightPlan = d.flightPlans[d.flightPlans.length - 1];
|
||||
const date = moment(d.completedAt);
|
||||
// console.log(date);
|
||||
const totalsByUserId = {};
|
||||
|
||||
if (!acc[userId]) {
|
||||
acc[userId] = {
|
||||
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] = {
|
||||
time: 0,
|
||||
flights: 0
|
||||
flights: 0,
|
||||
sessionsTime: 0,
|
||||
};
|
||||
}
|
||||
acc[userId].time += d.time;
|
||||
acc[userId].flights++;
|
||||
totalsByUserId[userId].time += calculated;
|
||||
totalsByUserId[userId].sessionsTime += session.time || 0;
|
||||
totalsByUserId[userId].flights++;
|
||||
|
||||
if (!acc[userId].lastFlight || date.isAfter(acc[userId].lastFlightDate)) {
|
||||
acc[userId].lastFlight = {...flightPlan };
|
||||
acc[userId].lastFlightDate = date;
|
||||
acc[userId].lastCallsign = d.callsign;
|
||||
if (!totalsByUserId[userId].lastFlight || date.isAfter(totalsByUserId[userId].lastFlightDate)) {
|
||||
totalsByUserId[userId].lastFlight = {...flightPlan };
|
||||
totalsByUserId[userId].lastFlightDate = date;
|
||||
totalsByUserId[userId].lastCallsign = session.callsign;
|
||||
|
||||
delete acc[userId].lastFlight.id;
|
||||
if (d.user.firstName) {
|
||||
acc[userId].name = `${d.user.firstName} ${d.user.lastName || ''}`;
|
||||
delete totalsByUserId[userId].lastFlight.id;
|
||||
if (session.user.firstName) {
|
||||
totalsByUserId[userId].name = `${session.user.firstName} ${session.user.lastName || ''}`;
|
||||
}
|
||||
acc[userId].division = d.user.divisionId;
|
||||
totalsByUserId[userId].division = session.user.divisionId;
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
const array = [];
|
||||
for (const key in totalsByUserId) {
|
||||
@ -96,11 +118,40 @@ async function getList(callsign) {
|
||||
array.push(user);
|
||||
}
|
||||
}
|
||||
return array.filter(d => d.time > 0);
|
||||
}
|
||||
|
||||
return array;
|
||||
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;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getList,
|
||||
getWhitelist,
|
||||
getLatestSessions,
|
||||
getLatestsFlightPlans,
|
||||
};
|
184
app/controllers/trackerAnalizer.js
Normal file
184
app/controllers/trackerAnalizer.js
Normal file
@ -0,0 +1,184 @@
|
||||
//50135458, 50949601
|
||||
|
||||
function getAirTime(tracks) {
|
||||
let accumTime = 0;
|
||||
if (tracks && tracks.length > 0) {
|
||||
let prev = tracks[0];
|
||||
let initialTime;
|
||||
let track;
|
||||
|
||||
if (tracks.length === 1) {
|
||||
return (prev.onGround === false) ? prev.time : 0;
|
||||
}
|
||||
|
||||
if (prev.onGround === false) {
|
||||
initialTime = 0;
|
||||
// console.log('initialTime', initialTime, prev.time);
|
||||
}
|
||||
for (let index = 1; index < tracks.length; index++) {
|
||||
track = tracks[index];
|
||||
if (prev.onGround !== track.onGround) {
|
||||
if (isInitialTimeSet(initialTime)) {
|
||||
accumTime += Math.round((prev.time + track.time) / 2) - initialTime;
|
||||
|
||||
// console.log('accumTime', accumTime, prev.time, track.time);
|
||||
initialTime = undefined;
|
||||
} else {
|
||||
initialTime = Math.round((prev.time + track.time) / 2);
|
||||
// console.log('initialTime', initialTime, prev.time, track.time);
|
||||
}
|
||||
}
|
||||
prev = track;
|
||||
}
|
||||
if (!track.onGround && isInitialTimeSet(initialTime)) {
|
||||
accumTime += track.time - initialTime;
|
||||
// console.log('accumTime', accumTime, track.time);
|
||||
}
|
||||
|
||||
}
|
||||
return accumTime;
|
||||
|
||||
}
|
||||
|
||||
function isInitialTimeSet(num) {
|
||||
return Number.isInteger(num);
|
||||
}
|
||||
|
||||
function calculateTime(tracks) {
|
||||
// const states = getShortStates(tracks);
|
||||
// const depTime = getDepartingTime(states);
|
||||
// const arrTime = getOnBlocksTime(states);
|
||||
// console.log((arrTime - depTime));
|
||||
// const time = ((arrTime - depTime));
|
||||
// console.log('time :>> ', `${Math.trunc(time / 60 / 60)}h ${Math.round((time % 1) * 60)}m`, );
|
||||
// return time;
|
||||
return getAirTime(tracks);
|
||||
}
|
||||
|
||||
function getShortStates(tracks) {
|
||||
let short = [];
|
||||
if (tracks && tracks.length > 0) {
|
||||
let track;
|
||||
let prev = tracks[0];
|
||||
short.push(getTrace(prev));
|
||||
let index = 1;
|
||||
for (; index < tracks.length - 1; index++) {
|
||||
track = tracks[index];
|
||||
if (prev.onGround !== track.onGround || prev.state !== track.state) {
|
||||
short.push(getTrace(track))
|
||||
}
|
||||
prev = track;
|
||||
}
|
||||
|
||||
short.push(getTrace(tracks[index]))
|
||||
|
||||
}
|
||||
|
||||
return short;
|
||||
|
||||
// if (!tracks || tracks.length === 0) {
|
||||
// return [];
|
||||
// }
|
||||
|
||||
// while (tracks[0] && tracks[0].state === 'En Route') {
|
||||
// tracks.shift();
|
||||
// }
|
||||
|
||||
// if (tracks.length === 0) {
|
||||
// return [];
|
||||
// }
|
||||
|
||||
// let lastState = '';
|
||||
// let lastOnGround;
|
||||
// let short = [];
|
||||
// let states = [];
|
||||
// let lastPushed = -1;
|
||||
// let index = 0;
|
||||
// let prevTrack;
|
||||
|
||||
// for (; index < tracks.length; index++) {
|
||||
// const track = tracks[index];
|
||||
// if (index > 0) {
|
||||
// prevTrack = tracks[index - 1];
|
||||
// }
|
||||
// if (track.state !== lastState || track.onGround !== lastOnGround) {
|
||||
// console.log(index, lastPushed);
|
||||
// if (index > 0 && index > lastPushed - 1) {
|
||||
// states.push(getTrace(prevTrack));
|
||||
// }
|
||||
// short.push(track);
|
||||
// states.push(getTrace(track));
|
||||
// lastPushed = index;
|
||||
// lastState = track.state;
|
||||
// lastOnGround = track.onGround;
|
||||
// }
|
||||
// }
|
||||
// if (index > 0 && index > lastPushed) {
|
||||
// states.push(getTrace(tracks[index - 1]));
|
||||
// }
|
||||
// return states;
|
||||
}
|
||||
|
||||
function getTrace(track) {
|
||||
const {
|
||||
state,
|
||||
time,
|
||||
onGround,
|
||||
groundSpeed,
|
||||
altitude,
|
||||
arrivalDistance,
|
||||
departureDistance,
|
||||
latitude,
|
||||
longitude,
|
||||
pitch,
|
||||
} = track;
|
||||
return {
|
||||
state,
|
||||
time,
|
||||
onGround,
|
||||
groundSpeed,
|
||||
altitude,
|
||||
arrivalDistance,
|
||||
departureDistance,
|
||||
latitude,
|
||||
longitude,
|
||||
pitch,
|
||||
};
|
||||
}
|
||||
|
||||
function getDepartingTime(states) {
|
||||
const prevState = 'Boarding';
|
||||
const nextState = 'Departing';
|
||||
|
||||
for (let index = 1; index < states.length; index++) {
|
||||
const prev = states[index - 1];
|
||||
const state = states[index];
|
||||
if (prev.state === prevState && state.state === nextState) {
|
||||
return Math.round((state.time + prev.time) / 2);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function getOnBlocksTime(states) {
|
||||
const STATE = 'On Blocks';
|
||||
const PREV_STATE = 'Landed';
|
||||
let time = -1;
|
||||
|
||||
for (let index = 1; index < states.length; index++) {
|
||||
const state = states[index];
|
||||
const prev = states[index - 1];
|
||||
if (prev.state === PREV_STATE && state.state === STATE) {
|
||||
time = Math.round((state.time + prev.time) / 2);
|
||||
}
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
calculateTime,
|
||||
getShortStates,
|
||||
getDepartingTime,
|
||||
getOnBlocksTime,
|
||||
getAirTime,
|
||||
};
|
Reference in New Issue
Block a user