Commit 0b41aa79 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

create query from inputs

parent a8f076e0
Loading
Loading
Loading
Loading
+22 −21
Original line number Diff line number Diff line
@@ -17,56 +17,55 @@ if (FnordMetric.views === undefined) {
}

FnordMetric.util.MetricPreviewWidget = function() {

  function updateResult(inputs) {
    console.log("update result");
  }
  var metric;

  function updateEventHandler(elems) {
    var inputs = {
      "show" : "Value",
      "show" : null,
      "aggregation" : {
        "time" : null,
        "step" : null
        },
      "seconds" : 10,
      "date" : null,
      "time" : null,
      "group_by" : []
    }

    elems.show.addEventListener('change', function() {
      inputs.show = this.value;
      updateResult(inputs);
      var value = (this.value == "Value") ? null : this.value;
      inputs.show = value;
      FnordMetric.util.createQuery(inputs, metric);
    }, false);

    elems.aggregation.time.addEventListener('change', function() {
      inputs.aggregation.time = this.value;
      updateResult(inputs);
      FnordMetric.util.createQuery(inputs, metric);
    }, false);

    elems.aggregation.step.addEventListener('change', function() {
      inputs.aggregation.step = this.value;
      updateResult(inputs);
      FnordMetric.util.createQuery(inputs, metric);
    }, false);

    elems.seconds.addEventListener('change', function() {
      inputs.seconds = this.value;
      updateResult(inputs);
      //inputs.last_seconds = this.value;
      FnordMetric.util.createQuery(inputs, metric);
    }, false);

    elems.group_by.map(function(column) {
      column.addEventListener('click', function(e) {
        e.preventDefault();
        //check if already set --> on /off
        var c = this.innerText;
        var index = inputs.group_by.indexOf(c);
        if (index == -1) {
          inputs.group_by.push(this.innerText);
        updateResult(inputs);
        } else {
          inputs.group_by.splice(index, 1);
        }
        FnordMetric.util.createQuery(inputs, metric);
      });
    }, false);

    //timespan & datepicker --> date



    //last_ seconds & timespan & datepicker --> date
  }

  function renderElems(elem, columns) {
@@ -155,10 +154,12 @@ FnordMetric.util.MetricPreviewWidget = function() {
    elem.appendChild(next_timespan);

    updateEventHandler(elems);
    //createQuery(

  }

  function render(elem, metric) {
  function render(elem, metricname) {
    metric = metricname;
    var columns = [];
    FnordMetric.httpGet("/metrics", function(r) {
      if (r.status == 200) {
+64 −0
Original line number Diff line number Diff line
@@ -363,3 +363,67 @@ FnordMetric.util.filterStringArray = function(strings, filter) {
}


/*
  Inputs Object with default values
  inputs = {
    "show" : null,
    "aggregation" : {
      "time" : null,
      "step" : null
      },
    "time" : null,
    "group_by" : []
  }
*/
FnordMetric.util.createQuery = function(inputs, metric) {
  var query = "";
  var timewindow = null;
  var where = null;

  var draw = "DRAW Linechart AXIS BOTTOM AXIS LEFT; ";
  var select = "SELECT 'mymetric' AS series, time AS x, ";
  var from = " FROM " + metric;
  var show;
  var hasAggr;

  if (inputs.show == null) {
    show = "value as y";
    hasAggr = false;
  } else {
    hasAggr = true;
    show = (inputs.show + "(value) as y");
  }

  query += select + show + from;

  /* check for time --> where clause and add to query */

  if (hasAggr) {
    /* group over timewindow needs a time and step info */
    if (inputs.aggregation.time != null &&
        inputs.aggregation.step != null) {
      timewindow = 
        " GROUP OVER TIMEWINDOW(" +
        inputs.aggregation.time + ", " +
        inputs.aggregation.step + ")";

      query += timewindow;
    }

    if (inputs.group_by.length > 0) {
      var columns = inputs.group_by.join(", ");
      if (timewindow != null) {
      /* Group over timewindow clause */
        query += " BY " + columns;
      } else {
      /* GROUP BY */
        query += " GROUP BY " + columns;
      }
    }
  }

  query += ";";
  return query;
}