Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(backend): add owner-scoped bulk analytics lookup

Richie McIlroy708f8c970ba8
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index 64482e7f2..0564da427 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -3,7 +3,7 @@ import { buildEnv, NODE_ENV, serverEnv } from "@cap/env";
import { dub } from "@cap/utils";
import { CurrentUser, type Folder, Policy, Video } from "@cap/web-domain";
import * as Dz from "drizzle-orm";
-import { Array, Effect, Exit, Option } from "effect";
+import { Effect, Array as EffectArray, Exit, Option } from "effect";
import type { Schema } from "effect/Schema";
import { Database } from "../Database.ts";
@@ -110,56 +110,25 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
Effect.withSpan("Videos.getById"),
);
- const getAnalyticsBulkInternal = Effect.fn("Videos.getAnalyticsBulk")(
- function* (videoIds: ReadonlyArray<Video.VideoId>) {
- if (videoIds.length === 0)
- return [] as Array<Exit.Exit<{ count: number }, unknown>>;
-
+ const getAnalyticsCounts = Effect.fn("Videos.getAnalyticsCounts")(
+ function* (
+ analyticsVideos: ReadonlyArray<{
+ id: Video.VideoId;
+ orgId: string;
+ }>,
+ ) {
const now = new Date();
const from = new Date(
now.getTime() - DEFAULT_ANALYTICS_RANGE_DAYS * 24 * 60 * 60 * 1000,
);
-
- const videoExits = yield* Effect.forEach(
- videoIds,
- (videoId) =>
- getByIdForViewing(videoId).pipe(
- Effect.map((video) => video),
- Effect.exit,
- ),
- { concurrency: 10 },
- );
-
- const successfulVideos: Array<{
- index: number;
- videoId: Video.VideoId;
- video: Video.Video;
- }> = [];
-
- for (let index = 0; index < videoExits.length; index++) {
- const exit = videoExits[index];
- if (!exit) continue;
- if (Exit.isSuccess(exit)) {
- const maybeVideo = exit.value;
- if (Option.isSome(maybeVideo)) {
- const [video] = maybeVideo.value;
- successfulVideos.push({
- index,
- videoId: videoIds[index] ?? "",
- video,
- });
- }
- }
- }
-
const countsByPathname = new Map<string, number>();
const videosByOrg = new Map<
string,
Array<{ videoId: Video.VideoId; pathname: string }>
>();
- for (const { video } of successfulVideos) {
- const key = video.orgId ?? "";
+ for (const video of analyticsVideos) {
+ const key = video.orgId;
if (!videosByOrg.has(key)) {
videosByOrg.set(key, []);
}
@@ -231,13 +200,34 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
}
}
- for (const { videoId } of successfulVideos) {
- const pathname = buildPathname(videoId);
+ for (const video of analyticsVideos) {
+ const pathname = buildPathname(video.id);
if (!countsByPathname.has(pathname)) {
countsByPathname.set(pathname, 0);
}
}
+ return countsByPathname;
+ },
+ );
+
+ const getAnalyticsBulkInternal = Effect.fn("Videos.getAnalyticsBulk")(
+ function* (videoIds: ReadonlyArray<Video.VideoId>) {
+ if (videoIds.length === 0)
+ return [] as Array<Exit.Exit<{ count: number }, unknown>>;
+
+ const videoExits = yield* Effect.forEach(
+ videoIds,
+ (videoId) => getByIdForViewing(videoId).pipe(Effect.exit),
+ { concurrency: 10 },
+ );
+ const analyticsVideos = videoExits.flatMap((exit) => {
+ if (!Exit.isSuccess(exit) || Option.isNone(exit.value)) return [];
+ const [video] = exit.value.value;
+ return [{ id: video.id, orgId: video.orgId }];
+ });
+ const countsByPathname = yield* getAnalyticsCounts(analyticsVideos);
+
return videoExits.map((exit, index) =>
Exit.map(exit, () => ({
count:
@@ -247,6 +237,32 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
},
);
+ const getAnalyticsBulkForOwner = Effect.fn(
+ "Videos.getAnalyticsBulkForOwner",
+ )(function* (
+ videoIds: ReadonlyArray<Video.VideoId>,
+ ownerId: (typeof Db.videos.$inferSelect)["ownerId"],
+ ) {
+ if (videoIds.length === 0) return [];
+ const uniqueVideoIds = Array.from(new Set(videoIds));
+ const rows = yield* db.use((database) =>
+ database
+ .select({ id: Db.videos.id, orgId: Db.videos.orgId })
+ .from(Db.videos)
+ .where(
+ Dz.and(
+ Dz.eq(Db.videos.ownerId, ownerId),
+ Dz.inArray(Db.videos.id, uniqueVideoIds),
+ ),
+ ),
+ );
+ const countsByPathname = yield* getAnalyticsCounts(rows);
+
+ return videoIds.map((videoId) => ({
+ count: countsByPathname.get(buildPathname(videoId)) ?? 0,
+ }));
+ });
+
return {
/*
* Get a video by ID. Will fail if the user does not have access.
@@ -311,7 +327,7 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
if (allObjects.Contents)
yield* Effect.all(
- Array.filterMap(allObjects.Contents, (obj) =>
+ EffectArray.filterMap(allObjects.Contents, (obj) =>
Option.map(Option.fromNullable(obj.Key), (key) => {
const newKey = key.replace(prefix, newPrefix);
return bucket.copyObject(
@@ -661,6 +677,7 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
});
}),
getAnalyticsBulk: getAnalyticsBulkInternal,
+ getAnalyticsBulkForOwner,
};
}),
dependencies: [