Merge branch 'master' of ssh://stash.dyson.global.corp:7999/~thowitz/tremor-tracker
This commit is contained in:
commit
8d007a7393
2
.env
2
.env
@ -1,2 +1,2 @@
|
||||
DATABASE_URL=""
|
||||
DATABASE_URL="sqlserver://UK-DIET-SQL-T1:1433;database=Group8_DB;user=UserGroup8;password=aFgbsH1f2evK6xyP;trustServerCertificate=true"
|
||||
JWT_SECRET_KEY=mysupersecretkey
|
||||
9
package-lock.json
generated
9
package-lock.json
generated
@ -14,6 +14,7 @@
|
||||
"axios": "^1.9.0",
|
||||
"bcryptjs": "^3.0.2",
|
||||
"body-parser": "^2.2.0",
|
||||
"csv-parse": "^5.6.0",
|
||||
"csv-parser": "^3.2.0",
|
||||
"dotenv": "^16.5.0",
|
||||
"easy-peasy": "^6.1.0",
|
||||
@ -2657,7 +2658,7 @@
|
||||
"integrity": "sha512-k38b3XOZKv60C4E2hVsXTolJWfkGRMbILBIe2IBITXciy5bOsTKot5kDrf3ZfufQtQOUN5mXceUEpU1rTl9Uog==",
|
||||
"license": "BSD-3-Clause",
|
||||
"bin": {
|
||||
"bcryptjs": "bin/bcryptjs"
|
||||
"bcrypt": "bin/bcrypt"
|
||||
}
|
||||
},
|
||||
"node_modules/binary-extensions": {
|
||||
@ -3122,6 +3123,12 @@
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/csv-parse": {
|
||||
"version": "5.6.0",
|
||||
"resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-5.6.0.tgz",
|
||||
"integrity": "sha512-l3nz3euub2QMg5ouu5U09Ew9Wf6/wQ8I++ch1loQ0ljmzhmfZYrH9fflS22i/PQEvsPvxCwxgz5q7UB8K1JO4Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/csv-parser": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.2.0.tgz",
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
"axios": "^1.9.0",
|
||||
"bcryptjs": "^3.0.2",
|
||||
"body-parser": "^2.2.0",
|
||||
"csv-parse": "^5.6.0",
|
||||
"csv-parser": "^3.2.0",
|
||||
"dotenv": "^16.5.0",
|
||||
"easy-peasy": "^6.1.0",
|
||||
|
||||
@ -6,54 +6,28 @@ const usingPrisma = false;
|
||||
let prisma: PrismaClient;
|
||||
if (usingPrisma) prisma = new PrismaClient();
|
||||
|
||||
export async function GET(request: Request) {
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const events = [
|
||||
{
|
||||
id: "1234",
|
||||
title: "Earthquake in Germany",
|
||||
text1: "Magnitude 8.5",
|
||||
text2: "30 minutes ago",
|
||||
magnitude: 8.5,
|
||||
longitude: 10.4515, // Near Berlin, Germany
|
||||
latitude: 52.52,
|
||||
},
|
||||
{
|
||||
id: "2134",
|
||||
title: "Earthquake in California",
|
||||
text1: "Magnitude 5.3",
|
||||
text2: "2 hours ago",
|
||||
magnitude: 5.3,
|
||||
longitude: -122.4194, // Near San Francisco, California, USA
|
||||
latitude: 37.7749,
|
||||
},
|
||||
{
|
||||
id: "2314",
|
||||
title: "Tremor in Japan",
|
||||
text1: "Magnitude 4.7",
|
||||
text2: "5 hours ago",
|
||||
magnitude: 4.7,
|
||||
longitude: 139.6917, // Near Tokyo, Japan
|
||||
latitude: 35.6762,
|
||||
},
|
||||
{
|
||||
id: "2341",
|
||||
title: "Tremor in Spain",
|
||||
text1: "Magnitude 2.1",
|
||||
text2: "10 hours ago",
|
||||
magnitude: 2.1,
|
||||
longitude: -3.7038, // Near Madrid, Spain
|
||||
latitude: 40.4168,
|
||||
},
|
||||
];
|
||||
const json = await req.json(); // Parse incoming JSON data
|
||||
const {rangeDaysPrev} = json.body
|
||||
|
||||
let earthquakes;
|
||||
if (usingPrisma) earthquakes = await prisma.earthquakes.findMany();
|
||||
const now = new Date()
|
||||
const rangeBeginning = new Date();
|
||||
rangeBeginning.setDate(rangeBeginning.getDate() - rangeDaysPrev)
|
||||
|
||||
const earthquakes = await prisma.earthquake.findMany(
|
||||
{where: {
|
||||
date: {
|
||||
gte: rangeBeginning,
|
||||
lte: now
|
||||
}
|
||||
}}
|
||||
);
|
||||
|
||||
if (earthquakes) {
|
||||
return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 });
|
||||
} else {
|
||||
return NextResponse.json({ message: "Got earthquakes successfully", earthquakes: events }, { status: 200 });
|
||||
return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 });
|
||||
// return NextResponse.json({ message: "Failed to get earthquakes" }, { status: 401 });
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
63
src/app/api/import-earthquakes/route.ts
Normal file
63
src/app/api/import-earthquakes/route.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { parse } from "csv-parse/sync";
|
||||
|
||||
// Define the path to your CSV file.
|
||||
// Place your earthquakes.csv in your project root or `public` directory
|
||||
const csvFilePath = path.resolve(process.cwd(), "public/earthquakes.csv");
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type CsvRow = {
|
||||
Date: string;
|
||||
Magnitude: string;
|
||||
Latitude: string;
|
||||
Longitude: string;
|
||||
Location: string;
|
||||
Depth: string;
|
||||
};
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
// 1. Read the CSV file
|
||||
const fileContent = await fs.readFile(csvFilePath, "utf8");
|
||||
|
||||
// 2. Parse the CSV
|
||||
const records: CsvRow[] = parse(fileContent, {
|
||||
columns: true,
|
||||
skip_empty_lines: true
|
||||
});
|
||||
|
||||
// 3. Transform each CSV row to Earthquake model
|
||||
// Since your prisma model expects: name, date (DateTime), location, magnitude (float), depth (float). We'll fill casualties/creatorId as zero/null for now.
|
||||
const earthquakes = records.map(row => {
|
||||
// You may want to add better parsing & validation depending on your actual data
|
||||
return {
|
||||
date: new Date(row.Date),
|
||||
location: row.Location,
|
||||
magnitude: parseFloat(row.Magnitude),
|
||||
latitude: row.Latitude,
|
||||
longitude: row.Longitude,
|
||||
depth: parseFloat(row.Depth.replace(" km", "")),
|
||||
// todo add creatorId
|
||||
creatorId: null
|
||||
};
|
||||
});
|
||||
|
||||
// 4. Bulk create earthquakes in database:
|
||||
// Consider chunking if your CSV is large!
|
||||
await prisma.earthquake.createMany({
|
||||
data: earthquakes,
|
||||
skipDuplicates: true, // in case the route is called twice
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, count: earthquakes.length });
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
@ -33,7 +33,7 @@ export async function POST(req: Request) {
|
||||
user = findUserByEmail(userData, email);
|
||||
}
|
||||
|
||||
if (user && bcrypt.compareSync(password, usingPrisma ? user.hashedPassword : user.password)) {
|
||||
if (user && bcryptjs.compareSync(password, usingPrisma ? user.passwordHash : user.password)) {
|
||||
// todo remove password from returned user
|
||||
|
||||
// get user and relations
|
||||
|
||||
@ -5,8 +5,8 @@ import "./globals.css";
|
||||
import { action, createStore, StoreProvider } from "easy-peasy";
|
||||
import { Inter } from "next/font/google";
|
||||
|
||||
import { StoreModel } from "@appTypes/StoreModel";
|
||||
import Navbar from "@components/Navbar";
|
||||
import { StoreModel } from '@appTypes/StoreModel';
|
||||
import Navbar from '@components/Navbar';
|
||||
|
||||
const inter = Inter({
|
||||
subsets: ["latin"],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user