Skip to content

Commit

Permalink
fix: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
olros committed Nov 17, 2023
1 parent 3db33d2 commit 4521ff7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,50 @@ export type PageviewRequestData = {
const parsePageviewRequestData = async (request: Request): Promise<PageviewRequestData> => {
const data = (await request.json()) as PageviewRequestData;
if (!data.data?.pathname) {
throw JSON.stringify({ pathname: `Pathname isn't defined` });
throw new Error(JSON.stringify({ pathname: `Pathname isn't defined` }));
}
if (!data.date) {
throw JSON.stringify({ pathname: `Date isn't defined` });
throw new Error(JSON.stringify({ pathname: `Date isn't defined` }));
}
if (!data.geo) {
throw JSON.stringify({ pathname: `geo isn't defined` });
throw new Error(JSON.stringify({ pathname: `geo isn't defined` }));
}
if (!data.userAgentData) {
throw JSON.stringify({ pathname: `userAgentData isn't defined` });
throw new Error(JSON.stringify({ pathname: `userAgentData isn't defined` }));
}
if (!data.user_hash) {
throw JSON.stringify({ pathname: `user_hash isn't defined` });
throw new Error(JSON.stringify({ pathname: `user_hash isn't defined` }));
}

return data;
};

const getLocation = async (geo: PageviewRequestData['geo']): Promise<Location> => {
const flag_country_city: Prisma.LocationFlagCountryCityCompoundUniqueInput = { flag: geo.flag, country: geo.country, city: geo.city };
const country_city: Prisma.LocationCountryCityCompoundUniqueInput = { country: geo.country, city: geo.city };
const location: Prisma.LocationCreateWithoutPageViewsInput = {
...flag_country_city,
...country_city,
flag: geo.flag,
latitude: Number(geo.latitude),
longitude: Number(geo.longitude),
};
return await prismaClient.location.upsert({
create: location,
update: {},
where: { flag_country_city },
where: { country_city },
});
};

const trackPageviewNext = async (request: Request, project: Project) => {
console.info('[API-Internal - TrackPageviewNext] 0');

const { data, date, geo, userAgentData, user_hash } = await parsePageviewRequestData(request);

console.info('[API-Internal - TrackPageviewNext] 1', { data, date, geo, userAgentData, user_hash });

const location = await getLocation(geo);

console.info('[API-Internal - TrackPageviewNext] 2', { location });

await prismaClient.pageViewNext.create({
data: {
date,
Expand All @@ -79,10 +86,9 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
const { project } = await getProjectAndCheckPermissions(request, params.teamSlug, params.projectSlug);

await trackPageviewNext(request, project);

return json({ ok: true }, { status: 202 });
} catch (e) {
console.error('[API-Internal - PageviewNext]', e);
return json({ ok: false }, { status: 400 });
}
return json({ ok: true }, { status: 202 });
};
2 changes: 0 additions & 2 deletions web/app/routes/api.$teamSlug.$projectSlug.pageview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ const getPageViewNextRequest = async (request: Request): Promise<Request | undef
const ua = userAgent(request);
const date = getDate(request);

console.info('[API - PageViewNext]', { geo, ua, ip });

const geoData: PageviewRequestData['geo'] | undefined =
geo.city && geo.country && geo.flag && geo.latitude && geo.longitude ? (geo as PageviewRequestData['geo']) : undefined;
if (!geoData || !ip || ua.isBot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[country,city]` on the table `Location` will be added. If there are existing duplicate values, this will fail.
*/
-- DropIndex
DROP INDEX "Location_flag_country_city_key";

-- CreateIndex
CREATE UNIQUE INDEX "Location_country_city_key" ON "Location"("country", "city");
2 changes: 1 addition & 1 deletion web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ model Location {
pageViews PageViewNext[]
@@unique([flag, country, city])
@@unique([country, city])
}

1 comment on commit 4521ff7

@vercel
Copy link

@vercel vercel bot commented on 4521ff7 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

stats – ./

stats-nu-nine.vercel.app
stats-olros.vercel.app
stats.olafros.com
stats-git-main-olros.vercel.app

Please sign in to comment.