Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend query with segment #31

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/db/services/dappAnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ interface ArgFilter {
interface GetMetricsRequest {
breakdown?: boolean;
filters?: Filter[];
segmentId?: string;
}
interface Filter {
name?: string;
Expand All @@ -156,12 +157,20 @@ const buildQueryForFilter = (
dAppId: string,
filter: Filter,
breakdown: boolean,
metric: "wallets" | "interactions" | "transferredTokens"
metric: "wallets" | "interactions" | "transferredTokens",
segmentId?: string
): { query: string; values: any[] } => {
const dapp_activity_table = `"dapp_analytics"."dapp_analytics_${dAppId}"`;
const conditions: string[] = [];
const values: any[] = [];

if (segmentId) {
const segment_members_table = `"dapp_analytics"."segment_members_${dAppId}"`;
const segmentQuery = `(SELECT user_address FROM ${segment_members_table} WHERE segment_id = ?)`;
conditions.push(`src.caller IN ${segmentQuery}`);
values.push(segmentId);
}

if (filter.name) {
conditions.push("src.name ILIKE ? ");
values.push(`%${filter.name.replace("::", "_")}%`);
Expand Down Expand Up @@ -254,13 +263,15 @@ const getDataForFilter = async (
dAppId: string,
filter: Filter,
breakdown: boolean,
metric: "wallets" | "interactions" | "transferredTokens"
metric: "wallets" | "interactions" | "transferredTokens",
segmentId?: string
): Promise<Map<string, Map<string, Set<string> | number>>> => {
const { query, values } = buildQueryForFilter(
dAppId,
filter,
breakdown,
metric
metric,
segmentId
);

try {
Expand Down Expand Up @@ -565,6 +576,7 @@ const intersectWalletsByDay = (

return result;
};

export const getUniqueWallets = async (
dAppId: string,
body: GetMetricsRequest
Expand All @@ -574,10 +586,11 @@ export const getUniqueWallets = async (
? body.filters
: [{ name: null, type: null, args: {} }];
const breakdown = body.breakdown || false;
const segmentId = body.segmentId;

const filterResults = await Promise.all(
filters.map((filter) =>
getDataForFilter(dAppId, filter, breakdown, "wallets")
getDataForFilter(dAppId, filter, breakdown, "wallets", segmentId)
)
);

Expand All @@ -596,10 +609,11 @@ export const getInteractions = async (
? body.filters
: [{ name: null, type: null, args: {} }];
const breakdown = body.breakdown || false;
const segmentId = body.segmentId;

const filterResults = await Promise.all(
filters.map((filter) =>
getDataForFilter(dAppId, filter, breakdown, "interactions")
getDataForFilter(dAppId, filter, breakdown, "interactions", segmentId)
)
);

Expand All @@ -618,10 +632,17 @@ export const getTransferredTokens = async (
? body.filters
: [{ name: null, type: null, args: {} }];
const breakdown = body.breakdown || false;
const segmentId = body.segmentId;

const filterResults = await Promise.all(
filters.map((filter) =>
getDataForFilter(dAppId, filter, breakdown, "transferredTokens")
getDataForFilter(
dAppId,
filter,
breakdown,
"transferredTokens",
segmentId
)
)
);

Expand Down
1 change: 1 addition & 0 deletions src/validation/dapp-analytics-validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const dappDataMetricsValidation = {
),
})
),
segmentId: Joi.string(),
}),
};

Expand Down
Loading