var tiers = [];
var defaultDatasource = "_default";
var ruleTypes = [
"loadByInterval",
"loadByPeriod",
"dropByInterval",
"dropByPeriod",
"loadForever",
"dropForever",
"JSON"
];
function makeRuleDiv(rule) {
var retVal = "
";
if (rule === null || typeof rule === "undefined") {
retVal += makeRuleComponents("") + makeRuleBody();
} else {
retVal += makeRuleComponents(rule.type) + makeRuleBody(rule);
}
retVal += "
";
return retVal;
}
function makeRuleComponents(type) {
var components = "";
components +=
"" +
"type" +
"" +
"";
return components;
}
function makeRuleBody(rule) {
var retVal = "";
if (rule !== null && typeof rule !== "undefined") {
switch (rule.type) {
case "loadByInterval":
retVal += makeLoadByInterval(rule);
break;
case "loadByPeriod":
retVal += makeLoadByPeriod(rule);
break;
case "loadForever":
retVal += makeLoadForever(rule);
break;
case "dropByInterval":
retVal += makeDropByInterval(rule);
break;
case "dropByPeriod":
retVal += makeDropByPeriod(rule);
break;
case "dropForever":
retVal += "";
break;
case "JSON":
retVal += makeJSON();
break;
default:
break;
}
}
retVal += ""
return retVal;
}
function makeLoadByInterval(rule) {
var retVal = "";
retVal += "interval";
retVal += "";
if (rule.tieredReplicants === undefined) {
retVal += makeTierLoad(null, 0);
}
for (var tier in rule.tieredReplicants) {
retVal += makeTierLoad(tier, rule.tieredReplicants[tier]);
}
return retVal;
}
function makeLoadByPeriod(rule) {
var retVal = "";
retVal += "period";
retVal += "";
if (rule.tieredReplicants === undefined) {
retVal += makeTierLoad(null, 0);
}
for (var tier in rule.tieredReplicants) {
retVal += makeTierLoad(tier, rule.tieredReplicants[tier]);
}
return retVal;
}
function makeLoadForever(rule) {
var retVal = "";
retVal += "";
if (rule.tieredReplicants === undefined) {
retVal += makeTierLoad(null, 0);
}
for (var tier in rule.tieredReplicants) {
retVal += makeTierLoad(tier, rule.tieredReplicants[tier]);
}
return retVal;
}
function makeTierLoad(tier, val) {
return "" +
"replicants" +
makeTiersDropdown(tier) +
"
";
}
function makeDropByInterval(rule) {
return "interval";
}
function makeDropByPeriod(rule) {
return "period";
}
function makeJSON() {
return "JSON";
}
function makeTiersDropdown(selTier) {
var retVal = "tier";
return retVal;
}
function getRules() {
var selected = $('#datasources option:selected').text();
if (selected !== "") {
$.getJSON("/druid/coordinator/v1/rules/" + selected, function(data) {
$('#rules_list').empty();
if (!$.isEmptyObject(data)) {
$.each(data, function(index, rule) {
$('#rules_list').append(makeRuleDiv(rule));
});
}
});
}
}
function domToRule(domRule) {
var ruleType = $($(domRule).find(".rule_dropdown_types:first")).val();
var inputs = $($(domRule).find(".rule_body:first")).children("input");
// Special case for free form JSON
if (ruleType === "JSON") {
return $.parseJSON($(inputs.children("input:first")).val());
}
var rule = {};
rule.type = ruleType;
$.each(inputs, function(index, input) {
var name = $(input).attr("name");
rule[name] = $(input).val();
});
var theTiers = $($(domRule).find(".rule_body:first")).children(".rule_tier");
var tieredReplicants = {};
$.each(theTiers, function(index, theTier) {
var tierName = $(theTier).find("select").val();
var replicants = $(theTier).find("[name=replicants]").val();
tieredReplicants[tierName] = replicants;
});
rule.tieredReplicants = tieredReplicants;
return rule;
}
$(document).ready(function() {
$("button").button();
$("#rules_list").sortable();
$("#error_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Ok : function() {
$(this).dialog("close");
}
}
});
$("#cancel_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Yes : function() {
getRules();
$(this).dialog("close");
},
No: function() {
$(this).dialog("close");
}
}
});
$("#update_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Yes : function() {
var rules = [];
$.each($("#rules_list").children(), function(index, domRule) {
rules.push(domToRule(domRule));
});
var selected = $('#datasources option:selected').text();
$.ajax({
type: 'POST',
url:'/druid/coordinator/v1/rules/' + selected,
data: JSON.stringify(rules),
contentType:"application/json; charset=utf-8",
dataType:"text",
error: function(xhr, status, error) {
$("#update_dialog").dialog("close");
$("#error_dialog").html(xhr.responseText);
$("#error_dialog").dialog("open");
},
success: function(data, status, xhr) {
$("#update_dialog").dialog("close");
}
});
},
Cancel: function() {
$(this).dialog("close");
}
}
});
$.getJSON("/druid/coordinator/v1/tiers", function(theTiers) {
tiers = theTiers;
});
$.getJSON("/druid/coordinator/v1/db/datasources", function(data) {
$.each(data, function(index, datasource) {
$('#datasources').append($('').val(datasource).text(datasource));
});
$('#datasources').append($('').val(defaultDatasource).text(defaultDatasource));
});
$("#datasources").change(function(event) {
getRules();
$("#rules").show();
});
$(document).on("change", '.rule_dropdown_types', null, function(event) {
var newRule = {
"type" : $(event.target).val()
};
var ruleBody = $(event.target).parent('.rule_dropdown').next('.rule_body');
ruleBody.replaceWith(makeRuleBody(newRule));
});
$(document).on("click", '.delete_rule', null, function(event) {
$(event.target).parent(".rule").remove();
});
$(document).on("click", '.add_tier', null, function(event) {
$(event.target).parent().append(makeTierLoad(null, 0));
});
$("#create_new_rule").click(function (event) {
$('#rules_list').prepend(makeRuleDiv());
});
$("#cancel").click(function() {
$("#cancel_dialog").dialog("open");
});
$('#update').click(function (){
$("#update_dialog").dialog("open")
});
});