-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(oianalytics): send oibus info to oianalytics on engine name or ve…
…rsion update
- Loading branch information
1 parent
fb5de79
commit 314f8ff
Showing
24 changed files
with
1,043 additions
and
32 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
backend/src/db/entity-migrations/v3.3.7-add-oia-message.ts
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,35 @@ | ||
import { Knex } from 'knex'; | ||
import { OIANALYTICS_MESSAGE_TABLE } from '../../repository/oianalytics-message.repository'; | ||
import { ENGINES_TABLE } from '../../repository/engine.repository'; | ||
import { version } from '../../../package.json'; | ||
import CreateTableBuilder = Knex.CreateTableBuilder; | ||
import { OIANALYTICS_MESSAGE_STATUS } from '../../../../shared/model/oianalytics-message.model'; | ||
|
||
function createDefaultEntityFields(table: CreateTableBuilder): void { | ||
table.uuid('id').primary(); | ||
table.timestamps(false, true); | ||
} | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await createOIAMessageTable(knex); | ||
await addVersionInEngineSettings(knex); | ||
} | ||
|
||
async function createOIAMessageTable(knex: Knex): Promise<void> { | ||
await knex.schema.createTable(OIANALYTICS_MESSAGE_TABLE, table => { | ||
createDefaultEntityFields(table); | ||
table.string('type').notNullable(); | ||
table.json('content').notNullable(); | ||
table.datetime('completed_date'); | ||
table.string('error'); | ||
table.enum('status', OIANALYTICS_MESSAGE_STATUS).notNullable().defaultTo('PENDING'); | ||
}); | ||
} | ||
|
||
async function addVersionInEngineSettings(knex: Knex) { | ||
await knex.schema.raw(`ALTER TABLE ${ENGINES_TABLE} ADD oibus_version NOT NULL DEFAULT "${version}"`); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.schema.dropTable(OIANALYTICS_MESSAGE_TABLE); | ||
} |
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
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
176 changes: 176 additions & 0 deletions
176
backend/src/repository/oianalytics-message.repository.spec.ts
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,176 @@ | ||
import SqliteDatabaseMock, { all, get, run } from '../tests/__mocks__/database.mock'; | ||
import { Database } from 'better-sqlite3'; | ||
import OianalyticsMessageRepository from './oianalytics-message.repository'; | ||
import { Page } from '../../../shared/model/types'; | ||
import { InfoMessageContent, OIAnalyticsMessageCommand, OIAnalyticsMessageDTO } from '../../../shared/model/oianalytics-message.model'; | ||
|
||
jest.mock('../tests/__mocks__/database.mock'); | ||
jest.mock('../service/utils', () => ({ | ||
generateRandomId: jest.fn(() => '123456') | ||
})); | ||
const nowDateString = '2020-02-02T02:02:02.222Z'; | ||
|
||
const existingMessage: OIAnalyticsMessageDTO = { | ||
id: '1234', | ||
status: 'ERRORED', | ||
type: 'INFO', | ||
content: {} as InfoMessageContent | ||
}; | ||
|
||
let database: Database; | ||
let repository: OianalyticsMessageRepository; | ||
describe('OIAnalytics Message repository', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
database = new SqliteDatabaseMock(); | ||
all.mockReturnValue([existingMessage]); | ||
database.prepare = jest.fn().mockReturnValue({ | ||
run, | ||
get, | ||
all | ||
}); | ||
repository = new OianalyticsMessageRepository(database); | ||
}); | ||
|
||
it('should create message', () => { | ||
run.mockReturnValueOnce({ lastInsertRowid: 1 }); | ||
get.mockReturnValueOnce({ ...existingMessage, content: '{}' }); | ||
const command: OIAnalyticsMessageCommand = { | ||
type: 'INFO', | ||
content: {} as InfoMessageContent | ||
}; | ||
repository.createOIAnalyticsMessages(command); | ||
expect(database.prepare).toHaveBeenCalledWith('INSERT INTO oianalytics_messages (id, type, status, content) VALUES (?, ?, ?, ?);'); | ||
expect(run).toHaveBeenCalledWith('123456', command.type, 'PENDING', JSON.stringify(command.content)); | ||
}); | ||
|
||
it('should update message', () => { | ||
const command: OIAnalyticsMessageCommand = { | ||
type: 'INFO', | ||
content: {} as InfoMessageContent | ||
}; | ||
repository.updateOIAnalyticsMessages('id', command); | ||
expect(database.prepare).toHaveBeenCalledWith('UPDATE oianalytics_messages SET content = ? WHERE id = ?;'); | ||
expect(run).toHaveBeenCalledWith(JSON.stringify(command.content), 'id'); | ||
}); | ||
|
||
it('should properly get messages page by search criteria', () => { | ||
const expectedValue: Page<OIAnalyticsMessageDTO> = { | ||
content: [ | ||
{ | ||
id: '1234', | ||
creationDate: '2023-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'PENDING', | ||
content: {} as InfoMessageContent | ||
}, | ||
{ | ||
id: '1234', | ||
creationDate: '2024-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'ERRORED', | ||
content: {} as InfoMessageContent | ||
} | ||
], | ||
size: 50, | ||
number: 0, | ||
totalElements: 2, | ||
totalPages: 1 | ||
}; | ||
all.mockReturnValueOnce([ | ||
{ | ||
id: '1234', | ||
creationDate: '2023-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'PENDING', | ||
content: '{}' | ||
}, | ||
{ | ||
id: '1234', | ||
creationDate: '2024-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'ERRORED', | ||
content: '{}' | ||
} | ||
]); | ||
get.mockReturnValueOnce({ count: 2 }); | ||
const logs = repository.searchMessagesPage( | ||
{ | ||
types: ['INFO'], | ||
status: ['PENDING', 'ERRORED'], | ||
start: '2023-01-01T00:00:00.000Z', | ||
end: '2023-01-02T00:00:00.000Z' | ||
}, | ||
0 | ||
); | ||
expect(database.prepare).toHaveBeenCalledWith( | ||
'SELECT id, created_at as creationDate, completed_date as compeltedDate, type, status, error, content FROM oianalytics_messages WHERE id IS NOT NULL ' + | ||
'AND type IN (?) AND status IN (?,?) AND created_at >= ? AND created_at <= ? ORDER BY created_at DESC LIMIT 50 OFFSET ?;' | ||
); | ||
expect(logs).toEqual(expectedValue); | ||
|
||
expect(database.prepare).toHaveBeenCalledWith( | ||
'SELECT COUNT(*) as count FROM oianalytics_messages WHERE id IS NOT NULL AND type IN (?) AND status IN (?,?) AND created_at >= ? AND created_at <= ?;' | ||
); | ||
}); | ||
|
||
it('should properly get messages list by search criteria', () => { | ||
const expectedValue: Array<OIAnalyticsMessageDTO> = [ | ||
{ | ||
id: '1234', | ||
creationDate: '2023-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'PENDING', | ||
content: {} as InfoMessageContent | ||
}, | ||
{ | ||
id: '1234', | ||
creationDate: '2024-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'ERRORED', | ||
content: {} as InfoMessageContent | ||
} | ||
]; | ||
all.mockReturnValueOnce([ | ||
{ | ||
id: '1234', | ||
creationDate: '2023-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'PENDING', | ||
content: '{}' | ||
}, | ||
{ | ||
id: '1234', | ||
creationDate: '2024-01-01T00:00:00.000Z', | ||
type: 'INFO', | ||
status: 'ERRORED', | ||
content: '{}' | ||
} | ||
]); | ||
const messages = repository.searchMessagesList({ | ||
types: ['INFO'], | ||
status: ['PENDING', 'ERRORED'], | ||
start: '2023-01-01T00:00:00.000Z', | ||
end: '2023-01-02T00:00:00.000Z' | ||
}); | ||
expect(database.prepare).toHaveBeenCalledWith( | ||
'SELECT id, created_at as creationDate, completed_date as compeltedDate, type, status, error, content FROM oianalytics_messages WHERE id IS NOT NULL ' + | ||
'AND type IN (?) AND status IN (?,?) AND created_at >= ? AND created_at <= ? ORDER BY created_at DESC;' | ||
); | ||
expect(messages).toEqual(expectedValue); | ||
}); | ||
|
||
it('should mark a command as COMPLETED', () => { | ||
repository.markAsCompleted('id1', nowDateString); | ||
const query = `UPDATE oianalytics_messages SET status = 'COMPLETED', completed_date = ? WHERE id = ?;`; | ||
expect(database.prepare).toHaveBeenCalledWith(query); | ||
expect(run).toHaveBeenCalledWith(nowDateString, 'id1'); | ||
}); | ||
|
||
it('should mark a command as ERRORED', () => { | ||
repository.markAsErrored('id1', nowDateString, 'not ok'); | ||
const query = `UPDATE oianalytics_messages SET status = 'ERRORED', completed_date = ?, error = ? WHERE id = ?;`; | ||
expect(database.prepare).toHaveBeenCalledWith(query); | ||
expect(run).toHaveBeenCalledWith(nowDateString, 'not ok', 'id1'); | ||
}); | ||
}); |
Oops, something went wrong.