first commit

This commit is contained in:
José Conde 2023-01-06 00:58:55 +01:00
commit 9a93aea9fe
18 changed files with 3080 additions and 0 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
node_modules/
.env

8
.env Normal file
View File

@ -0,0 +1,8 @@
PORT=3001
IVAO_APIKEY=RWTq29sxzAbVaerRnr4F2FLOskkLpqm5
TOKEN_AIRPORTDB=1419332340d1ac0b8c7921d2e38d763532b3d93e7f81382fd87356ac74347004877a5126bae6ef9de3a85f49b90e2426
MONGO_HOST=192.168.1.112
MONGO_PORT=27017
MONGO_USER=root
MONGO_PASS=UqaqF%26GKkQ4f2f
MONGO_DB=lts

16
.eslintrc.json Normal file
View File

@ -0,0 +1,16 @@
{
"env": {
"browser": false,
"node": true,
"commonjs": true,
"es2021": true
},
"extends": "eslint:recommended",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
}
}

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules
.history
.vscode
.env.local
.env.*.local

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM node:latest
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

View File

@ -0,0 +1,86 @@
const moment = require('moment');
const { getUserFromReferenceTable } = require('../db/mongo/mongoPilots');
const { getSessions } = require('../db/mongo/mongoSessions');
const { getHistoricalSessions } = require('../requests/ivao/session');
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;
}
async function checkUsername(user, key) {
const u = {...user };
if (!u.name) {
const ref = await getUserFromReferenceTable(key);
if (ref) {
u.name = ref.name;
} else {
console.log(`Couldn't find info for ${key}`);
}
}
return u;
}
async function getList(callsign) {
const from = moment().utc().startOf('month');
const to = moment().subtract(1, 'day').utc().endOf('day');
const todayData = await getTodaySessionsFromIvao(callsign);
const monthData = await getSessions(from, to);
const allData = [...todayData, ...monthData];
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);
if (!acc[userId]) {
acc[userId] = {
time: 0,
flights: 0
};
}
acc[userId].time += d.time;
acc[userId].flights++;
if (!acc[userId].lastFlight || date.isAfter(acc[userId].lastFlightDate)) {
acc[userId].lastFlight = {...flightPlan };
acc[userId].lastFlightDate = date;
acc[userId].lastCallsign = d.callsign;
delete acc[userId].lastFlight.id;
if (d.user.firstName) {
acc[userId].name = `${d.user.firstName} ${d.user.lastName || ''}`;
}
acc[userId].division = d.user.divisionId;
}
return acc;
}, {});
const array = [];
for (const key in totalsByUserId) {
if (Object.hasOwnProperty.call(totalsByUserId, key)) {
const user = await checkUsername(totalsByUserId[key], key);
user.vid = key;
array.push(user);
}
}
return array;
}
module.exports = {
getList,
};

View File

@ -0,0 +1,21 @@
const { MongoClient } = require('mongodb');
const {
MONGO_HOST,
MONGO_PORT,
MONGO_USER,
MONGO_PASS,
MONGO_DB,
} = process.env;
const uri = `mongodb://${MONGO_USER}:${MONGO_PASS}@${MONGO_HOST}:${MONGO_PORT}/?maxPoolSize=20`;
module.exports = {
getMongoConnection: async() => {
const client = new MongoClient(uri);
return await client.connect();
},
getMongoDatabase: (client, db) => {
const DB = db || MONGO_DB || 'lsa_leaderboard';
return client.db(DB);
}
};

View File

@ -0,0 +1,24 @@
const { getMongoConnection } = require("./mongoDBPool");
const {
MONGO_DB,
} = process.env;
const DB = MONGO_DB || 'lts';
async function getUserFromReferenceTable(vid) {
const conn = await getMongoConnection();
try {
const db = conn.db(DB);
const col = db.collection('pilots_ref');
const user = await col.findOne({ vid }, {});
return user;
} catch (err) {
console.error(err);
} finally {
await conn.close();
}
}
module.exports = {
getUserFromReferenceTable
};

View File

@ -0,0 +1,75 @@
const moment = require("moment/moment");
const { getMongoConnection } = require("./mongoDBPool");
const {
MONGO_DB,
} = process.env;
const DB = MONGO_DB || 'lts';
async function insertSessions(sessions, clear) {
const conn = await getMongoConnection();
try {
const db = conn.db(DB);
const sessionsCollection = db.collection('sessions');
if (clear) {
sessionsCollection.deleteMany({});
}
await sessionsCollection.insertMany(sessions);
} catch (err) {
console.error(err);
} finally {
await conn.close();
}
}
async function getSessions(start, end) {
const conn = await getMongoConnection();
try {
const db = conn.db(DB);
const sessionsCollection = db.collection('sessions');
const startDate = moment(start).utc().startOf('day').toDate();
const endDate = moment(end).utc().endOf('day').toDate();
const result = await sessionsCollection.aggregate([{
$addFields: {
completedDate: {
$dateFromString: {
"dateString": "$completedAt"
}
},
createdDate: {
$dateFromString: {
"dateString": "$createdAt"
}
},
updatedDate: {
$dateFromString: {
"dateString": "$updatedAt"
}
}
}
}, {
$match: {
createdDate: {
$gt: startDate,
$lte: endDate
}
}
}]).toArray();
return result;
} catch (err) {
console.error(err);
} finally {
await conn.close();
}
}
module.exports = {
insertSessions,
getSessions,
};
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2022-01-01T00:00:00&to=2023-01-04T23:59:59

