Skip to content

Support back-shift using e.g. "bshift_1_days" #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const units = [
'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond', 'quarter',
'y', 'M', 'w', 'd', 'h', 'm', 's', 'ms', 'Q',
];
const shift_re = new RegExp(`[Aa][Ss]\\s+"shift_([0-9]+)_(${units.join('|')})"`);
const shift_re = new RegExp(`[Aa][Ss]\\s+"(b|)shift_([0-9]+)_(${units.join('|')})"`);
const from = /(time\s*>=?\s*)([0-9]+)(ms)/;
const to = /(time\s*<=?\s*)([0-9]+)(ms)/;
const from_rel = /(time\s*>=?\s*)(now\(\)\s*-\s*)([0-9]+)([usmhdw])/;
Expand All @@ -26,7 +26,8 @@ function fix_query_time(q, reg, count, unit) {
const match = q.match(reg);
if (match) {
const time = moment(parseInt(match[2], 10));
time.subtract(count, unit);
//time.subtract(count, unit);
time.add(count, unit);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Why direction was changed here?

return q.replace(reg, match[1] + time.valueOf() + match[3]);
}
return q;
Expand Down Expand Up @@ -121,8 +122,9 @@ function forward(path, req, res) {
req.proxyShift = {};
}
req.proxyShift[idx] = {
back: (match.splice(1,1)[0] == "b"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not like this match.splice approach, it's is much better to just change all match indexes rather then manipulate match array in place

count: parseInt(match[1], 10),
unit: match[2]
unit: match[2],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coma will brake node 6 compatibility

};
deb_rewrite("<-- " + q);
let select = fix_query_time(q, from, parseInt(match[1], 10), match[2]);
Expand Down Expand Up @@ -168,7 +170,11 @@ function intercept(rsp, data, req, res) {
series: result.series.map(serie => {
return Object.assign({}, serie, { values: serie.values.map(item => {
const time = moment(item[0]);
time.add(req.proxyShift[idx].count, req.proxyShift[idx].unit);
if(req.proxyShift[idx].back) {
time.subtract(req.proxyShift[idx].count, req.proxyShift[idx].unit);
} else {
time.add(req.proxyShift[idx].count, req.proxyShift[idx].unit);
}
return [ time.valueOf(), item[1]];
})});
})
Expand Down
19 changes: 19 additions & 0 deletions influxdb-timeshift-proxy.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description=Add timeshift feature to influxdb for grafana users with mathematics between metrics feature
Documentation=https://github.com/maxsivanov/influxdb-timeshift-proxy
After=network-online.target
Requires=influxdb.service

[Service]
User=influxdb
Group=influxdb
Environment=INFLUXDB=127.0.0.1:8086
EnvironmentFile=-/etc/default/influxdb-timeshift-proxy
WorkingDirectory=/opt/influxdb-timeshift-proxy
ExecStart=/usr/bin/npm run start

Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=influxd.service