Skip to content

Commit

Permalink
Feat: Add daily block statistics (#1265)
Browse files Browse the repository at this point in the history
* feat: Refactor influx classes for stardust/nova (they now share functionaliy in super class InfluxClient). Add daily blocks statistics for nova (api).

* feat: Add statitics page for Nova. Add daily blocks chart.

* feat: Remove noPayload row from Daily Block query
  • Loading branch information
msarcev authored Mar 7, 2024
1 parent 6c30e49 commit 9362bbe
Show file tree
Hide file tree
Showing 24 changed files with 772 additions and 377 deletions.
16 changes: 14 additions & 2 deletions api/src/initServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import { LocalStorageService } from "./services/localStorageService";
import { NetworkService } from "./services/networkService";
import { ChronicleService as ChronicleServiceNova } from "./services/nova/chronicleService";
import { NovaFeed } from "./services/nova/feed/novaFeed";
import { InfluxServiceNova } from "./services/nova/influx/influxServiceNova";
import { NodeInfoService as NodeInfoServiceNova } from "./services/nova/nodeInfoService";
import { NovaApiService } from "./services/nova/novaApiService";
import { NovaStatsService } from "./services/nova/stats/novaStatsService";
import { ChronicleService as ChronicleServiceStardust } from "./services/stardust/chronicleService";
import { StardustFeed } from "./services/stardust/feed/stardustFeed";
import { InfluxDBService } from "./services/stardust/influx/influxDbService";
import { InfluxServiceStardust } from "./services/stardust/influx/influxServiceStardust";
import { NodeInfoService as NodeInfoServiceStardust } from "./services/stardust/nodeInfoService";
import { StardustApiService } from "./services/stardust/stardustApiService";
import { StardustStatsService } from "./services/stardust/stats/stardustStatsService";
Expand Down Expand Up @@ -192,7 +193,7 @@ function initStardustServices(networkConfig: INetwork): void {
const stardustStatsService = new StardustStatsService(networkConfig);
ServiceFactory.register(`stats-${networkConfig.network}`, () => stardustStatsService);

const influxDBService = new InfluxDBService(networkConfig);
const influxDBService = new InfluxServiceStardust(networkConfig);
influxDBService
.buildClient()
.then((hasClient) => {
Expand Down Expand Up @@ -244,6 +245,17 @@ function initNovaServices(networkConfig: INetwork): void {
ServiceFactory.register(`feed-${networkConfig.network}`, () => novaFeed);
});
});

const influxDBService = new InfluxServiceNova(networkConfig);
influxDBService
.buildClient()
.then((hasClient) => {
logger.debug(`[InfluxDb] Registering client with name "${networkConfig.network}". Has client: ${hasClient}`);
if (hasClient) {
ServiceFactory.register(`influxdb-${networkConfig.network}`, () => influxDBService);
}
})
.catch((e) => logger.warn(`Failed to build influxDb client for "${networkConfig.network}". Cause: ${e}`));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions api/src/models/influx/nova/IInfluxDbCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IBlocksDailyInflux } from "./IInfluxTimedEntries";
import { DayKey } from "../types";

/**
* The cache for influx graphs (daily).
*/
export interface IInfluxDailyCache {
blocksDaily: Map<DayKey, IBlocksDailyInflux>;
}

/**
* The helper to initialize empty maps
* @returns The initial cache object
*/
export const initializeEmptyDailyCache = () => ({
blocksDaily: new Map(),
});
8 changes: 8 additions & 0 deletions api/src/models/influx/nova/IInfluxTimedEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ITimedEntry } from "../types";

