Skip to content

Commit 553bddd

Browse files
authored
Merge pull request alexmojaki#234 from alexmojaki/logging-events
Log run_code event
2 parents 5709930 + a792c00 commit 553bddd

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

database.rules.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
".write": "auth != null && auth.uid == $uid"
77
}
88
},
9+
"code_entries": {
10+
"$uid": {
11+
"$code_entry": {
12+
".write": "auth != null && auth.uid == $uid && !data.exists()"
13+
}
14+
}
15+
},
916
"email_list": {
1017
"$email_entry": {
1118
".write": "!data.exists()"

frontend/src/RunCode.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// eslint-disable-next-line import/no-webpack-loader-syntax
22
import Worker from "worker-loader!./Worker.js";
33
import * as Comlink from 'comlink';
4-
import {bookSetState, bookState, currentStepName, moveStep, ranCode} from "./book/store";
4+
import {
5+
bookSetState,
6+
bookState,
7+
currentStepName,
8+
databaseRequest,
9+
isProduction,
10+
logEvent,
11+
moveStep,
12+
ranCode
13+
} from "./book/store";
514
import _ from "lodash";
615
import localforage from "localforage";
716
import {animateScroll} from "react-scroll";
@@ -52,21 +61,37 @@ const runCodeRemote = async (entry, onSuccess) => {
5261

5362
export const runCode = ({code, source}) => {
5463
const shell = source === "shell";
64+
const {route, user, questionWizard, editorContent, numHints, requestingSolution} = bookState;
5565
if (!shell && !code) {
56-
code = bookState.editorContent;
66+
code = editorContent;
5767
}
5868
bookSetState("processing", true);
5969
const entry = {
6070
input: code,
6171
source,
62-
page_slug: bookState.user.pageSlug,
72+
page_slug: user.pageSlug,
6373
step_name: currentStepName(),
64-
question_wizard: bookState.route === "question",
65-
expected_output: bookState.questionWizard.expectedOutput,
74+
question_wizard: route === "question",
75+
expected_output: questionWizard.expectedOutput,
6676
};
6777

6878
const onSuccess = (data) => {
6979
const {error} = data;
80+
81+
logEvent('run_code', {
82+
code_source: entry.source,
83+
page_slug: entry.page_slug,
84+
step_name: entry.step_name,
85+
entry_passed: data.passed,
86+
has_error: Boolean(error),
87+
num_messages: data.messages.length,
88+
// user: user.uid,
89+
// developerMode: user.developerMode,
90+
page_route: route,
91+
num_hints: numHints,
92+
requesting_solution: requestingSolution,
93+
});
94+
7095
if (error) {
7196
Sentry.captureEvent(error.sentry_event);
7297
delete error.sentry_event;
@@ -107,6 +132,24 @@ export const runCode = ({code, source}) => {
107132
showCodeResult(data);
108133
terminalRef.current.focusTerminal();
109134
}
135+
136+
if (isProduction) {
137+
databaseRequest("POST", {
138+
entry,
139+
result: {
140+
..._.omit(data, "output_parts"),
141+
messages: data.messages.map(m => _.truncate(m, {length: 1000})),
142+
output: _.truncate(data.output, {length: 1000}),
143+
},
144+
state: {
145+
developerMode: user.developerMode,
146+
page_route: route,
147+
num_hints: numHints,
148+
requesting_solution: requestingSolution,
149+
},
150+
timestamp: new Date().toISOString(),
151+
}, "code_entries");
152+
}
110153
}
111154

112155
runCodeRemote(entry, onSuccess);

frontend/src/book/store.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ if (process.env.REACT_APP_USE_FIREBASE_EMULATORS && window.location.hostname ===
2626
}
2727

2828
let firebaseAnalytics;
29-
if (window.location.hostname === "futurecoder.io") {
29+
export const isProduction = window.location.hostname === "futurecoder.io";
30+
if (isProduction) {
3031
firebaseAnalytics = firebase.analytics(firebaseApp);
3132
}
3233

@@ -218,14 +219,14 @@ export const updateUserData = async (user) => {
218219
});
219220
}
220221

221-
const databaseRequest = async (method, data={}) => {
222+
export const databaseRequest = async (method, data={}, endpoint="users") => {
222223
const currentUser = firebase.auth().currentUser;
223224
if (!currentUser) {
224225
return;
225226
}
226227
const auth = await currentUser.getIdToken();
227228
const response = await axios.request({
228-
url: `${databaseUrl}/users/${currentUser.uid}.json`,
229+
url: `${databaseUrl}/${endpoint}/${currentUser.uid}.json`,
229230
params: {auth},
230231
method,
231232
data,
@@ -413,3 +414,7 @@ export const reorderSolutionLines = makeAction(
413414
},
414415
(startIndex, endIndex) => ({startIndex, endIndex})
415416
)
417+
418+
export function logEvent(name, data = {}) {
419+
firebaseAnalytics?.logEvent(name, data);
420+
}

0 commit comments

Comments
 (0)