Multiplies each data point in the series by the given factor. @since 2.3
| 26 | * @since 2.3 |
| 27 | */ |
| 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 |
| 76 | * integers if both the data point and scale are integers. |
| 77 | * @param points The data points to factor |
| 78 | * @param scale_factor The factor to multiply by |
| 79 | * @return The resulting data points |
| 80 | */ |
| 81 | private DataPoints scale(final DataPoints points, final double scale_factor) { |
| 82 | // TODO(cl) - Using an array as the size function may not return the exact |
| 83 | // results and we should figure a way to avoid copying data anyway. |
| 84 | final List<DataPoint> dps = new ArrayList<DataPoint>(); |
| 85 | final boolean scale_is_int = (scale_factor == Math.floor(scale_factor)) && |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…