forked from Mike/freno-dev
The analytics page could not answer "how many downloads, of which
product". Three of the four download doors recorded nothing: the tRPC
click proxy only left the generic request row, whose path is the raw URL
with the asset buried in `?batch=1&input={"asset_name":...}`, and the S3
file route that serves Sparkle updates and the Nook app logged nothing
at all. The one door that did log (the Nook redirect) kept a private copy
of the logVisit/enrich/flush sequence, under its own path.
All doors now go through trackDownload, which writes one row per event
under /api/downloads/<asset> and flushes it inside the request — a click
is low-volume and serverless is not guaranteed to run the 30s batch
timer after the response. The file route maps its prefix allowlist to
asset keys, so a version bump stays one asset rather than one per
release. getAnalyticsSummary groups those rows into a downloads block and
the freno.me panel renders it under the overview cards.
History is recovered to the degree it exists: the Nook door's rows and
the tRPC clicks still carry enough to derive from (asset name in the URL).
scripts/backfill-downloads.ts inserts those derived rows with a
`backfill-<source id>` primary key and INSERT OR IGNORE, leaving the
originals untouched, so re-running is a no-op. Run against the live table
it added 66 rows: gaze 42, thenook 19, inputhalo 4, lineage 1.
Tests: new backfill suite (derivation, visitor detail carried over,
idempotency, empty history) and summariseDownloads cases; the Nook route
test now pins the canonical path, and the NOOK-337 allowlist audit reads
the map keys instead of quoted strings. Red-checked by stubbing the
downloads block back to zeros — the summary case fails, then passes.
Full suite matches master.
Co-Authored-By: nw-flash <noreply@nw-flash.local>
MISC-48
136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import { createTRPCRouter, publicProcedure } from "../utils";
|
|
import { z } from "zod";
|
|
import {
|
|
S3Client,
|
|
GetObjectCommand,
|
|
ListObjectsV2Command
|
|
} from "@aws-sdk/client-s3";
|
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
import { env } from "~/env/server";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { trackDownload } from "~/server/track-download";
|
|
|
|
const assets: Record<string, string> = {
|
|
lineage: "Life and Lineage.apk",
|
|
cork: "Cork.zip",
|
|
"shapes-with-abigail": "shapes-with-abigail.apk"
|
|
};
|
|
|
|
/**
|
|
* Find the most recent file in S3 matching a prefix and extension.
|
|
*/
|
|
async function getLatestFile(
|
|
client: S3Client,
|
|
bucket: string,
|
|
prefix: string,
|
|
ext: string
|
|
): Promise<string> {
|
|
try {
|
|
const listCommand = new ListObjectsV2Command({
|
|
Bucket: bucket,
|
|
Prefix: prefix,
|
|
MaxKeys: 100
|
|
});
|
|
|
|
const response = await client.send(listCommand);
|
|
|
|
if (!response.Contents || response.Contents.length === 0) {
|
|
throw new Error(`No files found in S3 with prefix ${prefix}`);
|
|
}
|
|
|
|
const files = response.Contents.filter((obj) =>
|
|
obj.Key?.endsWith(ext)
|
|
).sort((a, b) => {
|
|
const dateA = a.LastModified?.getTime() || 0;
|
|
const dateB = b.LastModified?.getTime() || 0;
|
|
return dateB - dateA;
|
|
});
|
|
|
|
if (files.length === 0) {
|
|
throw new Error(`No ${ext} files found in ${prefix} prefix`);
|
|
}
|
|
|
|
const latestFile = files[0].Key!;
|
|
console.log(`Latest file: ${latestFile}`);
|
|
return latestFile;
|
|
} catch (error) {
|
|
console.error(`Error finding latest file for ${prefix}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Per-asset S3 lookup config for macOS apps that auto-resolve latest version.
|
|
*/
|
|
const latestAssets: Record<
|
|
string,
|
|
{ prefix: string; ext: string }
|
|
> = {
|
|
gaze: { prefix: "downloads/Gaze-", ext: ".dmg" },
|
|
inputhalo: { prefix: "downloads/InputHalo-", ext: ".dmg" },
|
|
thenook: { prefix: "downloads/TheNook-", ext: ".dmg" }
|
|
};
|
|
|
|
export const downloadsRouter = createTRPCRouter({
|
|
getDownloadUrl: publicProcedure
|
|
.input(z.object({ asset_name: z.string() }))
|
|
.query(async ({ input, ctx }) => {
|
|
// Logged before signing: a click on a door that then fails to sign is
|
|
// exactly the event the funnel needs to show. Unknown asset names are
|
|
// skipped rather than recorded as a download that never happened.
|
|
if (assets[input.asset_name] || latestAssets[input.asset_name]) {
|
|
await trackDownload(ctx.event, input.asset_name);
|
|
}
|
|
|
|
const bucket = env.VITE_DOWNLOAD_BUCKET_STRING;
|
|
|
|
const credentials = {
|
|
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
|
secretAccessKey: env.MY_AWS_SECRET_KEY
|
|
};
|
|
|
|
const client = new S3Client({
|
|
region: env.AWS_REGION,
|
|
credentials: credentials
|
|
});
|
|
|
|
try {
|
|
let fileKey: string;
|
|
|
|
const latest = latestAssets[input.asset_name];
|
|
if (latest) {
|
|
fileKey = await getLatestFile(client, bucket, latest.prefix, latest.ext);
|
|
} else {
|
|
fileKey = assets[input.asset_name];
|
|
|
|
if (!fileKey) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Asset not found"
|
|
});
|
|
}
|
|
}
|
|
|
|
const command = new GetObjectCommand({
|
|
Bucket: bucket,
|
|
Key: fileKey
|
|
});
|
|
|
|
const signedUrl = await getSignedUrl(client, command, {
|
|
expiresIn: 120
|
|
});
|
|
|
|
return { downloadURL: signedUrl };
|
|
} catch (error) {
|
|
console.error(error);
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to generate download URL"
|
|
});
|
|
}
|
|
})
|
|
});
|