Skip to content

Commit

Permalink
🔧 add deces-updates index
Browse files Browse the repository at this point in the history
  • Loading branch information
rhanka committed May 1, 2021
1 parent 7886751 commit f6d38df
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "./controllers/status.controller";
import "./controllers/job.controller";
import "./controllers/auth.controller";

const log = (json:any) => {
export const log = (json:any) => {
loggerStream.write(JSON.stringify({
"backend": {
"server-date": new Date(Date.now()).toISOString(),
Expand All @@ -28,6 +28,7 @@ const log = (json:any) => {
}));
}


export const app = express();

morgan.token('fwd-addr', (req: any) => {
Expand Down Expand Up @@ -122,3 +123,7 @@ app.use((

// app.use(`${process.env.BACKEND_PROXY_PATH}/search`, bulk);
app.use(`${process.env.BACKEND_PROXY_PATH}/docs`, documentation);

import { initUpdateIndex } from './updatedIds';

initUpdateIndex();
60 changes: 60 additions & 0 deletions backend/src/updatedIds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
import { readFileSync, readdirSync, statSync } from 'fs';
import path from "path";
import axios from 'axios';
import { log } from './server';

const updateIndex = 'deces-updates';
const modelIndex = 'deces';
let updateIndexCreated = false;

export const initUpdateIndex = async (): Promise<boolean> => {
if (updateIndexCreated) { return true }
try {
const test = await axios(`http://elasticsearch:9200/${updateIndex}/_settings`);
if (test.status === 200) {
updateIndexCreated = true;
return true;
}
} catch(e) {
// create index
const settingsResponse = await axios(`http://elasticsearch:9200/${modelIndex}/_settings`);
if (settingsResponse.status !== 200) {
log({msg: 'failed initiating missing update index', error: "coudn't retrive settings from model index"});
return false;
}
const analysis = settingsResponse.data && settingsResponse.data[modelIndex] &&
settingsResponse.data[modelIndex].settings.index && settingsResponse.data[modelIndex].settings.index.analysis;
if (!analysis) {
log({msg: 'failed initiating missing update index', error: "coudn't retrive settings from model index"});
return false
};
const mappingsResponse = await axios(`http://elasticsearch:9200/${modelIndex}/_mappings`);
if (mappingsResponse.status !== 200) {
log({msg: 'failed initiating missing update index', error: "coudn't retrive mappings from model index"});
return false;
}
const mappings = mappingsResponse.data && mappingsResponse.data[modelIndex] &&
mappingsResponse.data[modelIndex].mappings;
if (!mappings) {
log({msg: 'failed initiating missing update index', error: "coudn't retrive mappings from model index"});
return false;
};
try {
const createIndexResponse = await axios(`http://elasticsearch:9200/${updateIndex}/`, {
method: 'PUT',
data: {
settings: { index: { analysis}},
mappings
},
headers: {
'Content-Type': 'application/x-ndjson',
'Cache-Control': 'no-cache'
}
});
updateIndexCreated = true;
log({msg: "initiated update index as didn't exists"});
return true;
} catch(e) {
log({msg: 'failed initiating missing update index', error: e.message});
return false;
}
}
}

const walk = (directory: string): string[]=> {
const fileList: string[] = [];
Expand Down

0 comments on commit f6d38df

Please sign in to comment.