Files
librenms-librenms/html/js/query-builder.standalone.min.js

8 lines
72 KiB
JavaScript
Raw Normal View History

feature: Added new alert rule builder UI and rule mapping UI (#8293) * feature: Added new alert rule builder UI * Updated to export sql queries * More updates * more changes * removed debug * fix scrut * Updated to include import options + various other fixes * fix rule * Populate name from collection rules. * Fix default rule import Allow new and old style rules in the collection. Don't add new yet as I'm not sure GenSQL() is working. * Fix GenSQL call * Extract filter building to class so it is nicely contained in one place * moved schema * some fixes and tweaks * travis fixes * Some more features / updates * Fix up my mistakes when adding default rules * Use a modal for new alert (Incomplete) Larger dialog!! Remove page loading stuff. Working: Loading rules, resetting dialog, importing from collection. Not working yet: select width device limited rule access? don't know what this is... Lots of unused stuff to delete... * reload "table" after save * fixed editing rule * Auto select2 width * Reload window on save * Restore per-device alert. Remove debug. * Small cleanups. Rule Name first. * Restore button to button type. Rename schema. * Fixes: wrong command to reload window, remove extra attributes, rule is never passed * Fixed old rule editing * some small updates for old imports * travis update to use trusty * maybe travis fix * Ability to set alert rule mappings on the rule edit screen * pip installs one line, no quiet for deploy * update schema def * Fix style and some copyright headers * fix docs missing file * Allow new versions of snmpsim and libraries * Parser WIP * Fix default rules insert * reorganize * Legacy import first draft done * Implement saving Skip translation to sql for now * Working on glues * small rule collection fix * Working on glues * Working on glues * Docs updates + small UI changes * Parser WIP * reorganize * Legacy import first draft done * Implement saving Skip translation to sql for now * Working on glues * Working on glues * Working on glues * Add table mapping, should move to it's own class * WIP * Glue working!! * Extract Schema class * Some final touches. revert alerts_rules.json for now. * Finish up initial implementation Needs more tests * Fix a few places * small doc updates * Fix finding tables in grouped rules. * remove unused code * code format fixes * Some quick tests for Schema Simplified output for findRelationshipPath. Always includes start and target in the result. This simplifies a lot of code in QueryBuilderParser.php This also always loads the target table data now (which we want) * Make bill_id the PRIMARY index for the bills table * Load macros from a json file in misc instead of the database. * Fix whitespace and wrong key for collection. * Handle IN properly when generating SQL * Fix glue (devices.device_id = ports.port_id) is incorrect :D Show ALL tables we can resolve relationships for in the query builder filter. * Remove all macros from the database Remove insert statements, leave updates to update user's existing rules.
2018-03-14 20:25:19 +00:00
/*!
* jQuery QueryBuilder 2.4.5
* Copyright 2014-2017 Damien "Mistic" Sorel (http://www.strangeplanet.fr)
* Licensed under MIT (http://opensource.org/licenses/MIT)
*/
!function(a,b){"function"==typeof define&&define.amd?define("jQuery.extendext",["jquery"],b):"object"==typeof module&&module.exports?module.exports=b(require("jquery")):b(a.jQuery)}(this,function($){"use strict";$.extendext=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1,k="default";for("boolean"==typeof g&&(j=g,g=arguments[h++]||{}),"string"==typeof g&&(k=g.toLowerCase(),"concat"!==k&&"replace"!==k&&"extend"!==k&&(k="default"),g=arguments[h++]||{}),"object"==typeof g||$.isFunction(g)||(g={}),h===i&&(g=this,h--);h<i;h++)if(null!==(a=arguments[h]))if($.isArray(a)&&"default"!==k)switch(f=g&&$.isArray(g)?g:[],k){case"concat":g=f.concat($.extend(j,[],a));break;case"replace":g=$.extend(j,[],a);break;case"extend":a.forEach(function(a,b){if("object"==typeof a){var c=$.isArray(a)?[]:{};f[b]=$.extendext(j,k,f[b]||c,a)}else f.indexOf(a)===-1&&f.push(a)}),g=f}else for(b in a)c=g[b],d=a[b],g!==d&&(j&&d&&($.isPlainObject(d)||(e=$.isArray(d)))?(e?(e=!1,f=c&&$.isArray(c)?c:[]):f=c&&$.isPlainObject(c)?c:{},g[b]=$.extendext(j,k,f,d)):void 0!==d&&(g[b]=d));return g}}),function(){"use strict";function a(b,c,d){return("string"==typeof c?c:c.toString()).replace(b.define||f,function(a,c,e,f){return 0===c.indexOf("def.")&&(c=c.substring(4)),c in d||(":"===e?(b.defineParams&&f.replace(b.defineParams,function(a,b,e){d[c]={arg:b,text:e}}),c in d||(d[c]=f)):new Function("def","def['"+c+"']="+f)(d)),""}).replace(b.use||f,function(c,e){b.useParams&&(e=e.replace(b.useParams,function(a,b,c,e){if(d[c]&&d[c].arg&&e){var f=(c+":"+e).replace(/'|\\/g,"_");return d.__exp=d.__exp||{},d.__exp[f]=d[c].text.replace(new RegExp("(^|[^\\w$])"+d[c].arg+"([^\\w$])","g"),"$1"+e+"$2"),b+"def.__exp['"+f+"']"}}));var f=new Function("def","return "+e)(d);return f?a(b,f,d):f})}function b(a){return a.replace(/\\('|\\)/g,"$1").replace(/[\r\t\n]/g," ")}var c,d={version:"1.0.3",templateSettings:{evaluate:/\{\{([\s\S]+?(\}?)+)\}\}/g,interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,useParams:/(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,defineParams:/^\s*([\w$]+):([\s\S]+)/,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:!0,append:!0,selfcontained:!1,doNotSkipEncoded:!1},template:void 0,compile:void 0,log:!0};d.encodeHTMLSource=function(a){var b={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"},c=a?/[&<>"'\/]/g:/&(?!#?\w+;)|<|>|"|'|\//g;return function(a){return a?a.toString().replace(c,function(a){return b[a]||a}):""}},c=function(){return this||(0,eval)("this")}(),"undefined"!=typeof module&&module.exports?module.exports=d:"function"==typeof define&&define.amd?define("doT",function(){return d}):c.doT=d;var e={append:{start:"'+(",end:")+'",startencode:"'+encodeHTML("},split:{start:"';out+=(",end:");out+='",startencode:"';out+=encodeHTML("}},f=/$^/;d.template=function(g,h,i){h=h||d.templateSettings;var j,k,l=h.append?e.append:e.split,m=0,n=h.use||h.define?a(h,g,i||{}):g;n=("var out='"+(h.strip?n.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""):n).replace(/'|\\/g,"\\$&").replace(h.interpolate||f,function(a,c){return l.start+b(c)+l.end}).replace(h.encode||f,function(a,c){return j=!0,l.startencode+b(c)+l.end}).replace(h.conditional||f,function(a,c,d){return c?d?"';}else if("+b(d)+"){out+='":"';}else{out+='":d?"';if("+b(d)+"){out+='":"';}out+='"}).replace(h.iterate||f,function(a,c,d,e){return c?(m+=1,k=e||"i"+m,c=b(c),"';var arr"+m+"="+c+";if(arr"+m+"){var "+d+","+k+"=-1,l"+m+"=arr"+m+".length-1;while("+k+"<l"+m+"){"+d+"=arr"+m+"["+k+"+=1];out+='"):"';} } out+='"}).replace(h.evaluate||f,function(a,c){return"';"+b(c)+"out+='"})+"';return out;").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r").replace(/(\s|;|\}|^|\{)out\+='';/g,"$1").replace(/\+''/g,""),j&&(h.selfcontained||!c||c._encodeHTML||(c._encodeHTML=d.encodeHTMLSource(h.do
var i,j=h.escapeElementId(a.id+"_value_"+f);switch(b.input){case"radio":d.push(e.find("[name="+j+"]:checked").val());break;case"checkbox":i=[],e.find("[name="+j+"]:checked").each(function(){i.push($(this).val())}),d.push(i);break;case"select":b.multiple?(i=[],e.find("[name="+j+"] option:selected").each(function(){i.push($(this).val())}),d.push(i)):d.push(e.find("[name="+j+"] option:selected").val());break;default:d.push(e.find("[name="+j+"]").val())}}c.multiple&&b.value_separator&&(d=d.map(function(a){return a.split(b.value_separator)})),1===c.nb_inputs&&(d=d[0]),b.valueParser&&(d=b.valueParser.call(this,a,d))}return this.change("getRuleValue",d,a)},g.prototype.setRuleInputValue=function(a,b){var c=a.filter,d=a.operator;if(c&&d){if(this._updating_input=!0,c.valueSetter)c.valueSetter.call(this,a,b);else{var e=a.$el.find(g.selectors.value_container);1==d.nb_inputs&&(b=[b]);for(var f=0;f<d.nb_inputs;f++){var i=h.escapeElementId(a.id+"_value_"+f);switch(c.input){case"radio":e.find("[name="+i+'][value="'+b[f]+'"]').prop("checked",!0).trigger("change");break;case"checkbox":$.isArray(b[f])||(b[f]=[b[f]]),b[f].forEach(function(a){e.find("[name="+i+'][value="'+a+'"]').prop("checked",!0).trigger("change")});break;default:d.multiple&&c.value_separator&&$.isArray(b[f])&&(b[f]=b[f].join(c.value_separator)),e.find("[name="+i+"]").val(b[f]).trigger("change")}}}this._updating_input=!1}},g.prototype.parseRuleFlags=function(a){var b=$.extend({},this.settings.default_rule_flags);return a.readonly&&$.extend(b,{filter_readonly:!0,operator_readonly:!0,value_readonly:!0,no_delete:!0}),a.flags&&$.extend(b,a.flags),this.change("parseRuleFlags",b,a)},g.prototype.getRuleFlags=function(a,b){if(b)return $.extend({},a);var c={};return $.each(this.settings.default_rule_flags,function(b,d){a[b]!==d&&(c[b]=a[b])}),c},g.prototype.parseGroupFlags=function(a){var b=$.extend({},this.settings.default_group_flags);return a.readonly&&$.extend(b,{condition_readonly:!0,no_add_rule:!0,no_add_group:!0,no_delete:!0}),a.flags&&$.extend(b,a.flags),this.change("parseGroupFlags",b,a)},g.prototype.getGroupFlags=function(a,b){if(b)return $.extend({},a);var c={};return $.each(this.settings.default_group_flags,function(b,d){a[b]!==d&&(c[b]=a[b])}),c},g.prototype.translate=function(a,b){b||(b=a,a=void 0);var c;return c="object"==typeof b?b[this.settings.lang_code]||b.en:(a?this.lang[a]:this.lang)[b]||b,this.change("translate",c,b,a)},g.prototype.getValidationMessage=function(a,b,c){return a.messages&&a.messages[b]||c},g.templates.group='<dl id="{{= it.group_id }}" class="rules-group-container"> <dt class="rules-group-header"> <div class="btn-group pull-right group-actions"> <button type="button" class="btn btn-xs btn-success" data-add="rule"> <i class="{{= it.icons.add_rule }}"></i> {{= it.translate("add_rule") }} </button> {{? it.settings.allow_groups===-1 || it.settings.allow_groups>=it.level }} <button type="button" class="btn btn-xs btn-success" data-add="group"> <i class="{{= it.icons.add_group }}"></i> {{= it.translate("add_group") }} </button> {{?}} {{? it.level>1 }} <button type="button" class="btn btn-xs btn-danger" data-delete="group"> <i class="{{= it.icons.remove_group }}"></i> {{= it.translate("delete_group") }} </button> {{?}} </div> <div class="btn-group group-conditions"> {{~ it.conditions: condition }} <label class="btn btn-xs btn-primary"> <input type="radio" name="{{= it.group_id }}_cond" value="{{= condition }}"> {{= it.translate("conditions", condition) }} </label> {{~}} </div> {{? it.settings.display_errors }} <div class="error-container"><i class="{{= it.icons.error }}"></i></div> {{?}} </dt> <dd class=rules-group-body> <ul class=rules-list></ul> </dd> </dl>',g.templates.rule='<li id="{{= it.rule_id }}" class="rule-container"> <div class="rule-header"> <div class="btn-group pull-right rule-actions"> <button type="button" class="btn btn-xs btn-danger" data-delete="ru
not_begins_with:{op:"NOT LIKE(?)",mod:"{0}%"},contains:{op:"LIKE(?)",mod:"%{0}%"},not_contains:{op:"NOT LIKE(?)",mod:"%{0}%"},ends_with:{op:"LIKE(?)",mod:"%{0}"},not_ends_with:{op:"NOT LIKE(?)",mod:"%{0}"},is_empty:{op:"= ''"},is_not_empty:{op:"!= ''"},is_null:{op:"IS NULL"},is_not_null:{op:"IS NOT NULL"}},sqlRuleOperator:{"=":function(a){return{val:a,op:""===a?"is_empty":"equal"}},"!=":function(a){return{val:a,op:""===a?"is_not_empty":"not_equal"}},LIKE:function(a){return"%"==a.slice(0,1)&&"%"==a.slice(-1)?{val:a.slice(1,-1),op:"contains"}:"%"==a.slice(0,1)?{val:a.slice(1),op:"ends_with"}:"%"==a.slice(-1)?{val:a.slice(0,-1),op:"begins_with"}:void h.error("SQLParse",'Invalid value for LIKE operator "{0}"',a)},"NOT LIKE":function(a){return"%"==a.slice(0,1)&&"%"==a.slice(-1)?{val:a.slice(1,-1),op:"not_contains"}:"%"==a.slice(0,1)?{val:a.slice(1),op:"not_ends_with"}:"%"==a.slice(-1)?{val:a.slice(0,-1),op:"not_begins_with"}:void h.error("SQLParse",'Invalid value for NOT LIKE operator "{0}"',a)},IN:function(a){return{val:a,op:"in"}},"NOT IN":function(a){return{val:a,op:"not_in"}},"<":function(a){return{val:a,op:"less"}},"<=":function(a){return{val:a,op:"less_or_equal"}},">":function(a){return{val:a,op:"greater"}},">=":function(a){return{val:a,op:"greater_or_equal"}},BETWEEN:function(a){return{val:a,op:"between"}},"NOT BETWEEN":function(a){return{val:a,op:"not_between"}},IS:function(a){return null!==a&&h.error("SQLParse","Invalid value for IS operator"),{val:null,op:"is_null"}},"IS NOT":function(a){return null!==a&&h.error("SQLParse","Invalid value for IS operator"),{val:null,op:"is_not_null"}}},sqlStatements:{question_mark:function(){var a=[];return{add:function(b,c){return a.push(c),"?"},run:function(){return a}}},numbered:function(a){(!a||a.length>1)&&(a="$");var b=0,c=[];return{add:function(d,e){return c.push(e),b++,a+b},run:function(){return c}}},named:function(a){(!a||a.length>1)&&(a=":");var b={},c={};return{add:function(d,e){b[d.field]||(b[d.field]=1);var f=d.field+"_"+b[d.field]++;return c[f]=e,a+f},run:function(){return c}}}},sqlRuleStatement:{question_mark:function(a){var b=0;return{parse:function(c){return"?"==c?a[b++]:c},esc:function(a){return a.replace(/\?/g,"'?'")}}},numbered:function(a,b){(!b||b.length>1)&&(b="$");var c=new RegExp("^\\"+b+"[0-9]+$"),d=new RegExp("\\"+b+"([0-9]+)","g");return{parse:function(b){return c.test(b)?a[b.slice(1)-1]:b},esc:function(a){return a.replace(d,"'"+("$"==b?"$$":b)+"$1'")}}},named:function(a,b){(!b||b.length>1)&&(b=":");var c=new RegExp("^\\"+b),d=new RegExp("\\"+b+"("+Object.keys(a).join("|")+")","g");return{parse:function(b){return c.test(b)?a[b.slice(1)]:b},esc:function(a){return a.replace(d,"'"+("$"==b?"$$":b)+"$1'")}}}}}),g.extend({getSQL:function(a,b,c){if(c=void 0===c?this.getRules():c,!c)return null;b=b?"\n":" ";var d=this.getPluginOptions("sql-support","boolean_as_integer");if(a===!0&&(a="question_mark"),"string"==typeof a){var e=f(a);a=this.settings.sqlStatements[e[1]](e[2])}var g=this,i=function j(c){if(c.condition||(c.condition=g.settings.default_condition),["AND","OR"].indexOf(c.condition.toUpperCase())===-1&&h.error("UndefinedSQLCondition",'Unable to build SQL query with condition "{0}"',c.condition),!c.rules)return"";var e=[];c.rules.forEach(function(c){if(c.rules&&c.rules.length>0)e.push("("+b+j(c)+b+")"+b);else{var f=g.settings.sqlOperators[c.operator],i=g.getOperatorByType(c.operator),k="";void 0===f&&h.error("UndefinedSQLOperator",'Unknown SQL operation for operator "{0}"',c.operator),0!==i.nb_inputs&&(c.value instanceof Array||(c.value=[c.value]),c.value.forEach(function(b,e){e>0&&(k+=f.sep),"integer"==c.type||"double"==c.type||"boolean"==c.type?b=h.changeType(b,c.type,d):a||(b=h.escapeString(b)),f.mod&&(b=h.fmt(f.mod,b)),a?k+=a.add(c,b):("string"==typeof b&&(b="'"+b+"'"),k+=b)}));var l=function(a){return f.op.replace(/\?/,a)},m=g.change("getSQLField",c.field,c),n=m+" "+l(k);e.push(g.change("ruleToSQL",n,c,k,l))}});var f=e.join(" "+c.condition+b);return g.change("groupToSQL",f,c)}(c);return a?{sql:i,params:a.run()}:{sql:i}},getRulesFromSQL:function(a,b){"SQ