Last active
December 12, 2015 01:38
-
-
Save maio/4692021 to your computer and use it in GitHub Desktop.
Whisper/Graphite retentions string parsing (precision:period,precision:period,...)
This file contains 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
func parse_time($str) { | |
# Mapping from whisper.py | |
my %unit_multiplier = ( | |
s => 1, | |
m => 60, | |
h => 3600, | |
d => 86400, | |
w => 86400 * 7, | |
y => 86400 * 365 | |
); | |
my $unit = $str =~ s/[0-9]+//r; | |
my $number = $str =~ s/[a-z]+//r; | |
return int($number) if not $unit; | |
return int($number * $unit_multiplier{$unit}); | |
} | |
# parse_time('10s') -> 10 | |
# parse_time('1h') -> 3600 | |
# ... | |
func first(@array) { shift @array } | |
func get_precision($retentions, $ago) { | |
my %period2precision = ( | |
reverse map { parse_time($_) } split(/:|,/, $retentions)); | |
return $period2precision{ | |
first sort { $a <=> $b } grep { $_ > $ago } keys %period2precision}; | |
} | |
# get_precision('1s:60m,10s:7d', $less_than_hour) -> 1 | |
# get_precision('1s:60m,10s:7d', $between_hour_and_week) -> 10 | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment