Skip to content
Merged
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
74 changes: 38 additions & 36 deletions app/models/activity.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { sql } from "kysely";

type MessageStats = DB["message_stats"];

// // Default allowed channel categories for analytics
// const ALLOWED_CATEGORIES: string[] = [
// "Need Help",
// "React General",
// "Advanced Topics",
// ];
// Default allowed channel categories for analytics
const ALLOWED_CATEGORIES: string[] = [
"Need Help",
"React General",
"Advanced Topics",
];

// // Default allowed channels (currently empty but can be configured)
// const ALLOWED_CHANNELS: string[] = [];
// Default allowed channels (currently empty but can be configured)
const ALLOWED_CHANNELS: string[] = [];

/**
* Creates a base query for message_stats filtered by guild, date range, and optionally user
Expand Down Expand Up @@ -58,12 +58,12 @@ export async function getUserMessageAnalytics(
.fn("date", [eb("sent_at", "/", lit(1000)), sql.lit("unixepoch")])
.as("date"),
])
// .where((eb) =>
// eb.or([
// eb("channel_id", "in", ALLOWED_CHANNELS),
// eb("channel_category", "in", ALLOWED_CATEGORIES),
// ]),
// )
.where((eb) =>
eb.or([
eb("channel_id", "in", ALLOWED_CHANNELS),
eb("channel_category", "in", ALLOWED_CATEGORIES),
]),
)
.orderBy("date", "asc")
.groupBy("date");

Expand All @@ -73,12 +73,12 @@ export async function getUserMessageAnalytics(
fn.count<number>("channel_category").as("messages"),
"channel_category",
])
// .where((eb) =>
// eb.or([
// eb("channel_id", "in", ALLOWED_CHANNELS),
// eb("channel_category", "in", ALLOWED_CATEGORIES),
// ]),
// )
.where((eb) =>
eb.or([
eb("channel_id", "in", ALLOWED_CHANNELS),
eb("channel_category", "in", ALLOWED_CATEGORIES),
]),
)
.groupBy("channel_category");

// Build channel stats query
Expand All @@ -94,15 +94,17 @@ export async function getUserMessageAnalytics(
"channel.id",
"message_stats.channel_id",
)
// .where((eb) =>
// eb.or([
// eb("channel_id", "in", ALLOWED_CHANNELS),
// eb("channel_category", "in", ALLOWED_CATEGORIES),
// ]),
// )
.where((eb) =>
eb.or([
eb("channel_id", "in", ALLOWED_CHANNELS),
eb("channel_category", "in", ALLOWED_CATEGORIES),
]),
)
.orderBy("messages", "desc")
.groupBy("channel_id");

console.log("sql:", { compiled: dailyQuery.compile() });

const [dailyResults, categoryBreakdown, channelBreakdown, userInfo] =
await Promise.all([
dailyQuery.execute(),
Expand Down Expand Up @@ -154,17 +156,17 @@ export async function getTopParticipants(
]);

// Apply channel filtering inline
// const filteredQuery = baseQuery.where((eb) =>
// eb.or([
// eb("channel_id", "in", ALLOWED_CHANNELS),
// eb("channel_category", "in", ALLOWED_CATEGORIES),
// ]),
// );
const filteredQuery = baseQuery.where((eb) =>
eb.or([
eb("channel_id", "in", ALLOWED_CHANNELS),
eb("channel_category", "in", ALLOWED_CATEGORIES),
]),
);

// get shortlist using inline selectors
const topMembersQuery = db
// .with("interval_message_stats", () => filteredQuery)
.with("interval_message_stats", () => baseQuery)
.with("interval_message_stats", () => filteredQuery)
// .with("interval_message_stats", () => baseQuery)
.selectFrom("interval_message_stats")
.select(({ fn }) => [
"author_id",
Expand All @@ -187,8 +189,8 @@ export async function getTopParticipants(
const topMembers = await topMembersQuery.execute();

const dailyParticipationQuery = db
// .with("interval_message_stats", () => filteredQuery)
.with("interval_message_stats", () => baseQuery)
.with("interval_message_stats", () => filteredQuery)
// .with("interval_message_stats", () => baseQuery)
.selectFrom("interval_message_stats")
.select(({ fn }) => [
"author_id",
Expand Down
Loading