Update log methods
This commit is contained in:
parent
cc2895bf40
commit
17ea61d35b
21
app.js
21
app.js
@ -9,18 +9,19 @@ const express = require('express');
|
|||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// Internal Requires
|
// Internal Requires
|
||||||
const version = require('./src/configs/version.configs');
|
const log = require('./src/utils/log.utils'); // Log Helper
|
||||||
const testRtr = require('./src/routes/test.routes');
|
const version = require('./src/configs/version.configs'); // Version Strings
|
||||||
const listRtr = require('./src/routes/list.routes');
|
const listRtr = require('./src/routes/list.routes'); // /list endpoints
|
||||||
const ldbRtr = require('./src/routes/ldb.routes');
|
const ldbRtr = require('./src/routes/ldb.routes'); // /ldb endpoints
|
||||||
const kubeRtr = require('./src/routes/kube.routes');
|
const kubeRtr = require('./src/routes/kube.routes'); // /kube endpoints
|
||||||
|
const initDb = require('./src/utils/dbinit.utils') // DB Init Utility
|
||||||
|
|
||||||
// Set Server Configurations
|
// Set Server Configurations
|
||||||
const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"
|
const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"
|
||||||
const srvPort = process.env.OWL_SRV_PORT || 8460
|
const srvPort = process.env.OWL_SRV_PORT || 8460
|
||||||
|
|
||||||
// Print version number:
|
// Print version number:
|
||||||
console.log(`Starting OwlBoard - App Version: ${version.app} - API versions: ${version.api}`);
|
log.out(`Starting OwlBoard - App Version: ${version.app} - API versions: ${version.api}`);
|
||||||
|
|
||||||
// Test for required vars:
|
// Test for required vars:
|
||||||
// const varTest = require('./src/utils/varTest.utils');
|
// const varTest = require('./src/utils/varTest.utils');
|
||||||
@ -31,6 +32,9 @@ console.log(`Starting OwlBoard - App Version: ${version.app} - API versions: ${v
|
|||||||
// console.log("Unable to start, missing required vars")
|
// console.log("Unable to start, missing required vars")
|
||||||
// exit app
|
// exit app
|
||||||
|
|
||||||
|
// DB Init
|
||||||
|
initDb.check();
|
||||||
|
|
||||||
// Express Error Handling:
|
// Express Error Handling:
|
||||||
app.use((err, req, res, next) => {
|
app.use((err, req, res, next) => {
|
||||||
const statusCode = err.statuscode || 500;
|
const statusCode = err.statuscode || 500;
|
||||||
@ -51,8 +55,9 @@ app.use('/api/v1/kube', kubeRtr);
|
|||||||
// Start Express
|
// Start Express
|
||||||
app.listen(srvPort, srvListen, (error) =>{
|
app.listen(srvPort, srvListen, (error) =>{
|
||||||
if(!error) {
|
if(!error) {
|
||||||
console.log(`Started server on http://${srvListen}:${srvPort}`);
|
log.out(`Server: Started on http://${srvListen}:${srvPort}`);
|
||||||
|
log.out("State: alive")
|
||||||
} else {
|
} else {
|
||||||
console.log("Error occurred, server can't start", error);
|
log.out("Error occurred, server can't start", error);
|
||||||
}
|
}
|
||||||
});
|
});
|
@ -9,6 +9,8 @@
|
|||||||
// extract() : Extract the CORPUS JSON file from the GZIP file.
|
// extract() : Extract the CORPUS JSON file from the GZIP file.
|
||||||
// clean() : Cleans the CORPUS data, removing unneccesary non-stations from the data.
|
// clean() : Cleans the CORPUS data, removing unneccesary non-stations from the data.
|
||||||
|
|
||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
|
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const gz = require('node-gzip')
|
const gz = require('node-gzip')
|
||||||
|
|
||||||
@ -27,6 +29,7 @@ async function get(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetch() {
|
async function fetch() {
|
||||||
|
log.out("Corpus: Fetching CORPUS Data from Network Rail")
|
||||||
authHead = Buffer.from(`${corpusUser}:${corpusPass}`).toString('base64')
|
authHead = Buffer.from(`${corpusUser}:${corpusPass}`).toString('base64')
|
||||||
const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS'
|
const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS'
|
||||||
const options = {
|
const options = {
|
||||||
@ -35,11 +38,18 @@ async function fetch() {
|
|||||||
headers: {'Authorization': `Basic ${authHead}`},
|
headers: {'Authorization': `Basic ${authHead}`},
|
||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
var { data } = await axios.get(url, options)
|
var { data } = await axios.get(url, options)
|
||||||
|
log.out("Corpus: CORPUS Data fetched")
|
||||||
|
} catch (error) {
|
||||||
|
log.out("Corpus: Error fetching CORPUS")
|
||||||
|
log.out(error)
|
||||||
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
async function extract(input) {
|
async function extract(input) {
|
||||||
|
log.out(`Corpus: Extracting CORPUS archive`)
|
||||||
var raw = await gz.ungzip(input)
|
var raw = await gz.ungzip(input)
|
||||||
var obj = await JSON.parse(raw)
|
var obj = await JSON.parse(raw)
|
||||||
var output = obj.TIPLOCDATA
|
var output = obj.TIPLOCDATA
|
||||||
@ -47,6 +57,7 @@ async function extract(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function clean(input) {
|
async function clean(input) {
|
||||||
|
log.out(`Corpus: Removing non-stations from CORPUS data`)
|
||||||
let clean = [];
|
let clean = [];
|
||||||
for (const element of input) {
|
for (const element of input) {
|
||||||
if (element.STANOX != ' ' && element['3ALPHA'] != ' '){
|
if (element.STANOX != ' ' && element['3ALPHA'] != ' '){
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
|
|
||||||
const dbUser = process.env.OWL_DB_USER || "owl"
|
const dbUser = process.env.OWL_DB_USER || "owl"
|
||||||
const dbPass = process.env.OWL_DB_PASS || "twittwoo"
|
const dbPass = process.env.OWL_DB_PASS || "twittwoo"
|
||||||
const dbName = process.env.OWL_DB_NAME || "owlboard"
|
const dbName = process.env.OWL_DB_NAME || "owlboard"
|
||||||
@ -10,39 +12,52 @@ const { MongoClient } = require('mongodb');
|
|||||||
const client = new MongoClient(uri);
|
const client = new MongoClient(uri);
|
||||||
const db = client.db(dbName);
|
const db = client.db(dbName);
|
||||||
|
|
||||||
|
|
||||||
async function putCorpus(data){
|
async function putCorpus(data){
|
||||||
|
log.out("DbAccess: Uploading Corpus data to database")
|
||||||
await client.connect();
|
await client.connect();
|
||||||
var coll = db.collection("corpus");
|
var coll = db.collection("corpus");
|
||||||
var result = coll.insertMany(data);
|
try {
|
||||||
console.log(result);
|
await coll.insertMany(data);
|
||||||
|
log.out("DbAccess: Corpus upload complete")
|
||||||
|
} catch (error) {
|
||||||
|
log.out("DbAccess: Error uploading Corpus data to database")
|
||||||
|
log.out(error)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function putStations(data){
|
async function putStations(data){
|
||||||
|
log.out("DbAccess: Uploading Stations data to database")
|
||||||
await client.connect();
|
await client.connect();
|
||||||
var coll = db.collection("stations");
|
var coll = db.collection("stations");
|
||||||
var result = coll.insertMany(data);
|
try {
|
||||||
console.log(result);
|
coll.insertMany(data);
|
||||||
|
log.out("DbAccess: Stations upload complete")
|
||||||
|
} catch (error) {
|
||||||
|
log.out("DbAccess: Error uploading Stations data to database")
|
||||||
|
log.out(error)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function putMeta(data){
|
async function putMeta(data){
|
||||||
await client.connect();
|
await client.connect();
|
||||||
var coll = db.collection("meta");
|
var coll = db.collection("meta");
|
||||||
var result = coll.insertMany(data);
|
coll.insertMany(data);
|
||||||
console.log(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//async function putData(data,collection) {
|
//async function putData(data,collection) {
|
||||||
// await client.connect()
|
// await client.connect()
|
||||||
// var coll = db.collection(collection);
|
// var coll = db.collection(collection);
|
||||||
// var result = coll.insertMany(data);
|
// var result = coll.insertMany(data);
|
||||||
// console.log(result);
|
// log.out(result);
|
||||||
//};
|
//};
|
||||||
|
|
||||||
async function query(collection, query){
|
async function query(collection, query){
|
||||||
await client.connect();
|
await client.connect();
|
||||||
var coll = db.collection(collection);
|
let qcoll = db.collection(collection);
|
||||||
var result = coll.find(query) //???
|
let qcursor = qcoll.find(query)
|
||||||
|
let qresult = await qcursor.toArray();
|
||||||
|
return qresult;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
// convertTiploc(TIPLOC) : Exported: Looks up CRS, Name & STANOX for Tiploc
|
// convertTiploc(TIPLOC) : Exported: Looks up CRS, Name & STANOX for Tiploc
|
||||||
|
|
||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
|
|
||||||
const ldb = require('ldbs-json')
|
const ldb = require('ldbs-json')
|
||||||
const util = require('../utils/ldb.utils')
|
const util = require('../utils/ldb.utils')
|
||||||
|
|
||||||
@ -27,6 +29,7 @@ async function get(body, id){
|
|||||||
|
|
||||||
async function arrDepBoard(CRS){
|
async function arrDepBoard(CRS){
|
||||||
var valid = await util.checkCrs(CRS)
|
var valid = await util.checkCrs(CRS)
|
||||||
|
log.out(`ldbService: Fetching ArrDep Board for ${CRS}`)
|
||||||
if (valid != false){
|
if (valid != false){
|
||||||
var options = {
|
var options = {
|
||||||
numRows: 10,
|
numRows: 10,
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// check() :
|
// check() :
|
||||||
// build() : Adds CORPUS Data to DB.
|
// build() : Adds CORPUS Data to DB.
|
||||||
|
|
||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
const corpus = require('../services/corpus.services');
|
const corpus = require('../services/corpus.services');
|
||||||
const dbAccess = require('../services/dbAccess.services');
|
const dbAccess = require('../services/dbAccess.services');
|
||||||
|
|
||||||
@ -12,27 +13,36 @@ async function init(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function check(){
|
async function check(){
|
||||||
// Check meta collection for last update
|
log.out("DbInit: Checking database state")
|
||||||
// If last update more than 14 days () ago then return false
|
let test = {'collection': 'any'};
|
||||||
// Check corpus and stations collections exist. If not return false
|
try {
|
||||||
// Return true if checks pass.
|
let meta = await dbAccess.query('meta',test);
|
||||||
|
log.out(`DbInit: Reading Database Collection: meta`);
|
||||||
|
} catch (error) {log.out(error)}
|
||||||
|
if (false){
|
||||||
|
log.out("DbInit: Database structure not initialised")
|
||||||
|
build("all")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
|
async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
|
||||||
|
log.out("DbInit: Building database structure")
|
||||||
var corpusAll = await corpus.get();
|
var corpusAll = await corpus.get();
|
||||||
if (db === "corpus" || "all") {
|
if (db === "corpus" || "all") {
|
||||||
dbAccess.putCorpus(corpusAll);
|
dbAccess.putCorpus(corpusAll);
|
||||||
meta = {collection:"any",updated:Math.floor(Date.now() / 1000)};
|
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
||||||
dbAccess.putMeta(meta);
|
dbAccess.putMeta(meta);
|
||||||
}
|
}
|
||||||
if (db === "stations" || "all") {
|
if (db === "stations" || "all") {
|
||||||
var corpusSubset = await corpus.subset(corpusAll);
|
var corpusSubset = await corpus.subset(corpusAll);
|
||||||
dbAccess.putStations(corpusSubset);
|
dbAccess.putStations(corpusSubset);
|
||||||
meta = {collection:"any",updated:Math.floor(Date.now() / 1000)};
|
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
||||||
dbAccess.putMeta(meta);
|
dbAccess.putMeta(meta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init
|
init,
|
||||||
|
check,
|
||||||
|
build
|
||||||
}
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
|
|
||||||
async function checkCrs(input){
|
async function checkCrs(input){
|
||||||
// Check whether CRS is valid
|
// Check whether CRS is valid
|
||||||
// if not, try to get tiploc
|
// if not, try to get tiploc
|
||||||
|
8
src/utils/log.utils.js
Normal file
8
src/utils/log.utils.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
function out(msg) {
|
||||||
|
var time = new Date().toISOString();
|
||||||
|
console.log(`${time} - ${msg}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
out
|
||||||
|
}
|
Reference in New Issue
Block a user