summaryrefslogtreecommitdiff
path: root/javascript/freq.js
diff options
context:
space:
mode:
Diffstat (limited to 'javascript/freq.js')
-rw-r--r--javascript/freq.js89
1 files changed, 66 insertions, 23 deletions
diff --git a/javascript/freq.js b/javascript/freq.js
index 6bf1517..5f740b7 100644
--- a/javascript/freq.js
+++ b/javascript/freq.js
@@ -5,24 +5,46 @@ var mysql = require('mysql');
const fs = require('fs');
+const {Storage} = require('@google-cloud/storage'); // import Google Cloud client library
-// ready to get main data to preccess
+
+
+// (setup/personalize) constants
////////////////////////////////////////////////////////////////////////////////
+// google-storage parametres
+// --- -- -- - - -
+const projectId = 'pythia-251711';
+const keyFilename = '../auth/pythia-251711-047e3d5e6608.json';
+
// db connection parametres
// -----------------------------------------------------------------------------
var con = mysql.createConnection({
- host: "127.0.0.1",
- user: "pythia_db_user_dev",
- password: "VnEP0eysjiXDHcfM",
- database: "dev_pythia_db"
+ host: "127.0.0.1",
+ user: "pythia_db_user_dev",
+ password: "VnEP0eysjiXDHcfM",
+ database: "dev_pythia_db"
});
-// products' (order-)frequecy array
+// MAIN OUTPUT OF THE SCRIPT
+// arrays that need to be constructed and filled with data
// -----------------------------------------------------------------------------
-var freq_ = []; //////////////////////////////// MAIN OUTPUT OF THE SCRIPT
-var prods_ = [];
+
+/** array of product frequencies
+ * array of obj:{ id, fq }
+ * @var id (int): product's eys code
+ * @var fq (int): (order) frerquency
+ */
+var freq_ = [];
+
+/** array of products
+ * array of obj: { id, w }
+ *
+ * @var id (int): product's eys_code
+ * @var w (string): descripion
+ */
+var prods_ = []; // products array
// connect;
@@ -63,7 +85,8 @@ con.connect(function(err) {
do_proccess(result); // proccess
console.log('Keywords proccesed!')
- save_keywords(); // save
+ save_local(freq_, '../results/freq.json'); // save frequencies
+ save_local(prods_, '../results/prods.json'); // save product (descriptionb per id)
console.log('Results saved! Exiting.')
// echo memory stats
@@ -88,6 +111,9 @@ function do_proccess(obj) {
var fq = rec.FREQuency;
var pid = rec.eys_code;
+
+
+
prods_.push({
id: pid,
w: description
@@ -102,28 +128,45 @@ function do_proccess(obj) {
}
-// save proccess
-// -----------------------------------------------------------------------------
-function save_keywords() {
- let fqStr = JSON.stringify(freq_);
+// save proccesses
+////////////////////////////////////////////////////////////////////////////////
- fs.writeFileSync("../results/freq.json", fqStr, 'utf8', (err) => {
+// Save Local
+// -----------------------------------------------------------------------------
+function save_local(jsonArray, filePath) {
+ let fileStr = JSON.stringify(jsonArray); // convert json to string
+
+ // write string to file
+ fs.writeFileSync(filePath, fileStr, 'utf8', (err) => {
if (err) {
console.log("An error occured while writing keywords.json");
return console.log(err);
}
console.log("JSON file has been saved.");
});
+}
- let prodStr = JSON.stringify(prods_);
+// Save to Cloud Storage
+// -----------------------------------------------------------------------------
- fs.writeFileSync("../results/prods.json", prodStr, 'utf8', (err) => {
- if (err) {
- console.log("An error occured while writing keywords.json");
- return console.log(err);
- }
- console.log("JSON file has been saved.");
- });
-}
+async function upload_file( bucketName, srcFilePath, trgFilePath ) {
+ // Creates a client
+
+ const storage = new Storage({projectId, keyFilename});
+
+ try {
+ await storage.bucket(bucketName).upload(srcFilePath, {
+ destination: trgFilePath,
+ gzip: true, // serve compressed
+ metadata: { // cache for 8 hours
+ cacheControl: 'public, max-age=60' // production set: 28800
+ }
+ });
+ console.log(`${srcFilePath} uploaded to ${bucketName}`);
+ }
+ catch(err) {
+ console.error('ERROR:', err);
+ }
+} \ No newline at end of file