Skip to content

Commit

Permalink
#312 WIP Add donut chart
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Oct 17, 2017
1 parent 023b6b0 commit 51b86ae
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 103 deletions.
1 change: 1 addition & 0 deletions ui/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
<script src="scripts/directives/charts/metric-histo-chart.js"></script>
<script src="scripts/directives/compare-to.js"></script>
<script src="scripts/directives/dashboard/donut.js"></script>
<script src="scripts/directives/dashboard/item.js"></script>
<script src="scripts/directives/dateTimePicker.js"></script>
<script src="scripts/directives/dt-picker.js"></script>
<script src="scripts/directives/entityLink.js"></script>
Expand Down
16 changes: 15 additions & 1 deletion ui/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,21 @@ angular.module('thehive', ['ngAnimate', 'ngMessages', 'ngSanitize', 'ui.bootstra
url: 'dashboards/{id}',
templateUrl: 'views/partials/dashboard/view.html',
controller: 'DashboardViewCtrl',
controllerAs: '$vm'
controllerAs: '$vm',
resolve: {
dashboard: function(DashboardSrv, $stateParams, $q) {
var defer = $q.defer();

DashboardSrv.get($stateParams.id)
.then(function(response) {
defer.resolve(response.data);
}, function(err) {
defer.reject(err);
});

return defer.promise;
}
}
})
.state('app.dashboards-edit', {
url: 'dashboards/edit/{id}',
Expand Down
38 changes: 17 additions & 21 deletions ui/app/scripts/controllers/dashboard/DashboardEditCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

angular
.module('theHiveControllers')
.controller('DashboardEditCtrl', function(dashboard) {
.controller('DashboardEditCtrl', function(DashboardSrv, NotificationSrv, dashboard) {
this.dashboard = dashboard;
this.options = {
dashboardAllowedTypes: ['container'],
containerAllowedTypes: ['bar', 'line', 'donut'],
Expand All @@ -16,33 +17,28 @@
}
};

this.toolbox = [
{
id: 1,
type: 'container',
items: []
},
{
id: 2,
type: 'bar'
},
{
id: 3,
type: 'line'
},
{
id: 4,
type: 'donut'
}
];
this.toolbox = DashboardSrv.toolbox;

this.dashboard = JSON.parse(dashboard.definition) || {
this.definition = JSON.parse(dashboard.definition) || {
items: [
{
type: 'container',
items: []
}
]
};

this.saveDashboard = function() {
var copy = _.pick(this.dashboard, 'title', 'description', 'status');
copy.definition = angular.toJson(this.definition);

DashboardSrv.update(this.dashboard.id, copy)
.then(function(response) {
NotificationSrv.log('The dashboard has been successfully updated', 'success');
})
.catch(function(err) {
NotificationSrv.error('DashboardEditCtrl', err.data, err.status);
})
}
});
})();
4 changes: 3 additions & 1 deletion ui/app/scripts/controllers/dashboard/DashboardViewCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

angular
.module('theHiveControllers')
.controller('DashboardViewCtrl', function() {
.controller('DashboardViewCtrl', function(dashboard) {
this.dashboard = dashboard;
this.definition = JSON.parse(this.dashboard.definition) || {};

});
})();
54 changes: 32 additions & 22 deletions ui/app/scripts/controllers/dashboard/DashboardsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@
angular
.module('theHiveControllers')
.controller('DashboardsCtrl', function($scope, $uibModal, PSearchSrv, NotificationSrv, DashboardSrv, AuthenticationSrv) {
//this.dashboards = dashboards.data || [];
this.dashboards = [];
var self = this;

this.dashboards = PSearchSrv('any', 'dashboard', {
scope: $scope,
baseFilter: {
_and: [
{
_not: { status: 'Deleted' }
},
{
_or: [
{ status: 'Shared' },
{ createdBy: AuthenticationSrv.currentUser.id }
]
}
]
},
sort: ['-status'],
loadAll: true
});
// this.dashboards = PSearchSrv('any', 'dashboard', {
// scope: $scope,
// baseFilter: {
// _and: [
// {
// _not: { status: 'Deleted' }
// },
// {
// _or: [
// { status: 'Shared' },
// { createdBy: AuthenticationSrv.currentUser.id }
// ]
// }
// ]
// },
// sort: ['-status'],
// loadAll: true
// });
//

this.load = function() {
DashboardSrv.list()
.then(function(response) {
self.dashboards = response.data;
});
}

this.load();

this.addDashboard = function() {
var modalInstance = $uibModal.open({
Expand Down Expand Up @@ -61,7 +71,7 @@
modalInstance.result.then(function(dashboard) {
return DashboardSrv.create(dashboard);
}).then(function(response) {
self.dashboards.update();
self.load();

NotificationSrv.log('The dashboard has been successfully created', 'success');
}).catch(function(err) {
Expand All @@ -74,8 +84,8 @@
this.deleteDashboard = function(id) {
DashboardSrv.remove(id)
.then(function(response) {
self.dashboards.update();
self.load();

NotificationSrv.log('The dashboard has been successfully removed', 'success');
});
}
Expand Down
3 changes: 2 additions & 1 deletion ui/app/scripts/directives/charts/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
type: '@',
autoload: '=',
options: '=',
refreshOn: '@'
refreshOn: '@',
mode: '@'
},
templateUrl: 'views/directives/charts/chart.html'
};
Expand Down
1 change: 1 addition & 0 deletions ui/app/scripts/directives/charts/donut-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
scope: {
'options': '=',
'autoload': '=',
'mode': '=',
'refreshOn': '@'
},
templateUrl: 'views/directives/charts/donut-chart.html',
Expand Down
96 changes: 86 additions & 10 deletions ui/app/scripts/directives/dashboard/donut.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,91 @@
(function() {
'use strict';
angular.module('theHiveDirectives')
.directive('donut', function() {
return {
scope: {
component: '='
},
templateUrl: 'views/directives/dashboard/donut.html',
link: function(scope, element) {
angular.module('theHiveDirectives').directive('dashboardDonut', function(StatSrv, $state, NotificationSrv) {
return {
restrict: 'E',
scope: {
options: '=',
autoload: '=',
mode: '=',
refreshOn: '@'
},
templateUrl: 'views/directives/dashboard/donut.html',
link: function(scope) {
scope.chart = {};

scope.buildQuery = function() {
var criteria = _.without([scope.options.filter, scope.options.query], null, undefined, '', '*');

return criteria.length === 1 ? criteria[0] : { _and: criteria };
};

scope.load = function() {
var query = scope.buildQuery();

var statConfig = {
query: query,
objectType: scope.options.entity,
field: scope.options.field,
sort: scope.options.sort,
limit: scope.options.limit
};

StatSrv.getPromise(statConfig).then(
function(response) {
var keys = _.without(_.keys(response.data), 'count');
var columns = keys.map(function(key) {
return [key, response.data[key].count];
});

scope.chart = {
data: {
columns: columns,
type: 'donut',
names: scope.options.names || {},
colors: scope.options.colors || {},
onclick: function(d) {
var criteria = [{ _type: scope.options.entity }, { _field: scope.options.field, _value: d.id }];

if (scope.options.query && scope.options.query !== '*') {
criteria.push(scope.options.query);
}

var searchQuery = {
_and: criteria
};

$state.go('app.search', {
q: Base64.encode(angular.toJson(searchQuery))
});
}
},
donut: {
title: 'Total: ' + response.data.count,
label: {
format: function(value) {
return value;
}
}
}
};
},
function(err) {
NotificationSrv.error('donutChart', err.data, err.status);
}
);
};

if (scope.autoload === true) {
scope.load();
}

if (!_.isEmpty(scope.refreshOn)) {
scope.$on(scope.refreshOn, function(event, queryFn) {
scope.options.query = queryFn(scope.options);
scope.load();
});
}
};
});
}
};
});
})();
20 changes: 20 additions & 0 deletions ui/app/scripts/directives/dashboard/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function() {
'use strict';
angular.module('theHiveDirectives')
.directive('dashboardItem', function() {
return {
restrict: 'E',
scope: {
type: '@',
autoload: '=',
options: '=',
refreshOn: '@',
mode: '@'
},
templateUrl: 'views/directives/dashboard/item.html',
link: function(scope, element) {

}
};
});
})();
48 changes: 46 additions & 2 deletions ui/app/scripts/services/DashboardSrv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
'use strict';
angular.module('theHiveServices').service('DashboardSrv', function(localStorageService, $q, $http) {
angular.module('theHiveServices').service('DashboardSrv', function(localStorageService, $q, AuthenticationSrv, $http) {
var baseUrl = './api/dashboard';

this.defaultDashboard = {
Expand All @@ -12,6 +12,37 @@
]
};

this.toolbox = [
{
id: 1,
type: 'container',
items: []
},
{
id: 2,
type: 'bar',
options: {

}
},
{
id: 3,
type: 'line',
options: {

}
},
{
id: 4,
type: 'donut',
options: {
title: null,
entity: null,
field: null
}
}
];

this.create = function(dashboard) {
return $http.post(baseUrl, dashboard);
}
Expand All @@ -23,7 +54,20 @@
this.list = function() {
return $http.post(baseUrl + '/_search', {
range: 'all',
query: {}
sort: ['-status', '-updatedAt', '-createdAt'],
query: {
_and: [
{
_not: { status: 'Deleted' }
},
{
_or: [
{ status: 'Shared' },
{ createdBy: AuthenticationSrv.currentUser.id }
]
}
]
}
});
}

Expand Down
Loading

0 comments on commit 51b86ae

Please sign in to comment.