Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor backend #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor code
  • Loading branch information
azkaiftikhar01 committed Jan 30, 2025
commit 3eaadb16b789f70ca1eb1ada97d85ae25d4d731d
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 29 additions & 3 deletions src/controller/website.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get_websites, store_website_stats } from "../database/website.query";
import { get_stats_q, get_websites, store_website_stats } from "../database/website.query";
import { fetchPageSpeedData } from "../services/pagespeed.service";
import pg from "pg";
import { config } from "../config/env";
Expand All @@ -13,8 +13,6 @@ export const fetchAndStoreWebsiteStats = async () => {

const query = get_websites();
const websites = await client.query(query);

// Process all websites in parallel using Promise.allSettled
await Promise.allSettled(
websites.rows.map(async (website) => {
try {
Expand All @@ -33,4 +31,32 @@ export const fetchAndStoreWebsiteStats = async () => {
console.error("Error fetching and storing stats:", error);
}
};
export const get_states = async (param: Param) => {
try {
await client.connect();

const query = get_stats_q(param);
const stats = await client.query(query);

if (stats.rows.length > 0) {

const parsedStats = stats.rows.map(row => {
return {
...row,
stats: row.stats ? JSON.parse(row.stats) : null
};
});

return parsedStats;
} else {
console.log("No data found");
return [];
}
} catch (error) {
console.error("Error fetching and storing stats:", error);
return { message: "Error fetching stats", error: error.message };
} finally {
await client.end(); // Make sure to close the client connection
}
};

15 changes: 14 additions & 1 deletion src/database/website.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ const store_website_stats = (websiteId: number, stats: object) => `
INSERT INTO website_stats (website_id, stats)
VALUES (${websiteId}, '${JSON.stringify(stats)}'::jsonb) returning id;
`;
export {get_websites,store_website_stats};

interface Param {
url?: string;
}

const get_stats_q = (param: Param) => `
SELECT ws.website_id, w.url,ws.stats
FROM ${process.env.schema}.website_stats ws
JOIN ${process.env.schema}.websites w
ON ws.website_id = w.id
${param.url && param.url !== "" ? `WHERE w.url = '${param.url}'` : ""}
`;

export {get_websites,store_website_stats,get_stats_q};
10 changes: 8 additions & 2 deletions src/routes/website.routes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Elysia } from "elysia";
import { fetchAndStoreWebsiteStats } from "../controller/website.controller";
import { fetchAndStoreWebsiteStats, get_states } from "../controller/website.controller";

const router = new Elysia();

router.post("/fetch-stats", async () => {
router.post("/store-stats", async () => {
await fetchAndStoreWebsiteStats();
return { message: "Response Stored Successfully." };
});

router.get("/get-stats", async () => {
return get_states();
return { message: "Response Stord Successfully." };
});


export default router;