-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from obeliss-nlesc/56-persist-data-between-res…
…tarts merge main
- Loading branch information
Showing
9 changed files
with
554 additions
and
131 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.env | ||
*.pem | ||
api.key | ||
*.key | ||
|
||
# Data folder | ||
data/* | ||
|
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,70 @@ | ||
// Persisting the User Map to disk | ||
|
||
const murmurhash = require("murmurhash") | ||
const fs = require("fs").promises | ||
const User = require("./user.js") | ||
|
||
class UserDb extends Map { | ||
constructor(file) { | ||
super() | ||
this.seed = 42 | ||
this.file = file | ||
this.lastHash = 0 | ||
} | ||
load() { | ||
let data = [] | ||
return fs | ||
.readFile(this.file) | ||
.then((d) => { | ||
data = JSON.parse(d) | ||
data.forEach((u) => { | ||
const user = new User(u.userId, u.experimentId) | ||
user.redirectedUrl = u.redirectedUrl | ||
user.oTreeId = u.oTreeId | ||
user.tokenParams = u.tokenParams | ||
user.state = u.redirectedUrl ? u.state : user.state | ||
const compoundKey = `${user.userId}:${user.experimentId}` | ||
this.set(compoundKey, user) | ||
}) | ||
}) | ||
.catch((err) => { | ||
console.error( | ||
`[WARNING] UserDb file ${this.file} file not found will be recreated!`, | ||
) | ||
}) | ||
} | ||
find(userId) { | ||
return Array.from(this.values()).filter((u) => { | ||
return u.userId == userId | ||
}) | ||
} | ||
dump() { | ||
const data = [] | ||
this.forEach((v, k) => { | ||
data.push(v.serialize()) | ||
}) | ||
|
||
return JSON.stringify(data) | ||
} | ||
save() { | ||
const data = [] | ||
this.forEach((v, k) => { | ||
data.push(v.serialize()) | ||
}) | ||
|
||
const dump = JSON.stringify(data) | ||
const h = murmurhash(dump, this.seed) | ||
console.log(`${h}:${this.lastHash}:${dump}`) | ||
if (this.lastHash !== h) { | ||
fs.writeFile(this.file, dump) | ||
.then(() => { | ||
this.lastHash = h | ||
}) | ||
.catch((err) => { | ||
console.error("[ERROR] Writing UserDb to file.") | ||
}) | ||
} | ||
} | ||
} | ||
|
||
module.exports = UserDb |
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
Oops, something went wrong.