Skip to content

Commit

Permalink
refactor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JefQuidousse2 committed Oct 9, 2024
1 parent 3d5c715 commit eba55ca
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/app/find-the-expert/[role]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ButtonSkeleton from "~/components/loading/button-loader";
import { Login } from "~/components/login";
import ShowDataTable from "~/components/show-data-table";
import { getServerAuthSession } from "~/server/auth";
import { logUsageMetric } from "~/server/log";
import { api } from "~/trpc/server";
import {
extractUniqueIds,
fetchUserAnswersForRole,
Expand Down Expand Up @@ -40,7 +40,7 @@ const ContentSection = () => (

const FindTheExpertPage = async (context: { params: { role: any; }; }) => {
const session = await getServerAuthSession();
await logUsageMetric("Find The Expert Page Accessed For Role: " + context.params.role);
api.usageMetricLogger.logUsageMetric.mutate({logMessage: 'find-the-expert-page-filtered-on-role'});

return (
<div className="container flex flex-col items-center justify-center gap-12 px-4 py-16">
Expand Down
13 changes: 11 additions & 2 deletions src/components/additional-buttons-homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { Login } from "./login";
import { Button } from "./ui/button";
import { ArrowRightDarkModeFriendly } from "./svg";
import { signIn } from "next-auth/react";
import { api } from "~/trpc/react";

const Buttons = ({ session }: { session: Session | null }) => {
const {mutate: logUsageMetric} = api.usageMetricLogger.logUsageMetric.useMutation();

const handleLogging = () => {
logUsageMetric({logMessage:'find-the-expert-page-accessed'});
}

return (
<div className="mt-5 flex justify-center">
<div className="mt-5 flex flex-col items-center gap-6 md:flex-row">
Expand All @@ -24,8 +31,10 @@ const Buttons = ({ session }: { session: Session | null }) => {
<ArrowRightDarkModeFriendly />
</Button>
<Button
onClick={() =>
signIn("azure-ad", { callbackUrl: "/find-the-expert/general" })
onClick={() => {
handleLogging();
signIn("azure-ad", { callbackUrl: "/find-the-expert/general" });
}
}
variant="outline"
className="border-2 border-[#bed62f]"
Expand Down
6 changes: 6 additions & 0 deletions src/components/select-role.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function SelectRoles({
} = api.survey.setRole.useMutation();
const { mutate: setDefaultRoleMutate, isSuccess: setDefaultRoleIsSuccess } =
api.survey.setDefaultRole.useMutation();
const {mutate: logUsageMetric} = api.usageMetricLogger.logUsageMetric.useMutation();

// Show a toast notification on role mutation error
useEffect(() => {
Expand Down Expand Up @@ -85,6 +86,10 @@ export default function SelectRoles({
roleIds: selectedRoles,
});
};

const handleLogging = () => {
logUsageMetric({logMessage:'find-the-expert-page-accessed'});
}

const [communicationMethodIsLoading, setCommunicationMethodIsLoading] =
useState(false);
Expand Down Expand Up @@ -168,6 +173,7 @@ export default function SelectRoles({
</Link>
<Link href="/find-the-expert/general">
<SpinnerButton
onClick={handleLogging}
state={communicationMethodIsLoading}
variant="outline"
className="border-2 border-[#bed62f]"
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { surveyRouter } from "~/server/api/routers/survey";
import { createTRPCRouter } from "~/server/api/trpc";
import { usageMetricLogger } from "../log";

/**
* This is the primary router for your server.
Expand All @@ -8,6 +9,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
*/
export const appRouter = createTRPCRouter({
survey: surveyRouter,
usageMetricLogger: usageMetricLogger
});

// export type definition of API
Expand Down
21 changes: 14 additions & 7 deletions src/server/log.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { createTRPCRouter, publicProcedure } from "./api/trpc";
import { z } from "zod";
import { db } from "./db"

export const logUsageMetric = async (action: string) => {
await db.usageMetrics.create({
data: {
action: action
}
});
}

export const usageMetricLogger = createTRPCRouter({
logUsageMetric: publicProcedure
.input(z.object({logMessage: z.string()}))
.mutation(async ({input}) => {
await db.usageMetrics.create({
data: {
action: input.logMessage
}
});
})
})

0 comments on commit eba55ca

Please sign in to comment.