-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: introduce trades endpoint to recreate MV #261
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! This will surely improve a lot our querying times 🚀
src/logic/http/auth.ts
Outdated
const isValid = await validateApiToken(context, apiTokenHeaderName) | ||
|
||
if (!isValid) { | ||
console.log('[tradesViewAuthMiddleware] Invalid token, returning unauthorized') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind changing the console.log
here for a logger? Logs made with the logger can be traced using the tracer component.
console.log('[tradesViewAuthMiddleware] Invalid token, returning unauthorized') | |
console.log('[tradesViewAuthMiddleware] Invalid token, returning unauthorized') |
test/unit/trades-handler.spec.ts
Outdated
describe('recreateTradesMaterializedViewHandler', () => { | ||
it('should recreate the materialized view successfully', async () => { | ||
const recreateMaterializedViewMock = jest.fn().mockResolvedValue(undefined) | ||
const mockTrades = { | ||
recreateMaterializedView: recreateMaterializedViewMock | ||
} | ||
|
||
const context = { | ||
components: { | ||
trades: mockTrades | ||
} | ||
} | ||
|
||
const result = await recreateTradesMaterializedViewHandler( | ||
context as unknown as Pick<HandlerContextWithPath<'trades', '/v1/trades/materialized-view/recreate'>, 'components'> | ||
) | ||
|
||
expect(result).toEqual({ | ||
status: StatusCode.OK, | ||
body: { | ||
ok: true, | ||
message: 'Materialized view recreated successfully' | ||
} | ||
}) | ||
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should handle errors when recreating the materialized view', async () => { | ||
const error = new Error('Test error') | ||
const recreateMaterializedViewMock = jest.fn().mockRejectedValue(error) | ||
const mockTrades = { | ||
recreateMaterializedView: recreateMaterializedViewMock | ||
} | ||
|
||
const context = { | ||
components: { | ||
trades: mockTrades | ||
} | ||
} | ||
|
||
const result = await recreateTradesMaterializedViewHandler( | ||
context as unknown as Pick<HandlerContextWithPath<'trades', '/v1/trades/materialized-view/recreate'>, 'components'> | ||
) | ||
|
||
expect(result).toEqual({ | ||
status: StatusCode.INTERNAL_SERVER_ERROR, | ||
body: { | ||
ok: false, | ||
message: 'Test error' | ||
} | ||
}) | ||
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind re-writting the tests to fit our standard?
Here's an example on how they should look:
describe('recreateTradesMaterializedViewHandler', () => { | |
it('should recreate the materialized view successfully', async () => { | |
const recreateMaterializedViewMock = jest.fn().mockResolvedValue(undefined) | |
const mockTrades = { | |
recreateMaterializedView: recreateMaterializedViewMock | |
} | |
const context = { | |
components: { | |
trades: mockTrades | |
} | |
} | |
const result = await recreateTradesMaterializedViewHandler( | |
context as unknown as Pick<HandlerContextWithPath<'trades', '/v1/trades/materialized-view/recreate'>, 'components'> | |
) | |
expect(result).toEqual({ | |
status: StatusCode.OK, | |
body: { | |
ok: true, | |
message: 'Materialized view recreated successfully' | |
} | |
}) | |
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | |
}) | |
it('should handle errors when recreating the materialized view', async () => { | |
const error = new Error('Test error') | |
const recreateMaterializedViewMock = jest.fn().mockRejectedValue(error) | |
const mockTrades = { | |
recreateMaterializedView: recreateMaterializedViewMock | |
} | |
const context = { | |
components: { | |
trades: mockTrades | |
} | |
} | |
const result = await recreateTradesMaterializedViewHandler( | |
context as unknown as Pick<HandlerContextWithPath<'trades', '/v1/trades/materialized-view/recreate'>, 'components'> | |
) | |
expect(result).toEqual({ | |
status: StatusCode.INTERNAL_SERVER_ERROR, | |
body: { | |
ok: false, | |
message: 'Test error' | |
} | |
}) | |
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | |
}) | |
}) | |
describe('when handling a materialized view recreation request', () => { | |
let context: Pick<HandlerContextWithPath<'trades', '/v1/trades/materialized-view/recreate'>, 'components'> | |
beforeEach(() => { | |
context = { | |
components: { | |
trades: { | |
recreateMaterializedView: () => undefined | |
} | |
} | |
} | |
}) | |
describe('and the recreation is successful', () => { | |
beforeEach(() => { | |
context.components.trades.recreateMaterializedView = jest.fn().mockResolvedValue(undefined) | |
}) | |
it('should respond with a 200 status code', async () => { | |
const result = await recreateTradesMaterializedViewHandler(context) | |
expect(result).toEqual({ | |
status: StatusCode.OK, | |
body: { | |
ok: true, | |
message: 'Materialized view recreated successfully' | |
} | |
}) | |
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | |
}) | |
describe('and the recreation fails', () => { | |
beforeEach(() => { | |
context.components.trades.recreateMaterializedView = jest.fn().mockRejectedValue(new Error('Test error')) | |
}) | |
it('should respond with a 500 status code', async () => { | |
const result = await recreateTradesMaterializedViewHandler(context) | |
expect(result).toEqual({ | |
status: StatusCode.INTERNAL_SERVER_ERROR, | |
body: { | |
ok: false, | |
message: 'Test error' | |
} | |
}) | |
expect(recreateMaterializedViewMock).toHaveBeenCalledTimes(1) | |
}) | |
}) | |
}) |
Pull Request Test Coverage Report for Build 13762414085Details
💛 - Coveralls |
Add Endpoint to Recreate Trades Materialized View
Overview
This PR adds a new protected endpoint that allows administrators to manually trigger the recreation of the trades materialized view. This provides an on-demand way to refresh the view without requiring a database migration or server restart.
Changes
POST /v1/trades/materialized-view/recreate
Technical Details
x-api-token
headerTRADES_API_TOKEN
environment variableWhy
The trades materialized view occasionally needs to be refreshed to ensure data consistency, especially after a squid promotion. Previously, this required a migration or manual database operation. This endpoint provides a safer, more controlled way to perform this operation through the API.
Testing