adding redis, tasks, api calls
This commit is contained in:
@ -72,4 +72,4 @@ module.exports = {
|
||||
getSessions,
|
||||
};
|
||||
|
||||
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2022-01-01T00:00:00&to=2023-01-04T23:59:59
|
||||
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2023-01-05T00:00:00&to=2023-01-05T23:59:59
|
29
app/db/mysql/lsaUsers.js
Normal file
29
app/db/mysql/lsaUsers.js
Normal file
@ -0,0 +1,29 @@
|
||||
const { query } = require("./mysqlPool");
|
||||
|
||||
async function getUsers() {
|
||||
const sql = `SELECT u.name,
|
||||
u.country, u.flights, u.flight_time AS flightTime, ufv.value as vid, u.email
|
||||
FROM users AS u
|
||||
INNER JOIN user_field_values AS ufv
|
||||
ON u.id = ufv.user_id
|
||||
WHERE ufv.user_field_id = 1`;
|
||||
|
||||
const result = await query(sql);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function getUsersWhitelist() {
|
||||
const sql = `SELECT ufv.value as vid, u.name, u.flights, u.flight_time AS flightTime
|
||||
FROM users AS u
|
||||
INNER JOIN user_field_values AS ufv
|
||||
ON u.id = ufv.user_id
|
||||
WHERE ufv.user_field_id = 1 AND u.flight_time > 60 * 200 AND u.state = 1`;
|
||||
|
||||
const result = await query(sql);
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUsers,
|
||||
getUsersWhitelist,
|
||||
}
|
47
app/db/mysql/mysqlPool.js
Normal file
47
app/db/mysql/mysqlPool.js
Normal file
@ -0,0 +1,47 @@
|
||||
const mysql = require('promise-mysql');
|
||||
|
||||
const {
|
||||
LSA_MYSQL_HOST,
|
||||
LSA_MYSQL_PORT,
|
||||
LSA_MYSQL_USER,
|
||||
LSA_MYSQL_PASS,
|
||||
LSA_MYSQL_DB,
|
||||
LSA_MYSQL_CONN_LIMIT,
|
||||
} = process.env;
|
||||
|
||||
const config = {
|
||||
connectionLimit: LSA_MYSQL_CONN_LIMIT,
|
||||
host: LSA_MYSQL_HOST,
|
||||
port: LSA_MYSQL_PORT,
|
||||
user: LSA_MYSQL_USER,
|
||||
password: LSA_MYSQL_PASS,
|
||||
database: LSA_MYSQL_DB
|
||||
};
|
||||
|
||||
let pool; //bunyan for logger
|
||||
|
||||
async function getMysqlPool() {
|
||||
try {
|
||||
if (!pool) {
|
||||
pool = await mysql.createPool(config);
|
||||
}
|
||||
return pool;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const query = async(sql) => {
|
||||
try {
|
||||
const pool = await getMysqlPool();
|
||||
return pool.query(sql);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMysqlPool,
|
||||
query
|
||||
};
|
82
app/db/redis/redis.js
Normal file
82
app/db/redis/redis.js
Normal file
@ -0,0 +1,82 @@
|
||||
const { createClient } = require('redis');
|
||||
|
||||
class RedisClient {
|
||||
constructor(host) {
|
||||
this._host = host;
|
||||
}
|
||||
|
||||
get client() {
|
||||
return this._client;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
const client = createClient({
|
||||
socket: {
|
||||
host: this._host,
|
||||
},
|
||||
});
|
||||
client.on('error', (err) => console.log('Redis Client Error', err));
|
||||
await client.connect();
|
||||
this._client = client;
|
||||
return client;
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
await this._client.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
async get(key) {
|
||||
const value = await this._client.get(key);
|
||||
return value;
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
await this._client.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RedisClient.setPair = async function(key, value) {
|
||||
const redis = new RedisClient(process.env.REDIS_HOST);
|
||||
await redis.connect();
|
||||
await redis.set(key, JSON.stringify(value));
|
||||
await redis.disconnect();
|
||||
}
|
||||
|
||||
RedisClient.getPair = async function(key) {
|
||||
const redis = new RedisClient(process.env.REDIS_HOST);
|
||||
await redis.connect();
|
||||
const value = await redis.get(key);
|
||||
await redis.disconnect();
|
||||
return JSON.parse(value);
|
||||
}
|
||||
|
||||
RedisClient.setCollection = async function(array) {
|
||||
const redis = new RedisClient(process.env.REDIS_HOST);
|
||||
|
||||
await redis.connect();
|
||||
if (Array.isArray(array)) {
|
||||
for (let index = 0; index < array.length; index++) {
|
||||
const pair = array[index];
|
||||
if (Array.isArray(pair)) {
|
||||
await redis.set(pair[0], JSON.stringify(pair[1]));
|
||||
} else {
|
||||
await redis.set(pair.key, JSON.stringify(pair.value));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const key in array) {
|
||||
if (Object.hasOwnProperty.call(array, key)) {
|
||||
const value = array[key];
|
||||
await redis.set(key, JSON.stringify(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
await redis.disconnect();
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
RedisClient,
|
||||
};
|
Reference in New Issue
Block a user