From 9846c696692eaa2b06fece6eda2ddc9df9d03d86 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Tue, 28 Mar 2023 14:49:54 +0300 Subject: linkeysearch:/javascript updated deployed cloud functions --- javascript/keygen_GCF.js | 92 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 28 deletions(-) (limited to 'javascript/keygen_GCF.js') diff --git a/javascript/keygen_GCF.js b/javascript/keygen_GCF.js index 1e22f18..77c1a1c 100644 --- a/javascript/keygen_GCF.js +++ b/javascript/keygen_GCF.js @@ -19,7 +19,6 @@ const fs = require('fs'); const os = require('os'); const fetch = require('node-fetch'); -const { ECDH } = require('crypto'); const {Storage} = require('@google-cloud/storage'); // Google Cloud Storage @@ -47,11 +46,14 @@ const request_settings = { method: "Get" }; // Global Variables // ----------------------------------------------------------------------------- +// Global arrays // keyword links (word-links dictionary; array of objects) // ----------------------------------------------------------------------------- -var kwlinks_ = []; ////////////////////////////// MAIN OUTPUT OF THE SCRIPT -var prods_ = []; -var fr_ =[]; +var kwlinks_ = []; // keyword-links structure (structure to construct) +var prods_ = []; // products (array to construct) +var _fr =[]; // product frequencies array (as read from endpiont) +var _pr =[]; // product array (as read from endpiont) + // #2.2 @@ -173,7 +175,10 @@ noRoot = [ 'Ολικής-Άλεσης', 'Ολικής-Aλέσεως', 'Ολικής', + 'Σοκολάτας', + 'Υγείας', 'Γαϊδούρας', + 'Δημητριακών', 'Γαϊδάρου', 'Ρούχων', 'Πιάτων', @@ -204,6 +209,9 @@ noRoot = [ 'Καλαθάκι', 'Σκύλου', 'Γάτας', + 'ΠΓΕ', + 'ΤΟ', + 'Μελί', 'Σώματος', 'Αδυνατίσματος', 'Προστασίας', @@ -276,7 +284,7 @@ removeOriginals.forEach( w => { removeList.push( kb_trans(w)); }); * @return frequency of product */ function freq_of(id) { - fr_.forEach( rec => { + _fr.forEach( rec => { if (rec.id == id) return rec.fq }); @@ -284,8 +292,6 @@ function freq_of(id) { } - - // proccess data // The MAIN proccess // ----------------------------------------------------------------------------- @@ -486,8 +492,8 @@ function do_replaces(str) { // preproccess description // --- function preproccess_text(str) { - str = do_replaces(str); str = clean_text(str); + str = do_replaces(str); str = mark_linked_words(str); return str; } @@ -530,10 +536,9 @@ async function save_local(jsonArray, fileName) { // write string to file fs.writeFileSync(fileName, fileStr, 'utf8', (err) => { if (err) { - console.log("An error occured while writing keywords.json"); - return console.log(err); + return err; } - console.log("JSON file", fileName, "has been saved."); + return true; }); @@ -581,31 +586,62 @@ async function upload_file( bucketName, srcFilePath, trgFilePath ) { // keep only 1 of next 2 lines exports.main = () => { // entry point functioon when google cloud function - - fetch(frequency_endpoint, request_settings) - .then(res => res.json()) - .then((json) => { - fr_ = json; - console.log('frequencies loaded from CDN'); + // PROMISE ... + // fetch all jsons from endpints ........................................... + Promise.all([ fetch(products_endpoint, request_settings) - .then(res => res.json()) - .then((json) => { + .then(res => res.json()) + .then((json) => { + _pr = json; + console.log('products data loaded from emarket api/endpoint'); + }), + + fetch(frequency_endpoint, request_settings) + .then(res => res.json()) + .then((json) => { + _fr = json; + console.log('frequencies loaded from CDN') + }) + + ]) + .then (() => { // then... + // proccess data and extract ketwords - console.log('products data loaded from emarket api/endpoint'); + console.log('All data from endpoints: LOADED!'); - // proccess data - extract_linked_keywords(json.data); + // proccess data + extract_linked_keywords(_pr.data); - save_local(kwlinks_, temp_kw_file); - save_local(kwlinks_, temp_prod_file); + console.log('keywords extracted; try to save results localy...'); - // upload temp files to cloud - upload_file('pythia-files', temp_kw_file, 'uploads/json/emarket-keywords.json'); - upload_file('pythia-files', temp_prod_file, 'uploads/json/emarket-products.json'); + // PROMISE ... + // save local files .................................................... + Promise.all([ + save_local(kwlinks_, temp_kw_file) + .then(() => { console.log('keywords saved localy');}), + + save_local(prods_, temp_prod_file) + .then(() => { console.log('products saved localy');}) + ]) + .then(() => { // then ... + + console.log('Local saves: DONE!'); + + // PROMISE ... + // upload keytword and product jsons into Cloud Storage ............ + Promise.all([ + upload_file('pythia-files', temp_kw_file, 'uploads/json/emarket-keywords.json'), + upload_file('pythia-files', temp_prod_file, 'uploads/json/emarket-products.json') + ]) + .then(() => { // then ... + + console.log('Everything seems ok; END._') + + }); }); }); -} +} \ No newline at end of file -- cgit v1.2.3