Commit 1c587036 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

test and refactor parseQueryString

parent 09019d22
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
fnordmetric-webui.js
tests/
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ build:
	      fnordmetric-webui-embedpopup.js \
	      fnordmetric-webui-autocomplete.js \
	      fnordmetric-webui-datepicker.js \
	      fnordmetric-webui-unittests.js \
	      codemirror.js \
	      codemirror-sql.js \
	) > fnordmetric-webui.js
+1 −0
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ FnordMetric.WebUI = function() {
  };

  function openUrl(raw_url, push_state) {
    FnordMetric.UnitTests();
    var url = FnordMetric.util.parseQueryString(raw_url);
    var query_params = url["query_params"];

+55 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Laura Schlimmer
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
if (FnordMetric === undefined) {
  FnordMetric = {};
}

if (FnordMetric.util === undefined) {
  FnordMetric.util = {};
}

/* Unit Tests for FnordMetric.util functions */
FnordMetric.UnitTests = function() {
  console.log("make unit tests");
  var results = {
    total : 0,
    bad : 0
  }

  var testParseQueryString = function() {
    function test(querystr, expected) {
      var result = FnordMetric.util.parseQueryString(querystr);
      result = JSON.stringify(result);
      expected = JSON.stringify(expected);
      if (result != expected) {
        results.bad++;
        console.log("Testing parseQueryString expected "
          + expected + ", but was " + result);
      }
    }
    test("metric_list?metric=/osx/load_avg_15m&view=value", 
        {path : "metric_list",
          query_params : {
            innerView : "metric",
            innerViewValue : "/osx/load_avg_15m",
            view : "value"}});

    test("metric_list", {path : "metric_list", query_params : {}});
    test("query_playground?sql_query=select%20*%20from%20http_status_codes%3B",
      {path : "query_playground",
        query_params : {
          innerView : "sql_query",
          innerViewValue : decodeURIComponent("select%20*%20from%20http_status_codes%3B")}});
    test("", {path : "", query_params: {}});
    test("undefined", {path: "", query_params:{}});
  }();

}
+15 −26
Original line number Diff line number Diff line
@@ -9,27 +9,6 @@
 * <http://www.gnu.org/licenses/>.
 */

/**
 *
 * TODOS:
 *
 * Query Playground:
 *  - "embed this query" opens up a popup with html/js/ruby snippets
 *  - prevent reload/navigation to other page (body onunload)
*  - stretch: display a [fake] loading bar
 *  - nice to have: represent current chart and table, view in url --> renderResultPane
 *
 * Metric list view:
 *  - write meaningful error messages
 *
 *  - fix menuitems
 *  - fix resize tooltip, vertical split
 *  - wrong URL: redirect and display message 
 *  - fix back and for (push and pop states)
 *
 */


if (FnordMetric === undefined) {
  FnordMetric = {};
}
@@ -38,12 +17,18 @@ if (FnordMetric.util === undefined) {
  FnordMetric.util = {};
}

/**
  * extracts the params from the url
  * @param qstr like metric_list?metric=/osx/load_avg_15m&view=value
  */
FnordMetric.util.parseQueryString = function(qstr) {
  var path;
  var query_params = {};

  if (qstr.indexOf("?") >= 0) {
    path = qstr.substr(0, qstr.indexOf("?"))
    path = (qstr.substr(0, qstr.indexOf("?")) != "undefined")? 
      qstr.substr(0, qstr.indexOf("?")) : "";


    var params_str = qstr.substr(qstr.indexOf("?") + 1);
    var raw_params = params_str.split('&');
@@ -54,13 +39,17 @@ FnordMetric.util.parseQueryString = function(qstr) {
    query_params.innerViewValue = decodeURIComponent(param[1]);

    for (var i = 1; i < raw_params.length; i++) {
      var param = raw_params[i].split('=');
      var param = (raw_params[i].split('=') != "undefined") ? 
        raw_params[i].split('=') : "";
      if (param[0] != "undefined") {
        query_params[decodeURIComponent(param[0])] =
          decodeURIComponent(param[1]);
           (param[1] != "undefined") ? 
           decodeURIComponent(param[1]) : "";
      }
    }

  } else {
    path = qstr;
    path = qstr != "undefined" ? qstr : "";
  }
  return {
    "path": path,