export type IBlocksDailyInflux = ITimedEntry & {
transaction: number | null;
taggedData: number | null;
validation: number | null;
candidacy: number | null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ import {
IUnclaimedTokensDailyInflux,
IUnlockConditionsPerTypeDailyInflux,
} from "./IInfluxTimedEntries";

/**
* The key is a date in string format "DD-MM-YYYY"
*/
export type DayKey = string;

/**
* The format used for moment.format(...)
*/
export const DAY_KEY_FORMAT = "DD-MM-YYYY";
import { DayKey } from "../types";

/**
* The cache for influx graphs (daily).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { INanoDate } from "influx";

export interface ITimedEntry {
time: INanoDate;
}
import { ITimedEntry } from "../types";

export type IBlocksDailyInflux = ITimedEntry & {
transaction: number | null;
Expand Down
15 changes: 15 additions & 0 deletions api/src/models/influx/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { INanoDate } from "influx";

export interface ITimedEntry {
time: INanoDate;
}

/**
* The key is a date in string format "DD-MM-YYYY"
*/
export type DayKey = string;

/**
* The format used for moment.format(...)
*/
export const DAY_KEY_FORMAT = "DD-MM-YYYY";
7 changes: 7 additions & 0 deletions api/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,11 @@ export const routes: IRoute[] = [
},
{ path: "/nova/slot/:network/:slotIndex", method: "get", folder: "nova/slot", func: "get" },
{ path: "/nova/epoch/committee/:network/:epochIndex", method: "get", folder: "nova/epoch/committee", func: "get" },
{
path: "/nova/analytics/daily/:network",
method: "get",
folder: "nova/analytics/influx/daily",
func: "get",
sign: true,
},
];
43 changes: 43 additions & 0 deletions api/src/routes/nova/analytics/influx/daily/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ServiceFactory } from "../../../../../factories/serviceFactory";
import { INetworkBoundGetRequest } from "../../../../../models/api/INetworkBoundGetRequest";
import { IConfiguration } from "../../../../../models/configuration/IConfiguration";
import { NOVA } from "../../../../../models/db/protocolVersion";
import { IBlocksDailyInflux } from "../../../../../models/influx/nova/IInfluxTimedEntries";
import { NetworkService } from "../../../../../services/networkService";
import { InfluxServiceNova } from "../../../../../services/nova/influx/influxServiceNova";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
* The response with the current cached analytic data.
*/
export interface IDailyAnalyticsResponse {
error?: string;
blocksDaily?: IBlocksDailyInflux[];
}

/**
* Get influx cached analytic stats for the requested network.
* @param _ The configuration.
* @param request The request.
* @returns The response.
*/
export async function get(_: IConfiguration, request: INetworkBoundGetRequest): Promise<IDailyAnalyticsResponse> {
const networkService = ServiceFactory.get<NetworkService>("network");
const networks = networkService.networkNames();
const networkConfig = networkService.get(request.network);
ValidationHelper.oneOf(request.network, networks, "network");

if (networkConfig.protocolVersion !== NOVA) {
return {};
}

const influxService = ServiceFactory.get<InfluxServiceNova>(`influxdb-${request.network}`);

return influxService
? {
blocksDaily: influxService.blocksDaily,
}
: {
error: "Influx service not found for this network.",
};
}
6 changes: 3 additions & 3 deletions api/src/routes/stardust/analytics/influx/daily/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
IUnclaimedGenesisOutputsDailyInflux,
IUnclaimedTokensDailyInflux,
IUnlockConditionsPerTypeDailyInflux,
} from "../../../../../models/influx/IInfluxTimedEntries";
} from "../../../../../models/influx/stardust/IInfluxTimedEntries";
import { NetworkService } from "../../../../../services/networkService";
import { InfluxDBService } from "../../../../../services/stardust/influx/influxDbService";
import { InfluxServiceStardust } from "../../../../../services/stardust/influx/influxServiceStardust";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function get(_: IConfiguration, request: INetworkBoundGetRequest):
return {};
}

const influxService = ServiceFactory.get<InfluxDBService>(`influxdb-${request.network}`);
const influxService = ServiceFactory.get<InfluxServiceStardust>(`influxdb-${request.network}`);

return influxService
? {
Expand Down
4 changes: 2 additions & 2 deletions api/src/routes/stardust/analytics/influx/stats/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IConfiguration } from "../../../../../models/configuration/IConfigurati
import { STARDUST } from "../../../../../models/db/protocolVersion";
import { IShimmerClaimedResponse } from "../../../../../models/services/stardust/IShimmerClaimedResponse";
import { NetworkService } from "../../../../../services/networkService";
import { InfluxDBService } from "../../../../../services/stardust/influx/influxDbService";
import { InfluxServiceStardust } from "../../../../../services/stardust/influx/influxServiceStardust";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
Expand All @@ -30,7 +30,7 @@ export async function get(_: IConfiguration, request: INetworkBoundGetRequest):
return {};
}

const influxService = ServiceFactory.get<InfluxDBService>(`influxdb-${request.network}`);
const influxService = ServiceFactory.get<InfluxServiceStardust>(`influxdb-${request.network}`);

return influxService
? {
Expand Down
4 changes: 2 additions & 2 deletions api/src/routes/stardust/milestone/influx/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IMilestoneAnalyticStats } from "../../../../models/api/stats/IMilestone
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { InfluxDBService } from "../../../../services/stardust/influx/influxDbService";
import { InfluxServiceStardust } from "../../../../services/stardust/influx/influxServiceStardust";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -25,7 +25,7 @@ export async function get(_: IConfiguration, request: IMilestoneStatsRequest): P
return {};
}

const influxService = ServiceFactory.get<InfluxDBService>(`influxdb-${request.network}`);
const influxService = ServiceFactory.get<InfluxServiceStardust>(`influxdb-${request.network}`);

if (!influxService) {
return { error: "Influx service not found for this network." };
Expand Down
Loading

0 comments on commit 9362bbe

Please sign in to comment.