Skip to content

Commit

Permalink
Address comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Nov 22, 2024
1 parent 1bde4f6 commit 0fb0b97
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<void> {
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<void> {
await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_hourly_pnl;')
await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_hourly_pnl;');
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<void> {
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<void> {
await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_daily_pnl;')
await knex.raw('DROP MATERIALIZED VIEW IF EXISTS vaults_daily_pnl;');
}
13 changes: 6 additions & 7 deletions indexer/packages/postgres/src/stores/vault-pnl-ticks-view.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return await rawQuery(
await rawQuery(
`REFRESH MATERIALIZED VIEW CONCURRENTLY ${VAULT_HOURLY_PNL_VIEW}`,
{
readReplica: false,
}
},
);
}

export async function refreshDailyView(): Promise<void> {
return await rawQuery(
await rawQuery(
`REFRESH MATERIALIZED VIEW CONCURRENTLY ${VAULT_DAILY_PNL_VIEW}`,
{
readReplica: false,
}
},
);
}

Expand All @@ -35,7 +34,7 @@ export async function getVaultsPnl(
earliestDate: DateTime,
): Promise<PnlTicksFromDatabase[]> {
let viewName: string = VAULT_DAILY_PNL_VIEW;
if (interval == PnlTickInterval.hour) {
if (interval === PnlTickInterval.hour) {
viewName = VAULT_HOURLY_PNL_VIEW;
}
const result: {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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([]);
});
Expand All @@ -128,4 +128,3 @@ describe('refresh-vault-pnl', () => {
return createdTick;
}
});

2 changes: 1 addition & 1 deletion indexer/services/roundtable/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
68 changes: 38 additions & 30 deletions indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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,
);
}
}

0 comments on commit 0fb0b97

Please sign in to comment.