Skip to content

Commit

Permalink
Merge pull request #36 from Klimatbyran/feat/save-responses-to-db
Browse files Browse the repository at this point in the history
Add elastic client
  • Loading branch information
schweinryder authored Feb 16, 2024
2 parents 0c1765f + 3f8c762 commit c3c857f
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 5 deletions.
122 changes: 122 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@bull-board/api": "^5.12.0",
"@bull-board/express": "^5.12.0",
"@elastic/elasticsearch": "^8.12.1",
"@google/generative-ai": "^0.1.3",
"bullmq": "^5.1.1",
"chromadb": "^1.7.3",
Expand Down
4 changes: 4 additions & 0 deletions src/config/elasticsearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
node: process.env.ELASTIC_NODE_URL,
indexName: process.env.ELASTIC_INDEX_NAME,
}
66 changes: 66 additions & 0 deletions src/elastic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import config from './config/elasticsearch';
import { Client } from '@elastic/elasticsearch';

class Elastic {
client: Client;
indexName: string;

constructor({ node, indexName }) {
this.client = new Client({ node });
this.indexName = indexName;
}

async setupIndex() {
const indexExists = await this.client.indices.exists({ index: this.indexName });
if (!indexExists) {
await this.client.indices.create({
index: this.indexName,
body: {
mappings: {
properties: {
url: { type: 'keyword' },
pdf: { type: 'binary' },
report: { type: 'object' },
state: { type: 'keyword' }
}
}
}
});
console.log(`Index ${this.indexName} created.`);
} else {
console.log(`Index ${this.indexName} already exists.`);
}
}

async indexDocument(documentId: string, pdfContent: string, reportData: object = null) {
const docBody = {
pdf: pdfContent,
report: reportData,
state: 'pending'
};

await this.client.index({
index: this.indexName,
id: documentId,
body: docBody
});

console.log(`Document ${documentId} indexed.`);
}

async updateDocumentState(documentId: string, newState: string) {
await this.client.update({
index: this.indexName,
id: documentId,
body: {
doc: {
state: newState
}
}
});

console.log(`Document ${documentId} state updated to ${newState}.`);
}
}

export default new Elastic(config)
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'
import { ExpressAdapter } from '@bull-board/express'

import discord from './discord'
import elastic from './elastic'

// keep this line, otherwise the workers won't be started
import * as workers from './workers'
Expand Down Expand Up @@ -56,6 +57,8 @@ createBullBoard({
})

const app = express()

elastic.setupIndex()
discord.login()

app.use('/admin/queues', serverAdapter.getRouter())
Expand Down
15 changes: 10 additions & 5 deletions src/workers/discordReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {
ModalActionRowComponentBuilder,
TextInputStyle,
} from 'discord.js'
import { Client } from '@elastic/elasticsearch';

const esClient = new Client({
node: 'http://elasticsearch.data-pipeline.svc.cluster.local:9200'
});

class JobData extends Job {
data: {
url: string
json: string
channelId: string
messageId: string
}
url: string;
json: string;
channelId: string;
messageId: string;
};
}

const worker = new Worker(
Expand Down
5 changes: 5 additions & 0 deletions src/workers/downloadPDF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import pdf from 'pdf-parse'
import { splitText } from '../queues'
import discord from '../discord'
import { TextChannel } from 'discord.js'
import { Client } from '@elastic/elasticsearch';

const esClient = new Client({
node: 'http://elasticsearch.data-pipeline.svc.cluster.local:9200'
});

class JobData extends Job {
data: {
Expand Down

0 comments on commit c3c857f

Please sign in to comment.