-
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.
Added new endpoints + fixed fetching data in parallel
- Loading branch information
maikReal
committed
Jul 17, 2024
1 parent
d63e57d
commit 0590ebc
Showing
9 changed files
with
370 additions
and
91 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
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
61 changes: 61 additions & 0 deletions
61
src/app/api/v2/get-fid-comparison-analytics/[fid]/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,61 @@ | ||
import { NextRequest } from "next/server"; | ||
import { headers } from "next/headers"; | ||
import { | ||
nonAuthHttpResponse, | ||
internalServerErrorHttpResponse, | ||
successHttpResponse, | ||
verifyAuth, | ||
generateApiResponse, | ||
getCurrentFilePath, | ||
unprocessableHttpResponse, | ||
} from "@/utils/helpers"; | ||
import { AnalyticalManager } from "@/utils/v2/database/analyticalManager"; | ||
import { logInfo } from "@/utils/v2/logs/sentryLogger"; | ||
|
||
const logsFilenamePath = getCurrentFilePath(); | ||
|
||
export type ComparisonAnalyticsDataPeriods = 7 | 14 | 28; | ||
|
||
/** | ||
* The route to get a fid's comparison analytics for last 60 days of his posts. Likes, recasts, replies, | ||
* and the number of casts will be included to this statisti | ||
* | ||
* @param request | ||
* @param params | ||
* @returns | ||
*/ | ||
export const GET = async ( | ||
request: NextRequest, | ||
{ params }: { params: { fid: number } } | ||
) => { | ||
const currentHeaders = headers(); | ||
const { searchParams } = new URL(request.url); | ||
|
||
let typePeriod: string | null = searchParams.get("period"); | ||
const parsedPeriod: ComparisonAnalyticsDataPeriods | null = typePeriod | ||
? (parseInt(typePeriod) as ComparisonAnalyticsDataPeriods) | ||
: null; | ||
|
||
logInfo( | ||
`[DEBUG - ${logsFilenamePath}] Trying to get comparison analytics for the following time period: ${typePeriod}` | ||
); | ||
|
||
if (!parsedPeriod) { | ||
return unprocessableHttpResponse(); | ||
} | ||
|
||
if (!verifyAuth(currentHeaders)) { | ||
return nonAuthHttpResponse(); | ||
} | ||
|
||
try { | ||
const analyticalManager = new AnalyticalManager(params.fid); | ||
|
||
const result = await analyticalManager.getComparisonAnalytics(parsedPeriod); | ||
|
||
return generateApiResponse({ status: 200 }, result); | ||
} catch (err) { | ||
console.log(err); | ||
return internalServerErrorHttpResponse(err); | ||
} | ||
}; |
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,59 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import neynarClient from "@/clients/neynar"; | ||
import { internalServerErrorHttpResponse } from "@/utils/helpers"; | ||
import { DatabaseManager } from "@/utils/v2/database/databaseManager"; | ||
|
||
export interface UserInfo { | ||
fid: number; | ||
username: string; | ||
display_name: string; | ||
pfp_url: string; | ||
follower_count: number; | ||
following_count: number; | ||
verifications: object; | ||
} | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: { fid: string } } | ||
) { | ||
try { | ||
const fid = parseInt(params.fid); | ||
|
||
const { users } = await neynarClient.fetchBulkUsers([fid]); | ||
|
||
if (users && users.length > 0) { | ||
let userData = users[0] as UserInfo; | ||
|
||
const dbManager = DatabaseManager.getInstance(); | ||
dbManager.initialize(); | ||
|
||
const query = ` | ||
INSERT INTO users_info (fid, username, display_name, pfp_url, followers, following, verified_address) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
`; | ||
|
||
const queryParams = [ | ||
fid, | ||
userData.username, | ||
userData.display_name, | ||
userData.pfp_url, | ||
userData.follower_count, | ||
userData.following_count, | ||
JSON.stringify(userData.verifications), | ||
]; | ||
|
||
dbManager.executeQuery(query, queryParams); | ||
|
||
return NextResponse.json({ user: userData }, { status: 200 }); | ||
} else { | ||
return NextResponse.json({ user: null }, { status: 201 }); | ||
} | ||
} catch (err) { | ||
console.error( | ||
"[ERROR - api/v2/farcaster-data/fetch-bio/[fid]] Error while getting bio info about a user", | ||
err | ||
); | ||
return internalServerErrorHttpResponse(err); | ||
} | ||
} |
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,5 @@ | ||
import { Pool } from "pg"; | ||
|
||
export const sharedDatabasePool = new Pool({ | ||
connectionString: process.env.POSTGRES_URL, | ||
}); |
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
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
Oops, something went wrong.