Created
November 13, 2019 21:20
-
-
Save replay/f98c139eef487164d7a2cb13ff877028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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