Skip to content

Commit

Permalink
fix: minor and rename for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
schweinryder committed Feb 28, 2024
1 parent 5fc3312 commit 5b54bbb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
16 changes: 9 additions & 7 deletions src/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,33 @@ class Elastic {
// Index the PDF using the hash as document ID and returning it for reference
async indexPdf(pdfBuffer: ArrayBuffer) {
const buffer = Buffer.from(pdfBuffer);
const documentId = this.hashPdf(buffer);
const pdfHash = this.hashPdf(buffer);

try {
const encodedPdf = buffer.toString('base64');
await this.client.index({
index: this.pdfIndex,
id: documentId,
id: pdfHash,
body: {
pdf: encodedPdf,
}
});
console.log(`PDF indexed. Document ID: ${documentId}`);
return documentId;
console.log(`PDF indexed. Document ID: ${pdfHash}`);
return pdfHash;
} catch (error) {
console.error(`Error indexing PDF for Document ID ${documentId}:`, error);
throw error;
console.error(`Error indexing PDF for Document ID ${pdfHash}:`, error);
// return anyway, as the report is still added later
return pdfHash;
}
}

async indexReport(documentId: string, reportData: string, url: string) {
async indexReport(pdfHash: string, reportData: string, url: string) {
try {
const response = await this.client.index({
index: this.indexName,
body: {
url: url,
pdfHash: pdfHash,
report: reportData,
state: 'pending',
timestamp: new Date()
Expand Down
8 changes: 4 additions & 4 deletions src/workers/discordReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JobData extends Job {
json: string
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand All @@ -32,9 +32,9 @@ const worker = new Worker(

job.updateProgress(10)
const parsedJson = JSON.parse(job.data.json)

let documentId = ''
try {
await elastic.indexReport(job.data.documentId, parsedJson, job.data.url)
documentId = await elastic.indexReport(job.data.pdfHash, parsedJson, job.data.url)
} catch (error) {
job.log(`Error indexing report: ${error.message}`)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ const worker = new Worker(

if (reportState !== '') {
try {
await elastic.updateDocumentState(job.data.documentId, reportState)
await elastic.updateDocumentState(documentId, reportState)
} catch (error) {
job.log(`Error updating document state: ${error.message}`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/workers/downloadPDF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const worker = new Worker(
}
const text = doc.text;

let documentId = '';
let pdfHash = '';
try {
documentId = await elastic.indexPdf(buffer);
pdfHash = await elastic.indexPdf(buffer);
} catch (error) {
job.log(`Error indexing PDF: ${error.message}`);
}
Expand All @@ -54,7 +54,7 @@ const worker = new Worker(
text,
channelId,
messageId,
documentId,
pdfHash,
});

return doc.text;
Expand Down
4 changes: 2 additions & 2 deletions src/workers/indexParagraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JobData extends Job {
url: string,
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ const worker = new Worker(
url,
channelId: job.data.channelId,
messageId: job.data.messageId,
documentId: job.data.documentId,
pdfHash: job.data.pdfHash,
})

return paragraphs
Expand Down
4 changes: 2 additions & 2 deletions src/workers/parseText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JobData extends Job {
paragraphs: string[]
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand Down Expand Up @@ -49,7 +49,7 @@ const worker = new Worker(
paragraphs: pdfParagraphs,
channelId: job.data.channelId,
messageId: job.data.messageId,
documentId: job.data.documentId,
pdfHash: job.data.pdfHash,
})

// Do something with job
Expand Down
4 changes: 2 additions & 2 deletions src/workers/reflectOnAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JobData extends Job {
answer: string
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ const worker = new Worker(
url: job.data.url,
channelId: job.data.channelId,
messageId: job.data.messageId,
documentId: job.data.documentId,
pdfHash: job.data.pdfHash,
})

// Do something with job
Expand Down
4 changes: 2 additions & 2 deletions src/workers/searchVectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JobData extends Job {
url: string
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ const worker = new Worker(
paragraphs: results.documents.flat(),
channelId: job.data.channelId,
messageId: job.data.messageId,
documentId: job.data.documentId,
pdfHash: job.data.pdfHash,
},
{
attempts: 5,
Expand Down
4 changes: 2 additions & 2 deletions src/workers/splitText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class JobData extends Job {
text: string
channelId: string
messageId: string
documentId: string
pdfHash: string
}
}

Expand All @@ -30,7 +30,7 @@ const worker = new Worker(
url: job.data.url,
channelId: job.data.channelId,
messageId: job.data.messageId,
documentId: job.data.documentId,
pdfHash: job.data.pdfHash,
})

job.updateProgress(100)
Expand Down

0 comments on commit 5b54bbb

Please sign in to comment.