harness-drone/server/static/scripts/services/logs.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-04-08 22:43:59 +00:00
'use strict';
(function () {
/**
* The LogService provides access to build
* log data using REST API calls.
*/
function LogService($http, $window) {
/**
* Gets a task logs.
*
* @param {string} Name of the repository.
* @param {number} Number of the build.
* @param {number} Number of the task.
*/
this.get = function(repoName, number, step) {
return $http.get('/api/repos/'+repoName+'/logs/'+number+'/'+step);
2015-04-08 22:43:59 +00:00
};
2015-04-30 02:57:43 +00:00
var callback,
websocket,
token = localStorage.getItem('access_token');
this.subscribe = function (repoName, number, step, _callback) {
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/logs/', repoName, '/', number, '/', step, '?access_token=', token].join('');
websocket = new WebSocket(route);
websocket.onmessage = function (event) {
if (callback !== undefined) {
callback(event.data);
}
};
websocket.onclose = function (event) {
console.log('logs websocket closed');
};
};
this.unsubscribe = function () {
callback = undefined;
if (websocket !== undefined) {
websocket.close();
websocket = undefined;
}
};
2015-04-08 22:43:59 +00:00
}
angular
.module('drone')
.service('logs', LogService);
})();