From 16eba2c7051e86ad40adcd7602e8ab0fba273342 Mon Sep 17 00:00:00 2001 From: mrkarimoff Date: Fri, 26 Jan 2024 22:49:29 +0500 Subject: [PATCH] add filter for event table and refactor --- .../_components/analytics/AnalyticsView.tsx | 17 +++- .../analytics/EventsTable/Columns.tsx | 23 +++++ .../analytics/EventsTable/EventsTable.tsx | 41 ++++++-- .../EventsTable}/StackTraceDialog.tsx | 4 +- .../analytics/EventsTable/TableFilter.tsx | 93 +++++++++++++++++++ .../analytics/cards/TotalDataExported.tsx | 15 +++ .../analytics/cards/TotalErrorsCard.tsx | 15 +++ .../errors/ErrorsTable/Columns.tsx | 56 ----------- .../errors/ErrorsTable/ErrorsTable.tsx | 15 --- .../app/_components/errors/ErrorsView.tsx | 13 --- .../errors/cards/TotalErrorsCard.tsx | 10 -- apps/analytics/app/page.tsx | 15 +-- .../DataTable/data-table-pagination.tsx | 6 +- apps/analytics/components/ExportButton.tsx | 5 +- apps/analytics/components/MetadataDialog.tsx | 4 +- apps/analytics/components/ui/checkbox.tsx | 30 ++++++ apps/analytics/db/getErrors.ts | 18 ---- apps/analytics/db/getEvents.ts | 6 -- apps/analytics/package.json | 1 + apps/analytics/utils/getTotalDataExported.ts | 14 +++ apps/analytics/utils/getTotalErrors.ts | 14 +++ pnpm-lock.yaml | 31 +++++++ 22 files changed, 293 insertions(+), 153 deletions(-) rename apps/analytics/app/_components/{errors/ErrorsTable => analytics/EventsTable}/StackTraceDialog.tsx (65%) create mode 100644 apps/analytics/app/_components/analytics/EventsTable/TableFilter.tsx create mode 100644 apps/analytics/app/_components/analytics/cards/TotalDataExported.tsx create mode 100644 apps/analytics/app/_components/analytics/cards/TotalErrorsCard.tsx delete mode 100644 apps/analytics/app/_components/errors/ErrorsTable/Columns.tsx delete mode 100644 apps/analytics/app/_components/errors/ErrorsTable/ErrorsTable.tsx delete mode 100644 apps/analytics/app/_components/errors/ErrorsView.tsx delete mode 100644 apps/analytics/app/_components/errors/cards/TotalErrorsCard.tsx create mode 100644 apps/analytics/components/ui/checkbox.tsx delete mode 100644 apps/analytics/db/getErrors.ts create mode 100644 apps/analytics/utils/getTotalDataExported.ts create mode 100644 apps/analytics/utils/getTotalErrors.ts diff --git a/apps/analytics/app/_components/analytics/AnalyticsView.tsx b/apps/analytics/app/_components/analytics/AnalyticsView.tsx index c7533a4d..e32024ee 100644 --- a/apps/analytics/app/_components/analytics/AnalyticsView.tsx +++ b/apps/analytics/app/_components/analytics/AnalyticsView.tsx @@ -5,8 +5,13 @@ import TotalInterviewsCompletedCard from "./cards/TotalInterviewsCompletedCard"; import TotalInterviewsStartedCard from "./cards/TotalInterviewsStartedCard"; import TotalProtocolsInstalledCard from "./cards/TotalProtocolsInstalledCard"; import RegionsTable from "./RegionsTable/RegionsTable"; +import getEvents from "~/db/getEvents"; +import TotalErrorsCard from "./cards/TotalErrorsCard"; +import TotalDataExported from "./cards/TotalDataExported"; + +export default async function AnalyticsView() { + const events = await getEvents(); -export default function AnalyticsView() { return (
@@ -14,10 +19,14 @@ export default function AnalyticsView() { + +
-
- - +
+
+ +
+ Regions diff --git a/apps/analytics/app/_components/analytics/EventsTable/Columns.tsx b/apps/analytics/app/_components/analytics/EventsTable/Columns.tsx index 11d449af..91bc7274 100644 --- a/apps/analytics/app/_components/analytics/EventsTable/Columns.tsx +++ b/apps/analytics/app/_components/analytics/EventsTable/Columns.tsx @@ -4,6 +4,7 @@ import { ColumnDef } from "@tanstack/react-table"; import { DataTableColumnHeader } from "~/components/DataTable/column-header"; import { MetadataDialog } from "~/components/MetadataDialog"; import type { Event } from "~/db/getEvents"; +import { StackTraceDialog } from "./StackTraceDialog"; export const columns: ColumnDef[] = [ { @@ -32,6 +33,28 @@ export const columns: ColumnDef[] = [ return
{row.original.installationId}
; }, }, + { + accessorKey: "name", + header: ({ column }) => ( + + ), + }, + { + accessorKey: "message", + header: ({ column }) => ( + + ), + }, + { + accessorKey: "stack", + header: "", + cell: ({ row }) => + row.original.stack && ( +
+ +
+ ), + }, { accessorKey: "metadata", header: "", diff --git a/apps/analytics/app/_components/analytics/EventsTable/EventsTable.tsx b/apps/analytics/app/_components/analytics/EventsTable/EventsTable.tsx index cd8b25c6..9b7a57a3 100644 --- a/apps/analytics/app/_components/analytics/EventsTable/EventsTable.tsx +++ b/apps/analytics/app/_components/analytics/EventsTable/EventsTable.tsx @@ -1,21 +1,48 @@ -// EventsTable.tsx +"use client"; + +import { useEffect, useMemo, useState } from "react"; import { DataTable } from "~/components/DataTable/data-table"; import ExportButton from "~/components/ExportButton"; -import getEvents from "~/db/getEvents"; +import { Event } from "~/db/getEvents"; import { columns } from "./Columns"; +import TableFilter from "./TableFilter"; + +export type EventType = { + text: string; + isSelected: boolean; +}; + +export default function EventsTable({ events }: { events: Event[] }) { + const [eventTypes, setEventTypes] = useState([]); + + useEffect(() => { + const eventTypesMap = new Map(); + events.forEach((event) => + eventTypesMap.set(event.type, { text: event.type, isSelected: true }) + ); -export default async function EventsTable() { - const events = await getEvents(); + setEventTypes([...Array.from(eventTypesMap.values())]); + }, [events]); + + const filteredEvents = useMemo(() => { + const filters = eventTypes + .filter((type) => type.isSelected) + .map((type) => type.text); + + return events.filter((event) => filters.includes(event.type)); + }, [eventTypes, events]); return (
-
+ + +

Events

-
- +
+
); diff --git a/apps/analytics/app/_components/errors/ErrorsTable/StackTraceDialog.tsx b/apps/analytics/app/_components/analytics/EventsTable/StackTraceDialog.tsx similarity index 65% rename from apps/analytics/app/_components/errors/ErrorsTable/StackTraceDialog.tsx rename to apps/analytics/app/_components/analytics/EventsTable/StackTraceDialog.tsx index e3e063e8..ed049cf2 100644 --- a/apps/analytics/app/_components/errors/ErrorsTable/StackTraceDialog.tsx +++ b/apps/analytics/app/_components/analytics/EventsTable/StackTraceDialog.tsx @@ -1,7 +1,7 @@ import { DialogButton } from "~/components/DialogButton"; -import { type ErrorEvent } from "~/db/getErrors"; +import { Event } from "~/db/getEvents"; -export function StackTraceDialog({ error }: { error: ErrorEvent }) { +export function StackTraceDialog({ error }: { error: Event }) { return ( >; +}; + +const TableFilter = ({ eventTypes, setEventTypes }: TableFilterProps) => { + const [allSelected, setAllSelected] = useState(true); + + useEffect(() => { + const updatedEventTypes = eventTypes.map((t) => ({ + ...t, + isSelected: allSelected, + })); + setEventTypes(updatedEventTypes); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [allSelected]); + + const handleCheckedChange = (value: boolean, currentType: string) => { + const updatedEventTypes = eventTypes.map((t) => { + if (t.text === currentType) { + return { ...t, isSelected: value }; + } + return t; + }); + + // If all event types are selected, set allSelected to true + if (updatedEventTypes.every((t) => t.isSelected)) { + setAllSelected(true); + return; + } + + // If no event types are selected, set allSelected to false + if (updatedEventTypes.every((t) => !t.isSelected)) { + setAllSelected(false); + return; + } + + setEventTypes(updatedEventTypes); + }; + + return ( + + + + + + Select events + + +
+ + + {eventTypes.map((type) => ( + + ))} +
+
+
+ ); +}; + +export default TableFilter; diff --git a/apps/analytics/app/_components/analytics/cards/TotalDataExported.tsx b/apps/analytics/app/_components/analytics/cards/TotalDataExported.tsx new file mode 100644 index 00000000..8325614d --- /dev/null +++ b/apps/analytics/app/_components/analytics/cards/TotalDataExported.tsx @@ -0,0 +1,15 @@ +import { getTotalInterviewsStarted } from "~/utils/getTotalInterviewsStarted"; +import { SummaryCard } from "~/components/SummaryCard"; + +const TotalDataExported = async () => { + const totalInterviewsStarted = await getTotalInterviewsStarted(); + return ( + + ); +}; + +export default TotalDataExported; diff --git a/apps/analytics/app/_components/analytics/cards/TotalErrorsCard.tsx b/apps/analytics/app/_components/analytics/cards/TotalErrorsCard.tsx new file mode 100644 index 00000000..fb0a3757 --- /dev/null +++ b/apps/analytics/app/_components/analytics/cards/TotalErrorsCard.tsx @@ -0,0 +1,15 @@ +import { SummaryCard } from "~/components/SummaryCard"; +import { getTotalErrors } from "~/utils/getTotalErrors"; + +const TotalErrorsCard = async () => { + const totalErrors = await getTotalErrors(); + return ( + + ); +}; + +export default TotalErrorsCard; diff --git a/apps/analytics/app/_components/errors/ErrorsTable/Columns.tsx b/apps/analytics/app/_components/errors/ErrorsTable/Columns.tsx deleted file mode 100644 index 26f265df..00000000 --- a/apps/analytics/app/_components/errors/ErrorsTable/Columns.tsx +++ /dev/null @@ -1,56 +0,0 @@ -"use client"; - -import { ColumnDef } from "@tanstack/react-table"; -import { StackTraceDialog } from "~/app/_components/errors/ErrorsTable/StackTraceDialog"; -import { DataTableColumnHeader } from "~/components/DataTable/column-header"; -import { MetadataDialog } from "~/components/MetadataDialog"; -import { type ErrorEvent } from "~/db/getErrors"; - -export const columns: ColumnDef[] = [ - { - accessorKey: "name", - header: ({ column }) => ( - - ), - }, - { - accessorKey: "message", - header: ({ column }) => ( - - ), - }, - { - accessorKey: "installationId", - header: ({ column }) => ( - - ), - }, - { - accessorKey: "timestamp", - header: ({ column }) => ( - - ), - }, - { - accessorKey: "stack", - header: "", - cell: ({ row }) => { - return ( -
- -
- ); - }, - }, - { - accessorKey: "metadata", - header: "", - cell: ({ row }) => { - return ( -
- -
- ); - }, - }, -]; diff --git a/apps/analytics/app/_components/errors/ErrorsTable/ErrorsTable.tsx b/apps/analytics/app/_components/errors/ErrorsTable/ErrorsTable.tsx deleted file mode 100644 index 80fd958c..00000000 --- a/apps/analytics/app/_components/errors/ErrorsTable/ErrorsTable.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import { DataTable } from "~/components/DataTable/data-table"; -import getErrors from "~/db/getErrors"; -import { columns } from "./Columns"; -import ExportButton from "~/components/ExportButton"; - -export default async function ErrorsTable() { - const errors = await getErrors(); - - return ( - <> - - - - ); -} diff --git a/apps/analytics/app/_components/errors/ErrorsView.tsx b/apps/analytics/app/_components/errors/ErrorsView.tsx deleted file mode 100644 index 45d81e43..00000000 --- a/apps/analytics/app/_components/errors/ErrorsView.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import ErrorsTable from "./ErrorsTable/ErrorsTable"; -import TotalErrorsCard from "./cards/TotalErrorsCard"; - -export default function ErrorsView() { - return ( -
-
- -
- -
- ); -} diff --git a/apps/analytics/app/_components/errors/cards/TotalErrorsCard.tsx b/apps/analytics/app/_components/errors/cards/TotalErrorsCard.tsx deleted file mode 100644 index 50246c5a..00000000 --- a/apps/analytics/app/_components/errors/cards/TotalErrorsCard.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import { SummaryCard } from "~/components/SummaryCard"; -import getErrors from "~/db/getErrors"; - -const TotalErrorsCard = async () => { - const errors = await getErrors(); - const totalErrors = errors.length; - return ; -}; - -export default TotalErrorsCard; diff --git a/apps/analytics/app/page.tsx b/apps/analytics/app/page.tsx index 57e5fde4..74d54fbe 100644 --- a/apps/analytics/app/page.tsx +++ b/apps/analytics/app/page.tsx @@ -1,7 +1,5 @@ import { UserButton } from "@clerk/nextjs"; import AnalyticsView from "~/app/_components/analytics/AnalyticsView"; -import ErrorsView from "~/app/_components/errors/ErrorsView"; -import { Tabs, TabsContent, TabsList, TabsTrigger } from "~/components/ui/tabs"; import UserManagementDialog from "./_components/users/UserManagementDialog"; export default function DashboardPage() { @@ -16,18 +14,7 @@ export default function DashboardPage() {
- - - Analytics - Errors - - - - - - - - +
); diff --git a/apps/analytics/components/DataTable/data-table-pagination.tsx b/apps/analytics/components/DataTable/data-table-pagination.tsx index 356c62c1..f731c9cf 100644 --- a/apps/analytics/components/DataTable/data-table-pagination.tsx +++ b/apps/analytics/components/DataTable/data-table-pagination.tsx @@ -25,11 +25,11 @@ export function DataTablePagination({ }: DataTablePaginationProps) { return (
-
+ {/*
{table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected. -
-
+
*/} +

Rows per page