progress
This commit is contained in:
@ -9,13 +9,34 @@ const {
|
||||
|
||||
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);
|
||||
const getMongoConnection = async() => {
|
||||
const client = new MongoClient(uri);
|
||||
return await client.connect();
|
||||
};
|
||||
|
||||
const getMongoDatabase = (client, db) => {
|
||||
const DB = db || MONGO_DB;
|
||||
return client.db(DB);
|
||||
};
|
||||
|
||||
const mongoExecute = async(fn, dbName = MONGO_DB) => {
|
||||
let conn;
|
||||
try {
|
||||
conn = await getMongoConnection();
|
||||
const db = conn.db(dbName);
|
||||
const result = await fn(db, conn);
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.log('MOMGODB ERROR:', err.message);
|
||||
} finally {
|
||||
if (conn) {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
mongoExecute,
|
||||
getMongoConnection,
|
||||
getMongoDatabase,
|
||||
};
|
@ -1,15 +1,7 @@
|
||||
const moment = require("moment/moment");
|
||||
const { getMongoConnection } = require("./mongoDBPool");
|
||||
const {
|
||||
MONGO_DB,
|
||||
} = process.env;
|
||||
|
||||
const DB = MONGO_DB || 'lts';
|
||||
const { mongoExecute } = require("./mongoDBPool");
|
||||
|
||||
async function insertSessions(sessions, clear) {
|
||||
const conn = await getMongoConnection();
|
||||
try {
|
||||
const db = conn.db(DB);
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessions');
|
||||
|
||||
if (clear) {
|
||||
@ -18,21 +10,36 @@ async function insertSessions(sessions, clear) {
|
||||
|
||||
await sessionsCollection.insertMany(sessions);
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function insertOneSessionTracks(sessionTracks) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessionTracks');
|
||||
|
||||
await sessionsCollection.insertOne(sessionTracks);
|
||||
});
|
||||
}
|
||||
|
||||
async function insertSessionTracks(sessionTracks, clear) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessionTracks');
|
||||
|
||||
if (clear) {
|
||||
sessionsCollection.deleteMany({});
|
||||
}
|
||||
|
||||
await sessionsCollection.insertMany(sessionTracks);
|
||||
});
|
||||
}
|
||||
|
||||
async function getSessions(start, end) {
|
||||
const conn = await getMongoConnection();
|
||||
try {
|
||||
const db = conn.db(DB);
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessions');
|
||||
|
||||
const startDate = moment(start).utc().startOf('day').toDate();
|
||||
const endDate = moment(end).utc().endOf('day').toDate();
|
||||
const startDate = start + 'T00:00:00.000Z';
|
||||
const endDate = end + 'T23:59:59.999Z';
|
||||
const result = await sessionsCollection.aggregate([{
|
||||
$addFields: {
|
||||
completedDate: {
|
||||
@ -54,22 +61,39 @@ async function getSessions(start, end) {
|
||||
}, {
|
||||
$match: {
|
||||
createdDate: {
|
||||
$gt: startDate,
|
||||
$lte: endDate
|
||||
$gt: new Date(startDate),
|
||||
$lte: new Date(endDate)
|
||||
}
|
||||
}
|
||||
}]).toArray();
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function getSessionTracks(sessionId) {
|
||||
return mongoExecute(
|
||||
async(db) => {
|
||||
const tracksCollection = db.collection('sessionTracks');
|
||||
const tracks = await tracksCollection.findOne({ sessionId });
|
||||
return tracks;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function updateSessionTracks(tracks) {
|
||||
return mongoExecute(async(db) => {
|
||||
const tracksCollection = db.collection('sessionTracks');
|
||||
await tracksCollection.updateOne({ _id: tracks._id }, { $set: { calculatedTime: tracks.calculatedTime } });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
insertSessions,
|
||||
insertSessionTracks,
|
||||
insertOneSessionTracks,
|
||||
getSessions,
|
||||
getSessionTracks,
|
||||
updateSessionTracks,
|
||||
};
|
||||
|
||||
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2023-01-05T00:00:00&to=2023-01-05T23:59:59
|
@ -27,7 +27,7 @@ async function getMysqlPool() {
|
||||
}
|
||||
return pool;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
console.log('error', err);
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ const query = async(sql) => {
|
||||
const pool = await getMysqlPool();
|
||||
return pool.query(sql);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
console.log('error', err);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user