logger upgraded, session server status upgraded

This commit is contained in:
2023-04-19 21:47:11 +03:00
parent ffa66977c3
commit 4895ca0d09
11 changed files with 135 additions and 80 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ const {port} = require('./config')
const app = require('express')()
const helmet = require('helmet')
const routes = require('./src/routes')
const { errors } = require('celebrate')
const {errors} = require('celebrate')
const body_parser = require('body-parser')
const {request_logger, error_logger} = require('./src/middlewares/logger')
const {options, cors} = require('./src/middlewares/cors')
+1 -1
Submodule lib updated: b3fa29e4c5...2daac94e92
+6 -7
View File
@@ -5,35 +5,34 @@ const {get_fastest_session_server, run_session} = require('../modules/session_se
const create_session = async (req, res, next) => {
const {title} = req.body
var free_servers = await database.get_free_session_servers(title)
let free_servers = await database.get_free_session_servers(title)
if (!free_servers) {
next(new not_found_error('no free session server or title not exists'))
return
}
var session_server = await get_fastest_session_server(free_servers)
let session_server = await get_fastest_session_server(free_servers)
if (!session_server) {
next(new not_found_error('all session servers are not available'))
return
}
var session_id = await database.generate_session_id()
let session_id = await database.generate_session_id()
if (!session_id) {
next(new not_found_error('can not generate session id'))
return
}
var websocket_url = await run_session(session_server.url, session_id, title)
let websocket_url = await run_session(session_server.url, session_id, title)
if (!websocket_url) {
next(new not_found_error('can not run session'))
return
}
var add_active_session_result = await database.add_active_session(session_server.url, session_id, websocket_url)
await database.add_session_to_history(session_server.url, session_id, websocket_url)
let add_active_session_result = await database.add_active_session(title, session_server.url, session_id, websocket_url)
if (!add_active_session_result) {
next(new not_found_error('add_active_session error'))
@@ -43,7 +42,7 @@ const create_session = async (req, res, next) => {
}
const connect_session = async (req, res, next) => {
var websocket_url = await database.get_session_websocket_url(req.query.session_id)
let websocket_url = await database.get_session_websocket_url(req.query.session_id)
if (!websocket_url) {
next(new not_found_error('session not exists'))
return
+32 -2
View File
@@ -2,12 +2,42 @@ const server_error = require('../../lib/src/http/errors/server_error')
const database = require('../database/database')
const session_closed = async (req, res, next) => {
var remove_result = await database.remove_active_session(req.body.session_id)
const {
session_id,
title,
max_uniq_users,
users_at_close,
time,
unix_timestamp,
time_end,
unix_timestamp_end
} = req.body
let active_session = await database.get_active_session(session_id)
if (active_session) {
await database.add_session_to_history(
session_id,
title,
active_session.server_url,
active_session.websocket_url,
max_uniq_users,
users_at_close,
time,
unix_timestamp,
time_end,
unix_timestamp_end
)
}
let remove_result = await database.remove_active_session(session_id)
if (!remove_result) {
next(new server_error('remove session error'))
return
}
res.json({message:"removed"})
res.json({message:"successfully removed"})
}
module.exports = {
+1 -1
View File
@@ -6,7 +6,7 @@ const {ping_session_server} = require('../modules/session_server.js')
const max_response_timeout = 500
const validate_url = async (domain) => {
var options = {
let options = {
url: domain,
method: "GET",
path: '/',
+3 -3
View File
@@ -2,7 +2,7 @@ const database = require('../database/database.js')
const not_found_error = require('../../lib/src/http/errors/not_found_error')
const get_titles = async (req, res, next) => {
var titles = await database.get_titles(req.query.start, req.query.count)
let titles = await database.get_titles(req.query.start, req.query.count)
if (!titles) {
next(new not_found_error('titles not found'))
@@ -13,7 +13,7 @@ const get_titles = async (req, res, next) => {
}
const get_titles_for_language = async (req, res, next) => {
var titles = await database.get_titles_for_language(req.query.start, req.query.count, req.query.language.toLowerCase())
let titles = await database.get_titles_for_language(req.query.start, req.query.count, req.query.language.toLowerCase())
if (!titles) {
next(new not_found_error('titles not found'))
@@ -25,7 +25,7 @@ const get_titles_for_language = async (req, res, next) => {
const get_private_titles = async (req, res, next) => {
const {key} = req.query
var titles = await database.get_private_titles_with_key(key)
let titles = await database.get_private_titles_with_key(key)
if (!titles) {
next(new not_found_error('title not found'))
+75 -42
View File
@@ -1,7 +1,7 @@
const { MongoClient } = require("mongodb")
const { database_url, database_name } = require('../../config')
const crypto = require("crypto")
const time = require('../modules/time')
const time = require('../../lib/src/time')
let max_database_connection_timeout = 1000
const client = new MongoClient(database_url, {
@@ -22,50 +22,50 @@ const get_db = async () => {
}
const get_titles = async (start, count) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var titles = await db.collection('title').find().skip(parseInt(start)).limit(parseInt(count)).toArray()
let titles = await db.collection('title').find().skip(parseInt(start)).limit(parseInt(count)).toArray()
return (!titles.length) ? null : titles
}
const get_titles_for_language = async (start, count, language) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var titles = await db.collection('title').find({language:language}).skip(parseInt(start)).limit(parseInt(count)).toArray()
let titles = await db.collection('title').find({language:language}).skip(parseInt(start)).limit(parseInt(count)).toArray()
return (!titles.length) ? null : titles
}
const get_private_titles_with_key = async (key) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var titles = await db.collection('private_title').find({key:key}).toArray()
let titles = await db.collection('private_title').find({key:key}).toArray()
return (!titles.length) ? null : titles
}
const get_free_session_servers = async (title) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var servers = await db.collection('session_server').find({title:title}).toArray()
let servers = await db.collection('session_server').find({title:title}).toArray()
if (!servers.length) {
return null
}
var active_session = db.collection('active_session')
let active_session = db.collection('active_session')
var free_servers = []
let free_servers = []
for (const server of servers) {
var sessions = await active_session.find({server_url:server.url}).toArray()
let sessions = await active_session.find({server_url:server.url}).toArray()
if (server.limit > sessions.length) {
free_servers.push(server)
}
@@ -73,33 +73,35 @@ const get_free_session_servers = async (title) => {
return (free_servers.length) ? free_servers : null
}
const add_active_session = async (server_url, session_id, websocket_url) => {
var db = await get_db()
const add_active_session = async (title, server_url, session_id, websocket_url) => {
let db = await get_db()
if (!db) {
return false
}
var active_session = db.collection('active_session')
let active_session = db.collection('active_session')
await active_session.insertOne({
title: title,
server_url: server_url,
session_id: session_id,
websocket_url: websocket_url
})
return true
}
const generate_session_id = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var active_session = db.collection('active_session')
var session_id = {}
let active_session = db.collection('active_session')
let session_id = {}
var max_id_len = 6
var sessions_with_id = 0
let max_id_len = 6
let sessions_with_id = 0
while(true) {
session_id = crypto.randomBytes(max_id_len/2).toString('hex').toUpperCase()
sessions_with_id = await active_session.find({session_id:session_id}).toArray().length
@@ -109,8 +111,18 @@ const generate_session_id = async () => {
}
}
const get_active_session = async (session_id) => {
let db = await get_db()
if (!db) {
return null
}
let active_session = await db.collection('active_session').findOne({session_id:session_id})
return active_session
}
const remove_active_session = async (session_id) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return false
}
@@ -120,12 +132,12 @@ const remove_active_session = async (session_id) => {
}
const get_session_websocket_url = async (session_id) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var active_session = db.collection('active_session')
var session = await active_session.findOne({session_id:session_id})
let active_session = db.collection('active_session')
let session = await active_session.findOne({session_id:session_id})
if (!session) {
return null
}
@@ -133,21 +145,21 @@ const get_session_websocket_url = async (session_id) => {
}
const get_statuses = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var statuses = await db.collection('status').find().toArray()
let statuses = await db.collection('status').find().toArray()
return (!statuses.length) ? null : statuses
}
const get_all_session_servers = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var servers = await db.collection('session_server').find().toArray()
let servers = await db.collection('session_server').find().toArray()
if (!servers.length) {
return null
@@ -156,33 +168,33 @@ const get_all_session_servers = async () => {
}
const add_error = async (error_message) => {
var db = await get_db()
let db = await get_db()
if (!db) {
return false
}
var error_collection = db.collection('error')
let error_collection = db.collection('error_history')
await error_collection.insertOne({error_message:error_message, time: time.get_date_time()})
await error_collection.insertOne({error_message:error_message, time: time.get_date_time(), unix_timestamp: time.get_unix_timestamp()})
return true
}
const get_errors = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var errors = await db.collection('error').find().toArray()
let errors = await db.collection('error_history').find().toArray()
return (!errors.length) ? null : errors
}
const get_all_active_sessions = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var active_sessions = await db.collection('active_session').find().toArray()
let active_sessions = await db.collection('active_session').find().toArray()
if (!active_sessions.length) {
return null
@@ -190,30 +202,50 @@ const get_all_active_sessions = async () => {
return active_sessions
}
const add_session_to_history = async (server_url, session_id, websocket_url) => {
var db = await get_db()
const add_session_to_history = async (session_id,
title,
server_url,
websocket_url,
max_uniq_users,
users_at_close,
time_start,
unix_timestamp_start,
time_end,
unix_timestamp_end
) => {
let db = await get_db()
if (!db) {
return false
}
var session_history = db.collection('session_history')
let session_history = db.collection('session_history')
let duration_in_seconds = unix_timestamp_end - unix_timestamp_start
await session_history.insertOne({
server_url: server_url,
session_id: session_id,
title: title,
server_url: server_url,
websocket_url: websocket_url,
time: time.get_date_time()
max_uniq_users: max_uniq_users,
users_at_close: users_at_close,
time_start: time_start,
unix_timestamp_start: unix_timestamp_start,
time_end: time_end,
unix_timestamp_end: unix_timestamp_end,
duration_in_seconds: duration_in_seconds,
duration_in_minutes: duration_in_seconds / 60
})
return true
}
const get_all_session_history = async () => {
var db = await get_db()
let db = await get_db()
if (!db) {
return null
}
var session_history = await db.collection('session_history').find().toArray()
let session_history = await db.collection('session_history').find().toArray()
if (!session_history.length) {
return null
@@ -227,6 +259,7 @@ module.exports = {
add_active_session,
generate_session_id,
remove_active_session,
get_active_session,
get_session_websocket_url,
get_titles_for_language,
get_private_titles_with_key,
+1 -1
View File
@@ -4,7 +4,7 @@ const {log_path} = require('../../config')
const logger_runtime = new _logger(log_path)
const request_logger = (req, res, next) => {
var {url} = req
let {url} = req
logger_runtime.log(url)
next()
}
+7 -7
View File
@@ -1,11 +1,11 @@
var request = require('request')
let request = require('request')
const max_response_timeout = 2000
const max_run_session_timeout = 20000
const ping_session_server = async (url) => {
var options = {
let options = {
url: url + '/ping',
method: "GET",
timeout: max_response_timeout
@@ -25,7 +25,7 @@ const ping_session_server = async (url) => {
}
const get_fastest_session_server = async (free_servers) => {
var fastest_server = null
let fastest_server = null
Promise.all(free_servers.map(async (server) => {
const is_working = await ping_session_server(server.url)
@@ -36,8 +36,8 @@ const get_fastest_session_server = async (free_servers) => {
}
}))
var timeout_count = 0
var check_times = 100
let timeout_count = 0
let check_times = 100
while (true) {
++timeout_count
@@ -53,7 +53,7 @@ const get_fastest_session_server = async (free_servers) => {
}
const run_session = async (server_url, session_id, title) => {
var options = {
let options = {
url: server_url + '/session/run',
method: "POST",
timeout: max_run_session_timeout,
@@ -81,7 +81,7 @@ const run_session = async (server_url, session_id, title) => {
const get_overal_status = async (url) => {
var options = {
let options = {
url: url + '/get_overal_status',
method: "GET",
timeout: max_response_timeout
-14
View File
@@ -1,14 +0,0 @@
const get_date_time = () => {
let currentdate = new Date();
let datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds()
return datetime
}
module.exports = {
get_date_time
}
+8 -1
View File
@@ -4,7 +4,14 @@ const { celebrate, Joi, Segments} = require('celebrate')
router.post('/session_closed', celebrate({
[Segments.BODY]: Joi.object().keys({
session_id: Joi.string().required()
session_id: Joi.string().required(),
title: Joi.string().required(),
max_uniq_users: Joi.number().required(),
users_at_close: Joi.number().required(),
time: Joi.string().required(),
unix_timestamp: Joi.number().required(),
time_end: Joi.string().required(),
unix_timestamp_end: Joi.number().required()
})
}), session_closed)