Skip to content

Instantly share code, notes, and snippets.

@replay
Created November 13, 2019 21:20
Show Gist options
  • Save replay/f98c139eef487164d7a2cb13ff877028 to your computer and use it in GitHub Desktop.
Save replay/f98c139eef487164d7a2cb13ff877028 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import time
import re
import sys
pattern = re.compile(os.getenv('METRIC_PATTERN', False) or '.*')
def duration_into_seconds(duration):
unit = duration[-1:]
number = int(duration[:-1])
if unit == 'y':
return number * 365 * 24 * 3600
elif duration[-1:] == 'm':
return number * 30 * 24 * 3600
elif duration[-1:] == 'd':
return number * 24 * 3600
raise(Exception('Invalid duration {duration}'.format(duration=duration)))
now = int(time.time())
ts1 = now - duration_into_seconds(os.getenv('TS1', '1y'))
ts2 = now - duration_into_seconds(os.getenv('TS2', '1m'))
ts1_count, ts2_count = 0, 0
for line in sys.stdin:
metric, timestampStr = line.rstrip().split(' ')
if not pattern.match(metric):
continue
try:
timestamp = int(timestampStr)
except ValueError:
print('Invalid timestamp: {ts}'.format(ts=timestampStr))
continue
if timestamp >= ts1:
ts1_count += 1
if timestamp >= ts2:
ts2_count += 1
print(
'ts1: {ts1}, ts2: {ts2}, difference: {diff}'
.format(ts1=ts1_count, ts2=ts2_count, diff=ts1_count-ts2_count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment