Permalink
Browse files

feat(dao): Add capability to fetch values from alarm v2 endpoint (JS-22)

  • Loading branch information...
mvrueden committed Aug 24, 2017
1 parent 0ea573f commit 637443f7f53db15d2bbe8a997e59e5caac317094
Showing with 86 additions and 15 deletions.
  1. +18 −0 src/api/SearchProperty.ts
  2. +53 −15 src/dao/AbstractDAO.ts
  3. +15 −0 src/dao/IValueProvider.ts
View
@@ -1,4 +1,5 @@
import {SearchPropertyType} from './SearchPropertyType';
import {IValueProvider} from '../dao/IValueProvider';
/**
* Represents a query search property.
@@ -19,4 +20,21 @@ export class SearchProperty {
/** the values if any */
public values: any;
/** The value provider */
private valueProvider: IValueProvider;
constructor(valueProvider: IValueProvider) {
this.valueProvider = valueProvider;
}
/**
* Hook to gather all the values for this property.
*
* @param options Some additional options, to for example restrict the values or limit the query.
* @returns {Promise<any>}
*/
public async findValues(options: any): Promise<any> {
return this.valueProvider.findValues(this.id, options);
}
}
View
@@ -22,6 +22,7 @@ const moment = require('moment');
/** @hidden */
// tslint:disable-next-line
import {Moment} from 'moment';
import {IValueProvider} from './IValueProvider';
/**
* An abstract data access layer API, meant to (somewhat) mirror the DAO interfaces
@@ -32,7 +33,7 @@ import {Moment} from 'moment';
* @param K the ID/key type (number, string, etc.)
* @param T the model type (OnmsAlarm, OnmsEvent, etc.)
*/
export abstract class AbstractDAO<K, T> {
export abstract class AbstractDAO<K, T> implements IValueProvider {
/**
* The [[IOnmsHTTP]] implementation to use internally when making DAO requests.
* @hidden
@@ -127,19 +128,8 @@ export abstract class AbstractDAO<K, T> {
return this.getOptions().then((opts) => {
opts.headers.accept = 'application/json';
return this.http.get(this.searchPropertyPath(), opts).then((result) => {
let data = result.data;
if (this.getCount(data) > 0 && data.searchProperty) {
data = data.searchProperty;
} else {
data = [];
}
if (!Array.isArray(data)) {
throw new OnmsError('Expected an array of search properties but got "' +
(typeof data) + '" instead: ' + this.searchPropertyPath());
}
const searchProperties = data.map((prop) => {
const searchProperties = this.parseResultList(result, 'searchProperty',
this.searchPropertyPath(), (prop) => {
return this.toSearchProperty(prop);
});
PropertiesCache.put(this, searchProperties);
@@ -151,11 +141,59 @@ export abstract class AbstractDAO<K, T> {
return Promise.resolve(PropertiesCache.get(this));
}
/**
* Finds the values for the given propertyId, if it exists.
*
* @param {string} propertyId The propertyId to find the values for
* @param options Some additional options. May be implementer dependent, such as limit, or value restrictions
* @returns {Promise<any>} A promise containing the values.
*/
public async findValues(propertyId: string, options?: any): Promise<any> {
return this.searchProperty(propertyId).then((property) => {
return this.getOptions().then((opts) => {
const path = this.searchPropertyPath() + '/' + property.id;
opts.headers.accept = 'application/json';
if (options) {
Object.assign(opts, options);
}
return this.http.get(path, opts).then((result) => {
return this.parseResultList(result, 'value', path, (value) => value);
});
});
});
}
/**
* The path to retrieve search properties for this DAO.
*/
protected abstract searchPropertyPath(): string;
/**
* Fetches the data from the result and verfifes that the <code>dataFieldName</code> exists in the data property.
* If it does not exist, an exception is thrown.
*
* @param result The result to fetch the data from
* @param dataFieldName The property name (basically <code>result.data[dataFieldName]</code>.
* @param path The path where the result was fetched from. This is for error handling
* @param mapCallbackFunction Callback function to convert each entry from <code>result.data[dataFieldName]</code>.
*/
protected parseResultList(result: any, dataFieldName: string, path: string, mapCallbackFunction: any): any {
let data = result.data;
if (this.getCount(data) > 0 && data[dataFieldName]) {
data = data[dataFieldName];
} else {
data = [];
}
if (!Array.isArray(data)) {
throw new OnmsError('Expected an array but got "' + (typeof data) + '" instead: ' + path);
}
if (mapCallbackFunction) {
return data.map(mapCallbackFunction);
}
return data;
}
/**
* A convenience method to make it easy for implementers to extract the count
* (or totalCount) values from response data.
@@ -235,7 +273,7 @@ export abstract class AbstractDAO<K, T> {
return null;
}
const prop = new SearchProperty();
const prop = new SearchProperty(this);
prop.id = data.id;
prop.name = data.name;
prop.orderBy = !!data.orderBy;
View
@@ -0,0 +1,15 @@
/**
* Interface to provide values to a certain property id.
*/
export interface IValueProvider {
/**
* Finds the values for the given propertyId, if it exists.
*
* @param {string} propertyId The propertyId to find the values for
* @param options Some additional options. May be implementer dependent, such as limit, or value restrictions
* @returns {Promise<any>} A promise containing the values.
*/
findValues(propertyId: string, options?: any): Promise<any>;
}

0 comments on commit 637443f

Please sign in to comment.