Skip to content

Commit 8b6269d

Browse files
committed
refactor: update analytics actions for improved readability and performance
- Increased MAX_TRACKED_ARTICLES from 500 to 5000 to accommodate more data. - Reformatted function parameters and code blocks for better readability. - Enhanced error handling in analytics input parsing functions.
1 parent 03774f6 commit 8b6269d

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

src/backend/services/analytics.actions.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { z } from "zod/v4";
44
import { and, eq, isNotNull } from "sqlkit";
5-
import { getClickHouseClient, isClickHouseConfigured } from "../persistence/clickhouse.client";
5+
import {
6+
getClickHouseClient,
7+
isClickHouseConfigured,
8+
} from "../persistence/clickhouse.client";
69
import { persistenceRepository } from "../persistence/persistence-repositories";
710
import { pgClient } from "../persistence/clients";
811
import { ActionException, handleActionException } from "./RepositoryException";
@@ -37,7 +40,7 @@ export type DashboardAnalyticsOverviewData = ResourceAnalyticsData & {
3740
publicGistCount: number;
3841
};
3942

40-
const MAX_TRACKED_ARTICLES = 500;
43+
const MAX_TRACKED_ARTICLES = 5000;
4144
const MAX_TRACKED_GISTS = 200;
4245
const MAX_CUSTOM_RANGE_MS = 366 * 86400000;
4346

@@ -86,7 +89,9 @@ function clickHouseTimePredicate(filter: ClickHouseTimeFilter): string {
8689
: "viewed_at >= parseDateTimeBestEffort({range_start:String}) AND viewed_at < parseDateTimeBestEffort({range_end_exclusive:String})";
8790
}
8891

89-
function clickHouseTimeParams(filter: ClickHouseTimeFilter): Record<string, string | number> {
92+
function clickHouseTimeParams(
93+
filter: ClickHouseTimeFilter,
94+
): Record<string, string | number> {
9095
if (filter.kind === "preset") {
9196
return { days: filter.days };
9297
}
@@ -96,15 +101,22 @@ function clickHouseTimeParams(filter: ClickHouseTimeFilter): Record<string, stri
96101
};
97102
}
98103

99-
function buildClickHouseResourceFilter(articleIds: string[], gistIds: string[]) {
104+
function buildClickHouseResourceFilter(
105+
articleIds: string[],
106+
gistIds: string[],
107+
) {
100108
const parts: string[] = [];
101109
const extra: Record<string, string[]> = {};
102110
if (articleIds.length > 0) {
103-
parts.push(`(resource_type = 'ARTICLE' AND resource_id IN {article_ids:Array(UUID)})`);
111+
parts.push(
112+
`(resource_type = 'ARTICLE' AND resource_id IN {article_ids:Array(UUID)})`,
113+
);
104114
extra.article_ids = articleIds;
105115
}
106116
if (gistIds.length > 0) {
107-
parts.push(`(resource_type = 'GIST' AND resource_id IN {gist_ids:Array(UUID)})`);
117+
parts.push(
118+
`(resource_type = 'GIST' AND resource_id IN {gist_ids:Array(UUID)})`,
119+
);
108120
extra.gist_ids = gistIds;
109121
}
110122
return { parts, extra };
@@ -163,7 +175,8 @@ export async function getResourceAnalytics(
163175
input: z.infer<typeof AnalyticsInput.getResourceAnalyticsInput>,
164176
): Promise<ActionResponse<ResourceAnalyticsData>> {
165177
try {
166-
const payload = await AnalyticsInput.getResourceAnalyticsInput.parseAsync(input);
178+
const payload =
179+
await AnalyticsInput.getResourceAnalyticsInput.parseAsync(input);
167180
const userId = await authID();
168181
if (!userId) {
169182
throw new ActionException("Unauthorized");
@@ -273,7 +286,10 @@ export async function getResourceAnalytics(
273286
}
274287
}
275288

276-
async function countReactionsForResources(articleIds: string[], gistIds: string[]) {
289+
async function countReactionsForResources(
290+
articleIds: string[],
291+
gistIds: string[],
292+
) {
277293
const q = sql`
278294
SELECT COUNT(*)::int AS c FROM reactions
279295
WHERE
@@ -302,7 +318,8 @@ export async function getDashboardAnalyticsOverview(
302318
input: z.infer<typeof AnalyticsInput.getDashboardAnalyticsOverviewInput>,
303319
): Promise<ActionResponse<DashboardAnalyticsOverviewData>> {
304320
try {
305-
const payload = await AnalyticsInput.getDashboardAnalyticsOverviewInput.parseAsync(input);
321+
const payload =
322+
await AnalyticsInput.getDashboardAnalyticsOverviewInput.parseAsync(input);
306323
const userId = await authID();
307324
if (!userId) {
308325
throw new ActionException("Unauthorized");

0 commit comments

Comments
 (0)