Skip to content

Commit

Permalink
session and visitors classified
Browse files Browse the repository at this point in the history
  • Loading branch information
Bereket Engida committed May 26, 2023
1 parent 267eb93 commit 7180eaa
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 13 deletions.
Binary file modified examples/nextjs/prisma/dev.db
Binary file not shown.
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@radix-ui/react-slot": "^1.0.1",
"@radix-ui/react-switch": "^1.0.3",
"@radix-ui/react-tabs": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.6",
"@tanstack/react-table": "^8.9.1",
"@types/lodash": "^4.14.194",
"add": "^2.0.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/react/components/EventTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function EventTableRow({ event }: { event: EventType }) {
<dl className="sm:divide-y sm:divide-slate-400 dark:sm:divide-slate-600 text-sm font-light text-slate-400 max-w-xs w-full">
<dt className="font-semibold capitalize p-2 text-slate-800">Payload</dt>
{
getNestedKeysAndValues(event.payload).map(({key , value }, i) => (
getNestedKeysAndValues(event.payload).map(({ key, value }, i) => (
<DetailValue key={i} keyName={key} value={value} />
))
}
Expand Down
30 changes: 30 additions & 0 deletions packages/ui/src/react/components/ui/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client"

import * as React from "react"
import * as TooltipPrimitive from "@radix-ui/react-tooltip"

import { cn } from "../../lib/utils"

const TooltipProvider = TooltipPrimitive.Provider

const Tooltip = TooltipPrimitive.Root

const TooltipTrigger = TooltipPrimitive.Trigger

const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
>(({ className, sideOffset = 4, ...props }, ref) => (
<TooltipPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
"z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-50 data-[side=bottom]:slide-in-from-top-1 data-[side=left]:slide-in-from-right-1 data-[side=right]:slide-in-from-left-1 data-[side=top]:slide-in-from-bottom-1",
className
)}
{...props}
/>
))
TooltipContent.displayName = TooltipPrimitive.Content.displayName

export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
8 changes: 4 additions & 4 deletions packages/ui/src/react/components/visitorsGraph.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
"use client"
import { User2 } from "lucide-react"
import { LucideIcon } from "lucide-react"
import React from "react"
import { Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"

export function Graph({ data }: { data: { date: string, visits: number }[] }) {
export function Graph({ data, name, Icon }: { data: { date: string, visits: number }[], name: string, Icon: LucideIcon }) {
const isMobile = window.innerWidth < 768
return (
<ResponsiveContainer width="100%" height={isMobile ? 250 : 350}>
Expand Down Expand Up @@ -37,8 +37,8 @@ export function Graph({ data }: { data: { date: string, visits: number }[] }) {
return (
<div className="custom-tooltip dark:bg-black bg-white/80 px-2 border rounded-md border-gray-700 py-2">
<div className=" flex items-center gap-2 dark:text-emphasis text-black" >
<User2 size={16} className=" " />
<p className=" font-medium">{`${payload[0].value} Visitors`}</p>
<Icon size={16} />
<p className=" font-medium">{`${payload[0].value} ${name}`}</p>
</div>
<p className="text-gray-400 text-sm">{label}</p>
</div>
Expand Down
32 changes: 25 additions & 7 deletions packages/ui/src/react/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useSWR from 'swr'
import { fetcher } from "./lib/utils";
import { GetInsightResponse } from "@loglib/core/types"
import { InsightCard } from "./components/insightCard";
import { Activity, Asterisk, Eye, MapPin, MonitorSmartphone, PanelTop, TimerIcon, UserIcon } from "lucide-react";
import { Activity, Asterisk, Eye, Laptop2, MapPin, MonitorSmartphone, PanelTop, TimerIcon, UserIcon, Users2 } from "lucide-react";
import { Switch } from "./components/ui/switch";
import { PagesComponent } from "./components/pages";
import { RefComponent } from "./components/referrer";
Expand Down Expand Up @@ -136,12 +136,30 @@ export function Dashboard() {
</div>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-7 grid-cols-1">
<Card className="col-span-4">
<CardHeader>
<CardTitle>Visitors</CardTitle>
</CardHeader>
<CardContent className="pl-2">
<Graph data={getVisitorsByDate(data.sessions, timeRange.startDate, timeRange.endDate)} />
</CardContent>
<Tabs defaultValue="visitors">
<CardHeader className=" flex-row justify-between items-center">
<CardTitle>Visitors</CardTitle>
<TabsList>
<TabsTrigger value="visitors">
Visitors

</TabsTrigger>
<TabsTrigger value="sessions">
Sessions

</TabsTrigger>
</TabsList>
</CardHeader>
<CardContent className="pl-2">
<TabsContent value="visitors">

<Graph data={getVisitorsByDate(data.sessions, timeRange.startDate, timeRange.endDate)} name="Visitors" Icon={Users2} />
</TabsContent>
<TabsContent value="sessions">
<Graph data={getVisitorsByDate(data.sessions, timeRange.startDate, timeRange.endDate, false)} name="Sessions" Icon={Laptop2} />
</TabsContent>
</CardContent>
</Tabs>
</Card>
<Card className=" md:col-span-3">
<Tabs defaultValue="pages">
Expand Down
8 changes: 7 additions & 1 deletion packages/ui/src/react/lib/insight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ export const getBrowser = (sessions: Session[]) => {
return browsers
}

export const getVisitorsByDate = (sessions: Session[], startDate: Date, endDate: Date) => {
export const getVisitorsByDate = (sessions: Session[], startDate: Date, endDate: Date, uniqueUsers = true) => {
const ONE_DAY = 1000 * 60 * 60 * 24;
const range = getTimeRange(startDate, endDate)
const uniqueUserSessions = sessions.filter((session, index, self) =>
index === self.findIndex((s) => (
s.userId === session.userId
))
)
sessions = uniqueUsers ? uniqueUserSessions : sessions
if (range / ONE_DAY <= 2) {
const visitors = sessions.reduce((acc, session) => {
//divide it by hour
Expand Down
Loading

0 comments on commit 7180eaa

Please sign in to comment.