@@ -3,8 +3,7 @@ const fs = require('fs')
3
3
const initSqlJs = require ( 'sql.js' )
4
4
const app = express ( )
5
5
const port = process . env . PORT || 5387
6
- const bodyParser = require ( 'body-parser' )
7
- const jsonParser = bodyParser . json ( )
6
+ const jsonParser = express . json ( )
8
7
9
8
const dbPath = 'db.sqlite'
10
9
@@ -20,6 +19,7 @@ initSqlJs().then(SQL => {
20
19
const sqlstr = "CREATE TABLE model (id INTEGER PRIMARY KEY, name TEXT, address TEXT, description TEXT, model_type TEXT, encoder TEXT, accuracy NUMBER);"
21
20
+ "CREATE TABLE data (transaction_hash TEXT PRIMARY KEY, text TEXT);"
22
21
+ "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));"
23
23
db . run ( sqlstr )
24
24
}
25
25
@@ -106,7 +106,7 @@ initSqlJs().then(SQL => {
106
106
delete model . model_type
107
107
res . send ( { model } )
108
108
} else {
109
- return res . status ( 400 ) . send ( { message : "Not found." } )
109
+ return res . status ( 404 ) . send ( { message : "Not found." } )
110
110
}
111
111
} )
112
112
@@ -151,4 +151,53 @@ initSqlJs().then(SQL => {
151
151
} )
152
152
153
153
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
+ } )
154
203
} )
0 commit comments