Last active
December 13, 2015 18:18
-
-
Save jathanism/4954213 to your computer and use it in GitHub Desktop.
Shon testing.
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
| # shon.py - Shon test for Graphite debugging... | |
| def eval_graphite_data(data, seconds): | |
| """Get the most recent correct value from the data""" | |
| _name = "eval_graphite_data" | |
| print _name, ' DATA:', data | |
| print _name, ' SECONDS:', seconds | |
| sample_period = int(data.split('|')[0].split(',')[-1]) | |
| all_data_points = data.split('|')[-1].split(',') | |
| print _name, ' SAMPLE_PERIOD:', sample_period | |
| print _name, 'ALL_DATA_POINTS:', all_data_points | |
| # Evaluate what graphite returned, should either be a float, or None | |
| # First, if the number of seconds of data we want to examine is smaller or | |
| # equals the graphite sample period, just grab the latest data point. | |
| # If that data point is None, grab the one before it. | |
| # If that is None too, return 0.0. | |
| if seconds <= sample_period: | |
| if eval(all_data_points[-1]): | |
| print '1st: check1' | |
| data_value = float(all_data_points[-1]) | |
| elif eval(all_data_points[-2]): | |
| print '1st: check2' | |
| data_value = float(all_data_points[-2]) | |
| else: | |
| print '1st: check3 (default)' | |
| data_value = 0.0 | |
| print _name, '1st: DATA_VALUE:', data_value | |
| else: | |
| # Second, if we requested more than on graphite sample period, work out how | |
| # many sample periods we wanted (python always rounds division *down*) | |
| data_points = (seconds/sample_period) | |
| print _name, ' DATA_POINTS:', data_points | |
| data_set = [ float(x) for x in all_data_points[-data_points:] | |
| if eval(x) ] | |
| print _name, ' DATA_SET:', data_set | |
| if data_set: | |
| print '2nd: check1' | |
| data_value = float( sum(data_set) / len(data_set) ) | |
| else: | |
| print '2nd: check2 (default)' | |
| data_value = 0.0 | |
| print _name, '2nd: DATA_VALUE:', data_value | |
| return data_value | |
| if __name__ == '__main__': | |
| #data = 'dropPercent,1360860120,1360860420,60|0.0,0.0,0.0,0.0,0.0' | |
| #seconds = 60 | |
| data = 'dropPercent,1360860120,1360860420,60|1.0,0.0,2.0,0.0,0.0' | |
| seconds = 300 | |
| retval = eval_graphite_data(data, seconds) | |
| print 'RESULT:', retval | |
| # Output | |
| """ | |
| eval_graphite_data DATA: dropPercent,1360860120,1360860420,60|0.0,0.0,0.0,0.0,0.0 | |
| eval_graphite_data SECONDS: 60 | |
| eval_graphite_data SAMPLE_PERIOD: 60 | |
| eval_graphite_data ALL_DATA_POINTS: ['0.0', '0.0', '0.0', '0.0', '0.0'] | |
| eval_graphite_data 1st: DATA_VALUE: 0.0 | |
| RESULT: 0.0 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment