Skip to content

Commit

Permalink
REFACTOR: UpdateAPI refactor
Browse files Browse the repository at this point in the history
- Update API Refactored
  • Loading branch information
ishaan812 committed Aug 8, 2023
1 parent e42c990 commit d3f200a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
6 changes: 5 additions & 1 deletion src/searchtools/searchutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export async function queryBuilder(
}
let query;
if (rootquery != undefined) {
query = rootquery;
if (rootquery === 'openapi') {
query = '"openapi:3"'
} else if (rootquery === 'swagger') {
query = '"swagger: \\"2"'
}
return query;
}
query = prompt + ' AND "openapi: 3"';
Expand Down
20 changes: 16 additions & 4 deletions src/updatetools/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { UpdateAllDocuments } from './updateutils.js';
import { UpdateDocument, scrollSearch } from './updateutils.js';

export async function UpdateOpenAPIFiles(): Promise<string> {
//Go through all rows in the database
await UpdateAllDocuments('openapi');
return 'Updated OpenAPI Files';
const params = {
index: 'openapi',
scroll: '30s',
size: 1,
_source: ['owner', 'repository', 'filepath', 'ETAG', 'isDeleted'],
body:{
query: {
match_all: {}
}
}
}
for await (const hit of scrollSearch(params)) {
await UpdateDocument(hit);
}
return 'Updated All OpenAPI Files';
}
63 changes: 24 additions & 39 deletions src/updatetools/updateutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@ import OASNormalize from "oas-normalize"
import { octokit, esClient } from "../app.js"
import { DeleteDocumentWithId, CreateDocument } from "../DB/dbutils.js"

async function * scrollSearch (params) {

async function ETAGRequestBuilder(document: any): Promise<any> {
const requestConfig = {
owner: document._source.owner,
repo: document._source.repository,
path: document._source.filepath,
};
requestConfig['headers'] = {
'If-None-Match': document._source.ETAG,
};
const request = {
method: 'GET',
url: "/repos/"+requestConfig.owner+"/"+requestConfig.repo+"/contents/"+requestConfig.path,
headers: {
'If-None-Match': document._source.ETAG,
}
};
return
}

export async function * scrollSearch (params) {
let response = await esClient.search(params)
while (true) {
const sourceHits = response.hits.hits
Expand All @@ -22,25 +42,11 @@ async function * scrollSearch (params) {
}
}

async function UpdateDocument(document: any): Promise<void> {
export async function UpdateDocument(document: any): Promise<void> {
if(document._source.isDeleted === true){
return
}
const requestConfig = {
owner: document._source.owner,
repo: document._source.repository,
path: document._source.filepath,
};
requestConfig['headers'] = {
'If-None-Match': document._source.ETAG,
};
const request = {
method: 'GET',
url: "/repos/"+requestConfig.owner+"/"+requestConfig.repo+"/contents/"+requestConfig.path,
headers: {
'If-None-Match': document._source.ETAG,
}
};
const request = await ETAGRequestBuilder(document)
await octokit.request(request).then(async(response) => {
console.info("File "+document._id+" to be updated")
// make isDeleted true for current Document and update
Expand Down Expand Up @@ -91,26 +97,5 @@ async function UpdateDocument(document: any): Promise<void> {
});
}

export async function UpdateAllDocuments(
index: string,
): Promise<string> {
// Perform the initial search request to initiate the scroll
const params = {
index: index,
scroll: '30s',
size: 1,
_source: ['owner', 'repository', 'filepath', 'ETAG', 'isDeleted'],
body:{
query: {
match_all: {}
}
}
}
for await (const hit of scrollSearch(params)) {
await UpdateDocument(hit);
}
return new Promise((resolve) => {
resolve('Database Updated');
});
}


0 comments on commit d3f200a

Please sign in to comment.