Skip to content

Commit ece2ec5

Browse files
authored
[demo] Server: Add accuracy tracking (#125)
1 parent 5f33a96 commit ece2ec5

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.idea/
22
.vscode/
3-
43
.venv/

demo/server.js

+52-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const fs = require('fs')
33
const initSqlJs = require('sql.js')
44
const app = express()
55
const port = process.env.PORT || 5387
6-
const bodyParser = require('body-parser')
7-
const jsonParser = bodyParser.json()
6+
const jsonParser = express.json()
87

98
const dbPath = 'db.sqlite'
109

@@ -20,6 +19,7 @@ initSqlJs().then(SQL => {
2019
const sqlstr = "CREATE TABLE model (id INTEGER PRIMARY KEY, name TEXT, address TEXT, description TEXT, model_type TEXT, encoder TEXT, accuracy NUMBER);"
2120
+ "CREATE TABLE data (transaction_hash TEXT PRIMARY KEY, text TEXT);"
2221
+ "CREATE INDEX index_address ON model(address);"
22+
+ "CREATE TABLE accuracy (transaction_hash TEXT, block_number INTEGER, model_id INTEGER, accuracy NUMBER, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (model_id) REFERENCES model (id));"
2323
db.run(sqlstr)
2424
}
2525

@@ -106,7 +106,7 @@ initSqlJs().then(SQL => {
106106
delete model.model_type
107107
res.send({ model })
108108
} else {
109-
return res.status(400).send({ message: "Not found." })
109+
return res.status(404).send({ message: "Not found." })
110110
}
111111
})
112112

@@ -151,4 +151,53 @@ initSqlJs().then(SQL => {
151151
})
152152

153153
app.listen(port, () => console.log(`Listening on port ${port}`))
154+
155+
// ACCURACY RECORD MANAGEMENT
156+
function presistAccuracyRecord(accuracy) {
157+
db.run('INSERT INTO accuracy VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP);', [
158+
accuracy.transactionHash,
159+
accuracy.blockNumber,
160+
accuracy.modelId,
161+
accuracy.accuracy,
162+
])
163+
// eslint-disable-next-line @typescript-eslint/no-empty-function
164+
fs.writeFile(dbPath, Buffer.from(db.export()), () => {})
165+
}
166+
167+
// Add a new accuracy record for a model
168+
app.post('/api/accuracy', jsonParser, (req, res) => {
169+
const body = req.body
170+
presistAccuracyRecord(body)
171+
return res.sendStatus(200)
172+
})
173+
174+
// Get the accuracy history
175+
app.get('/api/accuracy/model', (req, res) => {
176+
const { modelId } = req.query
177+
if (modelId != null) {
178+
const getStmt = db.prepare('SELECT * FROM accuracy where model_id == $modelId ORDER BY timestamp;',
179+
{
180+
$modelId: modelId
181+
})
182+
const accuracyHistory = []
183+
while (getStmt.step()) {
184+
const accuracy = getStmt.get()
185+
accuracyHistory.push({
186+
transactionHash: accuracy[0],
187+
blockNumber: accuracy[1],
188+
modelId: accuracy[2],
189+
accuracy: accuracy[3],
190+
timestamp: accuracy[4],
191+
})
192+
}
193+
getStmt.free()
194+
if (accuracyHistory.length) {
195+
res.send({ accuracyHistory })
196+
} else {
197+
res.status(404).send({ message: "No results found: Please try a different modelId." })
198+
}
199+
} else {
200+
return res.status(400).send({ message: "`modelId` was not given in the request." })
201+
}
202+
})
154203
})

0 commit comments

Comments
 (0)