| 28 | public class Scale implements Expression { |
| 29 | |
| 30 | @Override |
| 31 | public DataPoints[] evaluate(final TSQuery data_query, |
| 32 | final List<DataPoints[]> query_results, final List<String> params) { |
| 33 | if (data_query == null) { |
| 34 | throw new IllegalArgumentException("Missing time series query"); |
| 35 | } |
| 36 | if (query_results == null || query_results.isEmpty()) { |
| 37 | return new DataPoints[]{}; |
| 38 | } |
| 39 | if (params == null || params.isEmpty()) { |
| 40 | throw new IllegalArgumentException("Missing scaling factor"); |
| 41 | } |
| 42 | |
| 43 | double scale_factor = 0; // zero is fine, if useless *shrug* |
| 44 | final String factor = params.get(0); |
| 45 | if (factor != null && factor.matches("^[-0-9\\.]+$")) { |
| 46 | try { |
| 47 | scale_factor = Double.parseDouble(factor); |
| 48 | } catch (NumberFormatException nfe) { |
| 49 | throw new IllegalArgumentException( |
| 50 | "Invalid parameter, must be an integer or floating point", nfe); |
| 51 | } |
| 52 | } else { |
| 53 | throw new IllegalArgumentException("Unparseable scale factor value: " |
| 54 | + scale_factor); |
| 55 | } |
| 56 | |
| 57 | int num_results = 0; |
| 58 | for (DataPoints[] results: query_results) { |
| 59 | num_results += results.length; |
| 60 | } |
| 61 | |
| 62 | final DataPoints[] results = new DataPoints[num_results]; |
| 63 | int ix = 0; |
| 64 | // one or more sub queries (m=...&m=...&m=...) |
| 65 | for (final DataPoints[] sub_query_result : query_results) { |
| 66 | // group bys (m=sum:foo{host=*}) |
| 67 | for (final DataPoints dps : sub_query_result) { |
| 68 | results[ix++] = scale(dps, scale_factor); |
| 69 | } |
| 70 | } |
| 71 | return results; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Multiplies each data point in the series by the scale factor, maintaining |