Skip to content

Commit

Permalink
feat(watchtime): total time option
Browse files Browse the repository at this point in the history
  • Loading branch information
veryCrunchy committed May 12, 2024
1 parent 874cebe commit d4cbe64
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/gql/resolvers/watchtime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { db, watchtime } from "~/db";
import { type Resolvers } from "./";
import { asc, desc, inArray, not } from "drizzle-orm";
import { and, eq, asc, desc, inArray, not, sql } from "drizzle-orm";
import { getClientToken } from "~/auth";
const excludedBots = [
"100135110", //streamelements
Expand All @@ -18,19 +18,46 @@ const excludedUsers = [
const watchtimeResolver: Resolvers["Query"] = {
watchtime: async (_, query) => {
const limit = Math.min(query.limit, 100);
console.log(limit);
const times = await db
.select({
time: watchtime.time,
id: watchtime.twitchId,
})
.from(watchtime)
.limit(limit)
.where(
not(inArray(watchtime.twitchId, [...excludedBots, ...excludedUsers]))
)
// sort by oldest account (based on auto incremented twitch id) if times are similar
.orderBy(desc(watchtime.time), asc(watchtime.twitchId));
let times: {
time: number | null;
id: string;
}[];
if (query.total)
times = await db
.select({
time: sql<number>`sum(${watchtime.time})`,
id: watchtime.twitchId,
})
.from(watchtime)
.limit(limit)
.where(
not(inArray(watchtime.twitchId, [...excludedBots, ...excludedUsers]))
)
.groupBy(watchtime.twitchId)
// sort by oldest account (based on auto incremented twitch id) if times are similar
.orderBy(desc(watchtime.time), asc(watchtime.twitchId));
else {
const startOfMonth = new Date();
startOfMonth.setUTCDate(1);
startOfMonth.setUTCHours(0, 0, 0, 0);
times = await db
.select({
time: watchtime.time,
id: watchtime.twitchId,
})
.from(watchtime)
.limit(limit)
.where(
and(
not(
inArray(watchtime.twitchId, [...excludedBots, ...excludedUsers])
),
eq(watchtime.date, startOfMonth)
)
)
// sort by oldest account (based on auto incremented twitch id) if times are similar
.orderBy(desc(watchtime.time), asc(watchtime.twitchId));
}
const userIds = times.map(entry => entry.id);

const idParam = userIds.map(id => `id=${id}`).join("&");
Expand Down
1 change: 1 addition & 0 deletions src/gql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const queryType = g.type("Query", {
.list()
.args({
limit: g.int().default(10),
total: g.boolean().default(false),
}),
getTwitchUser: g.ref(TwitchUserType).list().args({
user: g.id(),
Expand Down

0 comments on commit d4cbe64

Please sign in to comment.