").append(that._createResultTable(response)));
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
});
},
_initializeSuggestResult: function() {
var that = this;
$("#suggest-result-tabs").tabs({
activate: function (event, ui) {
that._suggestResultType = ui.newPanel.attr('id').replace(/^suggest-result-tab-/, "");
$("#suggest-submit").click();
}
});
},
_selectTab: function(name) {
this.stop_status_timer();
this._$databaseTabs.hide();
this._$tableTabs.hide();
this._$suggestTabs.hide();
switch (name) {
case "table":
this._$tableTabs.show();
break;
case "suggest":
this._$suggestTabs.show();
break;
case "database":
default:
this._$databaseTabs.show();
break;
}
},
_initializeSideMenu: function () {
var that = this;
$('#side-menu-summary').click(function() {
that.current_table = null;
that._selectTab("database");
that._$databaseTabs.tabs("option", "active", 0); // #database-tab-summary
that.stop_status_timer();
that.start_status_timer();
});
$('#side-menu-suggest').click(function() {
that.current_table = null;
that._selectTab("suggest");
});
},
start_status_timer: function() {
var that = this;
this.stop_status_timer();
this.status();
this.statusTimer = setInterval(function() {that.status()}, 1000);
},
change_status_timer: function(time) {
var that = this;
this.stop_status_timer();
this.statusTimer = setInterval(function() {that.status()}, time);
},
stop_status_timer: function() {
if (this.statusTimer) {
clearInterval(this.statusTimer);
this.statusTimer = null;
}
},
_createResultTable: function (result, options) {
var that = this;
if (!options) {
options = {};
}
var table = $('
');
if ($.isArray(result)) {
var nEntries = result.length;
if (nEntries >= 1) {
var thead = $('');
table.append(thead);
var line = result[0];
if ($.isArray(line)) {
var tr = $('
');
thead.append(tr);
var m = line.length;
if (options.check) {
tr.append($('
'));
}
}
var tbody = $('');
table.append(tbody);
for (var i = 1; i < nEntries; i++) {
line = result[i];
if ($.isArray(line)) {
var tr = $('
');
table.append(tr);
var m = line.length;
switch(options.check) {// チェックボックスの値を何にするか
case 1: // 1番目の要素(レコード一覧の_id等)
case 2: // 2番目の要素(テーブル・カラム一覧のname等)
var td = $('
');
tr.append(td);
td.append($('')
.attr("type", "checkbox")
.attr("value", line[options.check-1]));
break;
}
for (var j = 0; j < m; j++) {
var td = $('
');
tr.append(td);
td.append(prim2html(line[j], 128));
}
switch(options.button) {
case 1: // Edit record
var td = $('
');
tr.append(td);
td.append($('')
.attr("type", "button")
.attr("value", "Detail")
.attr("data-table-name", line[1])
.click(function () {
var tableName = $(this).attr("data-table-name");
$("#side-menu-tablelist-link-" + tableName).click();
}));
break;
}
}
}
}
}
return table;
},
show_edit_record: function(id) {
$('#table-tabs').tabs("option", "active", 2);
this.update_createrecord(this.current_table, id);
},
format_unix_time: function(unix_time) {
var date = new Date();
date.setTime(unix_time * 1000);
return date.toLocaleString();
},
format_duration: function(duration_in_seconds) {
var duration = "";
var days = Math.floor(duration_in_seconds / 3600 / 24);
var hours = Math.floor(duration_in_seconds / 3600 % 24);
var minutes = Math.floor(duration_in_seconds / 60 % 60);
var seconds = Math.floor(duration_in_seconds % 60);
if (days > 0) {
duration += days;
if (days == 1) {
duration += " day, ";
} else {
duration += " days, ";
}
}
if (days > 0 || hours > 0) {
duration += hours + ":" + minutes + ":" + seconds;
} else if (minutes > 0) {
duration += minutes + ":" + seconds;
} else {
duration += seconds;
}
return duration;
},
maxThroughput: 0,
lastNQueries: -1,
keepLastNData: 100,
throughputData: [],
throughputChart: null,
updateThroughputChart: function(statusData) {
var maxThroughputUpdated = false;
if (this.lastNQueries >= 0) {
var throughput = statusData.n_queries - this.lastNQueries;
this.throughputData.push(throughput);
if (this.maxThroughput < throughput) {
this.maxThroughput = throughput;
maxThroughputUpdated = true;
}
}
if (this.throughputData.length > this.keepLastNData) {
this.throughputData.shift();
}
if (!this.throughputChart) {
this.throughputChart = $.plot($("#throughput-chart"),
[[]],
{xaxis: {min: -(this.keepLastNData - 1),
max: 0},
yaxis: {min: 0}});
}
var that = this;
var chartSeries = $.map(this.throughputData, function(n, i) {
return [[-(that.throughputData.length - i) + 1, n]];
});
this.throughputChart.setData([chartSeries]);
if (maxThroughputUpdated) {
this.throughputChart.setupGrid();
}
this.throughputChart.draw();
this.lastNQueries = statusData.n_queries;
},
status: function() {
if (this.current_status > 0) { return; }
this.current_status++;
var that = this;
$.ajax({
url: '/d/status',
data: {},
dataType: 'json',
success: function(b) {
that.current_status--;
if (!b) {
that.change_status_timer(10000);
return;
}
var d = b[1];
$('#status-starttime').text(that.format_unix_time(d.starttime));
$('#status-uptime').text(that.format_duration(d.uptime));
$('#status-n-queries').text(d.n_queries);
$('#status-cache-hit-rate').text(d.cache_hit_rate);
that.updateThroughputChart(d);
that.change_status_timer(1000);
},
error: function() {
that.current_status--;
that.change_status_timer(10000);
}
});
},
update_tablelist: function() {
var that = this;
this._tables = [];
this.showloading(
$.ajax({
url: '/d/table_list',
data: {},
dataType: 'json',
success: function(d) {
if (that.validateajax(d) < 0) { return; }
d.shift();
var tl = $('#side-menu-tablelist').empty();
var tt = $('#createtable-key-type-table').empty();
var vt = $('#createtable-value-type-table').empty();
var ct = $('#createcolumn-type-table').empty();
var b = d.shift();
b.shift();
$.each(b, function(i, val) {
var table_name = val[1];
that._tables.push(table_name);
tl.append(
$('').append(
$('')
.attr('id', 'side-menu-tablelist-link-' + table_name)
.attr('href', '#side-menu-tablelist-' + table_name)
.text(table_name)
.click(function() {
that.current_table = table_name;
$('#database-tabs').hide();
$('#suggest-tabs').hide();
that.stop_status_timer();
$('#table-tabs').show();
that.columnlist(table_name);
$('#tab-recordlist-simplequery').val('');
that.recordlist_simple(table_name, null, null, 1);
that.update_createrecord(that.current_table);
})
)
);
tt.append($('').val(val[1]).text(val[1]));
vt.append($('').val(val[1]).text(val[1]));
ct.append($('').val(val[1]).text(val[1]));
});
that.hideloading();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
that.errorloading(XMLHttpRequest);
}
})
);
},
tablelist: function() {
$('#tab-tablelist-table').empty();
var that = this;
this.showloading(
$.ajax({
url: '/d/table_list',
data: {},
dataType: 'json',
success: function(d) {
if (that.validateajax(d) < 0) { return; }
var b = d[1];
var table = that._createResultTable(b, {check: 2, button: 2});
$('#tab-tablelist-table').append($('').text('List of table')).append(table);
that.hideloading();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
that.errorloading(XMLHttpRequest);
}
})
);
},
recordlist_simple: function(table_name, simplequery, simplequery_type, page, hide_dialog) {
var d = {
'table': table_name,
'offset': (page - 1) * this.recordlist_count,
'limit': this.recordlist_count
}
switch (simplequery_type) {
case 'query':
case 'filter':
case null:
if (simplequery) {
d[simplequery_type] = simplequery;
}
this.recordlist(d, true, hide_dialog);
break;
}
},
recordlist: function(params, show_pager, hide_dialog) {
var that = this;
this.reload_record_func = function(){
that.recordlist(params, show_pager, hide_dialog);
};
this.showloading(
$.ajax({
url: '/d/select',
data: params,
dataType: 'json',
success: function(d) {
if (that.validateajax(d, hide_dialog) < 0) { return; }
var rc = d.shift();
if (rc[0] != 0) {
alert('error: ' + rc[3]);
that.hideloading();
return false;
}
var body = d.shift();
var recs = body.shift();
var all_count = recs.shift()[0];
var pager;
if (show_pager) {
var offset = params['offset'] || 0;
var rows = params['limit'] || 10;
if (rows < 0){
rows = all_count + parseInt(rows) + 1;
}
if (rows != '' && !parseInt(rows)) {
pager = $('');
} else {
pager = $("");
pager.paginate({
total: all_count,
nItemsPerPage: rows,
currentPage: Math.floor(offset/rows)+1,
callback: function(page) {
params['offset'] = page * rows;
that.recordlist(params, true, false);
return false;
}
});
}
} else {
pager = $('');
}
$('#tab-recordlist-table')
.empty()
.append($('').text('List of records: ' + params['table']))
.append($('').text('Total count: ' + all_count))
.append(pager.clone(true))
.append($('').append(that._createResultTable(recs, {check: 1, button: 1})))
.append(pager);
that.hideloading();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
that.errorloading(XMLHttpRequest, hide_dialog);
}
})
,hide_dialog);
},
columnlist: function(table_name) {
var that = this;
$('#tab-columnlist-table').empty();
this.showloading(
$.ajax({
url: '/d/column_list',
data: {'table': table_name},
dataType: 'json',
success: function(d) {
if (that.validateajax(d) < 0) { return; }
var b = d[1];
var table = that._createResultTable(b, {check: 2});
$('#tab-columnlist-table')
.append($('').text('List of columns: ' + table_name))
.append(table);
that.hideloading();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
that.errorloading(XMLHttpRequest);
}
})
);
},
add_record_inputbox: function(type, value) {
var inputbox = null;
switch(type){
case "Bool":
inputbox = $('')
.attr("type","checkbox")
.attr("value","true");
if (value) {
inputbox.attr("checked","");
}
break;
case "UInt8":
case "UInt16":
case "UInt32":
case "UInt64":
case "Int8":
case "Int16":
case "Int32":
case "Int64":
case "Float":
inputbox = $('')
.attr("type", "text")
.val(isNaN(value) ? "" : value);
break;
case "Text":
case "ShortText":
case "LongText":
inputbox = $('')
.attr("cols", "50")
.attr("rows", "2")
.text(value ? value : "");
break;
case "TokyoGeoPoint":
case "WGS84GeoPoint":
case "Time":
inputbox = $('')
.attr("type", "text")
.attr("size", "40")
.val(value ? value : "");
break;
case "Object":
inputbox = $('')
.attr("type", "text")
.attr("disabled", "disabled");
break;
default:
inputbox = $('')
.attr("type", "text")
.val(value ? value : "");
}
inputbox.addClass('column_values');
return inputbox;
},
add_record_deletebutton: function(){
var ret =
$('')
.append("[×]")
.css('cursor', 'pointer')
.click(function() {
$(this).prev().remove();
$(this).next().remove();
$(this).remove();
});
return ret;
},
update_createrecord_loadcomplete: function(d_sel, d_col) {
var that = this;
var b = d_sel[1][0];
var columns = $('
');
var listofs = b[1].length - (d_col[1].length - 1);
for (var i = 1; i < b[1].length; i++) {
var line = b[1][i];
var value = null;
if (b[2]) value = b[2][i];
if ($.isArray(line)) {
var tr = $('