@@ -14,7 +14,7 @@ import {
14
14
import { JsonObject } from 'type-fest' ;
15
15
import loglevel , { LogLevelDesc } from 'loglevel' ;
16
16
import { arrayify } from './utils.js' ;
17
- import { groupBy } from 'remeda' ;
17
+ import { pipe , sortBy } from 'remeda' ;
18
18
19
19
const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
20
20
const migrationFolder = path . join ( __dirname , 'db-migrations' ) ;
@@ -32,6 +32,9 @@ type LogTable = {
32
32
type : string ;
33
33
createdAt : string ;
34
34
clientDate ?: string ;
35
+ // batchOrder records the order in which the logs were sent by the client
36
+ // in a single request. This is used to sort logs with the same clientDate.
37
+ batchOrder ?: number ;
35
38
} ;
36
39
type LogValueTable = {
37
40
logId : bigint ;
@@ -127,34 +130,33 @@ export class Store {
127
130
) {
128
131
await this . #db. transaction ( ) . execute ( async ( trx ) => {
129
132
let createdAt = new Date ( ) . toISOString ( ) ;
130
- let dbLogGroups = groupBy (
133
+ let dbLogs = pipe (
131
134
await trx
132
135
. insertInto ( 'log' )
133
136
. values (
134
- logs . map ( ( { type, date } ) => {
137
+ logs . map ( ( { type, date } , i ) => {
135
138
return {
136
139
type,
137
140
runId,
138
141
experimentId,
139
142
createdAt,
140
143
clientDate : date . toISOString ( ) ,
144
+ batchOrder : i ,
141
145
} ;
142
146
} )
143
147
)
144
- . returning ( [ 'logId' , 'type ' ] )
148
+ . returning ( [ 'logId' , 'batchOrder ' ] )
145
149
. execute ( ) ,
146
- ( log ) => log . type
150
+ sortBy ( ( log ) => log . batchOrder ?? 0 )
147
151
) ;
148
152
149
153
// Bulk insert returning values does not guarantee order, so we need to
150
154
// match the log values logs to returned log ids.
151
155
let dbValues = [ ] ;
152
156
for ( let log of logs ) {
153
- let dbLog = dbLogGroups [ log . type ] . pop ( ) ;
157
+ let dbLog = dbLogs . shift ( ) ;
154
158
if ( dbLog == null ) {
155
- throw new Error (
156
- `failed to find an unassigned inserted log with type "${ log . type } "`
157
- ) ;
159
+ throw new Error ( `could not insert log values: log was not inserted` ) ;
158
160
}
159
161
dbValues . push ( ...deconstructValues ( log . values , { logId : dbLog . logId } ) ) ;
160
162
}
@@ -205,7 +207,7 @@ export class Store {
205
207
. orderBy ( 'log.type' )
206
208
. orderBy ( 'log.clientDate' )
207
209
. orderBy ( 'log.createdAt' )
208
- . orderBy ( 'log.logId ' )
210
+ . orderBy ( 'log.batchOrder ' )
209
211
. select ( [
210
212
'log.experimentId as experimentId' ,
211
213
'log.runId as runId' ,
0 commit comments