Fixed importing earthquakes
This commit is contained in:
parent
68d47a4fe3
commit
87d2cfbc25
@ -1,6 +1,6 @@
|
|||||||
- [x] Import users
|
- [x] Import users
|
||||||
- [x] Import artefacts
|
- [x] Import artefacts
|
||||||
- [ ] Import earthquakes
|
- [x] Import earthquakes
|
||||||
- [ ] Import observatoies
|
- [ ] Import observatoies
|
||||||
- [ ] Import requests
|
- [ ] Import requests
|
||||||
- [ ] Import scientists
|
- [ ] Import scientists
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import { parse } from "csv-parse/sync";
|
import { parse } from "csv-parse/sync";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { prisma } from "@utils/prisma";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { prisma } from "@utils/prisma";
|
||||||
|
import { getRandomNumber } from "@utils/maths";
|
||||||
|
|
||||||
// Path to your earthquakes.csv
|
|
||||||
const csvFilePath = path.resolve(process.cwd(), "public/earthquakes.csv");
|
const csvFilePath = path.resolve(process.cwd(), "public/earthquakes.csv");
|
||||||
|
|
||||||
type CsvRow = {
|
type CsvRow = {
|
||||||
@ -20,15 +20,29 @@ type CsvRow = {
|
|||||||
|
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
try {
|
try {
|
||||||
// 1. Read the CSV file
|
|
||||||
const fileContent = await fs.readFile(csvFilePath, "utf8");
|
const fileContent = await fs.readFile(csvFilePath, "utf8");
|
||||||
// 2. Parse the CSV
|
|
||||||
const records: CsvRow[] = parse(fileContent, {
|
const records: CsvRow[] = parse(fileContent, {
|
||||||
columns: true,
|
columns: true,
|
||||||
skip_empty_lines: true,
|
skip_empty_lines: true,
|
||||||
});
|
});
|
||||||
// 3. Transform to fit Earthquake model
|
|
||||||
const earthquakes = records.map((row) => ({
|
const failedImports: { row: CsvRow; reason: string }[] = [];
|
||||||
|
|
||||||
|
const earthquakes = await Promise.all(
|
||||||
|
records.map(async (row) => {
|
||||||
|
const creators = await prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
role: { in: ["SCIENTIST", "ADMIN"] },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const randomCreator = creators.length > 0 ? creators[getRandomNumber(0, creators.length - 1)] : null;
|
||||||
|
|
||||||
|
if (!randomCreator) {
|
||||||
|
failedImports.push({ row, reason: `RandomCreator: ${randomCreator}` });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
date: new Date(row.Date),
|
date: new Date(row.Date),
|
||||||
code: row.Code,
|
code: row.Code,
|
||||||
magnitude: parseFloat(row.Magnitude),
|
magnitude: parseFloat(row.Magnitude),
|
||||||
@ -36,15 +50,30 @@ export async function POST() {
|
|||||||
latitude: parseFloat(row.Latitude),
|
latitude: parseFloat(row.Latitude),
|
||||||
longitude: parseFloat(row.Longitude),
|
longitude: parseFloat(row.Longitude),
|
||||||
location: row.Location,
|
location: row.Location,
|
||||||
depth: row.Depth, // store as received
|
depth: row.Depth,
|
||||||
// todo add random selection for creatorId
|
creatorId: randomCreator.id,
|
||||||
creatorId: null,
|
};
|
||||||
}));
|
})
|
||||||
// 4. Bulk create earthquakes in database:
|
);
|
||||||
|
|
||||||
|
const validEarthquakes = earthquakes.filter(
|
||||||
|
(earthquake): earthquake is NonNullable<typeof earthquake> => earthquake !== null
|
||||||
|
);
|
||||||
|
|
||||||
await prisma.earthquake.createMany({
|
await prisma.earthquake.createMany({
|
||||||
data: earthquakes,
|
data: validEarthquakes,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (failedImports.length > 0) {
|
||||||
|
console.warn("Failed imports:", failedImports);
|
||||||
|
await fs.writeFile(path.resolve(process.cwd(), "failed_imports_earthquakes.json"), JSON.stringify(failedImports, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
count: validEarthquakes.length,
|
||||||
|
failedCount: failedImports.length,
|
||||||
});
|
});
|
||||||
return NextResponse.json({ success: true, count: earthquakes.length });
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user