From 0fb0b9715238abe37a22de5bf18de38123b1f093 Mon Sep 17 00:00:00 2001 From: Vincent Chau <99756290+vincentwschau@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:30:03 -0500 Subject: [PATCH] Address comments. --- ...20241119162238_create_vault_hourly_view.ts | 8 +-- .../20241119163402_create_vault_daily_view.ts | 8 +-- .../src/stores/vault-pnl-ticks-view.ts | 13 ++-- .../__tests__/tasks/refresh-vault-pnl.test.ts | 35 +++++----- indexer/services/roundtable/src/config.ts | 2 +- .../roundtable/src/tasks/refresh-vault-pnl.ts | 68 +++++++++++-------- 6 files changed, 70 insertions(+), 64 deletions(-) diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20241119162238_create_vault_hourly_view.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20241119162238_create_vault_hourly_view.ts index aff1144bc1..fe0e4336d1 100644 --- a/indexer/packages/postgres/src/db/migrations/migration_files/20241119162238_create_vault_hourly_view.ts +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20241119162238_create_vault_hourly_view.ts @@ -1,4 +1,4 @@ -import * as Knex from "knex"; +import * as Knex from 'knex'; const RAW_VAULTS_PNL_HOURLY_QUERY: string = ` CREATE MATERIALIZED VIEW IF NOT EXISTS vaults_hourly_pnl AS WITH vault_subaccounts AS @@ -35,13 +35,13 @@ FROM ( AND "blockTime" >= NOW() - interval '604800 second' ) AS pnl_intervals WHERE r = 1 ORDER BY "subaccountId"; -` +`; export async function up(knex: Knex): Promise { await knex.raw(RAW_VAULTS_PNL_HOURLY_QUERY); - await knex.raw(`CREATE UNIQUE INDEX ON vaults_hourly_pnl (id);`) + await knex.raw('CREATE UNIQUE INDEX ON vaults_hourly_pnl (id);'); } export async function down(knex: Knex): Promise { - await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_hourly_pnl;') + await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_hourly_pnl;'); } diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20241119163402_create_vault_daily_view.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20241119163402_create_vault_daily_view.ts index ad18d018f1..4469a8bec5 100644 --- a/indexer/packages/postgres/src/db/migrations/migration_files/20241119163402_create_vault_daily_view.ts +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20241119163402_create_vault_daily_view.ts @@ -1,4 +1,4 @@ -import * as Knex from "knex"; +import * as Knex from 'knex'; const RAW_VAULTS_PNL_DAILY_QUERY: string = ` CREATE MATERIALIZED VIEW IF NOT EXISTS vaults_daily_pnl AS WITH vault_subaccounts AS @@ -35,13 +35,13 @@ FROM ( AND "blockTime" >= NOW() - interval '7776000 second' ) AS pnl_intervals WHERE r = 1 ORDER BY "subaccountId"; -` +`; export async function up(knex: Knex): Promise { await knex.raw(RAW_VAULTS_PNL_DAILY_QUERY); - await knex.raw(`CREATE UNIQUE INDEX ON vaults_daily_pnl (id);`) + await knex.raw('CREATE UNIQUE INDEX ON vaults_daily_pnl (id);'); } export async function down(knex: Knex): Promise { - await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_daily_pnl;') + await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_daily_pnl;'); } diff --git a/indexer/packages/postgres/src/stores/vault-pnl-ticks-view.ts b/indexer/packages/postgres/src/stores/vault-pnl-ticks-view.ts index fb8b765827..abac0a88b2 100644 --- a/indexer/packages/postgres/src/stores/vault-pnl-ticks-view.ts +++ b/indexer/packages/postgres/src/stores/vault-pnl-ticks-view.ts @@ -1,31 +1,30 @@ -import _ from 'lodash'; import { DateTime } from 'luxon'; import { knexReadReplica } from '../helpers/knex'; +import { rawQuery } from '../helpers/stores-helpers'; import { PnlTickInterval, PnlTicksFromDatabase, } from '../types'; -import { rawQuery } from '../helpers/stores-helpers'; const VAULT_HOURLY_PNL_VIEW: string = 'vaults_hourly_pnl'; const VAULT_DAILY_PNL_VIEW: string = 'vaults_daily_pnl'; export async function refreshHourlyView(): Promise { - return await rawQuery( + await rawQuery( `REFRESH MATERIALIZED VIEW CONCURRENTLY ${VAULT_HOURLY_PNL_VIEW}`, { readReplica: false, - } + }, ); } export async function refreshDailyView(): Promise { - return await rawQuery( + await rawQuery( `REFRESH MATERIALIZED VIEW CONCURRENTLY ${VAULT_DAILY_PNL_VIEW}`, { readReplica: false, - } + }, ); } @@ -35,7 +34,7 @@ export async function getVaultsPnl( earliestDate: DateTime, ): Promise { let viewName: string = VAULT_DAILY_PNL_VIEW; - if (interval == PnlTickInterval.hour) { + if (interval === PnlTickInterval.hour) { viewName = VAULT_HOURLY_PNL_VIEW; } const result: { diff --git a/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts b/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts index 595bef1d6d..0078dbb74a 100644 --- a/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts +++ b/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts @@ -1,7 +1,7 @@ import config from '../../src/config'; import refreshVaulPnlTask from '../../src/tasks/refresh-vault-pnl'; import { Settings, DateTime } from 'luxon'; -import { +import { BlockTable, PnlTickInterval, PnlTicksFromDatabase, @@ -46,62 +46,62 @@ describe('refresh-vault-pnl', () => { Settings.now = () => new Date().valueOf(); }); - it('refreshes hourly view if within time window of an hour', async() => { + it('refreshes hourly view if within time window of an hour', async () => { Settings.now = () => currentTime.startOf('hour').plus( - { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS - 1}, - ).valueOf() + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS - 1 }, + ).valueOf(); const pnlTick: PnlTicksFromDatabase = await setupPnlTick(); await refreshVaulPnlTask(); const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( PnlTickInterval.hour, 86400, - currentTime.minus({ day: 1}), + currentTime.minus({ day: 1 }), ); expect(pnlTicks).toEqual([pnlTick]); }); - it('refreshes daily view if within time window of a day', async() => { + it('refreshes daily view if within time window of a day', async () => { Settings.now = () => currentTime.startOf('day').plus( - { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS - 1 }, - ).valueOf() + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS - 1 }, + ).valueOf(); const pnlTick: PnlTicksFromDatabase = await setupPnlTick(); await refreshVaulPnlTask(); const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( PnlTickInterval.day, 608400, - currentTime.minus({ day: 7}), + currentTime.minus({ day: 7 }), ); expect(pnlTicks).toEqual([pnlTick]); }); - it('does not refresh hourly view if outside of time window of an hour', async() => { + it('does not refresh hourly view if outside of time window of an hour', async () => { Settings.now = () => currentTime.startOf('hour').plus( - { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS + 1}, - ).valueOf() + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS + 1 }, + ).valueOf(); await setupPnlTick(); await refreshVaulPnlTask(); const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( PnlTickInterval.hour, 86400, - currentTime.minus({ day: 1}), + currentTime.minus({ day: 1 }), ); expect(pnlTicks).toEqual([]); }); - it('does not refresh hourly view if outside time window of a day', async() => { + it('does not refresh daily view if outside time window of a day', async () => { Settings.now = () => currentTime.startOf('day').plus( - { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS + 1}, - ).valueOf() + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS + 1 }, + ).valueOf(); await setupPnlTick(); await refreshVaulPnlTask(); const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( PnlTickInterval.day, 608400, - currentTime.minus({ day: 7}), + currentTime.minus({ day: 7 }), ); expect(pnlTicks).toEqual([]); }); @@ -128,4 +128,3 @@ describe('refresh-vault-pnl', () => { return createdTick; } }); - diff --git a/indexer/services/roundtable/src/config.ts b/indexer/services/roundtable/src/config.ts index 2b0a5bb965..3d03fd0e19 100644 --- a/indexer/services/roundtable/src/config.ts +++ b/indexer/services/roundtable/src/config.ts @@ -221,7 +221,7 @@ export const configSchema = { ATTEMPT_PER_SUBACCOUNT: parseInteger({ default: 3 }), // Refresh vault pnl view - TIME_WINDOW_FOR_REFRESH_MS: parseInteger({ default: 15 * ONE_MINUTE_IN_MILLISECONDS }), + TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS: parseInteger({ default: 15 * ONE_MINUTE_IN_MILLISECONDS }), }; export default parseSchema(configSchema); diff --git a/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts b/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts index b74b08e5ec..635ef5526f 100644 --- a/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts +++ b/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts @@ -1,45 +1,53 @@ import { logger, stats } from '@dydxprotocol-indexer/base'; import { - VaultPnlTicksView + VaultPnlTicksView, } from '@dydxprotocol-indexer/postgres'; import { DateTime } from 'luxon'; + import config from '../config'; /** - * Update the affiliate info for all affiliate addresses. + * Refresh the vault pnl ticks views. */ export default async function runTask(): Promise { const taskStart: number = Date.now(); - - const currentTime: DateTime = DateTime.utc(); - if (currentTime.diff( - currentTime.startOf('hour') - ).toMillis() < config.TIME_WINDOW_FOR_REFRESH_MS) { - logger.info({ - at: 'refresh-vault-pnl#runTask', - message: 'Refreshing vault hourly pnl view', - currentTime, - }); - await VaultPnlTicksView.refreshHourlyView(); - stats.timing( - `${config.SERVICE_NAME}.refresh-vault-pnl.hourly-view.timing`, - Date.now() - taskStart, - ) - } + try { + const currentTime: DateTime = DateTime.utc(); + if (currentTime.diff( + currentTime.startOf('hour'), + ).toMillis() < config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS) { + logger.info({ + at: 'refresh-vault-pnl#runTask', + message: 'Refreshing vault hourly pnl view', + currentTime, + }); + await VaultPnlTicksView.refreshHourlyView(); + stats.timing( + `${config.SERVICE_NAME}.refresh-vault-pnl.hourly-view.timing`, + Date.now() - taskStart, + ); + } - const refreshDailyStart: number = Date.now(); - if (currentTime.diff( - currentTime.startOf('day') - ).toMillis() < config.TIME_WINDOW_FOR_REFRESH_MS) { - logger.info({ + if (currentTime.diff( + currentTime.startOf('day'), + ).toMillis() < config.TIME_WINDOW_FOR_REFRESH_VAULT_PNL_MS) { + const refreshDailyStart: number = Date.now(); + logger.info({ + at: 'refresh-vault-pnl#runTask', + message: 'Refreshing vault daily pnl view', + currentTime, + }); + await VaultPnlTicksView.refreshDailyView(); + stats.timing( + `${config.SERVICE_NAME}.refresh-vault-pnl.daily-view.timing`, + Date.now() - refreshDailyStart, + ); + } + } catch (error) { + logger.error({ at: 'refresh-vault-pnl#runTask', - message: 'Refreshing vault daily pnl view', - currentTime, + message: 'Failed to refresh vault pnl views', + error, }); - await VaultPnlTicksView.refreshDailyView(); - stats.timing( - `${config.SERVICE_NAME}.refresh-vault-pnl.daily-view.timing`, - Date.now() - refreshDailyStart, - ); } }