Skip to content

Commit

Permalink
#1579 Update the date filter field to support dynamic dates
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani authored and To-om committed Nov 13, 2020
1 parent 382d717 commit 7ce1379
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 75 deletions.
57 changes: 0 additions & 57 deletions frontend/app/scripts/controllers/alert/AlertEventCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@

self.loading = true;

self.pagination = {
pageSize: 10,
currentPage: 1,
filter: '',
data: []
};

self.similarityFilters = {};
self.similaritySorts = ['-startDate', '-similarArtifactCount', '-similarIocCount', '-iocCount'];
self.currentSimilarFilter = '';
self.similarCasesStats = [];

self.customFieldsCache = CustomFieldsSrv;

self.counts = {
Expand All @@ -51,7 +39,6 @@
AlertingSrv.get(eventId).then(function(data) {
self.event = data;
self.loading = false;
self.initSimilarCasesFilter(self.event.similarCases);

self.dataTypes = _.countBy(self.event.artifacts, function(attr) {
return attr.dataType;
Expand Down Expand Up @@ -196,50 +183,6 @@
$uibModalInstance.dismiss();
};

self.initSimilarCasesFilter = function(data) {
var stats = {
'Open': 0
};

// Init the stats object
_.each(_.without(_.keys(CaseResolutionStatus), 'Duplicated'), function(key) {
stats[key] = 0;
});

_.each(data, function(item) {
if(item.status === 'Open') {
stats[item.status] = stats[item.status] + 1;
} else {
stats[item.resolutionStatus] = stats[item.resolutionStatus] + 1;
}
});

var result = [];
_.each(_.keys(stats), function(key) {
result.push({
key: key,
value: stats[key]
});
});

self.similarCasesStats = result;
};

self.filterSimilarCases = function(filter) {
self.currentSimilarFilter = filter;
if(filter === '') {
self.similarityFilters = {};
} else if(filter === 'Open') {
self.similarityFilters = {
status: filter
};
} else {
self.similarityFilters = {
resolutionStatus: filter
};
}
};

self.copyId = function(id) {
clipboard.copyText(id);
};
Expand Down
33 changes: 32 additions & 1 deletion frontend/app/scripts/directives/dashboard/filter-editor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
'use strict';
angular.module('theHiveDirectives').directive('filterEditor', function($q, AuthenticationSrv, UserSrv) {
angular.module('theHiveDirectives').directive('filterEditor', function($q, AuthenticationSrv, UserSrv, UtilsSrv) {
return {
restrict: 'E',
scope: {
Expand All @@ -10,6 +10,37 @@
},
templateUrl: 'views/directives/dashboard/filter-editor.html',
link: function(scope) {
scope.dateOperator = {
custom: 'Custom',
today: 'Today',
last7days: 'Last 7 days',
last30days: 'Last 30 days',
last3months: 'Last 3 months',
last6months: 'Last 6 months',
lastyear: 'Last year'
};

scope.setDateFilterOperator = function(filter, operator) {
operator = operator || 'custom';

var dateRange = UtilsSrv.getDateRange(operator);

if(operator === 'custom') {
filter.value = {
operator: operator,
from: dateRange.from,
to: dateRange.to
};
} else {
filter.value = {
operator: operator,
from: null,
to: null
};
}

};

scope.editorFor = function(filter) {
if (filter.type === null) {
return;
Expand Down
33 changes: 28 additions & 5 deletions frontend/app/scripts/filters/filter-value.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
(function() {
'use strict';

angular.module('theHiveFilters').filter('filterValue', function() {
angular.module('theHiveFilters').filter('filterValue', function(UtilsSrv) {
return function(value) {
if (angular.isArray(value)) {
return _.map(value, function(item) {
return item.label || item.text;
}).join(', ');
} else if(angular.isObject(value) && value.from !== undefined && value.to !== undefined) {
var result = [];
if(value.from !== null) {
result.push('From: ' + moment(value.from).hour(0).minutes(0).seconds(0).format('MM/DD/YY HH:mm'));

result.push({
custom: 'Custom',
today: 'Today',
last7days: 'Last 7 days',
last30days: 'Last 30 days',
last3months: 'Last 3 months',
last6months: 'Last 6 months',
lastyear: 'Last year'
}[value.operator] || 'Custom');

var start, end;

if(value.operator && value.operator !== 'custom') {
var dateRange = UtilsSrv.getDateRange(value.operator);

start = dateRange.from;
end = dateRange.to;
} else {
start = value.from;
end = value.to;
}

if(start !== null) {
result.push('From: ' + moment(start).hour(0).minutes(0).seconds(0).format('MM/DD/YY HH:mm'));
}

if(value.to !== null) {
result.push('To: ' + moment(value.to).hour(23).minutes(59).seconds(59).format('MM/DD/YY HH:mm'));
if(end !== null) {
result.push('To: ' + moment(end).hour(23).minutes(59).seconds(59).format('MM/DD/YY HH:mm'));
}

return result.join(', ');
Expand Down
26 changes: 17 additions & 9 deletions frontend/app/scripts/services/common/QueryBuilderSrv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
'use strict';
angular.module('theHiveServices').service('QueryBuilderSrv', function() {
angular.module('theHiveServices').service('QueryBuilderSrv', function(UtilsSrv) {
var self = this;

this._buildQueryFromDefaultFilter = function(fieldDef, filter) {
Expand Down Expand Up @@ -116,19 +116,27 @@

this._buildQueryFromDateFilter = function(fieldDef, filter) {
var value = filter.value,
operator = filter.value.operator || 'custom',
start,
end;

if(value.from && value.from !== null) {
start = _.isString(value.from) ? (new Date(value.from)).getTime() : value.from.getTime();
} else {
start = null;
}
if(operator === 'custom') {
if(value.from && value.from !== null) {
start = _.isString(value.from) ? (new Date(value.from)).getTime() : value.from.getTime();
} else {
start = null;
}

if(value.to && value.to !== null) {
end = _.isString(value.to) ? (new Date(value.to)).setHours(23, 59, 59, 999) : value.to.getTime();
if(value.to && value.to !== null) {
end = _.isString(value.to) ? (new Date(value.to)).setHours(23, 59, 59, 999) : value.to.getTime();
} else {
end = null;
}
} else {
end = null;
var dateRange = UtilsSrv.getDateRange(operator);

start = dateRange.from.getTime();
end = dateRange.to.getTime();
}

if (start !== null && end !== null) {
Expand Down
32 changes: 32 additions & 0 deletions frontend/app/scripts/services/common/UtilsSrv.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,38 @@
});
return parsedQuery ? parsedQuery.substr(paramName.length + 1) : undefined;
}
},

getDateRange: function(operator) {
var from,
to = moment();

switch(operator) {
case 'last7days':
from = moment().subtract(7, 'days');
break;
case 'last30days':
from = moment().subtract(30, 'days');
break;
case 'last3months':
from = moment().subtract(3, 'months');
break;
case 'last6months':
from = moment().subtract(6, 'months');
break;
case 'lastyear':
from = moment().subtract(1, 'years');
break;
case 'today':
default:
from = moment();
break;
}

return {
from: from.hour(0).minutes(0).seconds(0).toDate(),
to: to.hour(23).minutes(59).seconds(59).toDate()
};
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@
</div>

<div class="case-date">
<span uib-tooltip="{{item.case._createdAt | showDate}}" tooltip-popup-delay="500" tooltip-placement="bottom">{{item.case._createdAt | shortDate}}</span>
<a href ng-click="$cmp.addFilterValue('_createdAt', item.case._createdAt)">
<span uib-tooltip="{{item.case._createdAt | showDate}}" tooltip-popup-delay="500" tooltip-placement="bottom">{{item.case._createdAt | shortDate}}</span>
</a>
</div>

<div class="case-similarity">
Expand Down
21 changes: 19 additions & 2 deletions frontend/app/views/directives/dashboard/filter-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,27 @@
</div>

<div ng-switch-when="date" class="row">
<div class="col-xs-6 pl-0">
<div class="ph-0" uib-dropdown ng-class="{
'col-xs-2': !filter.value.operator || filter.value.operator === 'custom',
'col-xs-4': filter.value.operator && filter.value.operator !== 'custom'
}">
<button type="button" class="btn btn-block btn-default dropdown-toggle" uib-dropdown-toggle>
{{dateOperator[filter.value.operator || 'custom']}} <span class="caret"></span>
</button>
<ul class="dropdown-menu" uib-dropdown-menu>
<li><a href ng-click="setDateFilterOperator(filter, 'custom')">Custom</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'today')">Today</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'last7days')">Last 7 days</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'last30days')">Last 30 days</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'last3months')">Last 3 months</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'last6months')">Last 6 months</a></li>
<li><a href ng-click="setDateFilterOperator(filter, 'lastyear')">Last year</a></li>
</ul>
</div>
<div class="col-xs-5 pl-xxxs" ng-show="!filter.value.operator || filter.value.operator === 'custom'">
<dt-picker date="filter.value.from" placeholder="from"></dt-picker>
</div>
<div class="col-xs-6 pl-0">
<div class="col-xs-5 pl-0" ng-show="!filter.value.operator || filter.value.operator === 'custom'">
<dt-picker date="filter.value.to" placeholder="to"></dt-picker>
</div>
</div>
Expand Down

0 comments on commit 7ce1379

Please sign in to comment.