All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.opentsdb.query.expression.SumSeries Maven / Gradle / Ivy

Go to download

OpenTSDB is a distributed, scalable Time Series Database (TSDB) written on top of HBase. OpenTSDB was written to address a common need: store, index and serve metrics collected from computer systems (network gear, operating systems, applications) at a large scale, and make this data easily accessible and graphable.

There is a newer version: 2.4.1
Show newest version
// This file is part of OpenTSDB.
// Copyright (C) 2015  The OpenTSDB Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see .
package net.opentsdb.query.expression;

import java.util.List;

import net.opentsdb.core.DataPoints;
import net.opentsdb.core.TSDB;
import net.opentsdb.core.TSQuery;
import net.opentsdb.query.expression.VariableIterator.SetOperator;

/**
 * Performs a UNION set join on x metric query results and returns the results.
 */
public class SumSeries implements Expression {
  /** The TSDB used for UID to name lookups */
  final TSDB tsdb;
  
  /**
   * Default ctor.
   * @param tsdb The TSDB used for UID to name lookups
   */
  public SumSeries(final TSDB tsdb) {
    this.tsdb = tsdb;
  }
  
  @Override
  public DataPoints[] evaluate(final TSQuery data_query, 
      final List query_results, final List params) {
    if (data_query == null) {
      throw new IllegalArgumentException("Missing time series query");
    }
    if (query_results == null || query_results.isEmpty()) {
      return new DataPoints[]{};
    }
    
    if (query_results.size() < 2 || query_results.size() > 26) {
      throw new IllegalArgumentException("Must have 2 to 26 series, got " + 
          query_results.size() + " instead");
    }
    
    final StringBuilder buf = new StringBuilder();
    char v = 'a';
    for (int i = 0; i < query_results.size(); i++) {
      buf.append(v++);
      if (i < query_results.size() - 1) {
        buf.append(" + ");
      }
    }
    System.out.println("Expression: [" + buf.toString() + "]");
    final ExpressionIterator expression = new ExpressionIterator("sumSeries", 
        buf.toString(), SetOperator.UNION, false, false);
    v = 'a';
    
    for (final DataPoints[] dps : query_results) {
      final TimeSyncedIterator it = new TimeSyncedIterator(
          Character.toString(v++), null, dps);
      expression.addResults(it.getId(), it);
    }
    expression.compile();
    
    final DataPoints[] results = new DataPoints[expression.values().length];
    for (int i = 0; i < expression.values().length; i++) {
      results[i] = new EDPtoDPS(tsdb, i, expression);
    }
    return results;
  }

  @Override
  public String writeStringField(final List query_params, 
      final String inner_expression) {
    return "sumSeries(" + inner_expression + ")";
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy