-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(#138): move polling to openhim channel config #139
Open
witash
wants to merge
7
commits into
openmrs-mediator
Choose a base branch
from
138-make-openmrs-configureable
base: openmrs-mediator
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97b1348
feat(#138): move polling to openhim channel config
witash 07094b5
fix(#123): fixing tests
witash 61e26eb
fix(#123): fixing tests
witash 928e931
fix(#138): making SYNC_PERIOD configureable, adding tests
witash 1c7dc2b
fix(#138): sonar complexity fix
witash 5cc6525
fix(#138): sonar fix
witash c9c30c9
fix(#138): change http to https
witash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import * as dotenv from 'dotenv'; | |
dotenv.config(); | ||
|
||
export const PORT = process.env.PORT || 6000; | ||
const REQUEST_TIMEOUT = Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')); | ||
|
||
export const OPENHIM = { | ||
username: getEnvironmentVariable('OPENHIM_USERNAME', '[email protected]'), | ||
|
@@ -11,27 +12,30 @@ export const OPENHIM = { | |
}; | ||
|
||
export const FHIR = { | ||
url: getEnvironmentVariable('FHIR_URL', 'http://openhim-core:5001/fhir'), | ||
url: getEnvironmentVariable('FHIR_URL', 'https://openhim-core:5001/fhir'), | ||
username: getEnvironmentVariable('FHIR_USERNAME', 'interop-client'), | ||
password: getEnvironmentVariable('FHIR_PASSWORD', 'interop-password'), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const CHT = { | ||
url: getEnvironmentVariable('CHT_URL', 'https://nginx'), | ||
username: getEnvironmentVariable('CHT_USERNAME', 'admin'), | ||
password: getEnvironmentVariable('CHT_PASSWORD', 'password'), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const OPENMRS = { | ||
url: getEnvironmentVariable('OPENMRS_URL', ''), | ||
username: getEnvironmentVariable('OPENMRS_USERNAME', ''), | ||
password: getEnvironmentVariable('OPENMRS_PASSWORD', ''), | ||
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000')) | ||
url: getEnvironmentVariable('OPENMRS_CHANNEL_URL', 'https://openhim-core:5001/openmrs'), | ||
username: getEnvironmentVariable('OPENMRS_CHANNEL_USERNAME', 'interop-client'), | ||
password: getEnvironmentVariable('OPENMRS_CHANNEL_PASSWORD', 'interop-password'), | ||
timeout: REQUEST_TIMEOUT | ||
}; | ||
|
||
export const SYNC_INTERVAL = getEnvironmentVariable('SYNC_INTERVAL', '60000'); | ||
// hard code sync interval to 1 minute because it is hard coded in mediator config | ||
export const SYNC_INTERVAL = '60'; | ||
// how far back shoudl the sync look for new resources | ||
export const SYNC_PERIOD = getEnvironmentVariable('SYNC_PERIOD', '3600'); | ||
|
||
function getEnvironmentVariable(env: string, def: string) { | ||
if (process.env.NODE_ENV === 'test') { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,15 @@ import { mediatorConfig } from './config/mediator'; | |
import { openMRSMediatorConfig } from './config/openmrs_mediator'; | ||
import { logger } from './logger'; | ||
import bodyParser from 'body-parser'; | ||
import {PORT, OPENHIM, SYNC_INTERVAL, OPENMRS} from './config'; | ||
import {PORT, OPENHIM, OPENMRS} from './config'; | ||
import patientRoutes from './src/routes/patient'; | ||
import serviceRequestRoutes from './src/routes/service-request'; | ||
import encounterRoutes from './src/routes/encounter'; | ||
import organizationRoutes from './src/routes/organization'; | ||
import endpointRoutes from './src/routes/endpoint'; | ||
import chtRoutes from './src/routes/cht'; | ||
import openMRSRoutes from './src/routes/openmrs'; | ||
import { registerMediatorCallback } from './src/utils/openhim'; | ||
import { syncPatients, syncEncounters } from './src/utils/openmrs_sync' | ||
import os from 'os'; | ||
|
||
const {registerMediator} = require('openhim-mediator-utils'); | ||
|
@@ -21,11 +21,12 @@ const app = express(); | |
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({extended: true})); | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this code rather than commenting it out. |
||
app.get('*', (_: Request, res: Response) => { | ||
const osUptime = os.uptime(); | ||
const processUptime = process.uptime(); | ||
res.send({status: 'success', osuptime: osUptime, processuptime: processUptime}); | ||
}); | ||
});*/ | ||
|
||
// routes for valid fhir resources | ||
app.use('/patient', patientRoutes); | ||
|
@@ -34,30 +35,21 @@ app.use('/encounter', encounterRoutes); | |
app.use('/organization', organizationRoutes); | ||
app.use('/endpoint', endpointRoutes); | ||
|
||
// routes for cht docs | ||
// routes for CHT docs | ||
app.use('/cht', chtRoutes); | ||
|
||
// routes for OpenMRS | ||
app.use('/openmrs', openMRSRoutes); | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
app.listen(PORT, () => logger.info(`Server listening on port ${PORT}`)); | ||
|
||
// TODO => inject the 'port' and 'http scheme' into 'mediatorConfig' | ||
registerMediator(OPENHIM, mediatorConfig, registerMediatorCallback); | ||
|
||
// if OPENMRS is specified, register its mediator | ||
// and start the sync background task | ||
if (OPENMRS.url) { | ||
registerMediator(OPENHIM, openMRSMediatorConfig, registerMediatorCallback); | ||
// start patient and ecnounter sync in the background | ||
setInterval(async () => { | ||
try { | ||
const startTime = new Date(); | ||
startTime.setHours(startTime.getHours() - 1); | ||
await syncPatients(startTime); | ||
await syncEncounters(startTime); | ||
} catch (error: any) { | ||
logger.error(error); | ||
} | ||
}, Number(SYNC_INTERVAL)); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
witash marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { logger } from '../../logger'; | ||
import { syncPatients, syncEncounters } from '../utils/openmrs_sync' | ||
import { SYNC_PERIOD } from '../../config' | ||
|
||
export async function sync() { | ||
try { | ||
let now = Date.now(); | ||
let syncPeriod = parseInt(SYNC_PERIOD, 10); | ||
let startTime = new Date(now - syncPeriod); | ||
|
||
await syncPatients(startTime); | ||
await syncEncounters(startTime); | ||
witash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { status: 200, data: { message: `OpenMRS sync completed successfully`} }; | ||
} catch(error: any) { | ||
logger.error(error); | ||
return { status: 500, data: { message: `Error during OpenMRS Sync`} }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Router } from 'express'; | ||
import { requestHandler } from '../utils/request'; | ||
import { sync } from '../controllers/openmrs' | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/sync', | ||
requestHandler((req) => sync()) | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import request from 'supertest'; | ||
import app from '../../..'; | ||
import * as openmrs_sync from '../../utils/openmrs_sync'; | ||
import axios from 'axios'; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('GET /openmrs/sync', () => { | ||
it('calls syncPatients and syncEncouners', async () => { | ||
jest.spyOn(openmrs_sync, 'syncPatients').mockImplementation(async (startTime) => { | ||
}); | ||
|
||
jest.spyOn(openmrs_sync, 'syncEncounters').mockImplementation(async (startTime) => { | ||
}); | ||
|
||
const res = await request(app).get('/openmrs/sync').send(); | ||
|
||
expect(res.status).toBe(200); | ||
|
||
expect(openmrs_sync.syncPatients).toHaveBeenCalled(); | ||
expect(openmrs_sync.syncEncounters).toHaveBeenCalled(); | ||
}); | ||
|
||
it('returns 500 if syncPatients throws an error', async () => { | ||
jest.spyOn(openmrs_sync, 'syncPatients').mockImplementation(async (startTime) => { | ||
throw new Error('Sync Failed'); | ||
}); | ||
|
||
const res = await request(app).get('/openmrs/sync').send(); | ||
|
||
expect(res.status).toBe(500); | ||
|
||
expect(openmrs_sync.syncPatients).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the default sync period to the comment in a human-readable manner would be helpful. e.g.
How far back should the sync look for new resources. Defaults to 1 hour.