Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track User Search with Firestore #228

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion assets/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// for global constants. used for debugging


const CONSTANTS = Object.freeze({
OPEN_DEV_TOOLS: false, // set true to open dev tools on application launch
ENV: this.OPEN_DEV_TOOLS ? 'development.manage.development' : 'prod.manage.prod', // set true to use production environment and set false for test environment.
REPLACEMENTS: Object.freeze({ // for replacing commonly confused illegal characters with their legal counterparts
REPLACEMENTS: Object.freeze({
// single quote
'`': '\'',
'´': '\'',
Expand All @@ -26,6 +27,10 @@ const CONSTANTS = Object.freeze({
appId: '1:36956740284:web:561e9a73a0f3f4b08fceb9',
measurementId: 'G-112KCDJT5G',
},
FIRESTORE_EVENT_STARTAPP: 'Start App',
FIRESTORE_EVENT_SEARCH: 'Search Item',
FIRESTORE_EVENT_ADDTOINVENTORY: 'Add to Inventory',
FIRESTORE_EVENT_REQUESTITEM: 'Request Item',
});

module.exports = CONSTANTS;
43 changes: 43 additions & 0 deletions assets/firestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//for firestore app usage logging. Doesn't work for ES6
const {initializeApp} = require('firebase/app');
const {getFirestore, collection, addDoc} = require('firebase/firestore/lite');
const { isNull } = require('lodash');
const CONSTANTS = require('../assets/constants.js');


let fireApp = null;
let fireDB = null;

/**
* login to firestore
* can only initialize firestore once
*/
function init() {
if(isNull(fireDB)) {
fireApp = initializeApp(CONSTANTS.FIREBASECONFIG);
fireDB = getFirestore(fireApp);
} else {
console.log("firestore already initialized");
return;
}
}


/**
* Add data to fireStore
* userid + date is included by default
* @param {data} data Data dictionary to be included
*/
async function fslog(data) {
if (isNull(fireDB)){
console.log("firestore is not initialized");
return;
}
const fireRef = await addDoc(collection(fireDB, 'events'), {
time: Date.now(), user: process.env.USERNAME, ...data,
});
}

module.exports = {
init, fslog
};
21 changes: 4 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ const path = require('path');
const fs = require('fs');
const {appUpdater} = require('./assets/autoupdater');
const CONSTANTS = require('./assets/constants.js');
const {initializeApp} = require('firebase/app');
const {getFirestore, collection, addDoc} = require('firebase/firestore/lite');
const firestore = require('./assets/firestore.js');
let mainWindow;
let settingWindow;

const fireApp = initializeApp(CONSTANTS.FIREBASECONFIG);
const fireDB = getFirestore(fireApp);
fireStore({event: 'Start App'});

/**
* Add data to fireStore
* userid + date is included by default
* @param {data} data Data dictionary to be included
*/
async function fireStore(data) {
const fireRef = await addDoc(collection(fireDB, 'events'), {
time: Date.now(), user: process.env.USERNAME, ...data,
});
console.log('added: ' + fireRef.id);
};
firestore.init();
firestore.fslog({event: CONSTANTS.FIRESTORE_EVENT_STARTAPP});


if (CONSTANTS.OPEN_DEV_TOOLS) {
require('electron-reload')(__dirname);
Expand Down
2 changes: 2 additions & 0 deletions renderer/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');
const Translation = require('../assets/item_translation/item-translation');
const fs = require('fs');
const CONSTANTS = require('../assets/constants.js');
const {fslog} = require('../assets/firestore.js');
/**
* Handles messages from the WorkerHandler
*
Expand All @@ -27,6 +28,7 @@ onmessage = function (e) {
valid.validateSingle(e.data[1]).then((result) => {
postMessage(['result', result]);
});
fslog({event: CONSTANTS.FIRESTORE_EVENT_SEARCH});
break;
case 'validBatch':
valid = new Validate();
Expand Down
Loading