From 45128c971906e058925d4244bcc5389ccae79377 Mon Sep 17 00:00:00 2001 From: Oleg Kalenik Date: Wed, 19 Jan 2022 11:39:15 +0200 Subject: [PATCH 1/4] *calculating weekly active daos instead of daily --- apps/api/src/general/general.service.ts | 29 +++++++++------------ libs/transaction/src/transaction.service.ts | 19 +++++++++++--- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/apps/api/src/general/general.service.ts b/apps/api/src/general/general.service.ts index 09b016e..2e09920 100644 --- a/apps/api/src/general/general.service.ts +++ b/apps/api/src/general/general.service.ts @@ -29,15 +29,15 @@ export class GeneralService { async totals( context: DaoContractContext | ContractContext, ): Promise { - const dayAgo = moment().subtract(1, 'days'); + const weekAgo = moment().subtract(1, 'week'); - const [dao, groups, averageGroups, activity, dayAgoActivity] = + const [dao, groups, averageGroups, activity, weekAgoActivity] = await Promise.all([ this.metricService.total(context, DaoStatsMetric.DaoCount), this.metricService.total(context, DaoStatsMetric.GroupsCount), this.metricService.total(context, DaoStatsMetric.GroupsCount, true), this.transactionService.getContractActivityCount(context, { - to: dayAgo.valueOf(), + to: weekAgo.valueOf(), }), this.transactionService.getContractActivityCount(context), ]); @@ -46,7 +46,7 @@ export class GeneralService { dao, activity: { count: activity.count, - growth: getGrowth(activity.count, dayAgoActivity.count), + growth: getGrowth(activity.count, weekAgoActivity.count), }, groups, averageGroups, @@ -68,20 +68,17 @@ export class GeneralService { context: ContractContext, metricQuery: MetricQuery, ): Promise { - const metrics = await this.transactionService.getContractActivityCountDaily( - context, - metricQuery, - ); + const metrics = + await this.transactionService.getContractActivityCountWeekly( + context, + metricQuery, + ); return { - metrics: patchMetricDays( - metricQuery, - metrics.map(({ day, count }) => ({ - timestamp: moment(day).valueOf(), - count, - })), - MetricType.Daily, - ), + metrics: metrics.map(({ day, count }) => ({ + timestamp: moment(day).valueOf(), + count, + })), }; } diff --git a/libs/transaction/src/transaction.service.ts b/libs/transaction/src/transaction.service.ts index 255bf00..e6d991a 100644 --- a/libs/transaction/src/transaction.service.ts +++ b/libs/transaction/src/transaction.service.ts @@ -57,7 +57,7 @@ export class TransactionService { const query = ` with data as ( select - date_trunc('day', to_timestamp(block_timestamp / 1000 / 1000 / 1000)) as day, + date_trunc('day', to_timestamp(block_timestamp / 1e9)) as day, count(1) from transactions where contract_id = '${contractId}' and type = '${txType}' @@ -83,12 +83,12 @@ export class TransactionService { return this.getContractActivityCountQuery(context, metricQuery).getRawOne(); } - async getContractActivityCountDaily( + async getContractActivityCountWeekly( context: DaoContractContext | ContractContext, metricQuery?: MetricQuery, ): Promise { let queryBuilder = this.getContractActivityCountQuery(context, metricQuery); - queryBuilder = this.addDailySelection(queryBuilder); + queryBuilder = this.addWeeklySelection(queryBuilder); return queryBuilder.execute(); } @@ -244,7 +244,18 @@ export class TransactionService { ): SelectQueryBuilder { return qb .addSelect( - `date_trunc('day', to_timestamp(block_timestamp / 1000 / 1000 / 1000)) as day`, + `date_trunc('day', to_timestamp(block_timestamp / 1e9)) as day`, + ) + .groupBy('day') + .orderBy('day', 'ASC'); + } + + private addWeeklySelection( + qb: SelectQueryBuilder, + ): SelectQueryBuilder { + return qb + .addSelect( + `date_trunc('week', to_timestamp(block_timestamp / 1e9)) as day`, ) .groupBy('day') .orderBy('day', 'ASC'); From 8cc19cc089348068c6ae33b2d0b2bc5bdcf91cc2 Mon Sep 17 00:00:00 2001 From: Oleg Kalenik Date: Wed, 19 Jan 2022 14:19:21 +0200 Subject: [PATCH 2/4] *fixed github warnings --- apps/api/src/general/general.service.ts | 3 +-- apps/api/src/pipes/contract-context.pipe.ts | 2 +- libs/sputnik-dao/src/sputnik-dao.service.ts | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/api/src/general/general.service.ts b/apps/api/src/general/general.service.ts index 2e09920..0e25fc3 100644 --- a/apps/api/src/general/general.service.ts +++ b/apps/api/src/general/general.service.ts @@ -10,12 +10,11 @@ import { LeaderboardMetricResponse, MetricQuery, MetricResponse, - MetricType, } from '@dao-stats/common'; import { TransactionService } from '@dao-stats/transaction'; import { GeneralTotalResponse } from './dto'; import { MetricService } from '../common/metric.service'; -import { getDailyIntervals, getGrowth, patchMetricDays } from '../utils'; +import { getDailyIntervals, getGrowth } from '../utils'; @Injectable() export class GeneralService { diff --git a/apps/api/src/pipes/contract-context.pipe.ts b/apps/api/src/pipes/contract-context.pipe.ts index 1e9f1c5..aac66b6 100644 --- a/apps/api/src/pipes/contract-context.pipe.ts +++ b/apps/api/src/pipes/contract-context.pipe.ts @@ -4,7 +4,7 @@ import { RequestContext } from '@medibloc/nestjs-request-context'; @Injectable() export class ContractContextPipe implements PipeTransform { - async transform(context: ContractContext, metadata: ArgumentMetadata) { + async transform(context: ContractContext, _metadata: ArgumentMetadata) { return { ...context, ...RequestContext.get(), diff --git a/libs/sputnik-dao/src/sputnik-dao.service.ts b/libs/sputnik-dao/src/sputnik-dao.service.ts index 4e2bf29..66c4ebc 100644 --- a/libs/sputnik-dao/src/sputnik-dao.service.ts +++ b/libs/sputnik-dao/src/sputnik-dao.service.ts @@ -9,12 +9,12 @@ import { @Injectable() export class AggregationService implements Aggregator { - aggregateDaos(contractId: string): AsyncGenerator { + aggregateDaos(_contractId: string): AsyncGenerator { throw new Error('Method not implemented.'); } aggregateHistoricalMetrics( - contractId: string, + _contractId: string, ): AsyncGenerator { throw new Error('Method not implemented.'); } From 068a071f9f149cf0999edd8b6013e4e4fe6b0708 Mon Sep 17 00:00:00 2001 From: Oleg Kalenik Date: Wed, 19 Jan 2022 14:48:07 +0200 Subject: [PATCH 3/4] *fixed lint warnings --- apps/api/src/flow/flow.controller.ts | 4 +++- apps/api/src/pipes/contract-context.pipe.ts | 2 +- .../src/migrations/1642492135190-AddContractCoinType.ts | 4 +--- libs/exchange/src/coin-gecko.service.ts | 3 ++- libs/sputnik-dao/src/sputnik-dao.service.ts | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/apps/api/src/flow/flow.controller.ts b/apps/api/src/flow/flow.controller.ts index fbedb92..c175bdf 100644 --- a/apps/api/src/flow/flow.controller.ts +++ b/apps/api/src/flow/flow.controller.ts @@ -29,7 +29,9 @@ export class FlowController { description: 'Bad Request Response based on the query params set', }) @Get('/') - async totals(@Param(ContractContextPipe) context: ContractContext): Promise { + async totals( + @Param(ContractContextPipe) context: ContractContext, + ): Promise { return this.flowService.totals(context); } diff --git a/apps/api/src/pipes/contract-context.pipe.ts b/apps/api/src/pipes/contract-context.pipe.ts index aac66b6..1e9f1c5 100644 --- a/apps/api/src/pipes/contract-context.pipe.ts +++ b/apps/api/src/pipes/contract-context.pipe.ts @@ -4,7 +4,7 @@ import { RequestContext } from '@medibloc/nestjs-request-context'; @Injectable() export class ContractContextPipe implements PipeTransform { - async transform(context: ContractContext, _metadata: ArgumentMetadata) { + async transform(context: ContractContext, metadata: ArgumentMetadata) { return { ...context, ...RequestContext.get(), diff --git a/libs/common/src/migrations/1642492135190-AddContractCoinType.ts b/libs/common/src/migrations/1642492135190-AddContractCoinType.ts index 197222c..de5fbc7 100644 --- a/libs/common/src/migrations/1642492135190-AddContractCoinType.ts +++ b/libs/common/src/migrations/1642492135190-AddContractCoinType.ts @@ -1,8 +1,6 @@ import { MigrationInterface, QueryRunner } from 'typeorm'; -export class AddContractCoinType1642492135190 - implements MigrationInterface -{ +export class AddContractCoinType1642492135190 implements MigrationInterface { name = 'AddContractCoinType1642492135190'; public async up(queryRunner: QueryRunner): Promise { diff --git a/libs/exchange/src/coin-gecko.service.ts b/libs/exchange/src/coin-gecko.service.ts index 32dc9bd..395455e 100644 --- a/libs/exchange/src/coin-gecko.service.ts +++ b/libs/exchange/src/coin-gecko.service.ts @@ -50,6 +50,7 @@ export class CoinGeckoService { } private getBaseUri(): string { - return this.configService.get('exchange')?.coingeckoApiBaseUrl; + return this.configService.get('exchange') + ?.coingeckoApiBaseUrl; } } diff --git a/libs/sputnik-dao/src/sputnik-dao.service.ts b/libs/sputnik-dao/src/sputnik-dao.service.ts index 66c4ebc..4e2bf29 100644 --- a/libs/sputnik-dao/src/sputnik-dao.service.ts +++ b/libs/sputnik-dao/src/sputnik-dao.service.ts @@ -9,12 +9,12 @@ import { @Injectable() export class AggregationService implements Aggregator { - aggregateDaos(_contractId: string): AsyncGenerator { + aggregateDaos(contractId: string): AsyncGenerator { throw new Error('Method not implemented.'); } aggregateHistoricalMetrics( - _contractId: string, + contractId: string, ): AsyncGenerator { throw new Error('Method not implemented.'); } From 87fd917a90b14f3768a13a6da921bcbc229b9cdb Mon Sep 17 00:00:00 2001 From: Oleg Kalenik Date: Wed, 19 Jan 2022 14:48:19 +0200 Subject: [PATCH 4/4] *skipping linter 'no-unused-vars' in args --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index 0419a5d..c6a7291 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -21,5 +21,6 @@ module.exports = { '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-inferrable-types': 'off', + "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }] }, };