fix(anaylitics): unique values were missing causing a weird crash
This commit is contained in:
@@ -4,30 +4,42 @@ import {
|
|||||||
pgTable,
|
pgTable,
|
||||||
text,
|
text,
|
||||||
timestamp,
|
timestamp,
|
||||||
|
unique,
|
||||||
uuid,
|
uuid,
|
||||||
} from "drizzle-orm/pg-core";
|
} from "drizzle-orm/pg-core";
|
||||||
|
|
||||||
export const analyticsDaily = pgTable("analytics_daily", {
|
export const analyticsDaily = pgTable(
|
||||||
id: uuid("id").defaultRandom().primaryKey(),
|
"analytics_daily",
|
||||||
|
{
|
||||||
|
id: uuid("id").defaultRandom().primaryKey(),
|
||||||
|
|
||||||
businessDate: date("business_date").notNull(),
|
businessDate: date("business_date", { mode: "string" }).notNull(),
|
||||||
|
|
||||||
method: text("method").notNull(),
|
method: text("method").notNull(),
|
||||||
routePattern: text("route_pattern").notNull(),
|
routePattern: text("route_pattern").notNull(),
|
||||||
module: text("module").notNull(),
|
module: text("module").notNull(),
|
||||||
|
|
||||||
totalHits: integer("total_hits").notNull(),
|
totalHits: integer("total_hits").notNull(),
|
||||||
uniqueUsers: integer("unique_users").notNull(),
|
uniqueUsers: integer("unique_users").notNull(),
|
||||||
|
|
||||||
successCount: integer("success_count").notNull(),
|
successCount: integer("success_count").notNull(),
|
||||||
errorCount: integer("error_count").notNull(),
|
errorCount: integer("error_count").notNull(),
|
||||||
|
|
||||||
avgDurationMs: integer("avg_duration_ms").notNull(),
|
avgDurationMs: integer("avg_duration_ms").notNull(),
|
||||||
maxDurationMs: integer("max_duration_ms").notNull(),
|
maxDurationMs: integer("max_duration_ms").notNull(),
|
||||||
|
|
||||||
firstHitAt: timestamp("first_hit_at").notNull(),
|
firstHitAt: timestamp("first_hit_at").notNull(),
|
||||||
lastHitAt: timestamp("last_hit_at").notNull(),
|
lastHitAt: timestamp("last_hit_at").notNull(),
|
||||||
|
|
||||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||||
});
|
},
|
||||||
|
(table) => [
|
||||||
|
unique("analytics_daily_business_route_unique").on(
|
||||||
|
table.businessDate,
|
||||||
|
table.method,
|
||||||
|
table.routePattern,
|
||||||
|
table.module,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ export async function aggregateRouteHitsForBusinessDay() {
|
|||||||
|
|
||||||
const rows = await db
|
const rows = await db
|
||||||
.select({
|
.select({
|
||||||
businessDate: sql<string>`${businessDate}`,
|
businessDate: sql<string>`CAST(${businessDate} AS date)`,
|
||||||
method: analytics.method,
|
method: analytics.method,
|
||||||
routePattern: analytics.routePattern,
|
routePattern: analytics.routePattern,
|
||||||
module: sql<string>`COALESCE(${analytics.module}, 'unknown')`,
|
module: sql<string>`COALESCE(${analytics.module}, 'unknown')`,
|
||||||
@@ -105,9 +105,16 @@ export async function aggregateRouteHitsForBusinessDay() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const values = rows.map((row) => ({
|
||||||
|
...row,
|
||||||
|
businessDate: row.businessDate,
|
||||||
|
firstHitAt: new Date(row.firstHitAt),
|
||||||
|
lastHitAt: new Date(row.lastHitAt),
|
||||||
|
}));
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(analyticsDaily)
|
.insert(analyticsDaily)
|
||||||
.values(rows)
|
.values(values)
|
||||||
.onConflictDoUpdate({
|
.onConflictDoUpdate({
|
||||||
target: [
|
target: [
|
||||||
analyticsDaily.businessDate,
|
analyticsDaily.businessDate,
|
||||||
|
|||||||
Reference in New Issue
Block a user