From adbcb3af7e5c205193686202edde3ba1dd7ba515 Mon Sep 17 00:00:00 2001 From: Jesse White Date: Tue, 11 Jul 2017 15:54:57 -0400 Subject: [PATCH] feat(alarms): expose the sticky and journal memos --- src/dao/AlarmDAO.ts | 20 +++++ src/model/OnmsAlarm.ts | 7 ++ src/model/OnmsMemo.ts | 22 ++++++ test/dao/AlarmDAO.spec.ts | 7 ++ test/rest/21.0.0/get/api/v2/alarms/82416.json | 79 +++++++++++++++++++ test/rest/MockHTTP21.ts | 5 ++ 6 files changed, 140 insertions(+) create mode 100644 src/model/OnmsMemo.ts create mode 100644 test/rest/21.0.0/get/api/v2/alarms/82416.json diff --git a/src/dao/AlarmDAO.ts b/src/dao/AlarmDAO.ts index 0324e24a6..3878e0f00 100644 --- a/src/dao/AlarmDAO.ts +++ b/src/dao/AlarmDAO.ts @@ -13,6 +13,7 @@ import {OnmsParm} from '../model/OnmsParm'; import {OnmsServiceType} from '../model/OnmsServiceType'; import {Severities} from '../model/OnmsSeverity'; import {TroubleTicketStates} from '../model/OnmsTroubleTicketState'; +import {OnmsMemo} from '../model/OnmsMemo'; import {log, catDao} from '../api/Log'; import {Category} from 'typescript-logging'; @@ -104,6 +105,9 @@ export class AlarmDAO extends AbstractDAO { } } + alarm.sticky = this.toMemo(data.stickyMemo); + alarm.journal = this.toMemo(data.reductionKeyMemo); + return alarm; } @@ -150,4 +154,20 @@ export class AlarmDAO extends AbstractDAO { private pathToAlarmsEndpoint() { return this.getApiVersion() === 2 ? 'api/v2/alarms' : 'rest/alarms'; } + + /** generate a memo from the given dictionary */ + private toMemo(data: any): OnmsMemo { + if (!data) { + return null; + } + + const memo = new OnmsMemo(); + memo.id = data.id; + memo.author = data.author; + memo.body = data.body; + memo.created = this.toDate(data.created); + memo.updated = this.toDate(data.updated); + return memo; + } + } diff --git a/src/model/OnmsAlarm.ts b/src/model/OnmsAlarm.ts index 315c81088..7e5ed634b 100644 --- a/src/model/OnmsAlarm.ts +++ b/src/model/OnmsAlarm.ts @@ -6,6 +6,7 @@ import {OnmsParm} from './OnmsParm'; import {OnmsServiceType} from './OnmsServiceType'; import {OnmsSeverity} from './OnmsSeverity'; import {OnmsTroubleTicketState} from './OnmsTroubleTicketState'; +import {OnmsMemo} from './OnmsMemo'; /** * Represents an OpenNMS alarm. @@ -75,6 +76,12 @@ export class OnmsAlarm { /** the parameters emitted with this alarm's event */ public parameters: OnmsParm[]; + /** sticky memo - a note associated with this specific alarm instance */ + public sticky: OnmsMemo; + + /** journal memo - a note associated with the reduction key for this alarm */ + public journal: OnmsMemo; + /** the most recent time the event has triggered this alarm */ public get lastEventTime() { if (this.lastEvent && this.lastEvent.time) { diff --git a/src/model/OnmsMemo.ts b/src/model/OnmsMemo.ts new file mode 100644 index 000000000..18aa0eefb --- /dev/null +++ b/src/model/OnmsMemo.ts @@ -0,0 +1,22 @@ +import {Moment} from 'moment'; + +/** + * Represents an OpenNMS memo. + * @module OnmsMemo + */ /** */ +export class OnmsMemo { + /** the memo ID */ + public id: number; + + /** the content of the memo */ + public body: string; + + /** the user who last updated (or created) the memo */ + public author: string; + + /** when the memo was last updated */ + public updated: Moment; + + /** when the memo was created */ + public created: Moment; +} diff --git a/test/dao/AlarmDAO.spec.ts b/test/dao/AlarmDAO.spec.ts index 6e56ac3b8..ddd7d0717 100644 --- a/test/dao/AlarmDAO.spec.ts +++ b/test/dao/AlarmDAO.spec.ts @@ -84,4 +84,11 @@ describe('AlarmDAO with v2 API', () => { expect(alarms.length).toEqual(0); }); }); + it('should make the journal and sticky notes available - AlarmDAO.get(82416)', () => { + return dao.get(82416).then((alarm) => { + expect(alarm.id).toEqual(82416); + expect(alarm.sticky.body).toEqual('sticky'); + expect(alarm.journal.body).toEqual('journal'); + }); + }); }); diff --git a/test/rest/21.0.0/get/api/v2/alarms/82416.json b/test/rest/21.0.0/get/api/v2/alarms/82416.json new file mode 100644 index 000000000..c12152b44 --- /dev/null +++ b/test/rest/21.0.0/get/api/v2/alarms/82416.json @@ -0,0 +1,79 @@ +{ + "id": 82416, + "description": "A problem has been triggered.", + "ifIndex": null, + "uei": "uei.opennms.org/alarms/trigger", + "serviceType": null, + "x733AlarmType": null, + "x733ProbableCause": 0, + "reductionKey": "uei.opennms.org/alarms/trigger:f5:0.0.0.0:HTTPS_POOL", + "ackId": 1, + "stickyMemo": { + "id": 1, + "created": 1499786686515, + "body": "sticky", + "author": "admin", + "updated": 1499786686515 + }, + "reductionKeyMemo": { + "reductionKey": "uei.opennms.org/alarms/trigger:f5:0.0.0.0:HTTPS_POOL", + "id": 2, + "created": 1499786689405, + "body": "journal", + "author": "admin", + "updated": 1499786689405 + }, + "firstEventTime": 1499716228000, + "suppressedUntil": 1499716228000, + "suppressedTime": 1499716228000, + "lastEvent": { + "id": 34, + "ifIndex": null, + "serviceType": null, + "host": "noise", + "uei": "uei.opennms.org/alarms/trigger", + "time": 1499786650000, + "source": "perl_send_event", + "createTime": 1499786650999, + "description": "A problem has been triggered.", + "logMessage": "A problem has been triggered on f5/0.0.0.0/HTTPS_POOL.", + "log": "Y", + "display": "Y", + "severity": "CRITICAL", + "parameters": [{ + "name": "service", + "value": "HTTPS_POOL", + "type": "string" + }, { + "name": "ip", + "value": "0.0.0.0", + "type": "string" + }, { + "name": "node", + "value": "f5", + "type": "string" + }] + }, + "lastEventTime": 1499786650000, + "managedObjectInstance": null, + "managedObjectType": null, + "ossPrimaryKey": null, + "qosAlarmState": null, + "type": 1, + "count": 2, + "severity": "CRITICAL", + "logMessage": "A problem has been triggered on f5/0.0.0.0/HTTPS_POOL.", + "parameters": [{ + "name": "service", + "value": "HTTPS_POOL", + "type": "string" + }, { + "name": "ip", + "value": "0.0.0.0", + "type": "string" + }, { + "name": "node", + "value": "f5", + "type": "string" + }] +} \ No newline at end of file diff --git a/test/rest/MockHTTP21.ts b/test/rest/MockHTTP21.ts index 53561446d..261206bee 100644 --- a/test/rest/MockHTTP21.ts +++ b/test/rest/MockHTTP21.ts @@ -30,6 +30,11 @@ export class MockHTTP21 extends AbstractHTTP { result.type = 'application/json'; return Promise.resolve(result); } + case 'api/v2/alarms/82416': { + const result = OnmsResult.ok(require('./21.0.0/get/api/v2/alarms/82416.json')); + result.type = 'application/json'; + return Promise.resolve(result); + } case 'api/v2/alarms?limit=1000&_s=alarm.id%3D%3D6806': { const result = OnmsResult.ok(require('./21.0.0/get/api/v2/alarms/id.eq.6806.json')); result.type = 'application/json';