Skip to content

Commit

Permalink
Merge pull request #14 from near-daos/feature/weekly_active_daos
Browse files Browse the repository at this point in the history
Calculating weekly active daos instead of daily
  • Loading branch information
okalenyk authored Jan 19, 2022
2 parents 2be7109 + 87fd917 commit 01e49e6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]
},
};
4 changes: 3 additions & 1 deletion apps/api/src/flow/flow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlowTotalResponse> {
async totals(
@Param(ContractContextPipe) context: ContractContext,
): Promise<FlowTotalResponse> {
return this.flowService.totals(context);
}

Expand Down
32 changes: 14 additions & 18 deletions apps/api/src/general/general.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,15 +28,15 @@ export class GeneralService {
async totals(
context: DaoContractContext | ContractContext,
): Promise<GeneralTotalResponse> {
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),
]);
Expand All @@ -46,7 +45,7 @@ export class GeneralService {
dao,
activity: {
count: activity.count,
growth: getGrowth(activity.count, dayAgoActivity.count),
growth: getGrowth(activity.count, weekAgoActivity.count),
},
groups,
averageGroups,
Expand All @@ -68,20 +67,17 @@ export class GeneralService {
context: ContractContext,
metricQuery: MetricQuery,
): Promise<MetricResponse> {
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,
})),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
Expand Down
3 changes: 2 additions & 1 deletion libs/exchange/src/coin-gecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class CoinGeckoService {
}

private getBaseUri(): string {
return this.configService.get<ExchangeConfig>('exchange')?.coingeckoApiBaseUrl;
return this.configService.get<ExchangeConfig>('exchange')
?.coingeckoApiBaseUrl;
}
}
19 changes: 15 additions & 4 deletions libs/transaction/src/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -83,12 +83,12 @@ export class TransactionService {
return this.getContractActivityCountQuery(context, metricQuery).getRawOne();
}

async getContractActivityCountDaily(
async getContractActivityCountWeekly(
context: DaoContractContext | ContractContext,
metricQuery?: MetricQuery,
): Promise<DailyCountDto[]> {
let queryBuilder = this.getContractActivityCountQuery(context, metricQuery);
queryBuilder = this.addDailySelection(queryBuilder);
queryBuilder = this.addWeeklySelection(queryBuilder);

return queryBuilder.execute();
}
Expand Down Expand Up @@ -244,7 +244,18 @@ export class TransactionService {
): SelectQueryBuilder<Transaction> {
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<Transaction>,
): SelectQueryBuilder<Transaction> {
return qb
.addSelect(
`date_trunc('week', to_timestamp(block_timestamp / 1e9)) as day`,
)
.groupBy('day')
.orderBy('day', 'ASC');
Expand Down

0 comments on commit 01e49e6

Please sign in to comment.