Skip to content

Commit

Permalink
Add timezone offset to dates (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
juankaromo authored and Jesús Ángel committed May 17, 2019
1 parent a306f84 commit 4b8736f
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 109 deletions.
4 changes: 3 additions & 1 deletion public/controllers/agent/agents-preview.js
Expand Up @@ -35,7 +35,8 @@ export class AgentsPreviewController {
wzTableFilter,
commonData,
wazuhConfig,
$window
$window,
timeService
) {
this.$scope = $scope;
this.genericReq = genericReq;
Expand All @@ -49,6 +50,7 @@ export class AgentsPreviewController {
this.wazuhConfig = wazuhConfig;
this.errorInit = false;
this.$window = $window;
this.timeService = timeService;
}

/**
Expand Down
37 changes: 27 additions & 10 deletions public/controllers/agent/agents.js
Expand Up @@ -58,7 +58,8 @@ export class AgentsController {
wzTableFilter,
$mdDialog,
groupHandler,
wazuhConfig
wazuhConfig,
timeService
) {
this.$scope = $scope;
this.$location = $location;
Expand All @@ -76,6 +77,7 @@ export class AgentsController {
this.$mdDialog = $mdDialog;
this.groupHandler = groupHandler;
this.wazuhConfig = wazuhConfig;
this.timeService = timeService;

// Config on-demand
this.$scope.isArray = Array.isArray;
Expand Down Expand Up @@ -267,6 +269,7 @@ export class AgentsController {
this.$scope.isString = item => typeof item === 'string';
this.$scope.hasSize = obj =>
obj && typeof obj === 'object' && Object.keys(obj).length;
this.$scope.offsetTimestamp = (text, time) => this.offsetTimestamp(text, time);
this.$scope.switchConfigTab = (
configurationTab,
sections,
Expand Down Expand Up @@ -545,7 +548,7 @@ export class AgentsController {
(((agentInfo || {}).data || {}).data || {}).status ||
this.$scope.agent.status;
}
} catch (error) {} // eslint-disable-line
} catch (error) { } // eslint-disable-line

try {
this.$scope.showSyscheckFiles = false;
Expand Down Expand Up @@ -580,7 +583,7 @@ export class AgentsController {
if (tab === 'syscollector')
try {
await this.loadSyscollector(this.$scope.agent.id);
} catch (error) {} // eslint-disable-line
} catch (error) { } // eslint-disable-line
if (tab === 'configuration') {
this.$scope.switchConfigurationTab('welcome');
} else {
Expand Down Expand Up @@ -708,7 +711,7 @@ export class AgentsController {
{}
);
netifaceResponse = ((resultNetiface || {}).data || {}).data || false;
} catch (error) {} // eslint-disable-line
} catch (error) { } // eslint-disable-line

// This API call may fail so we put it out of Promise.all
let netaddrResponse = false;
Expand All @@ -720,7 +723,7 @@ export class AgentsController {
);
netaddrResponse =
((resultNetaddrResponse || {}).data || {}).data || false;
} catch (error) {} // eslint-disable-line
} catch (error) { } // eslint-disable-line

// Before proceeding, syscollector data is an empty object
this.$scope.syscollector = {};
Expand All @@ -736,7 +739,7 @@ export class AgentsController {
this.$scope.syscollector = {
hardware:
typeof hardwareResponse === 'object' &&
Object.keys(hardwareResponse).length
Object.keys(hardwareResponse).length
? { ...hardwareResponse }
: false,
os:
Expand Down Expand Up @@ -779,23 +782,23 @@ export class AgentsController {

try {
data[0] = await this.apiReq.request('GET', `/agents/${id}`, {});
} catch (error) {} //eslint-disable-line
} catch (error) { } //eslint-disable-line

try {
data[1] = await this.apiReq.request(
'GET',
`/syscheck/${id}/last_scan`,
{}
);
} catch (error) {} //eslint-disable-line
} catch (error) { } //eslint-disable-line

try {
data[2] = await this.apiReq.request(
'GET',
`/rootcheck/${id}/last_scan`,
{}
);
} catch (error) {} //eslint-disable-line
} catch (error) { } //eslint-disable-line

const result = data.map(item => ((item || {}).data || {}).data || false);

Expand Down Expand Up @@ -891,6 +894,20 @@ export class AgentsController {
this.$scope.editGroup = !!!this.$scope.editGroup;
this.$scope.$applyAsync();
}

/**
* This adds timezone offset to a given date
* @param {String} binding_text
* @param {String} date
*/
offsetTimestamp = (text, time) => {
try {
return text + this.timeService.offset(time);
} catch (error) {
return `${text}${time} (UTC)`;
}
}

/**
* Navigate to the groups of an agent
* @param {*} agent
Expand Down Expand Up @@ -964,7 +981,7 @@ export class AgentsController {
);
this.errorHandler.info(
`Policy monitoring scan launched successfully on agent ${
this.$scope.agent.id
this.$scope.agent.id
}`,
''
);
Expand Down
21 changes: 15 additions & 6 deletions public/controllers/management/logs.js
Expand Up @@ -21,7 +21,15 @@ export class LogsController {
* @param {*} appState
* @param {*} wzTableFilter
*/
constructor($scope, apiReq, errorHandler, csvReq, appState, wzTableFilter) {
constructor(
$scope,
apiReq,
errorHandler,
csvReq,
appState,
wzTableFilter,
timeService
) {
this.$scope = $scope;
this.apiReq = apiReq;
this.errorHandler = errorHandler;
Expand All @@ -32,6 +40,7 @@ export class LogsController {
this.type_log = 'all';
this.category = 'all';
this.sortFilter = false;
this.timeService = timeService;
}

/**
Expand Down Expand Up @@ -61,18 +70,18 @@ export class LogsController {

parseLogsToText(logs) {
let result = '';
logs.forEach(function(log, idx) {
logs.forEach((log, idx) => {
if (log) {
result = result.concat(
`${log.timestamp} ${log.tag} ${(log.level || '').toUpperCase()}: ${
log.description
}`
`${this.timeService.offset(log.timestamp)} ${log.tag} ${(
log.level || ''
).toUpperCase()}: ${log.description}`
);
if (idx !== logs.length - 1) {
result = result.concat('\n');
}
}
});
}, this);
return result;
}
/**
Expand Down
11 changes: 10 additions & 1 deletion public/controllers/misc/reporting.js
Expand Up @@ -17,7 +17,7 @@ export class ReportingController {
* @param {*} errorHandler
* @param {*} genericReq
*/
constructor($scope, errorHandler, genericReq, $window) {
constructor($scope, errorHandler, genericReq, $window, timeService) {
this.$scope = $scope;
this.$window = $window;
this.errorHandler = errorHandler;
Expand All @@ -28,6 +28,7 @@ export class ReportingController {
this.currentPage = 0;
this.items = [];
this.gap = 0;
this.timeService = timeService;
}

/**
Expand All @@ -46,6 +47,14 @@ export class ReportingController {
this.groupToPages();
}

offsetTimestamp(time) {
try {
return this.timeService.offset(time);
} catch (error) {
return `${time} (UTC)`;
}
}

/**
* This delete a report with a given name
*/
Expand Down
18 changes: 16 additions & 2 deletions public/directives/wz-table/lib/parse-value.js
Expand Up @@ -23,7 +23,13 @@ const checkIfArray = item => {
return typeof item === 'object' ? splitArray(item) : item === 0 ? '0' : item;
};

export function parseValue(key, item, instancePath, $sce = null) {
export function parseValue(
key,
item,
instancePath,
$sce = false,
timeService = false
) {
if (
(key === 'event' || (key.value && key.value === 'event')) &&
instancePath.includes('rootcheck') &&
Expand All @@ -45,6 +51,15 @@ export function parseValue(key, item, instancePath, $sce = null) {
}
}
}
if (key.offset && timeService) {
const date = (item || {})[key.value];
if (!item[`${key.value}offset`]) {
item[`${key.value}offset`] = date;
}
if (date) {
item[key.value] = timeService.offset(item[`${key.value}offset`]);
}
}
if (key === 'state' && instancePath.includes('processes')) {
return ProcessEquivalence[item.state] || '-';
}
Expand All @@ -54,7 +69,6 @@ export function parseValue(key, item, instancePath, $sce = null) {
) {
return '-';
}

if ((item || {})[key] === '(null)') {
return '-';
}
Expand Down
5 changes: 3 additions & 2 deletions public/directives/wz-table/wz-table-directive.js
Expand Up @@ -53,7 +53,8 @@ app.directive('wzTable', function() {
groupHandler,
rulesetHandler,
wazuhConfig,
$sce
$sce,
timeService
) {
$scope.showColumns = false;
$scope.scapepath = $scope.path.split('/').join('');
Expand Down Expand Up @@ -268,7 +269,7 @@ app.directive('wzTable', function() {
};

$scope.parseValue = (key, item) =>
parseValue(key, item, instance.path, $sce);
parseValue(key, item, instance.path, $sce, timeService);

/**
* On controller loads
Expand Down
2 changes: 2 additions & 0 deletions public/services/index.js
Expand Up @@ -27,6 +27,7 @@ import { uiModules } from 'ui/modules';
import { GroupHandler } from './group-handler';
import { RulesetHandler } from './ruleset-handler';
import { ConfigHandler } from './config-handler';
import { TimeService } from './time-service';
import { CheckDaemonsStatus } from './check-daemon-status';

const app = uiModules.get('app/wazuh', []);
Expand All @@ -45,4 +46,5 @@ app
.service('groupHandler', GroupHandler)
.service('rulesetHandler', RulesetHandler)
.service('configHandler', ConfigHandler)
.service('timeService', TimeService)
.service('checkDaemonsStatus', CheckDaemonsStatus);
28 changes: 28 additions & 0 deletions public/services/time-service.js
@@ -0,0 +1,28 @@
/*
* Wazuh app - Time and date functions
* Copyright (C) 2015-2019 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/

export class TimeService {
/**
* Returns given date adding the timezone offset
* @param {string} date Date
*/
offset(d) {
try {
const date = new Date(d);
const offset = new Date().getTimezoneOffset();
const offsetTime = new Date(date.getTime() - offset * 60000);
return offsetTime.toLocaleString('en-ZA').replace(',', '');
} catch (error) {
throw new Error(error);
}
}
}
2 changes: 1 addition & 1 deletion public/templates/agents-prev/agents-prev.html
Expand Up @@ -138,7 +138,7 @@
<md-card-content>
<div layout="row">
<wz-table custom-columns="true" flex path="'/agents'"
keys="[{value: 'id', width: '75px'},'name','ip','status','group','os.name','os.version','version', 'dateAdd', 'lastKeepAlive']"
keys="[{value: 'id', width: '75px'},'name','ip','status','group','os.name','os.version','version', {value: 'dateAdd', offset: true}, {value: 'lastKeepAlive', offset: true}]"
allow-click="true" row-sizes="[17,15,13]"></wz-table>
</div>
<div layout="row" layout-align="end center">
Expand Down
4 changes: 1 addition & 3 deletions public/templates/agents/agents-sca.html
Expand Up @@ -96,9 +96,7 @@
</td>
<td class="euiTableRowCell">
<div class="euiTableCellContent euiTableCellContent--overflowingContent no-wrap">
<span>{{policy.end_scan
|| '-'
}}</span>
<span ng-bind="policy.end_scan ? offsetTimestamp('', policy.end_scan) : '-'"></span>
</div>
</td>
<td class="euiTableRowCell">
Expand Down
8 changes: 4 additions & 4 deletions public/templates/agents/agents-syscollector.html
Expand Up @@ -62,7 +62,7 @@
<md-card-content ng-if="syscollector.netiface && syscollector.netiface.items.length">
<span class="wz-headline-title">
<react-component name="EuiIcon" props="{type:'indexMapping'}" /> Network interfaces</span>
<span class="color-grey pull-right">Last scan: {{syscollector.netiface.items[0].scan.time}}</span>
<span class="color-grey pull-right" ng-bind="offsetTimestamp('Last scan: ', syscollector.netiface.items[0].scan.time)"></span>
<md-divider class="wz-margin-top-10"></md-divider>

<table class="table table-striped table-condensed table-layout-fixed">
Expand Down Expand Up @@ -113,7 +113,7 @@
<md-card-content ng-if="syscollector.ports && syscollector.ports.items.length">
<span class="wz-headline-title">
<react-component name="EuiIcon" props="{type:'inputOutput'}" /> Network ports</span>
<span class="color-grey pull-right">Last scan: {{syscollector.ports.items[0].scan.time}}</span>
<span class="color-grey pull-right" ng-bind="offsetTimestamp('Last scan: ', syscollector.ports.items[0].scan.time)"></span>
<md-divider class="wz-margin-top-10"></md-divider>
<wz-table flex ng-if="agent && agent.os && agent.os.platform === 'windows'"
path="'/syscollector/' + agent.id + '/ports'" row-sizes="[6]"
Expand Down Expand Up @@ -150,7 +150,7 @@
<md-card-content>
<span class="wz-headline-title">
<react-component name="EuiIcon" props="{type:'apps'}" /> Packages</span>
<span class="color-grey pull-right">Last scan: {{syscollector.packagesDate}}</span>
<span class="color-grey pull-right" ng-bind="offsetTimestamp('Last scan: ', syscollector.packagesDate)"></span>
<md-divider class="wz-margin-top-10"></md-divider>
<div layout="row"
class="wz-margin-top-10 euiFlexGroup euiFlexGroup--alignItemsCenter euiFormControlLayout__childrenWrapper">
Expand Down Expand Up @@ -197,7 +197,7 @@
<md-card-content>
<span class="wz-headline-title">
<react-component name="EuiIcon" props="{type:'console'}" /> Processes</span>
<span class="color-grey pull-right">Last scan: {{syscollector.processesDate}}</span>
<span class="color-grey pull-right" ng-bind="offsetTimestamp('Last scan: ', syscollector.processesDate)"></span>
<md-divider class="wz-margin-top-10"></md-divider>
<div layout="row"
class="wz-margin-top-10 euiFlexGroup euiFlexGroup--alignItemsCenter euiFormControlLayout__childrenWrapper">
Expand Down

0 comments on commit 4b8736f

Please sign in to comment.