Skip to content

Commit 510196d

Browse files
committed
review change request
1 parent 50e09d4 commit 510196d

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

src/api/core/ChangeRequestDB.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ import dbSchema from '../../change_request_schema.json'
1010
import { doc } from "@terminusdb/terminusdb-client/dist/typescript/lib/woql"
1111
//import {v4 as uuidv4} from 'uuid';
1212
const { v4: uuidv4 } = require('uuid');
13-
14-
//const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
15-
//const key = process.env.USER_KEY || "root"
16-
//const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
17-
//const user = process.env.USER_NAME || "admin"
1813
class ChangeRequestDB {
1914
client: WOQLClient;
2015
request : Request
@@ -24,8 +19,8 @@ class ChangeRequestDB {
2419
dbName : string | undefined
2520
changeRequestDbName: string | undefined
2621
logger : typeDef.Logger
27-
endStatus:any = { Assigned: true, Error: true }
28-
availableStatus:any = { Assigned: true, Error: true, Progress: true }
22+
endStatus:any = { Assigned: true, Error: true, Close: true }
23+
availableStatus:any = { Assigned: true, Error: true, Progress: true, Close: true }
2924
errorMessage:any = {'api:UnknownDatabase':true,'api:NoUniqueIdForOrganizationName':true}
3025

3126
constructor(req : Request) {
@@ -221,22 +216,21 @@ class ChangeRequestDB {
221216

222217
// check if there is an old CR db
223218
async updateDocumentFixSchema(changeRequestDoc:typeDef.ChangeReqDoc|typeDef.IndexedCommit, message = 'add new doc') {
224-
let docResult
225219
try {
226220
const putDocParams : DocParamsPut = { create: true }
227-
docResult = await this.client.updateDocument(changeRequestDoc, { create: true }, undefined, message)
221+
await this.client.updateDocument(changeRequestDoc, { create: true }, undefined, message)
228222
} catch (err:any) {
229223
const errData = err.data || {}
230224
// I have the old schema
231225
if (errData['api:message'] === 'Schema check failure') {
232226
// update old schema
233227
await this.client.addDocument(dbSchema, { graph_type: 'schema', full_replace: true })
234-
docResult = await this.client.updateDocument(changeRequestDoc, { create: true }, undefined, message)
228+
await this.client.updateDocument(changeRequestDoc, { create: true }, undefined, message)
235229
} else {
236230
throw err
237231
}
238232
}
239-
return docResult
233+
return changeRequestDoc
240234
}
241235

242236
async getCROriginCommitId (userDBClient:WOQLClient, originBranch:string, trackingBranch:string) {
@@ -379,10 +373,11 @@ class ChangeRequestDB {
379373
async checkStatus(taskId:string){
380374
try {
381375
const progress = await IndexClient.checkStatus(this.logger, taskId)
382-
if (progress.data < 1) {
383-
return { status: 'Progress' }
376+
if (progress.status === "Complete") {
377+
const indexedDocuments = progress.indexed_documents || 0
378+
return { status: 'Complete',indexed_documents:indexedDocuments }
384379
}
385-
return { status: 'Complete' }
380+
return { status: 'Progress' }
386381
} catch (err:any) {
387382
const message = err.response && err.response.data ? err.response.data : err.message
388383
return { status: 'Error', error_message: message }
@@ -402,11 +397,12 @@ class ChangeRequestDB {
402397
await this.client.updateDocument(doc)
403398
break
404399
case 'Complete':
400+
doc.indexed_documents = statusObj.indexed_documents
405401
// eslint-disable-next-line no-case-declarations
406402
const result = await this.assignCommit(domain, commit, doc.searchable_branch_commit_id, apiKey)
407403
doc.indexing_status = result.status
408404
if (result.error_message)doc.error_message = result.error_message
409-
await this.client.updateDocument(doc)
405+
await this.updateDocumentFixSchema(doc)
410406
break
411407
case 'Error':
412408
doc.indexing_status = 'Error'
@@ -459,6 +455,7 @@ class ChangeRequestDB {
459455
WOQL.triple('v:index', '@schema:indexed_at', 'v:time'),
460456
WOQL.opt().triple('v:index', '@schema:task_id', 'v:task_id'),
461457
WOQL.opt().triple('v:index', '@schema:error_message', 'v:error_message'),
458+
WOQL.opt().triple('v:index', '@schema:indexed_documents', 'v:indexed_documents'),
462459
WOQL.triple('v:changeR', '@schema:indexing_info', 'v:index'),
463460
WOQL.triple('v:changeR', '@schema:name', 'v:name'),
464461
WOQL.triple('v:changeR', '@schema:tracking_branch', 'v:tracking_branch')

src/api/core/IndexClient.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export async function indexDatabase (logger:any, domain:string, commit:string, a
1515
return axios.get(`${baseUrl}index`, { headers, params: { commit: commit, domain: domain } })
1616
}
1717

18-
export function checkStatus (logger:any, taskId:string) {
18+
export async function checkStatus (logger:any, taskId:string) {
1919
logger.info(`check status ${taskId}`)
20-
return axios.get(`${baseUrl}check`, { params: { task_id: taskId } })
20+
const res = await axios.get(`${baseUrl}check`, { params: { task_id: taskId } })
21+
return typeof res.data === "string" ? JSON.parse(res.data) : res.data
2122
}
2223

2324
// -- source_commit: commit that you already built an index for

src/api/core/typeDef.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export type ChangeReqStatus = "Submitted" | "Open" | "Rejected" | "Merged"
2+
export type ChangeReqStatus = "Submitted" | "Open" | "Rejected" | "Merged" | "Close"
33
export type Logger = {error:Function, debug:Function, info:Function, warn:Function}
44
export type MessageObj = {"@type" : "Message",
55
"text":string,
@@ -42,6 +42,7 @@ export type IndexedCommit={
4242
"searchable_branch_name"?:string,
4343
"task_id"?:string,
4444
"error_message"?:string
45+
"indexed_documents"?:number
4546
}
4647

4748
export type AdvancedSearchField = {

src/api/paths/api/indexes/{org}/{db}/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import ChangeRequestDB from "../../../../../core/ChangeRequestDB";
77
const changeR = new ChangeRequestDB(req)
88
// check if the user has read access
99
await changeR.checkUserReadAuthorization()
10-
const limitPar:string = typeof req.query.limit === "string" ? req.query.limit : '5'
11-
const limit:number = parseInt(limitPar)
10+
const limit:number = typeof req.query.limit === "string" || typeof req.query.limit === "number" ? parseInt(req.query.limit) : 5
1211
const status = typeof req.query.status === "string" ? req.query.status : undefined
1312
const branch = typeof req.query.branch === "string" ? req.query.branch : undefined
1413
const commits = await changeR.getLastCommitsIndexed(limit, status, branch)
1514
res.status(200).json(commits)
1615
}catch(err:any){
1716
console.log(err.message)
1817
const status = err.status || 500
19-
const errData = err.data || {message: "I can not get commit indexed list"}
18+
const errData = err.data || {message: `I can not get commit indexed list ${err.message}`}
2019
res.status(status).send(errData);
2120
}
2221
}

src/api/paths/api/indexes/{org}/{db}/search/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import ChangeRequestDB from "../../../../../../core/ChangeRequestDB";
1414
res.json(result.data)
1515
} catch (err:any) {
1616
//req.context.logger.error(err)
17+
1718
const status = err.status || 500
18-
res.status(status).send({ message: 'Failed to search', err: err.message })
19+
const message = err.response && err.response.data ? err.response.data : err.message
20+
res.status(status).send({ message: 'Failed to search', error: message })
1921
}
2022
}
2123

src/change_request_schema.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Sent",
1414
"Progress",
1515
"Assigned",
16-
"Create"
16+
"Create",
17+
"Close"
1718
]
1819
},
1920
{
@@ -91,6 +92,10 @@
9192
"error_message": {
9293
"@class": "xsd:string",
9394
"@type": "Optional"
95+
},
96+
"indexed_documents": {
97+
"@class": "xsd:decimal",
98+
"@type": "Optional"
9499
}
95100
},
96101
{
@@ -100,7 +105,8 @@
100105
"Open",
101106
"Merged",
102107
"Rejected",
103-
"Submitted"
108+
"Submitted",
109+
"Close"
104110
]
105111
}
106112
]

0 commit comments

Comments
 (0)