<commit_before>/*jshint esversion: 6 */

// initial DB object
var mysql = require('promise-mysql');
var sql_config = require('../sql_config');
var db = mysql.createPool(sql_config.db);
var debug = require('debug')('myBus:cron');
var util = require('./util');
const BusArrival = require('./BusArrival');

var taichung = require('./fetch_taichung');
const routeToFetch = [
    {route: 5, isReverse: true},
    {route: 5, isReverse: false},
    {route: 160, isReverse: true},
    {route: 160, isReverse: false}
];

db.on('enqueue', function () {
    console.log('Waiting for available connection slot');
});

function updateBusTable (route) {
    clearTimetable(route).then(
        taichung.fetchTimeTable(route).then(function (timetable) {
            console.log(timetable);
            return walkThroughTimetable(timetable.timeList);
        }).then((tableEntry) => {
            console.log(JSON.stringify(tableEntry));
            saveTimeTable(route, tableEntry);
        })
    );
}

function isReverse (direction) {
    if (typeof direction === 'boolean') {
        return direction;
    } else if (direction.toLowerCase() === 'foeard') {
        return false;
    } else if (direction.toLowerCase() === 'reverse') {
        return true;
    } else {
        throw new Exception('Unknown direction');
    }
}

function clearTimetable (route) {
    var query = `DELETE FROM \`Time_table\` WHERE \`route\`=${route} `;
    console.log(query);
    return db.getConnection().then((connection) => {
        connection.query(query).then((rows) => {
            console.log('Delete OK');
        }).catch((err) => {
            console.log(err.code);
        }).finally(() => {
            db.releaseConnection(connection);
        });
    });
}

function walkThroughTimetable (timetable, location, result) {
    if (!location) {location = '';}
    if (!result) {result = [];}

    for (var key in timetable) {
        if (!timetable.hasOwnProperty(key)) {
            continue;       // skip this property
        }
        if (typeof timetable[key] == "object" && timetable[key] !== null && isNaN(key)) {
            //console.log(location + '\t' + key);
            walkThroughTimetable(timetable[key], (location + `.${key}`), result);
        } else {
            //console.log(timetable[key]);
            var entry = `${location}.${timetable[key].hour}.${timetable[key].minute}.${timetable[key].local}`;
            //console.log(`${location}.${timetable[key].hour}.${timetable[key].minute}.${timetable[key].local}`);
            result.push(entry);
            //console.log(entry);
        }
    }

    return result;
}

function saveTimeTable(route, entryArray) {
    console.log(`saveTimeTable(${entryArray.length})`);
    if (entryArray.length > 0) {
        var entry = entryArray.pop();
        var column = entry.split('.');
        //saveTimetableEntry(route, column[1], isReverse(column[2]), column[3], column[4], Number(column[5]));
        //return saveTimeTable(route, entryArray);
        return saveTimetableEntry(
            route, column[1], isReverse(column[2]), column[3], column[4],Number(column[5])
        ).then(
            saveTimeTable(route, entryArray)
        );
        //(route, column[1], isReverse(column[2]), column[3], column[4], Number(column[5]));
    }
}

function saveTimetableEntry (route, weekday, is_reverse, hour, minute, local) {
    var query = `INSERT INTO \`Time_table\` (\`route\`, \`weekday\`, \`hour\`, \`minute\`, \`local\`) VALUES ('${route}', '${weekday}', '${hour}', '${minute}', '${local}')`;
    return db.getConnection().then((connection) => {
        connection.query(query).then((rows) => {
            console.log('\tquery ok');
        }).catch((err) => {
            console.log('\t' + err.code);
        }).finally(() => {
            db.releaseConnection(connection);
        });
    });
}

/* ========================================================================================================= */

function updateRealTime(pos) {
    if (pos === undefined) {
        updateRealTime(0);
    } else if (pos < routeToFetch.length) {
        var mergedData;
        db.query(`SELECT COUNT(*) AS total FROM \`Bus_stop\` WHERE \`route\` = ${routeToFetch[pos].route} AND \`is_reverse\` = ${routeToFetch[pos].isReverse}`)
            .then((data) => {
                return taichung.fetchBusStatus(routeToFetch[pos].route, routeToFetch[pos].isReverse, data.total);
            })
            .then(mergeWithStopInfo)
            .then((data) => {
                mergedData = data;
                debug(`Updating realtime data for route ${mergedData.route} ${mergedData.isReverse?'foward':'reverse'} [ ${mergedData.stopInfo.length} stops, ${mergedData.busInfo.length} bus online ]`);
            })
            .then(() => {
                return saveBusArrival(mergedData);
            })
            .then(() => {
                return saveBusStatus(mergedData);
            })
            .then(() => {
                updateRealTime(pos+1);
            })
            .catch((err) => {
                debug(`ERROR when Updating realtime data for route ${mergedData.route} ${mergedData.isReverse?'foward':'reverse'}`);
                console.error(err);
            });
    }
}

