Skip to content

Commit

Permalink
fix bug with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
squidgetx committed Dec 18, 2022
1 parent 584119c commit 73b727a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
48 changes: 40 additions & 8 deletions extension/src/js/background.js
Original file line number Diff line number Diff line change
@@ -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..");
Expand All @@ -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;
};
2 changes: 1 addition & 1 deletion extension/src/js/fetch_tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 13 additions & 23 deletions extension/src/js/log.js
Original file line number Diff line number Diff line change
@@ -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;
}
};

Expand All @@ -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,
Expand Down

0 comments on commit 73b727a

Please sign in to comment.