diff options
Diffstat (limited to 'javascript/freq.js')
| -rw-r--r-- | javascript/freq.js | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/javascript/freq.js b/javascript/freq.js index 0ac7779..19c42f5 100644 --- a/javascript/freq.js +++ b/javascript/freq.js @@ -20,6 +20,8 @@ var mysql = require('mysql'); const fs = require('fs'); +const os = require('os'); + const {Storage} = require('@google-cloud/storage'); // import Google Cloud client library @@ -35,8 +37,20 @@ const {Storage} = require('@google-cloud/storage'); // import Google Cloud c const projectId = 'pythia-251711'; const keyFilename = '../auth/pythia-251711-047e3d5e6608.json'; + +// cloud_environment (boolean) +// set value to: +// true : if running on Google Cloud Functions +// false : if running localy +// --- -- -- - - - +const cloud_environment = false; // ** check + + // temp file of frequencies (json) -const temp_file = '../results/freq.json'; +const saves_dir = '../results'; // ** check +const temp_file = 'freq.json'; // ** check + +const local_dir = (cloud_environment) ? os.tmpdir() : saves_dir; // db connection parametres @@ -44,7 +58,7 @@ const temp_file = '../results/freq.json'; // ----------------------------------------------------------------------------- // while on local machine using cloud_sql_proxy -var con = mysql.createConnection({ +var con = mysql.createConnection({ // ** check host: "127.0.0.1", user: "pythia_db_user_dev", password: "VnEP0eysjiXDHcfM", @@ -52,7 +66,7 @@ var con = mysql.createConnection({ }); // // while on Google Cloud Functions -// var con = mysql.createConnection({ +// var con = mysql.createConnection({ // ** check // socketPath: "/cloudsql/pythia-251711:europe-west4:pythia-db-eu", // user: "pythia_services", // password: process.env.DB_PASSWORD, @@ -103,11 +117,11 @@ function extract_freq(obj) { // Save Local // ----------------------------------------------------------------------------- -function save_local(jsonArray, filePath) { +function save_local(jsonArray, fileName) { let fileStr = JSON.stringify(jsonArray); // convert json to string // write string to file - fs.writeFileSync(filePath, fileStr, 'utf8', (err) => { + fs.writeFileSync(local_dir +'/ '+ fileName, fileStr, 'utf8', (err) => { if (err) { console.log("An error occured while writing keywords.json"); return console.log(err); @@ -119,7 +133,6 @@ function save_local(jsonArray, filePath) { } - // Save to Google Cloud Storage // ----------------------------------------------------------------------------- async function upload_file( bucketName, srcFilePath, trgFilePath ) { @@ -163,10 +176,10 @@ var main = () => { // save dictionary; // end script; // ----------------------------------------------------------------------------- - con.connect(function(err) { - // connect; - if (err) throw err; - console.log("Connected!"); + con.connect(function(err) { // comment this out for 'Cannot enqueue Handshake after already enqueuing' error + // connect; // comment this out for 'Cannot enqueue Handshake after already enqueuing' error + if (err) throw err; // comment this out for 'Cannot enqueue Handshake after already enqueuing' error + console.log("Connected!"); // comment this out for 'Cannot enqueue Handshake after already enqueuing' error // SQL to get needed data var sql = "SELECT count(pl.eys_code) as FREQuency,\ @@ -211,11 +224,12 @@ var main = () => { // process.exit(1); // exit }); - }); + }); // comment this out for 'Cannot enqueue Handshake after already enqueuing' error } // NOTE: // if running the script directry you need to call the main function // if running through google cloud-functions you do NOT need to call main() // (you need define main() as the entry-point function instead) -main();
\ No newline at end of file +// --- -- -- - - - +main(); // ** check
\ No newline at end of file |
