@@ -2,72 +2,9 @@ import { readFileSync, readdirSync } from "fs"
22import path from "path"
33import { 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
729export 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