| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | public class TimeShift implements Expression { |
| 25 | /** |
| 26 | * in place modify of TsdbResult array to increase timestamps by timeshift |
| 27 | * |
| 28 | * @param data_query |
| 29 | * @param results |
| 30 | * @param params |
| 31 | * @return |
| 32 | */ |
| 33 | @Override |
| 34 | public DataPoints[] evaluate(TSQuery data_query, List<DataPoints[]> results, List<String> params) { |
| 35 | //not 100% sure what to do here -> do I need to think of the case where I have no data points |
| 36 | if (results == null || results.isEmpty()) { |
| 37 | return new DataPoints[]{}; |
| 38 | } |
| 39 | if (params == null || params.isEmpty()) { |
| 40 | throw new IllegalArgumentException("Need amount of timeshift to perform timeshift"); |
| 41 | } |
| 42 | |
| 43 | String param = params.get(0); |
| 44 | if (param == null || param.length() == 0) { |
| 45 | throw new IllegalArgumentException("Invalid timeshift='" + param + "'"); |
| 46 | } |
| 47 | |
| 48 | param = param.trim(); |
| 49 | |
| 50 | long timeshift = -1; |
| 51 | if (param.startsWith("'") && param.endsWith("'")) { |
| 52 | timeshift = parseParam(param); |
| 53 | } else { |
| 54 | throw new RuntimeException("Invalid timeshift parameter: eg '10min'"); |
| 55 | } |
| 56 | |
| 57 | if (timeshift <= 0) { |
| 58 | throw new RuntimeException("timeshift <= 0"); |
| 59 | } |
| 60 | |
| 61 | return performShift(results.get(0), timeshift); |
| 62 | } |
| 63 | |
| 64 | private static Boolean timeshiftIsInt(final long timeshift) { |
| 65 | return (timeshift == Math.floor(timeshift)) && |
| 66 | !Double.isInfinite(timeshift); |
| 67 | } |
| 68 | |
| 69 | DataPoints[] performShift(DataPoints[] inputPoints, long timeshift) { |
| 70 | DataPoints[] outputPoints = new DataPoints[inputPoints.length]; |
| 71 | for (int n = 0; n < inputPoints.length; n++) { |
| 72 | outputPoints[n] = shift(inputPoints[n], timeshift); |
| 73 | } |
| 74 | return outputPoints; |
| 75 | } |
| 76 | |
| 77 | long parseParam(String param) { |
| 78 | char[] chars = param.toCharArray(); |
| 79 | int tuIndex = 0; |
| 80 | for (int c = 1; c < chars.length; c++) { |
| 81 | if (Character.isDigit(chars[c])) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…