Skip to content

Commit

Permalink
Merge pull request #80 from Jiri-Kremser/inventory-events
Browse files Browse the repository at this point in the history
Inventory events
  • Loading branch information
mtho11 committed Jan 13, 2016
2 parents 980a4b8 + feb9ff8 commit 03ef856
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"hawkular-ui"
],
"main": [
"dist/hawkular-ui-service.js"
"dist/hawkular-ui-service.js",
"dist/defs/hawkRest.d.ts"
],
"ignore": [
"*",
Expand Down
26 changes: 20 additions & 6 deletions gulp/scripts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
'use strict';

import gulp from 'gulp';
import gulp from 'gulp';
import tslintRules from '../tslint.json';
import merge from 'merge2';

const paths = gulp.paths;

const $ = require('gulp-load-plugins')({
pattern: ['gulp-*', 'del']
});

const config = {
main: '.',
ts: ['src/**/*.ts'],
tsProject: $.typescript.createProject({
target: 'ES5',
module: 'commonjs',
declaration: true,
removeComments: true,
noExternalResolve: false
})
};


gulp.task('tslint', function () {
gulp.src([paths.src + '/hawkRest.ts', paths.src + '/hawkRest-*.ts'])
.pipe($.tslint({
Expand All @@ -21,20 +35,20 @@ gulp.task('scripts', function () {

const license = tslintRules.rules['license-header'][1];

return gulp.src([paths.src + '/hawkRest.ts', paths.src + '/hawkRest-*.ts'])
.pipe($.typescript({
removeComments: true
}))
const tsResult = gulp.src([paths.src + '/hawkRest.ts', paths.src + '/hawkRest*.ts'])
.pipe($.typescript(config.tsProject))
.on('error', function handleError(err) {
console.error(err.toString());
this.emit('end');
})
});
const jsPipe = tsResult
.pipe($.concat('hawkular-ui-service.js'))
.pipe($.header(license))
.pipe(gulp.dest(paths.dist + '/'))
.pipe($.concat('hawkular-ui-service.min.js'))
//.pipe($.uglify())
.pipe(gulp.dest(paths.dist + '/'));
return merge([tsResult.dts.pipe(gulp.dest(paths.dist + '/defs')), jsPipe]);
});

gulp.task('clean', function (done) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"gulp-tslint": "3.3.1",
"gulp-typescript": "2.10.0",
"gulp-uglify": "1.5.1",
"merge2": "0.3.6",
"karma-jasmine": "0.3.6",
"karma-phantomjs-launcher": "0.2.1",
"lodash": "3.10.1",
Expand Down
49 changes: 39 additions & 10 deletions src/rest/hawkRest-inventory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ module hawkularRest {
[new RegExp('.+/inventory/.+/resources/.+%2F.+', 'i'), new RegExp('.+/inventory/.+/resources/.+%252F.+', 'i')]);

_module.config(['$httpProvider', 'inventoryInterceptURLS', function($httpProvider, inventoryInterceptURLS) {
var SLASH = '/';
const SLASH = '/';

var ENCODED_SLASH = '%2F';
var ENCODED_SLASH_RE = new RegExp(ENCODED_SLASH, 'gi');
const ENCODED_SLASH = '%2F';
const ENCODED_SLASH_RE = new RegExp(ENCODED_SLASH, 'gi');

var DOUBLE_ENCODED_SLASH = '%252F';
var DOUBLE_ENCODED_SLASH_RE = new RegExp(DOUBLE_ENCODED_SLASH, 'gi');
const DOUBLE_ENCODED_SLASH = '%252F';
const DOUBLE_ENCODED_SLASH_RE = new RegExp(DOUBLE_ENCODED_SLASH, 'gi');

$httpProvider.interceptors.push(function ($q) {
return {
Expand Down Expand Up @@ -75,18 +75,19 @@ module hawkularRest {
return this;
};

this.$get = ['$resource', '$location', function($resource, $location) {
this.$get = ['$resource', '$location', '$rootScope', '$log', function($resource, $location, $rootScope, $log) {

// If available, used pre-configured values, otherwise use values from current browser location of fallback to
// defaults
this.setProtocol(this.protocol || $location.protocol() || 'http');
this.setHost(this.host || $location.host() || 'localhost');
this.setPort(this.port || $location.port() || 8080);

var prefix = this.protocol + '://' + this.host + ':' + this.port;
var inventoryUrlPart = '/hawkular/inventory';
var url = prefix + inventoryUrlPart;
var factory: any = {};
const prefix = this.protocol + '://' + this.host + ':' + this.port;
const inventoryUrlPart = '/hawkular/inventory';
const url = prefix + inventoryUrlPart;
const wsUrl = 'ws://' + this.host + ':' + this.port + inventoryUrlPart + '/ws/events';
let factory: any = {};


// helper methods for making the code DRY
Expand Down Expand Up @@ -279,6 +280,34 @@ module hawkularRest {
// Get whole graph (read-only)
factory.Graph = $resource(url + '/graph');

// Listen to inventory events
factory.Events = (tenantId) => {
return {
listen: (handler: IWebSocketHandler) => {
const ws = new WebSocket(wsUrl + `?tenantId=${tenantId}`);

ws.onmessage = (event) => {
const eventData = JSON.parse(event.data);
if (handler && handler.onmessage) {
handler.onmessage(eventData);
} else {
$log.log('ws: received event');
$log.log(eventData);
}
};

ws.onopen = (handler && handler.onopen) || ((event) => $log.log('ws: Listening on inventory events..'));

ws.onclose = (handler && handler.onclose) || ((event) => {
$log.warn('ws: Stop listening on inventory events.');
$rootScope.$broadcast('WebSocketClosed', event.reason);
});

ws.onerror = (handler && handler.onerror) || ((event) => $log.log('ws: Error: ' + event));
}
};
};

return factory;
}];

Expand Down
9 changes: 9 additions & 0 deletions src/rest/hawkRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ module hawkularRest {

export var _module = angular.module('hawkular.services', ['ngResource']);

// here comes type definitions and interfaces that can be reused by consumers of this module
// these types/ifaces can define a contract
export interface IWebSocketHandler {
onmessage?(json: any): void;
onopen?(event: any): void;
onclose?(event: any): void;
onerror?(event: any): void;
}

}

0 comments on commit 03ef856

Please sign in to comment.