22
app/requests/airports.js Normal file
View File

@ -0,0 +1,22 @@
//https://www.airport-data.com/api/ap_info.json?icao=KMIA
//https://airportdb.io/api/v1/airport/KMIA?apiToken=1419332340d1ac0b8c7921d2e38d763532b3d93e7f81382fd87356ac74347004877a5126bae6ef9de3a85f49b90e2426
const {
request
} = require('./request');
const apiToken = process.env.TOKEN_AIRPORTDB;
async function getAirport(icao = '') {
const url = `https://airportdb.io/api/v1/airport/${icao.toUpperCase()}`;
const options = {
params: {
apiToken
}
};
return await request(url, options);
}
module.exports = {
getAirport
};

View File

@ -0,0 +1,47 @@
const { request } = require('../request');
const getHistoricalSessions = async({ callsign, userId, from, to }) => {
//https://api.ivao.aero/v2/tracker/sessions?connectionType=PILOT&callsign=LTS&from=2022-01-01T00:00:00&to=2023-01-04T23:59:59&perPage=50
const url = 'https://api.ivao.aero/v2/tracker/sessions';
const params = {
connectionType: 'PILOT',
from,
page: 1,
perPage: 100,
to,
};
if (callsign) {
params.callsign = callsign;
}
if (userId) {
params.userId = userId;
}
const options = {
headers: {
apiKey: process.env.IVAO_APIKEY,
},
params,
};
return await _requestHistoricalRecursive([], url, options);
};
async function _requestHistoricalRecursive(data, url, options) {
console.log('options :>> ', options);
const { page, pages, items } = await request(url, options);
data = [...data, ...items];
if (page !== pages) {
options.params.page++;
return await _requestHistoricalRecursive(data, url, options)
} else {
return data;
}
}
module.exports = {
getHistoricalSessions,
}

View File

View File

@ -0,0 +1,8 @@
const { request } = require('../request');
const url = 'https://api.ivao.aero/v2/tracker/whazzup';
const getIvaoWazzup = async() => {
return await request(url);
};
module.exports = { getIvaoWazzup };

10
app/requests/request.js Normal file
View File

@ -0,0 +1,10 @@
const axios = require('axios');
const request = async(url, options) => {
const response = await axios.get(url, options);
return response.data;
};
module.exports = {
request,
};

50
app/routes/ivao.js Normal file
View File

@ -0,0 +1,50 @@
const express = require('express');
const { getList } = require('../controllers/sessionsController');
const { insertSessions, getSessions } = require('../db/mongo/mongoSessions');
const { getHistoricalSessions } = require('../requests/ivao/session');
const router = express.Router();
const { getIvaoWazzup } = require('../requests/ivao/wazzup');
router.get('/wazzup', async(req, res) => {
try {
const data = await getIvaoWazzup();
res.status(200).json(data);
} catch (err) {
console.log(err);
}
});
router.get('/sessions', async(req, res) => {
try {
const data = await getSessions(req.query);
res.status(200).json(data);
} catch (err) {
console.log(err);
}
});
router.get('/list/today', async(req, res) => {
try {
const data = await getList('LTS');
res.status(200).json(data);
} catch (err) {
console.log(err);
}
})
router.get('/init-sessions', async(req, res) => {
try {
const data = await getHistoricalSessions(req.query);
await insertSessions(data);
console.log(`${data.length} sessions inserted.`);
res.status(200);
} catch (err) {
console.log(err);
}
});
module.exports = router;
// http://localhost:3001/api/v1/ivao/sessions?callsign=LTS&from=2022-01-01T00:00:00&to=2022-12-31T23:59:59
// http://localhost:3001/api/v1/ivao/sessions?start=2023-01-05&to=2023-01-05

34
index.js Normal file
View File

@ -0,0 +1,34 @@
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const ivaoRoutes = require('./app/routes/ivao');
const app = express();
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
var corsOptions = {
origin: 'http://localhost:3000'
};
// use cors options
app.use(cors(corsOptions));
app.use(express.static('assets'));
// routes
app.use('/api/v1/ivao', ivaoRoutes);
// listening port
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});

2639
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "lts-stats",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon ./index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.2.2",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"moment": "^2.29.4",
"mongodb": "^4.13.0",
"node": "^19.3.0"
},
"devDependencies": {
"eslint": "^8.31.0",
"nodemon": "^2.0.20"
}
}