Skip to content

Commit

Permalink
Merge pull request #15 from ataccama/master
Browse files Browse the repository at this point in the history
Fix Timestamp format to be compatible with Grafana
  • Loading branch information
Jef Spaleta authored Apr 24, 2020
2 parents cd49e99 + 752fa8e commit aba5a13
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/sensu/sensu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Sensu {
* @param options the options specifying the query's request
*/
static query(datasource: any, options: QueryOptions) {
const { method, url, namespace, limit, forceAccessTokenRefresh } = options;
const {method, url, namespace, limit, forceAccessTokenRefresh} = options;
if (forceAccessTokenRefresh) {
delete datasource.instanceSettings.tokens;
}
Expand All @@ -44,7 +44,7 @@ export default class Sensu {

return this._authenticate(datasource)
.then(() => this._request(datasource, method, fullUrl))
.catch(() => this.query(datasource, { ...options, forceAccessTokenRefresh: true }));
.catch(() => this.query(datasource, {...options, forceAccessTokenRefresh: true}));
}

/**
Expand Down Expand Up @@ -126,11 +126,27 @@ export default class Sensu {

/**
* Is called when the request is ending successfully. In case of a 401 error, the request is not throwing an error but returning no result object.
* It also modifies standard timestamp fields of Sensu Events (UNIX) to be compatible with Grafana's resolution (UNIX_MS)
*
* @param result the request's result object
*/
static _handleRequestResult(result: any) {
if (result) {
if (Array.isArray(result.data)) {
result.data.forEach(o => {
if (o.timestamp) {
o.timestamp = o.timestamp * 1000;
}
if (o.check) {
o.check.executed = o.check.executed * 1000;
o.check.issued = o.check.issued * 1000;
o.check.last_ok = o.check.last_ok * 1000;
}
if (o.entity) {
o.entity.last_seen = o.entity.last_seen * 1000;
}
});
}
return result;
} else {
throw {
Expand Down

0 comments on commit aba5a13

Please sign in to comment.