exometer_slide: normalise last_two
If a slide has been idle for some time then sees a single sample the interval between the samples is much greater than the slide interval which would result in an articifially low rate.
This commit is contained in:
		
							parent
							
								
									69f530b873
								
							
						
					
					
						commit
						d697327f88
					
				|  | @ -294,21 +294,24 @@ first_max(_, X) -> X. | |||
| %% @doc Returns the newest 2 elements on the sample | ||||
| last_two(#slide{buf1 = [{TS, Evt} = H1, {_, drop} | _], interval = Interval}) -> | ||||
|     [H1, {TS - Interval, Evt}]; | ||||
| last_two(#slide{buf1 = [H1, H2 | _]}) -> | ||||
| last_two(#slide{buf1 = [H1, H2_0 | _], interval = Interval}) -> | ||||
|     H2 = adjust_timestamp(H1, H2_0, Interval), | ||||
|     [H1, H2]; | ||||
| last_two(#slide{buf1 = [H1], buf2 = [H2 | _]}) -> | ||||
| last_two(#slide{buf1 = [H1], buf2 = [H2_0 | _], | ||||
|                 interval = Interval}) -> | ||||
|     H2 = adjust_timestamp(H1, H2_0, Interval), | ||||
|     [H1, H2]; | ||||
| last_two(#slide{buf1 = [H1], buf2 = []}) -> | ||||
|     [H1]; | ||||
| last_two(#slide{buf1 = [], buf2 = [{TS, Evt} = H1, {_, drop} | _], interval = Interval}) -> | ||||
|     [H1, {TS - Interval, Evt}]; | ||||
| last_two(#slide{buf1 = [], buf2 = [H1, H2 | _]}) -> | ||||
|     [H1, H2]; | ||||
| last_two(#slide{buf1 = [], buf2 = [H1]}) -> | ||||
|     [H1]; | ||||
| last_two(_) -> | ||||
|     []. | ||||
| 
 | ||||
| adjust_timestamp({TS1, _}, {TS2, V2}, Interval) -> | ||||
|     case TS1 - TS2 > Interval of | ||||
|         true -> {TS1 - Interval, V2}; | ||||
|         false -> {TS2, V2} | ||||
|     end. | ||||
| 
 | ||||
| -spec last(slide()) -> value() | undefined. | ||||
| last(#slide{total = T}) when T =/= undefined -> | ||||
|     T; | ||||
|  |  | |||
|  | @ -27,6 +27,7 @@ groups() -> | |||
|     [ | ||||
|      {parallel_tests, [parallel], [ | ||||
|                   incremental_add_element_basics, | ||||
|                   last_two_normalises_old_sample_timestamp, | ||||
|                   incremental_last_two_returns_last_two_completed_samples, | ||||
|                   incremental_sum, | ||||
|                   incremental_sum_stale, | ||||
|  | @ -110,6 +111,17 @@ incremental_add_element_basics(_Config) -> | |||
|     % contains single element with incremented value | ||||
|     [{Then, {2}}] = exometer_slide:to_list(Then, S2). | ||||
| 
 | ||||
| last_two_normalises_old_sample_timestamp(_Config) -> | ||||
|     Now = 0, | ||||
|     S0 = exometer_slide:new(Now, 10, [{incremental, true}, | ||||
|                                       {interval, 100}]), | ||||
| 
 | ||||
|     S1 = exometer_slide:add_element(100, {1}, S0), | ||||
|     S2 = exometer_slide:add_element(500, {1}, S1), | ||||
| 
 | ||||
|     [{500, {2}}, {400, {1}}] = exometer_slide:last_two(S2). | ||||
| 
 | ||||
| 
 | ||||
| incremental_last_two_returns_last_two_completed_samples(_Config) -> | ||||
|     Now = exometer_slide:timestamp(), | ||||
|     S0 = exometer_slide:new(Now, 10, [{incremental, true}, | ||||
|  | @ -121,7 +133,7 @@ incremental_last_two_returns_last_two_completed_samples(_Config) -> | |||
|     S1 = exometer_slide:add_element(Now100, {1}, S0), | ||||
|     S2 = exometer_slide:add_element(Now200, {1}, S1), | ||||
|     S3 = exometer_slide:add_element(Now + 210, {1}, S2), | ||||
|     %% to_list is empty | ||||
| 
 | ||||
|     [{Now200, {2}}, {Now100, {1}}] = exometer_slide:last_two(S3). | ||||
| 
 | ||||
| incremental_sum(_Config) -> | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue