-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/nextjs/app/api/analytics/unique_fids/graph/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import Analytics from "~~/model/analytics"; | ||
import connectDB from "~~/services/connectDB"; | ||
|
||
type DateFilter = { | ||
$gte?: Date; | ||
$lte?: Date; | ||
}; | ||
|
||
export async function GET(request: NextRequest) { | ||
await connectDB(); | ||
const { searchParams } = new URL(request.url); | ||
const startDate = searchParams.get("startDate"); | ||
const frameId = searchParams.get("frameId"); | ||
const journeyId = searchParams.get("journeyId"); | ||
const dateFilter: DateFilter = {}; | ||
console.log(startDate, frameId, journeyId); | ||
if (startDate) { | ||
dateFilter.$gte = new Date(startDate); | ||
} | ||
const query: any = {}; | ||
if (startDate) { | ||
query.createdAt = dateFilter; | ||
} | ||
if (frameId) { | ||
query.frameId = frameId; | ||
} | ||
if (journeyId) { | ||
query.journeyId = journeyId; | ||
} | ||
const aggregationPipeline = [ | ||
{ | ||
$match: query, | ||
}, | ||
{ | ||
$group: { | ||
_id: { | ||
date: { | ||
$dateToString: { | ||
format: "%Y-%m-%d", | ||
date: "$createdAt", | ||
}, | ||
}, | ||
fid: "$fid", // assuming the unique identifier is stored in the 'fid' field | ||
}, | ||
}, | ||
}, | ||
{ | ||
$group: { | ||
_id: "$_id.date", | ||
count: { $sum: 1 }, | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
date: "$_id", | ||
count: 1, | ||
_id: 0, | ||
}, | ||
}, | ||
{ | ||
$sort: { date: 1 } as any, | ||
}, | ||
]; | ||
const results = await Analytics.aggregate(aggregationPipeline).exec(); | ||
console.log(results); | ||
// write a loop iterating from startDate to endDate and filling in the missing dates with 0 | ||
return new NextResponse(JSON.stringify(results), { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters