Skip to content

Commit

Permalink
Merge branch 'fix-revenue-chart' into 'dev'
Browse files Browse the repository at this point in the history
fix revenue chart

See merge request ergo/rosen-bridge/watcher!207
  • Loading branch information
vorujack committed Dec 22, 2023
2 parents b3868bd + 8bcf21b commit 84ecab1
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 12 deletions.
3 changes: 3 additions & 0 deletions services/watcher/src/api/revenue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ revenueRouter.get('/chart', async (req, res) => {
: Math.min(Number(limitString), MAX_API_LIMIT);

let queryResult;
const wid = Transaction.watcherWID || '';
if (periodString === 'week') {
queryResult = await watcherDatabase.getWeeklyRevenueChartData(
wid,
finalOffset,
finalLimit
);
} else if (periodString === 'month' || periodString === 'year') {
queryResult = await watcherDatabase.getRevenueChartData(
wid,
periodString,
finalOffset,
finalLimit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ViewEntity, ViewColumn } from 'typeorm';
.addSelect(`be.day`, 'day')
.addSelect(`be.month`, 'month')
.addSelect(`be.year`, 'year')
.addSelect('pe.WID', 'wid')
.from('revenue_entity', 're')
.innerJoin('permit_entity', 'pe', 're."permitId" = pe.id')
.innerJoin('block_entity', 'be', 'pe.block = be.hash'),
Expand All @@ -28,6 +29,9 @@ export class RevenueChartDataView {
@ViewColumn()
month!: number;

@ViewColumn()
wid!: string;

@ViewColumn()
year!: number;

Expand Down
6 changes: 4 additions & 2 deletions services/watcher/src/database/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { WatcherMigration1700710099334 } from './postgres/1700710099334-watcherMigration';
import { WatcherMigration1703244656364 } from './postgres/1703244656364-watcherMigration';
import { WatcherMigration1700641198429 } from './sqlite/1700641198429-watcherMigration';
import { WatcherMigration1703244614956 } from './sqlite/1703244614956-watcherMigration';

export default {
sqlite: [WatcherMigration1700641198429],
postgres: [WatcherMigration1700710099334],
sqlite: [WatcherMigration1700641198429, WatcherMigration1703244614956],
postgres: [WatcherMigration1700710099334, WatcherMigration1703244656364],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class WatcherMigration1703244656364 implements MigrationInterface {
name = 'WatcherMigration1703244656364';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_chart_data']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "pe"."WID" AS "wid",
"be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_chart_data',
'SELECT "pe"."WID" AS "wid", "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_chart_data']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_chart_data',
'SELECT "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class WatcherMigration1703244614956 implements MigrationInterface {
name = 'WatcherMigration1703244614956';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_chart_data']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "pe"."WID" AS "wid",
"be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_chart_data',
'SELECT "pe"."WID" AS "wid", "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_chart_data']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_chart_data',
'SELECT "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}
}
24 changes: 16 additions & 8 deletions services/watcher/src/database/models/watcherModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,23 @@ class WatcherDataBase {
* @param offset
* @param limit
*/
getWeeklyRevenueChartData = async (offset: number, limit: number) => {
let qb = this.revenueChartView.createQueryBuilder('rcv');
qb = qb
getWeeklyRevenueChartData = async (
wid: string,
offset: number,
limit: number
) => {
return this.revenueChartView
.createQueryBuilder('rcv')
.select('"tokenId"')
.addSelect('timestamp/604800 as week_number')
.addSelect('sum(amount) as revenue')
.where('wid = :wid', { wid })
.groupBy('"tokenId"')
.addGroupBy('week_number')
.orderBy('week_number', 'DESC');

return qb.offset(offset).limit(limit).execute();
.orderBy('week_number', 'DESC')
.offset(offset)
.limit(limit)
.execute();
};

/**
Expand All @@ -909,15 +915,17 @@ class WatcherDataBase {
* @param limit
*/
getRevenueChartData = async (
wid: string,
period: string,
offset: number,
limit: number
) => {
let qb = this.revenueChartView.createQueryBuilder('rcv');
qb = qb
let qb = this.revenueChartView
.createQueryBuilder('rcv')
.select('"tokenId"')
.addSelect('year')
.addSelect('sum(amount) as revenue')
.where('wid = :wid', { wid })
.groupBy('"tokenId"')
.addGroupBy('year')
.orderBy('year', 'DESC');
Expand Down
9 changes: 8 additions & 1 deletion services/watcher/src/ergo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ export class ErgoUtils {

static transformChartData = (chartData: RevenueChartRecord[]) => {
const chartMap = new Map<string, Array<ChartRecord>>();
const labels: Array<string> = [];
chartData.forEach((record) => {
const year = Number(record.year);
const month = Number(record.month) || 1;
Expand All @@ -535,6 +536,7 @@ export class ErgoUtils {
label: String(timestamp),
amount: record.revenue.toString(),
};
if (!labels.includes(String(timestamp))) labels.push(String(timestamp));
const chartRecords = chartMap.get(record.tokenId) || [];
chartRecords.push(chartRecord);
chartMap.set(record.tokenId, chartRecords);
Expand All @@ -545,9 +547,14 @@ export class ErgoUtils {
data: Array<ChartRecord>;
}[] = [];
chartMap.forEach((records, tokenId) => {
const filledRecords = labels.map((timestamp) => {
const filtered = records.filter((rec) => rec.label === timestamp);
if (filtered.length) return filtered[0];
return { label: timestamp, amount: '0' };
});
jsonObject.push({
title: this.tokenDetailByTokenMap(tokenId, ERGO_CHAIN_NAME),
data: records,
data: filledRecords,
});
});
return jsonObject;
Expand Down
5 changes: 4 additions & 1 deletion services/watcher/tests/ergo/statistics/mockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ const revenueWeeklyChart = [
tokenId:
'0034c44f0c7a38f833190d44125ff9b3a0dd9dbb89138160182a930bc521db95',
},
data: [{ label: '123379200000', amount: '10' }],
data: [
{ label: '123379200000', amount: '10' },
{ label: '0', amount: '0' },
],
},
{
title: {
Expand Down

0 comments on commit 84ecab1

Please sign in to comment.