Skip to content

Commit

Permalink
feat : migration from Ch to Pg
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshettyin committed Jan 14, 2025
1 parent 5fb2903 commit 81319c7
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 192 deletions.
3 changes: 1 addition & 2 deletions app/[short_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
setPrivateShortCode,
} from "@/lib/services/redisPublicGenerate";
import { NextRequest } from "next/server";
import PrismaClientManager from "@/lib/services/pgConnect";
import prisma from "@/lib/services/pgConnect";

export async function GET(req: NextRequest) {
const path = req.nextUrl.pathname;
Expand All @@ -28,7 +28,6 @@ export async function GET(req: NextRequest) {
HTTP_STATUS.BAD_REQUEST
);
} else if (!long_url && !checkIfShortCodePublic(shortCode)) {
const prisma = PrismaClientManager.getInstance().getPrismaClient();
const link = await prisma.links.findFirst({
where: {
short_code: shortCode,
Expand Down
4 changes: 1 addition & 3 deletions app/api/list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { getServerSession } from "next-auth";

import { ISessionType } from "@/interfaces/url";
import authOptions from "@/lib/authOptions";
import PrismaClientManager from "@/lib/services/pgConnect";
import prisma from "@/lib/services/pgConnect";
import { HTTP_STATUS, RESPONSE } from "@/lib/constants";

export async function GET(req: NextRequest) {
const posgresInstance = PrismaClientManager.getInstance();
const prisma = posgresInstance.getPrismaClient();
const session: ISessionType | null = await getServerSession(authOptions);
const searchParams = req.nextUrl.searchParams;

Expand Down
7 changes: 4 additions & 3 deletions app/api/ping/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest } from "next/server";

import PrismaClientManager from "@/lib/services/pgConnect";
import prisma from "@/lib/services/pgConnect";
import RedisClientManager from "@/lib/services/redisConnect";

import userAgentAnlytics from "@/lib/services/ua";
Expand All @@ -19,8 +19,9 @@ export async function GET(req: NextRequest) {
user_anlytics: userAgent,
};

const posgresInstance = PrismaClientManager.getInstance();
const postgresStatus = await posgresInstance.checkStatus();
const checkPg = await prisma.$queryRaw`SELECT 1`;
const postgresStatus = checkPg === 1;


const redisInstance = RedisClientManager.getInstance();
const redisStatus = await redisInstance.checkStatus();
Expand Down
123 changes: 82 additions & 41 deletions app/app/(dashboard)/links/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ import {
} from "@/components/ui/table";
import { Label } from "@radix-ui/react-label";
import { PieChart } from "@/components/GraphicalComponents/PieChart";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { LinkShareDialog } from "@/components/DialogComponents/LinkShareDialog";
import { EditLinkDialog } from "@/components/DialogComponents/EditLinkDialog";
import { copyToClipboard } from "@/lib/utils";
import { LinkType, linkType } from "@/interfaces/types";
import { headerAnalyticsType, linkType } from "@/interfaces/types";
import { useEffect, useState } from "react";
import { getAnalyticsAction } from "@/lib/actions/getAnalyticsAction";
import { getLinkDetails } from "@/lib/actions/getLinksAction";
import { HTTP_STATUS } from "@/lib/constants";
import { toast } from "sonner";
import { getLinkDetailsWithAnalytics } from "@/lib/actions/getLinksAction";
import { useRouter } from "next/navigation";
import { LinkPageLoading } from "@/components/LoadingComponents/LinkPageLoading";
import { NoDataLoading } from "@/components/LoadingComponents/NoDataLoading";
Expand Down Expand Up @@ -67,9 +63,6 @@ export default function Page({
}

const [link, setLink] = useState({
engagement: 1200,
last7DaysEngage: 900,
weeklyChange: -25,
devices: {
Desktop: 720,
Mobile: 420,
Expand Down Expand Up @@ -109,16 +102,50 @@ export default function Page({
created_at: new Date("2023-05-28T12:34:56Z"),
title: "Sample Title",
_count: {
click_analytics: 100
}
click_analytics: 100,
},
});

const [headerAnalytics, setHeaderAnalytics] = useState<headerAnalyticsType>({
totalVisitsThisWeek: 0,
totalVisitsLastWeek: 0,
percentageChange: 0,
devices: [
{ device: "Desktop", engagements: 720 },
{ device: "Mobile", engagements: 420 },
{ device: "Tablet", engagements: 60 },
],
os: [
{ os: "windows11", engagements: 48 },
{ os: "macOS", engagements: 50 },
],
browsers: [
{ browser: "chrome", engagements: 45 },
{ browser: "firefox", engagements: 50 },
{ browser: "safari", engagements: 70 },
],
cities: [
{ city: "New York", engagements: 100 },
{ city: "Los Angeles", engagements: 50 },
{ city: "Chicago", engagements: 40 },
{ city: "Houston", engagements: 30 },
],
locations: [
{ id: 1, country: "United States", engagements: 600, percentage: "50%" },
{ id: 2, country: "United Kingdom", engagements: 300, percentage: "20%" },
{ id: 3, country: "Canada", engagements: 180, percentage: "20%" },
{ id: 4, country: "Australia", engagements: 120, percentage: "10%" },
],
});

const router = useRouter();

useEffect(() => {
setLoading(true);
getLinkDetails(decodeId(params.id).toString()).then((res) => {
setfetchLink(res.link)
setLoading(false)
getLinkDetailsWithAnalytics(decodeId(params.id).toString()).then((res) => {
setfetchLink(res.link as linkType);
setHeaderAnalytics(res.analytics as headerAnalyticsType);
setLoading(false);
});
}, [router, params.id]);

Expand Down Expand Up @@ -244,17 +271,23 @@ export default function Page({
<div className="flex md:flex-row flex-col mt-6 mr-0">
<div className="flex-1 flex justify-between shadow-md p-4 rounded-xl items-center border-[0.5px]">
<h1 className="text-md">Engagements</h1>
<h1 className="text-2xl font-bold">{fetchLink._count.click_analytics}</h1>
<h1 className="text-2xl font-bold">
{fetchLink._count.click_analytics}
</h1>
</div>
<div className="flex-1 flex justify-between shadow-md ml-0 md:mt-0 mt-4 md:ml-4 p-4 rounded-xl items-center border-[0.5px]">
<h1 className="text-md">Last 7 days</h1>
<h1 className="text-2xl font-bold">{link.last7DaysEngage}</h1>
<h1 className="text-2xl font-bold">
{headerAnalytics.totalVisitsThisWeek}
</h1>
</div>
<div className="flex-1 flex justify-between shadow-md ml-0 md:mt-0 mt-4 md:ml-4 p-4 rounded-xl items-center border-[0.5px]">
<h1 className="text-md">Weekly change</h1>
<div className="flex items-center">
<ArrowUpDownIcon size={25} />
<h1 className="text-2xl font-bold ml-1">{link.weeklyChange}%</h1>
<h1 className="text-2xl font-bold ml-1">
{headerAnalytics.percentageChange}%
</h1>
</div>
</div>
</div>
Expand All @@ -270,7 +303,7 @@ export default function Page({
</TableRow>
</TableHeader>
<TableBody>
{link.locations.map((location: any) => {
{headerAnalytics.locations.map((location: any) => {
return (
<TableRow key={location.id}>
<TableCell className="font-medium">{location.id}</TableCell>
Expand All @@ -279,7 +312,7 @@ export default function Page({
{location.engagements}
</TableCell>
<TableCell className="text-right">
{location.percentage}%
{location.percentage}
</TableCell>
</TableRow>
);
Expand All @@ -298,8 +331,12 @@ export default function Page({
</div>
) : (
<PieChart
devices={Object.keys(link.devices ?? {})}
data={Object.values(link.devices ?? {})}
devices={headerAnalytics.devices.map(
(device) => device.device
)}
data={headerAnalytics.devices.map((device) => {
return device.engagements;
})}
/>
)}
</div>
Expand All @@ -314,8 +351,8 @@ export default function Page({
</div>
) : (
<PieChart
devices={Object.keys(link.os ?? {})}
data={Object.values(link.os ?? {})}
devices={headerAnalytics.os.map((os) => os.os)}
data={headerAnalytics.os.map((os) => os.engagements)}
/>
)}
</div>
Expand All @@ -330,30 +367,34 @@ export default function Page({
</div>
) : (
<PieChart
devices={Object.keys(link.browser ?? {})}
data={Object.values(link.browser ?? {})}
devices={headerAnalytics.browsers.map(
(browser) => browser.browser
)}
data={headerAnalytics.browsers.map(
(browser) => browser.engagements
)}
/>
)}
</div>
</div>
</div>
</div>
<div className="flex justify-center md:justify-start">
<div className="mt-8 shadow-md p-6 rounded-xl w-fit border-[0.5px]">
<Label className="font-bold ml-3 text-lg">City</Label>
<div className="mt-4 md:w-[300px] md:h-[300px] w-[250px] h-[250px]">
{NoDataSet == 1 ? (
<div>
<NoDataLoading />
</div>
) : (
<PieChart
devices={Object.keys(link.city ?? {})}
data={Object.values(link.city ?? {})}
/>
)}
</div>
</div>
<div className="flex justify-center md:justify-start">
<div className="mt-8 shadow-md p-6 rounded-xl w-fit border-[0.5px]">
<Label className="font-bold ml-3 text-lg">City</Label>
<div className="mt-4 md:w-[300px] md:h-[300px] w-[250px] h-[250px]">
{NoDataSet == 1 ? (
<div>
<NoDataLoading />
</div>
) : (
<PieChart
devices={headerAnalytics.cities.map((city) => city.city)}
data={headerAnalytics.cities.map((city) => city.engagements)}
/>
)}
</div>
</div>
</div>
</div>
);
Expand Down
73 changes: 52 additions & 21 deletions interfaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export interface gettingStartedStatus {
}

export interface locationsDetails {
id:number,
id: number;
country: string;
engagements: number;
percentage: number;
}


export interface LinkType {
id?: number;
title?: string;
Expand All @@ -26,14 +25,13 @@ export interface LinkType {
weeklyChange?: number;
locations?: any;
devices?: Record<string, number>;
refs?: Record<string, number>,
os?:Record<string, number>,
browser?:Record<string, number>,
refs?: Record<string, number>;
os?: Record<string, number>;
browser?: Record<string, number>;
}


export interface QRCodeType {
id:number;
id: number;
title?: string;
shortLink: string;
longLink: string;
Expand All @@ -47,8 +45,8 @@ export interface User {
startStatus?: gettingStartedStatus;
}

export type EditLink = Pick<LinkType,'title' | 'shortLink'>
export type EditQR = Pick<LinkType,'title' | 'shortLink'>
export type EditLink = Pick<LinkType, "title" | "shortLink">;
export type EditQR = Pick<LinkType, "title" | "shortLink">;

export type createLink = {
longUrl: string;
Expand All @@ -59,26 +57,59 @@ export type createLink = {

export type createQR = Pick<createLink, "longUrl" | "title" | "shortLink">;

export type publicLinkType = {
longUrl : string;
shortUrl : string,
clicks? : string
}
export type publicLinkType = {
longUrl: string;
shortUrl: string;
clicks?: string;
};

type countType = Record<string,number>
type countType = {
click_analytics: number;
};

export type linkType = {
id:number,
id: number;
user_id: number;
short_code: string;
long_url: string;
created_at: Date;
title: string | null;
engagements?: number;
_count:countType
}
_count: countType;
};



export type headerAnalyticsType = {
totalVisitsThisWeek: number;
totalVisitsLastWeek: number;
percentageChange: number;
devices: Array<{
device: string;
engagements: number;
}>;
os: Array<{
os: string;
engagements: number;
}>;
browsers: Array<{
browser: string;
engagements: number;
}>;
cities: Array<{
city: string;
engagements: number;
}>;
locations: Array<{
id: number;
country: string;
engagements: number;
percentage: string;
}>;
};


export type paginationType = {
value: number,
pageActive: pageOrder
}
value: number;
pageActive: pageOrder;
};
6 changes: 0 additions & 6 deletions lib/actions/getAnalyticsAction.ts

This file was deleted.

Loading

0 comments on commit 81319c7

Please sign in to comment.