Skip to content

Commit

Permalink
refactor, and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zotya committed Sep 27, 2023
1 parent e776630 commit e8ee669
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 491 deletions.
198 changes: 102 additions & 96 deletions src/config/healthcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,104 +23,85 @@ export function buildQuery(query, values) {
return JSON.parse(q);
}

async function executeQuery(q, appConfig, params = {}) {
async function executeQuery(q, appConfig, params = {}, callback) {
return new Promise(async (resolve, reject) => {
params['index_name'] = 'status_' + appConfig['index_name'];
const query = buildQuery(q, params);
//console.log(JSON.stringify(query));
const resp = await runRequest(query, appConfig);
// console.log(JSON.stringify(resp.body));
resolve(resp.body);
try {
params['index_name'] = 'status_' + appConfig['index_name'];
const query = buildQuery(q, params);
//console.log(JSON.stringify(query));
const resp = await runRequest(query, appConfig);
// console.log(JSON.stringify(resp.body));
resolve(callback(resp.body, params));
} catch (e) {
reject({ error: e.message });
}
});
}

export async function getlastandnext_started_execution(appConfig) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(last_scheduled_started_indexing, appConfig);
resolve({
export function getlastandnext_started_execution(body, params = {}) {
if (body.hits.total.value > 0) {
return {
last_started: body.hits.hits[0]._source.start_time_ts,
next_execution_date: body.hits.hits[0]._source.next_execution_date_ts,
});
});
};
} else {
throw new Error('no results');
}
}

export async function getlastfailed_execution(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
failed_scheduled_atempts_since_last_started,
appConfig,
params,
);
if (body.hits.total.value > 0) {
resolve({
last_started: body.hits.hits[0]._source.start_time_ts,
next_execution_date: body.hits.hits[0]._source.next_execution_date_ts,
});
} else {
reject({ error: 'no results' });
}
});
export function getlastfailed_execution(body, params = {}) {
if (body.hits.total.value > 0) {
return {
last_started: body.hits.hits[0]._source.start_time_ts,
next_execution_date: body.hits.hits[0]._source.next_execution_date_ts,
};
} else {
throw new Error('no results');
}
}

export async function getlastsynctaskssincestarted(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
last_sync_task_since_last_start,
appConfig,
params,
);
if (body.hits.total.value > 0) {
resolve({
sites: body.hits.hits[0]._source.sites,
});
} else {
reject({ error: 'no results' });
}
});
export function getlastsynctaskssincestarted(body, params = {}) {
if (body.hits.total.value > 0) {
return {
sites: body.hits.hits[0]._source.sites,
};
} else {
throw new Error('no results');
}
}

export async function getlastsuccessfultasks_for_site(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
started_or_finished_site_since_last_started,
appConfig,
params,
);
if (body.hits.total.value > 0) {
resolve(true);
} else {
reject(false);
}
});
export function getlastsuccessfultasks_for_site(body, params = {}) {
if (body.hits.total.value > 0) {
return true;
} else {
return false;
}
}

