Skip to content

Commit 414d3d3

Browse files
Refactored ingestion logic to utilize API functions for database operations, improving code organization and readability.
1 parent df07742 commit 414d3d3

File tree

5 files changed

+74
-69
lines changed

5 files changed

+74
-69
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { RC_APP_URI } from "../../../constants"
2+
import { DBNodeRelation } from "../../../core/dbNode"
3+
4+
export async function establishRelations(relations: DBNodeRelation[]): Promise<boolean> {
5+
try {
6+
const res = await fetch(`${RC_APP_URI}/establishRelations`, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
},
11+
body: JSON.stringify({ relations }),
12+
})
13+
14+
return res.status === 200
15+
} catch (e) {
16+
console.log(e)
17+
return false
18+
}
19+
}

ingestion/src/lib/api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { purgeDB } from './purge-db'
2+
export { insertBatch } from './insert-batch'
3+
export { establishRelations } from './establish-relations'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { RC_APP_URI } from "../../../constants"
2+
import { DBNode } from "../../../core/dbNode"
3+
4+
export async function insertBatch(batchID: string, nodes: DBNode[]): Promise<boolean> {
5+
let tries = 5
6+
while (tries--) {
7+
try {
8+
const res = await fetch(`${RC_APP_URI}/ingest`, {
9+
method: "POST",
10+
headers: {
11+
accept: "application/json",
12+
"Content-Type": "application/json",
13+
},
14+
body: JSON.stringify({ nodes, batchID }),
15+
})
16+
17+
if (res.status !== 200) {
18+
console.log(res)
19+
return false
20+
}
21+
22+
return true
23+
} catch (e) {
24+
console.log(e)
25+
}
26+
}
27+
return false
28+
}

ingestion/src/lib/api/purge-db.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { RC_APP_URI } from "../../../constants"
2+
3+
export async function purgeDB(): Promise<boolean> {
4+
try {
5+
const res = await fetch(`${RC_APP_URI}/purgeDB`, {
6+
method: "POST",
7+
headers: {
8+
accept: "application/json",
9+
"Content-Type": "application/json",
10+
},
11+
})
12+
13+
return res.status === 200
14+
} catch (e) {
15+
console.log(e)
16+
return false
17+
}
18+
}

ingestion/src/process/ingest/ingest.ts

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,9 @@ import { readFileSync, readdirSync } from "fs"
22
import path from "path"
33
import { v4 as uuid } from "uuid"
44

5-
import { RC_APP_URI } from "../../constants"
6-
import { DBNode, DBNodeRelation } from "../../core/dbNode"
7-
import { DevDocDBNodeRelation } from "../../core/devDocDBNode"
8-
9-
namespace Algorithms {
10-
export async function purgeDB(): Promise<boolean> {
11-
try {
12-
const res = await fetch(`${RC_APP_URI}/purgeDB`, {
13-
method: "POST",
14-
headers: {
15-
accept: "application/json",
16-
"Content-Type": "application/json",
17-
},
18-
})
19-
20-
return res.status === 200
21-
} catch (e) {
22-
console.log(e)
23-
return false
24-
}
25-
}
26-
27-
export async function insertBatch(batchID: string, nodes: DBNode[]): Promise<boolean> {
28-
let tries = 5
29-
while (tries--) {
30-
try {
31-
const res = await fetch(`${RC_APP_URI}/ingest`, {
32-
method: "POST",
33-
headers: {
34-
accept: "application/json",
35-
"Content-Type": "application/json",
36-
},
37-
body: JSON.stringify({ nodes, batchID }),
38-
})
39-
40-
if (res.status !== 200) {
41-
console.log(res)
42-
return false
43-
}
44-
45-
return true
46-
// return res.status === 200
47-
} catch (e) {
48-
console.log(e)
49-
}
50-
}
51-
return false
52-
}
53-
54-
export async function establishRelations(relations: DBNodeRelation[]): Promise<boolean> {
55-
try {
56-
const res = await fetch(`${RC_APP_URI}/establishRelations`, {
57-
method: "POST",
58-
headers: {
59-
"Content-Type": "application/json",
60-
},
61-
body: JSON.stringify({ relations }),
62-
})
63-
64-
return res.status === 200
65-
} catch (e) {
66-
console.log(e)
67-
return false
68-
}
69-
}
70-
}
5+
import { DBNode, DBNodeRelation } from "@/core/dbNode"
6+
import { DevDocDBNodeRelation } from "@/core/devDocDBNode"
7+
import { purgeDB, insertBatch, establishRelations } from "@/lib/api"
718

729
export async function insertDataIntoDB(batchesDirPath: string) {
7310
console.log("🕒 Inserting")
@@ -76,7 +13,7 @@ export async function insertDataIntoDB(batchesDirPath: string) {
7613

7714
// /* Step 1: Empty DB */
7815
{
79-
const success = await Algorithms.purgeDB()
16+
const success = await purgeDB()
8017
if (!success) {
8118
console.log("❌ Error emptying db")
8219
return
@@ -117,7 +54,7 @@ export async function insertDataIntoDB(batchesDirPath: string) {
11754
}
11855
}
11956

120-
const success = await Algorithms.insertBatch(batchID, nodes)
57+
const success = await insertBatch(batchID, nodes)
12158
if (success) {
12259
console.log(`📦 ${batchID} inserted`)
12360
} else {
@@ -135,7 +72,7 @@ export async function insertDataIntoDB(batchesDirPath: string) {
13572
// Establish relations
13673
const batchSize = 1000
13774
for (let i = 0; i < relations.length; i += batchSize) {
138-
const success = await Algorithms.establishRelations(relations.slice(i, i + batchSize))
75+
const success = await establishRelations(relations.slice(i, i + batchSize))
13976
if (success) {
14077
console.log(`🔗 Relations established ${i + 1000}/${relations.length}`)
14178
} else {

0 commit comments

Comments
 (0)