Generate tabular console view from JSON data.

This commit is contained in:
Julius Volz 2013-07-24 02:18:49 +02:00
parent 665512f5c7
commit 9f07f8677a
5 changed files with 54 additions and 30 deletions

View File

@ -24,7 +24,7 @@ import (
type MetricsService struct { type MetricsService struct {
gorest.RestService `root:"/api/" consumes:"application/json" produces:"application/json"` gorest.RestService `root:"/api/" consumes:"application/json" produces:"application/json"`
query gorest.EndPoint `method:"GET" path:"/query?{expr:string}&{json:string}" output:"string"` query gorest.EndPoint `method:"GET" path:"/query?{expr:string}&{as_text:string}" output:"string"`
queryRange gorest.EndPoint `method:"GET" path:"/query_range?{expr:string}&{end:int64}&{range:int64}&{step:int64}" output:"string"` queryRange gorest.EndPoint `method:"GET" path:"/query_range?{expr:string}&{end:int64}&{range:int64}&{step:int64}" output:"string"`
metrics gorest.EndPoint `method:"GET" path:"/metrics" output:"string"` metrics gorest.EndPoint `method:"GET" path:"/metrics" output:"string"`

View File

@ -37,7 +37,7 @@ func (serv MetricsService) setAccessControlHeaders(rb *gorest.ResponseBuilder) {
rb.AddHeader("Access-Control-Expose-Headers", "Date") rb.AddHeader("Access-Control-Expose-Headers", "Date")
} }
func (serv MetricsService) Query(expr string, formatJson string) string { func (serv MetricsService) Query(expr string, asText string) string {
exprNode, err := rules.LoadExprFromString(expr) exprNode, err := rules.LoadExprFromString(expr)
if err != nil { if err != nil {
return ast.ErrorToJSON(err) return ast.ErrorToJSON(err)
@ -48,7 +48,8 @@ func (serv MetricsService) Query(expr string, formatJson string) string {
rb := serv.ResponseBuilder() rb := serv.ResponseBuilder()
serv.setAccessControlHeaders(rb) serv.setAccessControlHeaders(rb)
var format ast.OutputFormat var format ast.OutputFormat
if formatJson != "" { // BUG(julius): Use Content-Type negotiation.
if asText == "" {
format = ast.JSON format = ast.JSON
rb.SetContentType(gorest.Application_Json) rb.SetContentType(gorest.Application_Json)
} else { } else {

View File

@ -107,9 +107,8 @@ input[title=*]:hover:after {
display: inline; display: inline;
} }
.console { .console_table {
white-space: pre; margin-top: 5px;
overflow: scroll;
} }
.ui-widget { .ui-widget {

View File

@ -293,26 +293,22 @@ Prometheus.Graph.prototype.submitQuery = function() {
self.queryForm.find("input[name=end]").val(endDate); self.queryForm.find("input[name=end]").val(endDate);
if (self.queryXhr) { if (self.queryXhr) {
self.queryXhr.abort() self.queryXhr.abort();
} }
var url var url;
var data var success;
if (self.tab[0] == self.graphTab[0]) { if (self.tab[0] == self.graphTab[0]) {
url = self.queryForm.attr("action") url = self.queryForm.attr("action");
data = self.queryForm.serialize() success = function(json, textStatus) { self.handleGraphResponse(json, textStatus); };
dataType = "json"
success = function(json, textStatus) { self.handleGraphResponse(json, textStatus) }
} else { } else {
url = '/api/query' url = "/api/query";
data = self.expr.serialize() success = function(text, textStatus) { self.handleConsoleResponse(text, textStatus); };
dataType = "text"
success = function(text, textStatus) { self.handleConsoleResponse(text, textStatus) }
} }
self.queryXhr = $.ajax({ self.queryXhr = $.ajax({
method: self.queryForm.attr("method"), method: self.queryForm.attr("method"),
url: url, url: url,
dataType: dataType, dataType: "json",
data: self.queryForm.serialize(), data: self.queryForm.serialize(),
success: success, success: success,
error: function(xhr, resp) { error: function(xhr, resp) {
@ -507,20 +503,39 @@ Prometheus.Graph.prototype.handleGraphResponse = function(json, textStatus) {
self.updateGraph(true); self.updateGraph(true);
} }
Prometheus.Graph.prototype.handleConsoleResponse = function(text, textStatus) { Prometheus.Graph.prototype.handleConsoleResponse = function(data, textStatus) {
var self = this; var self = this;
var body = "<table class=\"table-striped console_table\"></table>"; self.consoleTab.removeClass("reload");
self.consoleTab.removeClass('reload');
self.consoleTab.html(body);
var elements = text.split("\n"); var tBody = self.consoleTab.find(".console_table tbody");
var table = $("#console_table"); tBody.empty();
table.find("tr:gt(0)").remove();
for (var i = 0; i < elements.length; i++) { switch(data.Type) {
var e = "<tr><td>" + elements[i] + "</td></tr>"; case "vector":
table.append(e); for (var i = 0; i < data.Value.length; i++) {
var v = data.Value[i];
var tsName = self.metricToTsName(v.Metric);
tBody.append("<tr><td>" + tsName + "</td><td>" + v.Value + "</td></tr>")
}
break;
case "matrix":
for (var i = 0; i < data.Value.length; i++) {
var v = data.Value[i];
var tsName = self.metricToTsName(v.Metric);
var valueText = "";
for (var j = 0; j < v.Values.length; j++) {
valueText += v.Values[j].Value + " @" + v.Values[j].Timestamp + "<br/>";
}
tBody.append("<tr><td>" + tsName + "</td><td>" + valueText + "</td></tr>")
}
break;
case "scalar":
tBody.append("<tr><td>scalar</td><td>" + data.Value + "</td></tr>");
break;
default:
alert("Unsupported value type!");
break;
} }
console.log(table);
} }
function parseGraphOptionsFromUrl() { function parseGraphOptionsFromUrl() {

View File

@ -104,7 +104,16 @@
</div> </div>
<div class="legend"></div> <div class="legend"></div>
</div> </div>
<div id="console{{id}}" class="console reload"></div> <div id="console{{id}}" class="console reload">
<table class="table table-condensed table-bordered table-hover console_table">
<thead>
<th>Element</th>
<th>Value</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div> </div>
</form> </form>
</div> </div>