From 73b727a90b133f32dcab856ac98b1bd3d3cd88fc Mon Sep 17 00:00:00 2001 From: Sylvan Zheng Date: Sun, 18 Dec 2022 12:15:48 -0500 Subject: [PATCH] fix bug with logging --- extension/src/js/background.js | 48 ++++++++++++++++++++++++++------ extension/src/js/fetch_tweets.js | 2 +- extension/src/js/log.js | 36 +++++++++--------------- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/extension/src/js/background.js b/extension/src/js/background.js index 2d50137..8c3e01d 100644 --- a/extension/src/js/background.js +++ b/extension/src/js/background.js @@ -1,5 +1,6 @@ import { CONFIG } from "./config"; const FETCH_ENDPOINT = CONFIG.serverEndpoint + "/fresh_tweets"; +const LOG_ENDPOINT = CONFIG.serverEndpoint + "/log_tweets"; async function refresh_tweet_pool(username, install_code, ideo) { console.log("Refreshing tweet pool.."); @@ -16,26 +17,57 @@ async function refresh_tweet_pool(username, install_code, ideo) { }); const fresh_tweets = await response.json(); // todo send to twitter tab - const [tab] = await chrome.tabs.query({ - active: true, - lastFocusedWindow: true, - }); - chrome.tabs.sendMessage(tab.id, { - message: { name: "fresh_tweets", tweets: fresh_tweets }, - }); + return fresh_tweets; } // Temp hack: fetch new data every second // TODO make it so that the frontend can request data chrome.runtime.onMessage.addListener((message) => { + const tab_promise = chrome.tabs.query({ + active: true, + lastFocusedWindow: true, + }); console.log("Received message", message); if (message.message == "fetch") { refresh_tweet_pool( message.username, message.install_code, message.ideo - ).then(() => {}); + ).then((fresh_tweets) => { + tab_promise.then((tab) => { + chrome.tabs.sendMessage(tab[0].id, { + message: { name: "fresh_tweets", tweets: fresh_tweets }, + }); + }); + }); + } else if ((message.message = "log")) { + uploadLog(message.username, message.install_code, message.data).then( + (status) => { + tab_promise.then((tab) => { + chrome.tabs.sendMessage(tab[0].id, { + message: { name: "logEvent", status: status }, + }); + }); + } + ); } }); + +const uploadLog = async (username, install_code, data) => { + const response = await fetch(LOG_ENDPOINT, { + method: "POST", + body: JSON.stringify({ + username: username, + password: install_code, + tweets: data, + }), + headers: { + "Content-Type": "application/json", + }, + }); + const response_json = await response.json(); + const status = response_json.status == 200; + return status; +}; diff --git a/extension/src/js/fetch_tweets.js b/extension/src/js/fetch_tweets.js index 968ac0f..410d417 100644 --- a/extension/src/js/fetch_tweets.js +++ b/extension/src/js/fetch_tweets.js @@ -31,7 +31,7 @@ chrome.runtime.onMessage.addListener((message) => { // Non-blocking func returns undefined if the fresh_tweets pool is empty export const fetch_tweet = function (exp_config) { if (!waiting && fresh_tweets.length < 4) { - console.log(`requesting more tweets... for ${exp_config}`); + console.log(`requesting more tweets... for ${JSON.stringify(exp_config)}`); chrome.runtime.sendMessage({ message: "fetch", username: exp_config.workerID, diff --git a/extension/src/js/log.js b/extension/src/js/log.js index 846e5cb..2fb0daf 100644 --- a/extension/src/js/log.js +++ b/extension/src/js/log.js @@ -1,34 +1,18 @@ import { CONFIG } from "./config"; -const LOG_ENDPOINT = CONFIG.serverEndpoint + "/log_tweets"; - export const getLogger = function (workerID, installCode, treatment_group) { const LOG = []; - const uploadLog = async () => { - const response = await fetch(LOG_ENDPOINT, { - method: "POST", - body: JSON.stringify({ - username: workerID, - password: installCode, - tweets: LOG, - }), - headers: { - "Content-Type": "application/json", - }, - }); - const response_json = await response.json(); - const status = response_json.status == 200; - if (status) { - console.log("uploaded log"); - LOG.length = 0; - } - }; - const flushLog = function () { // Flush the log to the cloud if (LOG.length > 0) { - uploadLog().then(() => {}); + chrome.runtime.sendMessage({ + message: "log", + username: workerID, + install_code: installCode, + data: LOG, + }); + LOG.length = 0; } }; @@ -45,6 +29,12 @@ export const getLogger = function (workerID, installCode, treatment_group) { setInterval(flushLog, 1000 * 60 * CONFIG.logUploadRateMinutes); + chrome.runtime.onMessage.addListener((message) => { + if (message.message.name == "logEvent") { + console.log("uploaded log ", message.message.status); + } + }); + return { logEvent, flushLog,