Skip to content

Commit

Permalink
fix(cache): improve perf on values query in sqlite cache
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 authored and Nicolas Burger committed Oct 27, 2022
1 parent 776c092 commit 51e8f9a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions src/service/database.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ const getCount = (database) => {
const getValuesToSend = (database, count) => {
const query = 'SELECT id, timestamp, data, point_id AS pointId, south as dataSourceId '
+ `FROM ${CACHE_TABLE_NAME} `
+ 'ORDER BY timestamp '
+ `LIMIT ${count}`
const values = []
const stmt = database.prepare(query)
// eslint-disable-next-line no-restricted-syntax
for (const value of stmt.iterate()) {
values.push({ ...value, data: JSON.parse(decodeURI(value.data)) })
}
return values

return database.prepare(query).all().map((value) => ({
id: value.id,
timestamp: value.timestamp,
pointId: value.pointId,
data: JSON.parse(decodeURI(value.data)),
}))
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/service/database.service.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const databaseService = require('./database.service')

const values = [
{ timestamp: '2022-08-25T12:58:00.000Z', data: { value: 1, quality: 192 }, pointId: 'point001' },
{ timestamp: '2022-08-25T12:59:00.000Z', data: { value: 2, quality: 192 }, pointId: 'point002' },
{ id: 1, timestamp: '2022-08-25T12:58:00.000Z', data: { value: 1, quality: 192 }, pointId: 'point001' },
{ id: 2, timestamp: '2022-08-25T12:59:00.000Z', data: { value: 2, quality: 192 }, pointId: 'point002' },
]

let all
Expand All @@ -18,13 +18,14 @@ beforeEach(() => {
jest.useFakeTimers()

const retrievedValues = values.map((value) => ({
...value,
id: value.id,
timestamp: value.timestamp,
pointId: value.pointId,
data: encodeURI(JSON.stringify(value.data)),
}))
all = jest.fn().mockReturnValue([{ path: 'myFilePath', timestamp: 123 }])
all = jest.fn().mockReturnValue(retrievedValues)
get = jest.fn().mockReturnValue(retrievedValues)
run = jest.fn()
iterate = jest.fn().mockReturnValue(retrievedValues)
prepare = jest.fn()
prepare.mockReturnValue({
all,
Expand Down Expand Up @@ -122,7 +123,6 @@ describe('Database service', () => {
expect(prepare).toHaveBeenCalledTimes(1)
expect(prepare).toHaveBeenCalledWith('SELECT id, timestamp, data, point_id AS pointId, south as dataSourceId '
+ 'FROM cache '
+ 'ORDER BY timestamp '
+ 'LIMIT 50')

expect(valuesToSend).toEqual(values)
Expand Down

0 comments on commit 51e8f9a

Please sign in to comment.