function mergeWithStopInfo(arrivalTime) {<commit_msg>updateArrivalMongo() after updateRealTime()<commit_after>/*jshint esversion: 6 */

// initial DB object
var mysql = require('promise-mysql');
var sql_config = require('../sql_config');
var db = mysql.createPool(sql_config.db);
var debug = require('debug')('myBus:cron');
var util = require('./util');
const BusArrival = require('./BusArrival');

var taichung = require('./fetch_taichung');
const routeToFetch = [
    {route: 5, isReverse: true},
    {route: 5, isReverse: false},
    {route: 160, isReverse: true},
    {route: 160, isReverse: false}
];

db.on('enqueue', function () {
    console.log('Waiting for available connection slot');
});

function updateBusTable (route) {
    clearTimetable(route).then(
        taichung.fetchTimeTable(route).then(function (timetable) {
            console.log(timetable);
            return walkThroughTimetable(timetable.timeList);
        }).then((tableEntry) => {
            console.log(JSON.stringify(tableEntry));
            saveTimeTable(route, tableEntry);
        })
    );
}

function isReverse (direction) {
    if (typeof direction === 'boolean') {
        return direction;
    } else if (direction.toLowerCase() === 'foeard') {
        return false;
    } else if (direction.toLowerCase() === 'reverse') {
        return true;
    } else {
        throw new Exception('Unknown direction');
    }
}

function clearTimetable (route) {
    var query = `DELETE FROM \`Time_table\` WHERE \`route\`=${route} `;
    console.log(query);
    return db.getConnection().then((connection) => {
        connection.query(query).then((rows) => {
            console.log('Delete OK');
        }).catch((err) => {
            console.log(err.code);
        }).finally(() => {
            db.releaseConnection(connection);
        });
    });
}

function walkThroughTimetable (timetable, location, result) {
    if (!location) {location = '';}
    if (!result) {result = [];}

    for (var key in timetable) {
        if (!timetable.hasOwnProperty(key)) {
            continue;       // skip this property
        }
        if (typeof timetable[key] == "object" && timetable[key] !== null && isNaN(key)) {
            //console.log(location + '\t' + key);
            walkThroughTimetable(timetable[key], (location + `.${key}`), result);
        } else {
            //console.log(timetable[key]);
            var entry = `${location}.${timetable[key].hour}.${timetable[key].minute}.${timetable[key].local}`;
            //console.log(`${location}.${timetable[key].hour}.${timetable[key].minute}.${timetable[key].local}`);
            result.push(entry);
            //console.log(entry);
        }
    }

    return result;
}

function saveTimeTable(route, entryArray) {
    console.log(`saveTimeTable(${entryArray.length})`);
    if (entryArray.length > 0) {
        var entry = entryArray.pop();
        var column = entry.split('.');
        //saveTimetableEntry(route, column[1], isReverse(column[2]), column[3], column[4], Number(column[5]));
        //return saveTimeTable(route, entryArray);
        return saveTimetableEntry(
            route, column[1], isReverse(column[2]), column[3], column[4],Number(column[5])
        ).then(
            saveTimeTable(route, entryArray)
        );
        //(route, column[1], isReverse(column[2]), column[3], column[4], Number(column[5]));
    }
}

function saveTimetableEntry (route, weekday, is_reverse, hour, minute, local) {
    var query = `INSERT INTO \`Time_table\` (\`route\`, \`weekday\`, \`hour\`, \`minute\`, \`local\`) VALUES ('${route}', '${weekday}', '${hour}', '${minute}', '${local}')`;
    return db.getConnection().then((connection) => {
        connection.query(query).then((rows) => {
            console.log('\tquery ok');
        }).catch((err) => {
            console.log('\t' + err.code);
        }).finally(() => {
            db.releaseConnection(connection);
        });
    });
}

/* ========================================================================================================= */

function updateRealTime(pos) {
    if (pos === undefined) {
        updateRealTime(0);
    } else if (pos < routeToFetch.length) {
        var mergedData;
        db.query(`SELECT COUNT(*) AS total FROM \`Bus_stop\` WHERE \`route\` = ${routeToFetch[pos].route} AND \`is_reverse\` = ${routeToFetch[pos].isReverse}`)
            .then((data) => {
                return taichung.fetchBusStatus(routeToFetch[pos].route, routeToFetch[pos].isReverse, data.total);
            })
            .then(mergeWithStopInfo)
            .then((data) => {
                mergedData = data;
                debug(`Updating realtime data for route ${mergedData.route} ${mergedData.isReverse?'foward':'reverse'} [ ${mergedData.stopInfo.length} stops, ${mergedData.busInfo.length} bus online ]`);
            })
            .then(() => {
                return saveBusArrival(mergedData);
            })
            .then(() => {
                return saveBusStatus(mergedData);
            })
            .then(() => {
                updateRealTime(pos+1);
            })
            .catch((err) => {
                debug(`ERROR when Updating realtime data for route ${mergedData.route} ${mergedData.isReverse?'foward':'reverse'}`);
                console.error(err);
            });
    } else if (pos === routeToFetch.length) {           // updateArrivalMongo() after updateRealTime()
        BusArrival.updateArrivalMongo();
    }
}

function mergeWithStopInfo(arrivalTime) {
{'fraction_non_alphanumeric': 0.1907321594068582, 'fraction_numerical': 0.005004633920296571, 'mean_word_length': 2.6030050083472456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1502955', 'n_tokens_mistral': 3275, 'n_tokens_neox': 3144, 'n_words': 600}