From 7f532e17f27cfb1d1436e31f466d1f44b872564f Mon Sep 17 00:00:00 2001 From: ayushhunt Date: Mon, 2 Dec 2024 15:23:40 +0530 Subject: [PATCH] Made the suggested changes --- apps/triggers/src/collaborator.status.ts | 24 ++++++++++++++----- apps/www/app/(routes)/about/page.tsx | 2 -- apps/www/app/api/getDummyData.ts | 17 ------------- apps/www/app/layout.tsx | 4 ++-- .../{Colloborators.tsx => Collaborators.tsx} | 0 .../custom/ColloboratorGridWrapper.tsx | 10 ++++---- packages/crypt/.gitignore | 3 ++- 7 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 apps/www/app/api/getDummyData.ts rename apps/www/components/custom/{Colloborators.tsx => Collaborators.tsx} (100%) diff --git a/apps/triggers/src/collaborator.status.ts b/apps/triggers/src/collaborator.status.ts index d3bcecb..3f69865 100644 --- a/apps/triggers/src/collaborator.status.ts +++ b/apps/triggers/src/collaborator.status.ts @@ -12,9 +12,13 @@ export const publishContributorsTask = schedules.task({ let contributors = []; let page = 1; - + const MAX_PAGES = 10; // Limit total pages to prevent excessive API calls try { do { + if (page > MAX_PAGES) { + logger.warn(`Reached maximum page limit of ${MAX_PAGES}`); + break; + } const response = await fetch( `https://api.github.com/repos/${owner}/${repo}/contributors?per_page=100&page=${page}`, { @@ -24,7 +28,11 @@ export const publishContributorsTask = schedules.task({ }, } ); - + const rateLimit = response.headers.get('x-ratelimit-remaining'); + if (rateLimit === '0') { + logger.error('GitHub API rate limit exceeded'); + return; + } if (!response.ok) { logger.error(`GitHub API request failed with status ${response.status}`); return; @@ -38,7 +46,7 @@ export const publishContributorsTask = schedules.task({ contributors = contributors.concat(data); page += 1; - } while (true); + } while (page <=MAX_PAGES); // Filter out bots based on type or if 'bot' appears in their login const filteredContributors = contributors.filter( @@ -57,9 +65,13 @@ export const publishContributorsTask = schedules.task({ // Store data in Redis under a fixed key const redisKey = 'contributors'; - await cache.del(redisKey); // Clear existing data - await cache.rpush(redisKey, ...contributorData.map((c) => JSON.stringify(c))); - + try { + await cache.del(redisKey); // Clear existing data + await cache.rpush(redisKey, ...contributorData.map((c) => JSON.stringify(c))); + } catch (error) { + logger.error('Failed to store contributors in Redis', { error }); + throw error; // Re-throw to trigger task retry + } logger.log('Published contributors data', { contributorData }); } catch (error) { logger.error('Error fetching contributors from GitHub', { error }); diff --git a/apps/www/app/(routes)/about/page.tsx b/apps/www/app/(routes)/about/page.tsx index 9d4057b..91a8389 100644 --- a/apps/www/app/(routes)/about/page.tsx +++ b/apps/www/app/(routes)/about/page.tsx @@ -1,7 +1,5 @@ import ContributorsGridWrapper from "@/components/custom/ColloboratorGridWrapper"; -import ContributorsGrid from "@/components/custom/Colloborators"; -import DummyDataPage from "@/components/custom/Colloborators"; import { SectionHeader, SectionHeaderDescription, diff --git a/apps/www/app/api/getDummyData.ts b/apps/www/app/api/getDummyData.ts deleted file mode 100644 index 3910951..0000000 --- a/apps/www/app/api/getDummyData.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import { cache } from '@repo/cache'; - -type DummyData = { - timestamp: string; - message: string; -}; - -export default async (req: NextApiRequest, res: NextApiResponse) => { - try { - const rawData = await cache.lrange("dummy-data:history", 0, -1); - const data: DummyData[] = rawData.map((item) => JSON.parse(item)); - res.status(200).json(data); - } catch (error) { - res.status(500).json({ error: 'Failed to fetch data' }); - } -}; diff --git a/apps/www/app/layout.tsx b/apps/www/app/layout.tsx index 301a6ce..d5c94c2 100644 --- a/apps/www/app/layout.tsx +++ b/apps/www/app/layout.tsx @@ -17,7 +17,7 @@ export default function RootLayout({ }>) { return ( - + @@ -33,7 +33,7 @@ export default function RootLayout({ - + ); } diff --git a/apps/www/components/custom/Colloborators.tsx b/apps/www/components/custom/Collaborators.tsx similarity index 100% rename from apps/www/components/custom/Colloborators.tsx rename to apps/www/components/custom/Collaborators.tsx diff --git a/apps/www/components/custom/ColloboratorGridWrapper.tsx b/apps/www/components/custom/ColloboratorGridWrapper.tsx index adb0ba3..e24745e 100644 --- a/apps/www/components/custom/ColloboratorGridWrapper.tsx +++ b/apps/www/components/custom/ColloboratorGridWrapper.tsx @@ -1,15 +1,17 @@ import { cache } from '@repo/cache'; -import ContributorsGrid from './Colloborators'; +import ContributorsGrid from './Collaborators'; export default async function ContributorsGridWrapper() { - let contributors = []; + let contributors: ContributorData[] = []; try { + const BATCH_SIZE = 20; const redisKey = 'contributors'; - const rawData = await cache.lrange(redisKey, 0, -1); - contributors = rawData + const rawData = await cache.lrange(redisKey, 0, BATCH_SIZE - 1); + contributors = rawData.map(item => item as ContributorData); } catch (error) { console.error('Error fetching contributors from Redis:', error); + return
Unable to load contributors. Please try again later.
; } return ; diff --git a/packages/crypt/.gitignore b/packages/crypt/.gitignore index b512c09..fbb82ad 100644 --- a/packages/crypt/.gitignore +++ b/packages/crypt/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +tsconfig.tsbuildinfo \ No newline at end of file