Skip to content

Commit

Permalink
testing remote url updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorezz committed Oct 29, 2024
1 parent 8349111 commit 72e8db2
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@ dist
.DS_Store

.vercel

data
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as middlewares from "./lib/middlewares.ts";
import authRouter from "./routes/auth.ts";
import chartRouter from "./routes/charts.ts";
// import seedUsers from "./seeds/seed-users.ts";
import * as db from "./lib/db.ts";
// import * as db from "./lib/db.ts";

const app = express();
app.use(helmet());
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@prisma/client": "^5.17.0",
"@types/express": "^4.17.21",
"axios": "^1.7.7",
"bcrypt": "^5.1.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand Down
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ model Chart {
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
remoteUrl String?
isRemote Boolean @default(false)
}
40 changes: 36 additions & 4 deletions routes/charts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Router } from "express";
import * as db from "../lib/db";
import * as z from "zod";
import axios from "axios";

import { requireUser } from "../lib/middlewares";
import type { ParsedToken } from "../types";
import { validateRequest } from "../lib/middlewares";
import { convertToObject } from "typescript";

const router = Router();

Expand All @@ -22,6 +23,8 @@ const createChartSchema = z.object({
}),
config: z.unknown().optional(),
data: z.unknown().optional(),
remoteUrl: z.string().optional(),
isRemote: z.boolean().optional(),
publish: z.boolean().optional(),
});

Expand All @@ -32,6 +35,8 @@ const updateChartSchema = z.object({
config: z.unknown().optional(),
data: z.unknown().optional(),
publish: z.boolean().optional(),
remoteUrl: z.string().optional(),
isRemote: z.boolean().optional(),
id: z.string().optional(),
});

Expand Down Expand Up @@ -72,9 +77,30 @@ router.get(
async (req: any, res, next) => {
try {
const id = req.params.id;
const result = await db.findChartById(id);
let result = await db.findChartById(id);
if (result?.publish !== true) {
return res.json({ message: "Not Published" }).status(401);
return res.json({
error: { message: "Not Authorized, This chart is not public" },
});
}
console.log("chart name", result?.name);
console.log("Remote", result?.isRemote, result?.remoteUrl);
if (result?.isRemote && result?.remoteUrl) {
const lastUpdate = new Date(result.updatedAt);
const now = Date.now();
const diff = now - lastUpdate.getTime();
console.log("Diff", diff / 1000 / 60, "minutes");
const isToUpdate = true; // diff > 1000 * 60 * 60 * 24;
if (isToUpdate) {
//update data.
console.log("Updating remote data");
const remote = await axios.get(result.remoteUrl);
if (remote.data) {
console.log("Remote data", remote.data);
await db.updateChart(id, { data: remote.data });
result = await db.findChartById(id);
}
}
}
return res.json(result);
} catch (err) {
Expand Down Expand Up @@ -155,7 +181,11 @@ router.delete(
/** Update ID */
router.put(
"/:id",
[validateRequest({ params: detailSchema }), requireUser],
[
validateRequest({ body: updateChartSchema }),
validateRequest({ params: detailSchema }),
requireUser,
],
async (req: any, res: any, next: any) => {
try {
const user: ParsedToken = req.user;
Expand All @@ -167,7 +197,9 @@ router.put(
if (chart.userId !== user.userId) {
return res.json({ message: "Not Authorized" }).status(401);
}
console.log("Updating chart", chartId);
const chartData = req.body;
console.log("Chart Data", chartData);
const result = await db.updateChart(chartId, chartData);
return res.json(result);
} catch (err) {
Expand Down
34 changes: 34 additions & 0 deletions sample_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
[
"_",
"2024-09-18",
"2024-09-19",
"2024-09-20",
"2024-09-21",
"2024-09-22"
],
[
"Visits",
4319,
4405,
3821,
485,
251
],
[
"Uniq Visitors",
3292,
3303,
2891,
407,
224
],
[
"New Visits",
1102,
1109,
920,
177,
108
]
]
34 changes: 34 additions & 0 deletions sample_data2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
[
"_",
"2024-09-18",
"2024-09-19",
"2024-09-20",
"2024-09-21",
"2024-09-22"
],
[
"Visits",
1001,
996,
794,
145,
104
],
[
"Bounce Count",
1001,
996,
794,
145,
104
],
[
"Total Visits",
2240651,
2265214,
2027302,
208967,
77372
]
]
Loading

0 comments on commit 72e8db2

Please sign in to comment.