export async function getlatesttasks_for_site(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(latest_tasks_for_site, appConfig, params);
if (body.hits.total.value > 0) {
let status = 'OK';
let i = 0;
while (true) {
const doc = body.hits.hits[i]._source;
if (doc.status === 'Finished') {
break;
}
i++;
if (i === params.THRESHOLD_WARNING) {
break;
}
}
if (i >= params.THRESHOLD_OK && i < params.THRESHOLD_WARNING) {
status = 'WARNING';
export function getlatesttasks_for_site(body, params = {}) {
if (body.hits.total.value > 0) {
let status = 'OK';
let i = 0;
while (true) {
const doc = body.hits.hits[i]._source;
if (doc.status === 'Finished') {
break;
}
if (i >= params.THRESHOLD_WARNING) {
status = 'CRITICAL';
i++;
if (i === params.THRESHOLD_WARNING) {
break;
}
resolve(status);
} else {
reject('Failed to get info');
}
});
if (i >= params.THRESHOLD_OK && i < params.THRESHOLD_WARNING) {
status = 'WARNING';
}
if (i >= params.THRESHOLD_WARNING) {
status = 'CRITICAL';
}
return status;
} else {
throw new Error('Failed to get info');
}
}

async function getStatus(appConfig, params) {
Expand All @@ -129,7 +110,12 @@ async function getStatus(appConfig, params) {
let error = null;
// console.log('=======================================');
// console.log('STEP 1');
const step1 = await getlastandnext_started_execution(appConfig);
const step1 = await executeQuery(
last_scheduled_started_indexing,
appConfig,
{},
getlastandnext_started_execution,
);

// console.log(step1);

Expand All @@ -141,7 +127,12 @@ async function getStatus(appConfig, params) {
try {
// console.log('=======================================');
// console.log('STEP 2');
const step2 = await getlastfailed_execution(appConfig, step1);
const step2 = await executeQuery(
failed_scheduled_atempts_since_last_started,
appConfig,
step1,
getlastfailed_execution,
);
next_schedule = step2.next_execution_date;
// console.log(step2);
} catch {
Expand All @@ -155,31 +146,46 @@ async function getStatus(appConfig, params) {
error = 'Airflow stopped indexing, no new schedules in the queue';
} else {
try {
const step3 = await getlastsynctaskssincestarted(appConfig, step1);
const step3 = await executeQuery(
last_sync_task_since_last_start,
appConfig,
step1,
getlastsynctaskssincestarted,
);
// console.log(step3.sites);
const all_sites_status = {};
for (let i = 0; i < step3.sites.length; i++) {
try {
// console.log('=======================================');
// console.log('STEP 4');
// const step4 =
await getlastsuccessfultasks_for_site(appConfig, {
site_name: step3.sites[i],
last_started: step1['last_started'],
});
await executeQuery(
started_or_finished_site_since_last_started,
appConfig,
{
site_name: step3.sites[i],
last_started: step1['last_started'],
},
getlastsuccessfultasks_for_site,
);
all_sites_status[step3.sites[i]] = 'OK';
// console.log(step4);
} catch {
// console.log('=======================================');
// console.log('STEP 5');
const step5 = await getlatesttasks_for_site(appConfig, {
site_name: step3.sites[i],
last_started: step1['last_started'],
THRESHOLD_WARNING: parseInt(
params.FAILED_SYNC_THRESHOLD_WARNING,
),
THRESHOLD_OK: parseInt(params.FAILED_SYNC_THRESHOLD_OK),
});
const step5 = await executeQuery(
latest_tasks_for_site,
appConfig,
{
site_name: step3.sites[i],
last_started: step1['last_started'],
THRESHOLD_WARNING: parseInt(
params.FAILED_SYNC_THRESHOLD_WARNING,
),
THRESHOLD_OK: parseInt(params.FAILED_SYNC_THRESHOLD_OK),
},
getlatesttasks_for_site,
);
all_sites_status[step3.sites[i]] = step5;
}
}
Expand Down
98 changes: 95 additions & 3 deletions src/config/healthcheck.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { buildQuery } from './healthcheck';

import {
buildQuery,
getlastfailed_execution,
getlastandnext_started_execution,
getlastsynctaskssincestarted,
getlatesttasks_for_site,
getlastsuccessfultasks_for_site,
} from './healthcheck';
import failed_scheduled_atempts_since_last_started_resp from './healthcheck_queries/failed_scheduled_atempts_since_last_started_resp.json';
import last_scheduled_started_indexing_resp from './healthcheck_queries/last_scheduled_started_indexing_resp.json';
import last_sync_task_since_last_start_resp from './healthcheck_queries/last_sync_task_since_last_start_resp.json';
import latest_tasks_for_site_resp from './healthcheck_queries/latest_tasks_for_site_resp.json';
import started_or_finished_site_since_last_started_resp from './healthcheck_queries/started_or_finished_site_since_last_started_resp.json';
import empty_resp from './healthcheck_queries/empty_resp.json';
const SLOTS = [
'aboveSearchInput',
'belowSearchInput',
Expand Down Expand Up @@ -108,7 +120,7 @@ const built_query3 = {
index: 'test_index',
};

describe('build_test_query', () => {
describe('test building the queries', () => {
it('should replace 1 string variable with the value in the query', () => {
const params = { index_name: 'test_index' };
const bQuery = buildQuery(query1, params);
Expand All @@ -131,3 +143,83 @@ describe('build_test_query', () => {
expect(bQuery).toEqual(built_query3);
});
});

describe('test parsing the response from elasticsearch for correct response', () => {
it('should return last_started and next_execution_date', () => {
const resp = getlastandnext_started_execution(
last_scheduled_started_indexing_resp,
);
expect(resp).toEqual({
last_started: 1695732613000,
next_execution_date: 1695732900000,
});
});

it('should return last_started and next_execution_date of failed task', () => {
const resp = getlastfailed_execution(
failed_scheduled_atempts_since_last_started_resp,
);
expect(resp).toEqual({
last_started: 1695732613000,
next_execution_date: 1695732900000,
});
});

it('should return list of clusters', () => {
const resp = getlastsynctaskssincestarted(
last_sync_task_since_last_start_resp,
);
expect(resp).toEqual({ sites: ['test_site1', 'test_site2'] });
});

it('if the last task for a site did not fail, return "OK"', async () => {
const resp = await getlatesttasks_for_site(latest_tasks_for_site_resp);
expect(resp).toEqual('OK');
});

it('if the last task for a site was succesful, return true', async () => {
const resp = getlastsuccessfultasks_for_site(
started_or_finished_site_since_last_started_resp,
);
expect(resp).toEqual(true);
});
});

describe('test parsing the response from elasticsearch for empty response', () => {
it('should return no results', () => {
try {
getlastandnext_started_execution(empty_resp);
} catch (e) {
expect(e.message).toEqual('no results');
}
});

it('should return no results', () => {
try {
getlastfailed_execution(empty_resp);
} catch (e) {
expect(e.message).toEqual('no results');
}
});

it('should return list of clusters', () => {
try {
getlastsynctaskssincestarted(empty_resp);
} catch (e) {
expect(e.message).toEqual('no results');
}
});

it('if the last task for a site did not fail, return "OK"', async () => {
try {
await getlatesttasks_for_site(empty_resp);
} catch (e) {
expect(e.message).toEqual('Failed to get info');
}
});

it("if the can't take the status for the last task for a site, return false", async () => {
const resp = getlastsuccessfultasks_for_site(empty_resp);
expect(resp).toEqual(false);
});
});
Loading

0 comments on commit e8ee669

Please sign in to comment.