-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
recorder: resume logs recorded within 4 hours #3136
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b35547
recorder: resume logs recorded within 4 hours
bobrippling 2b8399b
recorder: manually runnable tests for setRecording()
bobrippling f68504e
recorder: simplify log listing/sorting
bobrippling bc5d88b
recorder: fix lint
bobrippling 3dde5af
Merge branch 'master' into fix/recorder-midnight
bobrippling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
let writes = {}; | ||
let testDate, now; | ||
let files; | ||
|
||
const testDateStamp = () => `${testDate.getYear() + 1900}${testDate.getMonth() + 1}${testDate.getDate()}`; | ||
|
||
const mockStorage = { | ||
readJSON(f, err) { | ||
if(f === "recorder.json"){ | ||
return {}; | ||
} | ||
throw new Error(`unimplemented readJSON(${args})`); | ||
}, | ||
writeJSON(fname, json) { | ||
if(!writes[fname]) | ||
writes[fname] = []; | ||
writes[fname].push(json); | ||
}, | ||
list(re) { | ||
let files = [ | ||
"recorder.json", | ||
"recorder.log20230802a.csv\1", | ||
`recorder.log${testDateStamp()}a.csv\x01`, | ||
"recorder.log20181219a.csv\1", | ||
"abc", | ||
]; | ||
|
||
if (re) | ||
files = files.filter(f => re.test ? re.test(f) : re === f); | ||
return files; | ||
}, | ||
read(...args) { | ||
throw new Error(`unimplemented read(${args})`); | ||
}, | ||
write(...args) { | ||
throw new Error(`unimplemented write(${args})`); | ||
}, | ||
open(f, mode) { | ||
f = f.replace(/\1$/, ""); | ||
|
||
const lines = files[f] || []; | ||
|
||
return { | ||
readLine() { | ||
return lines.shift() || ""; | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
const assertEq = (a, b) => { | ||
if(typeof a !== typeof b) | ||
throw new Error("type mismatch"); | ||
if(typeof a !== "string") | ||
throw new Error("unimplemented"); | ||
if(a !== b) | ||
throw new Error(`string mismatch, ${JSON.stringify(a)} != ${JSON.stringify(b)}`); | ||
} | ||
|
||
require = (origRequire => req => { | ||
if (req === "Storage") { | ||
return mockStorage; | ||
} | ||
return origRequire(req); | ||
})(require); | ||
|
||
Date = (OrigDate => function(...args) { | ||
if (args.length === 0) { | ||
// new Date(), pretend the time is what we're testing | ||
return new OrigDate(now); | ||
} | ||
return new OrigDate(...args); | ||
})(Date); | ||
|
||
Bangle = { | ||
removeListener(){}, | ||
drawWidgets(){}, | ||
} | ||
WIDGETS = {}; | ||
const w = require("fs").readFileSync("./widget.js", "utf8"); | ||
eval(w); | ||
|
||
let file; | ||
|
||
/* | ||
* if it's almost midnight but we have a recording | ||
* from ~15 mins ago, we select it | ||
*/ | ||
testDate = new Date("2023-11-23T23:45:00.000Z"); | ||
now = new Date("2023-11-23T23:59:00.000Z"); | ||
files = { | ||
[`recorder.log${testDateStamp()}a.csv`]: [ | ||
"Time,Steps", | ||
"2023-12-20T19:27:45.000Z,9", | ||
testDate.toISOString() + ",21", | ||
], | ||
}; | ||
writes = {}; | ||
|
||
WIDGETS["recorder"].setRecording(true); | ||
|
||
file = writes["recorder.json"][0].file; | ||
assertEq(file, `recorder.log20231123a.csv`); | ||
|
||
/* | ||
* if it's past midnight but we have a recording | ||
* from ~15 mins ago, we select it | ||
*/ | ||
testDate = new Date("2023-11-23T23:45:00.000Z"); | ||
now = new Date("2023-11-24T00:01:00.000Z"); | ||
files = { | ||
[`recorder.log${testDateStamp()}a.csv`]: [ | ||
"Time,Steps", | ||
"2023-12-20T19:27:45.000Z,9", | ||
testDate.toISOString() + ",21", | ||
], | ||
}; | ||
writes = {}; | ||
|
||
WIDGETS["recorder"].setRecording(true); | ||
|
||
file = writes["recorder.json"][0].file; | ||
assertEq(file, `recorder.log20231123a.csv`); | ||
|
||
/* | ||
* if it's a fair bit after the last recording, | ||
* we pick a new one | ||
*/ | ||
testDate = new Date("2023-11-23T23:45:00.000Z"); | ||
now = new Date("2023-11-24T03:45:00.000Z"); // just over the 4 hour limit | ||
files = { | ||
[`recorder.log${testDateStamp()}a.csv`]: [ | ||
"Time,Steps", | ||
"2023-12-20T19:27:45.000Z,9", | ||
testDate.toISOString() + ",21", | ||
], | ||
}; | ||
writes = {}; | ||
|
||
WIDGETS["recorder"].setRecording(true); | ||
|
||
file = writes["recorder.json"][0].file; | ||
assertEq(file, `recorder.log20231124a.csv`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to clarify that the previous recorder log is only resumed on append