Skip to content

Commit

Permalink
Improve debug handler logic and make guide to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ShootingStar91 committed Jan 23, 2024
1 parent 524d847 commit e808dd9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 56 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ New data from Sisu is fetched once an hour.
* https://importer.cs.helsinkif/exploder/reset/:table?token= deletes a single table and triggers fetch. See tables [here](https://github.com/UniversityOfHelsinkiCS/sis-importer/blob/master/importer-api/src/explorer/index.js#L53) and token from the server.
* https://importer.cs.helsinkif/exploder/force_update?token= triggers a fetch for all tables
* **To add new fields to be fetched from Sisu:** Modify [message handlers](https://github.com/UniversityOfHelsinkiCS/sis-importer/tree/master/importer-mankeli/src/messageHandlers). Remember to add any new columns to models importer-mankeli models.
* **To fetch new model from Sisu:** Crate new [message handler](https://github.com/UniversityOfHelsinkiCS/sis-importer/tree/master/importer-mankeli/src/messageHandlers) and [service](https://github.com/UniversityOfHelsinkiCS/sis-importer/tree/master/importer-api/src/services). Finaly add new service to [index.js](https://github.com/UniversityOfHelsinkiCS/sis-importer/blob/master/importer-api/src/services/index.js) and test locally that importing works.

* **To fetch new model from Sisu:** Create new [message handler](https://github.com/UniversityOfHelsinkiCS/sis-importer/tree/master/importer-mankeli/src/messageHandlers) and [service](https://github.com/UniversityOfHelsinkiCS/sis-importer/tree/master/importer-api/src/services). Finally add new service to [index.js](https://github.com/UniversityOfHelsinkiCS/sis-importer/blob/master/importer-api/src/services/index.js) and test locally that importing works.
* **Debug data coming from specific channel**:
+ First, comment out any other channels in the serviceIds folder in [this](importer-api/src/services/index.js) file.
+ Then, go to [importer-mankeli/src/debug](importer-mankeli/src/debug). Add a custom debug handler into the customHandlers-folder: view the existing custom handlers for how-to.
+ Add your handler to the index.js file in the debug-folder (in the array).
+ Run with "npm start". It will log stuff into console. If you want, make it write the relevant logs to a file for much better experience. (And please push that code to repo too)
+ **Remember** to remove any sensitive identifiers in your matcher before pushing code. Also remove the handler from the array, but you can leave the file in the repository if it may be useful in the future.

## API catalogs

Expand Down
52 changes: 0 additions & 52 deletions importer-mankeli/src/debug/curEnrolmentsDebug.js

This file was deleted.

30 changes: 30 additions & 0 deletions importer-mankeli/src/debug/customHandlers/attainmentDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { ORI_ATTAINMENT_CHANNEL } = require('../../utils/stan')

const matcher = attainment => attainment.personId === 'RETRACTED'

const format = ({
id,
personId,
state,
attainmentDate,
type,
credits,
documentState,
metadata
}) => ({
id,
personId,
state,
attainmentDate,
type,
credits,
documentState,
lastModifiedOn: metadata.lastModifiedOn,
modificationOrdinal: metadata.modificationOrdinal
})

module.exports = {
channel: ORI_ATTAINMENT_CHANNEL,
matcher,
format
}
24 changes: 24 additions & 0 deletions importer-mankeli/src/debug/customHandlers/curEnrolmentsDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ILMO_ENROLMENT_CHANNEL } = require('../../utils/stan')

const matcher = enrolment => enrolment.courseUnitRealisationId === 'hy-opt-cur-2324-f0c0d370-0087-4bb9-93aa-387e76bd11f2'

const format = ({
id,
state,
personId,
documentState,
metadata
}) => ({
id,
state,
personId,
documentState,
lastModifiedOn: metadata.lastModifiedOn,
modificationOrdinal: metadata.modificationOrdinal
})

module.exports = {
channel: ILMO_ENROLMENT_CHANNEL,
matcher,
format
}
39 changes: 39 additions & 0 deletions importer-mankeli/src/debug/debugHandlerBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { logger } = require('../utils/logger')

let counter = 5

let interestingItems = []

const debugPrinter = (channel, object) => {
const string = `[DEBUGGER_${channel}] Item of interest found: \n${JSON.stringify({
object
}, null, 2)}\n`
logger.info(
string
)
interestingItems.push(string)
counter -= 1
if (counter === 0) {
counter = 5
logger.info("INTERESTING_ITEMS")
interestingItems.forEach(item => logger.info(item))
}
}

const createDebugHandler = (channel, matcher, format) => {
return {
channel,
handler: msg => {
const entitiesOfInterest = msg.entities.filter(matcher)
if (entitiesOfInterest.length > 0) {
try {
entitiesOfInterest.forEach(obj => debugPrinter(channel, format(obj)))
/* eslint-disable-next-line no-empty */
} catch (e) {}
}
return msg
}
}
}

module.exports = { createDebugHandler }
6 changes: 4 additions & 2 deletions importer-mankeli/src/debug/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const curEnrolmentsDebug = require('./curEnrolmentsDebug')
const attainmentDebug = require('./customHandlers/attainmentDebug')
const { createDebugHandler } = require('./debugHandlerBuilder')

const debugHandlers = [] // [curEnrolmentsDebug]
// Create handler in customhandler folder. Import it and add it in this array.
const debugHandlers = [attainmentDebug].map(({ channel, matcher, format }) => createDebugHandler(channel, matcher, format))

module.exports = {
debugHandlers
Expand Down

0 comments on commit e808dd9

Please sign in to comment.