Skip to content

Business intelligence made simple

Added checks

Andrew Kane3f537aac14ab
diff --git a/README.md b/README.md
index 4b739c03..60269126 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ Works with PostgreSQL and MySQL
- **Smart Variables** - no need to remember ids
- **Charts** - visualize the data
- **Audits** - all queries are tracked
+- **Checks & Alerts** - get emailed when bad data appears [master]
## Installation
@@ -67,7 +68,7 @@ ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO blazer;
COMMIT;
```
-It is **highly, highly recommended** to protect sensitive information with views. Documentation coming soon.
+It is recommended to protect sensitive information with views. Documentation coming soon.
### MySQL
@@ -78,7 +79,7 @@ GRANT SELECT, SHOW VIEW ON database_name.* TO blazer@’127.0.0.1′ IDENTIFIED
FLUSH PRIVILEGES;
```
-It is **highly, highly recommended** to protect sensitive information with views. Documentation coming soon.
+It is recommended to protect sensitive information with views. Documentation coming soon.
## Authentication
@@ -101,6 +102,26 @@ authenticate :user, lambda{|user| user.admin? } do
end
```
+## Checks [master]
+
+Set up checks to run every 5 minutes.
+
+```sh
+rake blazer:run_checks
+```
+
+Be sure to set a host in `config/environments/production.rb` for emails to work.
+
+```ruby
+config.action_mailer.default_url_options = {host: "blazerme.herokuapp.com"}
+```
+
+We also recommend setting up failing checks to be sent once a day.
+
+```sh
+rake blazer:send_failing_checks
+```
+
## Customization
Change time zone
@@ -145,6 +166,27 @@ If there are at least 2 columns and the first is a timestamp and all other colum
If there are 2 columns and the first column is a string and the second column is a numeric, a pie chart will be generated
+## Upgrading
+
+### [master]
+
+Add a migration for checks
+
+```sh
+rails g migration create_blazer_checks
+```
+
+with
+
+```ruby
+create_table :blazer_checks do |t|
+ t.references :blazer_query
+ t.string :state
+ t.text :emails
+ t.timestamps
+end
+```
+
## TODO
- better readme
diff --git a/app/assets/javascripts/blazer/selectize.js b/app/assets/javascripts/blazer/selectize.js
index 882a5891..63ec321f 100644
--- a/app/assets/javascripts/blazer/selectize.js
+++ b/app/assets/javascripts/blazer/selectize.js
@@ -221,7 +221,7 @@
* @param {object} result
* @return {mixed}
*/
- get_field = function(name, result) {
+ get_field = function(name, result) {
if (name === '$score') return result.score;
return self.items[result.id][name];
};
@@ -391,8 +391,8 @@
if (typeof a === 'number' && typeof b === 'number') {
return a > b ? 1 : (a < b ? -1 : 0);
}
- a = String(a || '').toLowerCase();
- b = String(b || '').toLowerCase();
+ a = asciifold(String(a || ''));
+ b = asciifold(String(b || ''));
if (a > b) return 1;
if (b > a) return -1;
return 0;
@@ -425,21 +425,44 @@
};
var DIACRITICS = {
- 'a': '[aÀÁÂÃÄÅàáâãäå]',
+ 'a': '[aÀÁÂÃÄÅàáâãäåĀāąĄ]',
'c': '[cÇçćĆčČ]',
'd': '[dđĐďĎ]',
- 'e': '[eÈÉÊËèéêëěĚ]',
- 'i': '[iÌÍÎÏìíîï]',
- 'n': '[nÑñňŇ]',
- 'o': '[oÒÓÔÕÕÖØòóôõöø]',
+ 'e': '[eÈÉÊËèéêëěĚĒēęĘ]',
+ 'i': '[iÌÍÎÏìíîïĪī]',
+ 'l': '[lłŁ]',
+ 'n': '[nÑñňŇńŃ]',
+ 'o': '[oÒÓÔÕÕÖØòóôõöøŌō]',
'r': '[rřŘ]',
- 's': '[sŠš]',
+ 's': '[sŠšśŚ]',
't': '[tťŤ]',
- 'u': '[uÙÚÛÜùúûüůŮ]',
+ 'u': '[uÙÚÛÜùúûüůŮŪū]',
'y': '[yŸÿýÝ]',
- 'z': '[zŽž]'
+ 'z': '[zŽžżŻźŹ]'
};
+ var asciifold = (function() {
+ var i, n, k, chunk;
+ var foreignletters = '';
+ var lookup = {};
+ for (k in DIACRITICS) {
+ if (DIACRITICS.hasOwnProperty(k)) {
+ chunk = DIACRITICS[k].substring(2, DIACRITICS[k].length - 1);
+ foreignletters += chunk;
+ for (i = 0, n = chunk.length; i < n; i++) {
+ lookup[chunk.charAt(i)] = k;
+ }
+ }
+ }
+ var regexp = new RegExp('[' + foreignletters + ']', 'g');
+ return function(str) {
+ return str.replace(regexp, function(foreignletter) {
+ return lookup[foreignletter];
+ }).toLowerCase();
+ };
+ })();
+
+
// export
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -585,8 +608,8 @@
}));
/**
- * selectize.js (v0.10.1)
- * Copyright (c) 2013 Brian Reavis & contributors
+ * selectize.js (v0.12.1)
+ * Copyright (c) 2013–2015 Brian Reavis & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
@@ -707,6 +730,8 @@
var TAG_SELECT = 1;
var TAG_INPUT = 2;
+ // for now, android support in general is too spotty to support validity
+ var SUPPORTS_VALIDITY_API = !/android/i.test(window.navigator.userAgent) && !!document.createElement('form').validity;
var isset = function(object) {
return typeof object !== 'undefined';
@@ -726,10 +751,10 @@
* 1 -> '1'
*
* @param {string} value
- * @returns {string}
+ * @returns {string|null}
*/
var hash_key = function(value) {
- if (typeof value === 'undefined' || value === null) return '';
+ if (typeof value === 'undefined' || value === null) return null;
if (typeof value === 'boolean') return value ? '1' : '0';
return value + '';
};
@@ -793,25 +818,6 @@
};
};
- /**
- * Builds a hash table out of an array of
- * objects, using the specified `key` within
- * each object.
- *
- * @param {string} key
- * @param {mixed} objects
- */
- var build_hash_table = function(key, objects) {
- if (!$.isArray(objects)) return objects;
- var i, n, table = {};
- for (i = 0, n = objects.length; i < n; i++) {
- if (objects[i].hasOwnProperty(key)) {
- table[objects[i][key]] = objects[i];
- }
- }
- return table;
- };
-
/**
* Wraps `fn` so that it can only be invoked once.
*
@@ -1053,13 +1059,16 @@
input.selectize = self;
// detect rtl environment
- dir = window.getComputedStyle ? window.getComputedStyle(input, null).getPropertyValue('direction') : input.currentStyle && input.currentStyle.direction;
+ var computedStyle = window.getComputedStyle && window.getComputedStyle(input, null);
+ dir = computedStyle ? computedStyle.getPropertyValue('direction') : input.currentStyle && input.currentStyle.direction;
dir = dir || $input.parents('[dir]:first').attr('dir') || '';
// setup default state
$.extend(self, {
+ order : 0,
settings : settings,
$input : $input,
+ tabIndex : $input.attr('tabindex') || '',
tagType : input.tagName.toLowerCase() === 'select' ? TAG_SELECT : TAG_INPUT,
rtl : /rtl/i.test(dir),
@@ -1101,12 +1110,20 @@
self.sifter = new Sifter(this.options, {diacritics: settings.diacritics});
// build options table
- $.extend(self.options, build_hash_table(settings.valueField, settings.options));
- delete self.settings.options;
+ if (self.settings.options) {
+ for (i = 0, n = self.settings.options.length; i < n; i++) {
+ self.registerOption(self.settings.options[i]);
+ }
+ delete self.settings.options;
+ }
// build optgroup table
- $.extend(self.optgroups, build_hash_table(settings.optgroupValueField, settings.optgroups));
- delete self.settings.optgroups;
+ if (self.settings.optgroups) {
+ for (i = 0, n = self.settings.optgroups.length; i < n; i++) {
+ self.registerOptionGroup(self.settings.optgroups[i]);
+ }
+ delete self.settings.optgroups;
+ }
// option-dependent defaults
self.settings.mode = self.settings.mode || (self.settings.maxItems === 1 ? 'single' : 'multi');
@@ -1114,16 +1131,6 @@
self.settings.hideSelected = self.settings.mode === 'multi';
}
- if (self.settings.create) {
- self.canCreate = function(input) {
- var filter = self.settings.createFilter;
- return input.length
- && (typeof filter !== 'function' || filter.apply(self, [input]))
- && (typeof filter !== 'string' || new RegExp(filter).test(input))
- && (!(filter instanceof RegExp) || filter.test(input));
- };
- }
-
self.initializePlugins(self.settings.plugins);
self.setupCallbacks();
self.setupTemplates();
@@ -1161,21 +1168,23 @@
var inputMode;
var timeout_blur;
var timeout_focus;
- var tab_index;
var classes;
var classes_plugins;
inputMode = self.settings.mode;
- tab_index = $input.attr('tabindex') || '';
classes = $input.attr('class') || '';
$wrapper = $('<div>').addClass(settings.wrapperClass).addClass(classes).addClass(inputMode);
$control = $('<div>').addClass(settings.inputClass).addClass('items').appendTo($wrapper);
- $control_input = $('<input type="text" autocomplete="off" />').appendTo($control).attr('tabindex', tab_index);
+ $control_input = $('<input type="text" autocomplete="off" />').appendTo($control).attr('tabindex', $input.is(':disabled') ? '-1' : self.tabIndex);
$dropdown_parent = $(settings.dropdownParent || $wrapper);
- $dropdown = $('<div>').addClass(settings.dropdownClass).addClass(classes).addClass(inputMode).hide().appendTo($dropdown_parent);
+ $dropdown = $('<div>').addClass(settings.dropdownClass).addClass(inputMode).hide().appendTo($dropdown_parent);
$dropdown_content = $('<div>').addClass(settings.dropdownContentClass).appendTo($dropdown);
+ if(self.settings.copyClassesToDropdown) {
+ $dropdown.addClass(classes);
+ }
+
$wrapper.css({
width: $input[0].style.width
});
@@ -1194,6 +1203,12 @@
$control_input.attr('placeholder', settings.placeholder);
}
+ // if splitOn was not passed in, construct it from the delimiter to allow pasting universally
+ if (!self.settings.splitOn && self.settings.delimiter) {
+ var delimiterEscaped = self.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
+ self.settings.splitOn = new RegExp('\\s*' + delimiterEscaped + '+\\s*');
+ }
+
if ($input.attr('autocorrect')) {
$control_input.attr('autocorrect', $input.attr('autocorrect'));
}
@@ -1209,7 +1224,7 @@
self.$dropdown_content = $dropdown_content;
$dropdown.on('mouseenter', '[data-selectable]', function() { return self.onOptionHover.apply(self, arguments); });
- $dropdown.on('mousedown', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); });
+ $dropdown.on('mousedown click', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); });
watchChildEvent($control, 'mousedown', '*:not(input)', function() { return self.onItemSelect.apply(self, arguments); });
autoGrow($control_input);
@@ -1249,7 +1264,7 @@
}
// blur on click outside
if (!self.$control.has(e.target).length && e.target !== self.$control[0]) {
- self.blur();
+ self.blur(e.target);
}
}
});
@@ -1278,7 +1293,7 @@
}
// feature detect for the validation API
- if ($input[0].validity) {
+ if (SUPPORTS_VALIDITY_API) {
$input.on('invalid' + eventNS, function(e) {
e.preventDefault();
self.isInvalid = true;
@@ -1344,17 +1359,23 @@
*/
setupCallbacks: function() {
var key, fn, callbacks = {
- 'initialize' : 'onInitialize',
- 'change' : 'onChange',
- 'item_add' : 'onItemAdd',
- 'item_remove' : 'onItemRemove',
- 'clear' : 'onClear',
- 'option_add' : 'onOptionAdd',
- 'option_remove' : 'onOptionRemove',
- 'option_clear' : 'onOptionClear',
- 'dropdown_open' : 'onDropdownOpen',
- 'dropdown_close' : 'onDropdownClose',
- 'type' : 'onType'
+ 'initialize' : 'onInitialize',
+ 'change' : 'onChange',
+ 'item_add' : 'onItemAdd',
+ 'item_remove' : 'onItemRemove',
+ 'clear' : 'onClear',
+ 'option_add' : 'onOptionAdd',
+ 'option_remove' : 'onOptionRemove',
+ 'option_clear' : 'onOptionClear',
+ 'optgroup_add' : 'onOptionGroupAdd',
+ 'optgroup_remove' : 'onOptionGroupRemove',
+ 'optgroup_clear' : 'onOptionGroupClear',
+ 'dropdown_open' : 'onDropdownOpen',
+ 'dropdown_close' : 'onDropdownClose',
+ 'type' : 'onType',
+ 'load' : 'onLoad',
+ 'focus' : 'onFocus',
+ 'blur' : 'onBlur'
};
for (key in callbacks) {
@@ -1427,7 +1448,6 @@
this.$input.trigger('change');
},
-
/**
* Triggered on <input> paste.
*
@@ -1438,6 +1458,17 @@
var self = this;
if (self.isFull() || self.isInputHidden || self.isLocked) {
e.preventDefault();
+ } else {
+ // If a regex or string is included, this will split the pasted
+ // input and create Items for each separate value
+ if (self.settings.splitOn) {
+ setTimeout(function() {
+ var splitInput = $.trim(self.$control_input.val() || '').split(self.settings.splitOn);
+ for (var i = 0, n = splitInput.length; i < n; i++) {
+ self.createItem(splitInput[i]);
+ }
+ }, 0);
+ }
}
},
@@ -1450,7 +1481,7 @@
onKeyPress: function(e) {
if (this.isLocked) return e && e.preventDefault();
var character = String.fromCharCode(e.keyCode || e.which);
- if (this.settings.create && character === this.settings.delimiter) {
+ if (this.settings.create && this.settings.mode === 'multi' && character === this.settings.delimiter) {
this.createItem();
e.preventDefault();
return false;
@@ -1482,7 +1513,11 @@
}
break;
case KEY_ESC:
- self.close();
+ if (self.isOpen) {
+ e.preventDefault();
+ e.stopPropagation();
+ self.close();
+ }
return;
case KEY_N:
if (!e.ctrlKey || e.altKey) break;
@@ -1509,8 +1544,8 @@
case KEY_RETURN:
if (self.isOpen && self.$activeOption) {
self.onOptionSelect({currentTarget: self.$activeOption});
+ e.preventDefault();
}
- e.preventDefault();
return;
case KEY_LEFT:
self.advanceSelection(-1, e);
@@ -1521,7 +1556,12 @@
case KEY_TAB:
if (self.settings.selectOnTab && self.isOpen && self.$activeOption) {
self.onOptionSelect({currentTarget: self.$activeOption});
- e.preventDefault();
+
+ // Default behaviour is to jump to the next field, we only want this
+ // if the current field doesn't accept any more entries
+ if (!self.isFull()) {
+ e.preventDefault();
+ }
}
if (self.settings.create && self.createItem()) {
e.preventDefault();
@@ -1585,8 +1625,8 @@
*/
onFocus: function(e) {
var self = this;
+ var wasFocused = self.isFocused;
- self.isFocused = true;
if (self.isDisabled) {
self.blur();
e && e.preventDefault();
@@ -1594,8 +1634,11 @@
}
if (self.ignoreFocus) return;
+ self.isFocused = true;
if (self.settings.preload === 'focus') self.onSearchChange('');
+ if (!wasFocused) self.trigger('focus');
+
if (!self.$activeItems.length) {
self.showInput();
self.setActiveItem(null);
@@ -1609,31 +1652,43 @@
* Triggered on <input> blur.
*
* @param {object} e
- * @returns {boolean}
+ * @param {Element} dest
*/
- onBlur: function(e) {
+ onBlur: function(e, dest) {
var self = this;
+ if (!self.isFocused) return;
self.isFocused = false;
- if (self.ignoreFocus) return;
- // necessary to prevent IE closing the dropdown when the scrollbar is clicked
- if (!self.ignoreBlur && document.activeElement === self.$dropdown_content[0]) {
+ if (self.ignoreFocus) {
+ return;
+ } else if (!self.ignoreBlur && document.activeElement === self.$dropdown_content[0]) {
+ // necessary to prevent IE closing the dropdown when the scrollbar is clicked
self.ignoreBlur = true;
self.onFocus(e);
-
return;
}
+ var deactivate = function() {
+ self.close();
+ self.setTextboxValue('');
+ self.setActiveItem(null);
+ self.setActiveOption(null);
+ self.setCaret(self.items.length);
+ self.refreshState();
+
+ // IE11 bug: element still marked as active
+ (dest || document.body).focus();
+
+ self.ignoreFocus = false;
+ self.trigger('blur');
+ };
+
+ self.ignoreFocus = true;
if (self.settings.create && self.settings.createOnBlur) {
- self.createItem(false);
+ self.createItem(null, false, deactivate);
+ } else {
+ deactivate();
}
-
- self.close();
- self.setTextboxValue('');
- self.setActiveItem(null);
- self.setActiveOption(null);
- self.setCaret(self.items.length);
- self.refreshState();
},
/**
@@ -1665,14 +1720,20 @@
$target = $(e.currentTarget);
if ($target.hasClass('create')) {
- self.createItem();
+ self.createItem(null, function() {
+ if (self.settings.closeAfterSelect) {
+ self.close();
+ }
+ });
} else {
value = $target.attr('data-value');
- if (value) {
+ if (typeof value !== 'undefined') {
self.lastQuery = null;
self.setTextboxValue('');
self.addItem(value);
- if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
+ if (self.settings.closeAfterSelect) {
+ self.close();
+ } else if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
self.setActiveOption(self.getOption(value));
}
}
@@ -1705,7 +1766,7 @@
*/
load: function(fn) {
var self = this;
- var $wrapper = self.$wrapper.addClass('loading');
+ var $wrapper = self.$wrapper.addClass(self.settings.loadingClass);
self.loading++;
fn.apply(self, [function(results) {
@@ -1715,7 +1776,7 @@
self.refreshOptions(self.isFocused && !self.isInputHidden);
}
if (!self.loading) {
- $wrapper.removeClass('loading');
+ $wrapper.removeClass(self.settings.loadingClass);
}
self.trigger('load', results);
}]);
@@ -1756,10 +1817,12 @@
*
* @param {mixed} value
*/
- setValue: function(value) {
- debounce_events(this, ['change'], function() {
- this.clear();
- this.addItems(value);
+ setValue: function(value, silent) {
+ var events = silent ? [] : ['change'];
+
+ debounce_events(this, events, function() {
+ this.clear(silent);
+ this.addItems(value, silent);
});
},
@@ -1903,11 +1966,7 @@
},
/**
- * Gives the control focus. If "trigger" is falsy,
- * focus handlers won't be fired--causing the focus
- * to happen silently in the background.
- *
- * @param {boolean} trigger
+ * Gives the control focus.
*/
focus: function() {
var self = this;
@@ -1923,9 +1982,12 @@
/**
* Forces the control out of focus.
+ *
+ * @param {Element} dest
*/
- blur: function() {
- this.$control_input.trigger('blur');
+ blur: function(dest) {
+ this.$control_input[0].blur();
+ this.onBlur(null, dest);
},
/**
@@ -1952,7 +2014,7 @@
var settings = this.settings;
var sort = settings.sortField;
if (typeof sort === 'string') {
- sort = {field: sort};
+ sort = [{field: sort}];
}
return {
@@ -2026,7 +2088,7 @@
}
var self = this;
- var query = self.$control_input.val();
+ var query = $.trim(self.$control_input.val());
var results = self.search(query);
var $dropdown_content = self.$dropdown_content;
var active_before = self.$activeOption && hash_key(self.$activeOption.attr('data-value'));
@@ -2039,15 +2101,7 @@
// render and group available options individually
groups = {};
-
- if (self.settings.optgroupOrder) {
- groups_order = self.settings.optgroupOrder;
- for (i = 0; i < groups_order.length; i++) {
- groups[groups_order[i]] = [];
- }
- } else {
- groups_order = [];
- }
+ groups_order = [];
for (i = 0; i < n; i++) {
option = self.options[results.items[i].id];
@@ -2068,6 +2122,15 @@
}
}
+ // sort optgroups
+ if (this.settings.lockOptgroupOrder) {
+ groups_order.sort(function(a, b) {
+ var a_order = self.optgroups[a].$order || 0;
+ var b_order = self.optgroups[b].$order || 0;
+ return a_order - b_order;
+ });
+ }
+
// render optgroup headers & join groups
html = [];
for (i = 0, n = groups_order.length; i < n; i++) {
@@ -2102,7 +2165,7 @@
}
// add create option
- has_create_option = self.settings.create && self.canCreate(results.query);
+ has_create_option = self.canCreate(query);
if (has_create_option) {
$dropdown_content.prepend(self.render('option_create', {input: query}));
$create = $($dropdown_content[0].childNodes[0]);
@@ -2146,10 +2209,10 @@
*
* this.addOption(data)
*
- * @param {object} data
+ * @param {object|array} data
*/
addOption: function(data) {
- var i, n, optgroup, value, self = this;
+ var i, n, value, self = this;
if ($.isArray(data)) {
for (i = 0, n = data.length; i < n; i++) {
@@ -2158,13 +2221,40 @@
return;
}
- value = hash_key(data[self.settings.valueField]);
- if (!value || self.options.hasOwnProperty(value)) return;
+ if (value = self.registerOption(data)) {
+ self.userOptions[value] = true;
+ self.lastQuery = null;
+ self.trigger('option_add', value, data);
+ }
+ },
+
+ /**
+ * Registers an option to the pool of options.
+ *
+ * @param {object} data
+ * @return {boolean|string}
+ */
+ registerOption: function(data) {
+ var key = hash_key(data[this.settings.valueField]);
+ if ((!key || this.options.hasOwnProperty(key)) && !this.settings.allowEmptyOption) return false;
+ data.$order = data.$order || ++this.order;
+ this.options[key] = data;
+ return key;
+ },
- self.userOptions[value] = true;
- self.options[value] = data;
- self.lastQuery = null;
- self.trigger('option_add', value, data);
+ /**
+ * Registers an option group to the pool of option groups.
+ *
+ * @param {object} data
+ * @return {boolean|string}
+ */
+ registerOptionGroup: function(data) {
+ var key = hash_key(data[this.settings.optgroupValueField]);
+ if (!key) return false;
+
+ data.$order = data.$order || ++this.order;
+ this.optgroups[key] = data;
+ return key;
},
/**
@@ -2175,8 +2265,32 @@
* @param {object} data
*/
addOptionGroup: function(id, data) {
- this.optgroups[id] = data;
- this.trigger('optgroup_add', id, data);
+ data[this.settings.optgroupValueField] = id;
+ if (id = this.registerOptionGroup(data)) {
+ this.trigger('optgroup_add', id, data);
+ }
+ },
+
+ /**
+ * Removes an existing option group.
+ *
+ * @param {string} id
+ */
+ removeOptionGroup: function(id) {
+ if (this.optgroups.hasOwnProperty(id)) {
+ delete this.optgroups[id];
+ this.renderCache = {};
+ this.trigger('optgroup_remove', id);
+ }
+ },
+
+ /**
+ * Clears all existing option groups.
+ */
+ clearOptionGroups: function() {
+ this.optgroups = {};
+ this.renderCache = {};
+ this.trigger('optgroup_clear');
},
/**
@@ -2190,14 +2304,17 @@
updateOption: function(value, data) {
var self = this;
var $item, $item_new;
- var value_new, index_item, cache_items, cache_options;
+ var value_new, index_item, cache_items, cache_options, order_old;
value = hash_key(value);
value_new = hash_key(data[self.settings.valueField]);
// sanity checks
+ if (value === null) return;
if (!self.options.hasOwnProperty(value)) return;
- if (!value_new) throw new Error('Value must be set in option data');
+ if (typeof value_new !== 'string') throw new Error('Value must be set in option data');
+
+ order_old = self.options[value].$order;
// update references
if (value_new !== value) {
@@ -2207,6 +2324,7 @@
self.items.splice(index_item, 1, value_new);
}
}
+ data.$order = data.$order || order_old;
self.options[value_new] = data;
// invalidate render cache
@@ -2230,6 +2348,9 @@
$item.replaceWith($item_new);
}
+ // invalidate last query because we might have updated the sortField
+ self.lastQuery = null;
+
// update dropdown contents
if (self.isOpen) {
self.refreshOptions(false);
@@ -2240,8 +2361,9 @@
* Removes a single option.
*
* @param {string} value
+ * @param {boolean} silent
*/
- removeOption: function(value) {
+ removeOption: function(value, silent) {
var self = this;
value = hash_key(value);
@@ -2254,7 +2376,7 @@
delete self.options[value];
self.lastQuery = null;
self.trigger('option_remove', value);
- self.removeItem(value);
+ self.removeItem(value, silent);
},
/**
@@ -2309,7 +2431,7 @@
getElementWithValue: function(value, $els) {
value = hash_key(value);
- if (value) {
+ if (typeof value !== 'undefined' && value !== null) {
for (var i = 0, n = $els.length; i < n; i++) {
if ($els[i].getAttribute('data-value') === value) {
return $($els[i]);
@@ -2336,12 +2458,13 @@
* at the current caret position.
*
* @param {string} value
+ * @param {boolean} silent
*/
- addItems: function(values) {
+ addItems: function(values, silent) {
var items = $.isArray(values) ? values : [values];
for (var i = 0, n = items.length; i < n; i++) {
this.isPending = (i < n - 1);
- this.addItem(items[i]);
+ this.addItem(items[i], silent);
}
},
@@ -2350,9 +2473,12 @@
* at the current caret position.
*
* @param {string} value
+ * @param {boolean} silent
*/
- addItem: function(value) {
- debounce_events(this, ['change'], function() {
+ addItem: function(value, silent) {
+ var events = silent ? [] : ['change'];
+
+ debounce_events(this, events, function() {
var $item, $option, $options;
var self = this;
var inputMode = self.settings.mode;
@@ -2365,7 +2491,7 @@
}
if (!self.options.hasOwnProperty(value)) return;
- if (inputMode === 'single') self.clear();
+ if (inputMode === 'single') self.clear(silent);
if (inputMode === 'multi' && self.isFull()) return;
$item = $(self.render('item', self.options[value]));
@@ -2398,7 +2524,7 @@
self.updatePlaceholder();
self.trigger('item_add', value, $item);
- self.updateOriginalInput();
+ self.updateOriginalInput({silent: silent});
}
});
},
@@ -2409,7 +2535,7 @@
*
* @param {string} value
*/
- removeItem: function(value) {
+ removeItem: function(value, silent) {
var self = this;
var $item, i, idx;
@@ -2427,7 +2553,7 @@
self.items.splice(i, 1);
self.lastQuery = null;
if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) {
- self.removeOption(value);
+ self.removeOption(value, silent);
}
if (i < self.caretPos) {
@@ -2436,9 +2562,9 @@
self.refreshState();
self.updatePlaceholder();
- self.updateOriginalInput();
+ self.updateOriginalInput({silent: silent});
self.positionDropdown();
- self.trigger('item_remove', value);
+ self.trigger('item_remove', value, $item);
}
},
@@ -2450,19 +2576,30 @@
* Once this completes, it will be added
* to the item list.
*
+ * @param {string} value
+ * @param {boolean} [triggerDropdown]
+ * @param {function} [callback]
* @return {boolean}
*/
- createItem: function(triggerDropdown) {
+ createItem: function(input, triggerDropdown) {
var self = this;
- var input = $.trim(self.$control_input.val() || '');
var caret = self.caretPos;
- if (!self.canCreate(input)) return false;
- self.lock();
+ input = input || $.trim(self.$control_input.val() || '');
- if (typeof triggerDropdown === 'undefined') {
+ var callback = arguments[arguments.length - 1];
+ if (typeof callback !== 'function') callback = function() {};
+
+ if (typeof triggerDropdown !== 'boolean') {
triggerDropdown = true;
}
+ if (!self.canCreate(input)) {
+ callback();
+ return false;
+ }
+
+ self.lock();
+
var setup = (typeof self.settings.create === 'function') ? this.settings.create : function(input) {
var data = {};
data[self.settings.labelField] = input;
@@ -2473,15 +2610,16 @@
var create = once(function(data) {
self.unlock();
- if (!data || typeof data !== 'object') return;
+ if (!data || typeof data !== 'object') return callback();
var value = hash_key(data[self.settings.valueField]);
- if (!value) return;
+ if (typeof value !== 'string') return callback();
self.setTextboxValue('');
self.addOption(data);
self.setCaret(caret);
self.addItem(value);
self.refreshOptions(triggerDropdown && self.settings.mode !== 'single');
+ callback(data);
});
var output = setup.apply(this, [input, create]);
@@ -2499,9 +2637,7 @@
this.lastQuery = null;
if (this.isSetup) {
- for (var i = 0; i < this.items.length; i++) {
- this.addItem(this.items);
- }
+ this.addItem(this.items);
}
this.refreshState();
@@ -2561,13 +2697,15 @@
* Refreshes the original <select> or <input>
* element to reflect the current state.
*/
- updateOriginalInput: function() {
- var i, n, options, self = this;
+ updateOriginalInput: function(opts) {
+ var i, n, options, label, self = this;
+ opts = opts || {};
if (self.tagType === TAG_SELECT) {
options = [];
for (i = 0, n = self.items.length; i < n; i++) {
- options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected"></option>');
+ label = self.options[self.items[i]][self.settings.labelField] || '';
+ options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected">' + escape_html(label) + '</option>');
}
if (!options.length && !this.$input.attr('multiple')) {
options.push('<option value="" selected="selected"></option>');
@@ -2579,7 +2717,9 @@
}
if (self.isSetup) {
- self.trigger('change', self.$input.val());
+ if (!opts.silent) {
+ self.trigger('change', self.$input.val());
+ }
}
},
@@ -2654,8 +2794,10 @@
/**
* Resets / clears all selected items
* from the control.
+ *
+ * @param {boolean} silent
*/
- clear: function() {
+ clear: function(silent) {
var self = this;
if (!self.items.length) return;
@@ -2665,7 +2807,7 @@
self.setCaret(0);
self.setActiveItem(null);
self.updatePlaceholder();
- self.updateOriginalInput();
+ self.updateOriginalInput({silent: silent});
self.refreshState();
self.showInput();
self.trigger('clear');
@@ -2876,6 +3018,7 @@
disable: function() {
var self = this;
self.$input.prop('disabled', true);
+ self.$control_input.prop('disabled', true).prop('tabindex', -1);
self.isDisabled = true;
self.lock();
},
@@ -2887,6 +3030,7 @@
enable: function() {
var self = this;
self.$input.prop('disabled', false);
+ self.$control_input.prop('disabled', false).prop('tabindex', self.tabIndex);
self.isDisabled = false;
self.unlock();
},
@@ -2937,7 +3081,7 @@
var html = '';
var cache = false;
var self = this;
- var regex_tag = /^[\t ]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;
+ var regex_tag = /^[\t \r\n]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;
if (templateName === 'option' || templateName === 'item') {
value = hash_key(data[self.settings.valueField]);
@@ -2991,16 +3135,36 @@
} else {
delete self.renderCache[templateName];
}
- }
+ },
+ /**
+ * Determines whether or not to display the
+ * create item prompt, given a user input.
+ *
+ * @param {string} input
+ * @return {boolean}
+ */
+ canCreate: function(input) {
+ var self = this;
+ if (!self.settings.create) return false;
+ var filter = self.settings.createFilter;
+ return input.length
+ && (typeof filter !== 'function' || filter.apply(self, [input]))
+ && (typeof filter !== 'string' || new RegExp(filter).test(input))
+ && (!(filter instanceof RegExp) || filter.test(input));
+ }
});
Selectize.count = 0;
Selectize.defaults = {
+ options: [],
+ optgroups: [],
+
plugins: [],
delimiter: ',',
+ splitOn: null, // regexp or string for splitting up values from a paste command
persist: true,
diacritics: true,
create: false,
@@ -3014,9 +3178,12 @@
addPrecedence: false,
selectOnTab: false,
preload: false,
+ allowEmptyOption: false,
+ closeAfterSelect: false,
scrollDuration: 60,
loadThrottle: 300,
+ loadingClass: 'loading',
dataAttr: 'data-data',
optgroupField: 'optgroup',
@@ -3024,7 +3191,7 @@
labelField: 'text',
optgroupLabelField: 'label',
optgroupValueField: 'value',
- optgroupOrder: null,
+ lockOptgroupOrder: false,
sortField: '$order',
searchField: ['text'],
@@ -3038,21 +3205,26 @@
dropdownParent: null,
+ copyClassesToDropdown: true,
+
/*
- load : null, // function(query, callback) { ... }
- score : null, // function(search) { ... }
- onInitialize : null, // function() { ... }
- onChange : null, // function(value) { ... }
- onItemAdd : null, // function(value, $item) { ... }
- onItemRemove : null, // function(value) { ... }
- onClear : null, // function() { ... }
- onOptionAdd : null, // function(value, data) { ... }
- onOptionRemove : null, // function(value) { ... }
- onOptionClear : null, // function() { ... }
- onDropdownOpen : null, // function($dropdown) { ... }
- onDropdownClose : null, // function($dropdown) { ... }
- onType : null, // function(str) { ... }
- onDelete : null, // function(values) { ... }
+ load : null, // function(query, callback) { ... }
+ score : null, // function(search) { ... }
+ onInitialize : null, // function() { ... }
+ onChange : null, // function(value) { ... }
+ onItemAdd : null, // function(value, $item) { ... }
+ onItemRemove : null, // function(value) { ... }
+ onClear : null, // function() { ... }
+ onOptionAdd : null, // function(value, data) { ... }
+ onOptionRemove : null, // function(value) { ... }
+ onOptionClear : null, // function() { ... }
+ onOptionGroupAdd : null, // function(id, data) { ... }
+ onOptionGroupRemove : null, // function(id) { ... }
+ onOptionGroupClear : null, // function() { ... }
+ onDropdownOpen : null, // function($dropdown) { ... }
+ onDropdownClose : null, // function($dropdown) { ... }
+ onType : null, // function(str) { ... }
+ onDelete : null, // function(values) { ... }
*/
render: {
@@ -3066,6 +3238,7 @@
}
};
+
$.fn.selectize = function(settings_user) {
var defaults = $.fn.selectize.defaults;
var settings = $.extend({}, defaults, settings_user);
@@ -3083,19 +3256,27 @@
* @param {object} settings_element
*/
var init_textbox = function($input, settings_element) {
- var i, n, values, option, value = $.trim($input.val() || '');
- if (!value.length) return;
+ var i, n, values, option;
- values = value.split(settings.delimiter);
- for (i = 0, n = values.length; i < n; i++) {
- option = {};
- option[field_label] = values[i];
- option[field_value] = values[i];
+ var data_raw = $input.attr(attr_data);
- settings_element.options[values[i]] = option;
+ if (!data_raw) {
+ var value = $.trim($input.val() || '');
+ if (!settings.allowEmptyOption && !value.length) return;
+ values = value.split(settings.delimiter);
+ for (i = 0, n = values.length; i < n; i++) {
+ option = {};
+ option[field_label] = values[i];
+ option[field_value] = values[i];
+ settings_element.options.push(option);
+ }
+ settings_element.items = values;
+ } else {
+ settings_element.options = JSON.parse(data_raw);
+ for (i = 0, n = settings_element.options.length; i < n; i++) {
+ settings_element.items.push(settings_element.options[i][field_value]);
+ }
}
-
- settings_element.items = values;
};
/**
@@ -3107,6 +3288,7 @@
var init_select = function($input, settings_element) {
var i, n, tagName, $children, order = 0;
var options = settings_element.options;
+ var optionsMap = {};
var readData = function($el) {
var data = attr_data && $el.attr(attr_data);
@@ -3117,37 +3299,36 @@
};
var addOption = function($option, group) {
- var value, option;
-
$option = $($option);
- value = $option.attr('value') || '';
- if (!value.length) return;
+ var value = hash_key($option.attr('value'));
+ if (!value && !settings.allowEmptyOption) return;
// if the option already exists, it's probably been
// duplicated in another optgroup. in this case, push
// the current group to the "optgroup" property on the
// existing option so that it's rendered in both places.
- if (options.hasOwnProperty(value)) {
+ if (optionsMap.hasOwnProperty(value)) {
if (group) {
- if (!options[value].optgroup) {
- options[value].optgroup = group;
- } else if (!$.isArray(options[value].optgroup)) {
- options[value].optgroup = [options[value].optgroup, group];
+ var arr = optionsMap[value][field_optgroup];
+ if (!arr) {
+ optionsMap[value][field_optgroup] = group;
+ } else if (!$.isArray(arr)) {
+ optionsMap[value][field_optgroup] = [arr, group];
} else {
- options[value].optgroup.push(group);
+ arr.push(group);
}
}
return;
}
- option = readData($option) || {};
+ var option = readData($option) || {};
option[field_label] = option[field_label] || $option.text();
option[field_value] = option[field_value] || value;
option[field_optgroup] = option[field_optgroup] || group;
- option.$order = ++order;
- options[value] = option;
+ optionsMap[value] = option;
+ options.push(option);
if ($option.is(':selected')) {
settings_element.items.push(value);
@@ -3164,7 +3345,7 @@
optgroup = readData($optgroup) || {};
optgroup[field_optgroup_label] = id;
optgroup[field_optgroup_value] = id;
- settings_element.optgroups[id] = optgroup;
+ settings_element.optgroups.push(optgroup);
}
$options = $('option', $optgroup);
@@ -3192,10 +3373,15 @@
var instance;
var $input = $(this);
var tag_name = this.tagName.toLowerCase();
+ var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder');
+ if (!placeholder && !settings.allowEmptyOption) {
+ placeholder = $input.children('option[value=""]').text();
+ }
+
var settings_element = {
- 'placeholder' : $input.children('option[value=""]').text() || $input.attr('placeholder'),
- 'options' : {},
- 'optgroups' : {},
+ 'placeholder' : placeholder,
+ 'options' : [],
+ 'optgroups' : [],
'items' : []
};
@@ -3210,6 +3396,9 @@
};
$.fn.selectize.defaults = Selectize.defaults;
+ $.fn.selectize.support = {
+ validity: SUPPORTS_VALIDITY_API
+ };
Selectize.define('drag_drop', function(options) {
@@ -3452,7 +3641,7 @@
return option[this.settings.labelField];
};
- this.onKeyDown = (function(e) {
+ this.onKeyDown = (function() {
var original = self.onKeyDown;
return function(e) {
var index, option;
@@ -3473,5 +3662,6 @@
})();
});
+
return Selectize;
}));
diff --git a/app/assets/stylesheets/blazer/selectize.default.css b/app/assets/stylesheets/blazer/selectize.default.css
index 06d39647..ffd219c4 100644
--- a/app/assets/stylesheets/blazer/selectize.default.css
+++ b/app/assets/stylesheets/blazer/selectize.default.css
@@ -1,6 +1,6 @@
/**
- * selectize.default.css (v0.10.1) - Default Theme
- * Copyright (c) 2013 Brian Reavis & contributors
+ * selectize.default.css (v0.12.1) - Default Theme
+ * Copyright (c) 2013–2015 Brian Reavis & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
@@ -189,6 +189,7 @@
border: 1px solid #aaaaaa;
}
.selectize-input > input {
+ display: inline-block !important;
padding: 0 !important;
min-height: 0 !important;
max-height: none !important;
diff --git a/app/controllers/blazer/base_controller.rb b/app/controllers/blazer/base_controller.rb
new file mode 100644
index 00000000..965ab09d
--- /dev/null
+++ b/app/controllers/blazer/base_controller.rb
@@ -0,0 +1,22 @@
+module Blazer
+ class BaseController < ApplicationController
+ # skip all filters
+ skip_filter *_process_action_callbacks.map(&:filter)
+
+ protect_from_forgery with: :exception
+
+ if ENV["BLAZER_PASSWORD"]
+ http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
+ end
+
+ layout "blazer/application"
+
+ before_action :ensure_database_url
+
+ private
+
+ def ensure_database_url
+ render text: "BLAZER_DATABASE_URL required" if !ENV["BLAZER_DATABASE_URL"] && !Rails.env.development?
+ end
+ end
+end
diff --git a/app/controllers/blazer/checks_controller.rb b/app/controllers/blazer/checks_controller.rb
new file mode 100644
index 00000000..71055286
--- /dev/null
+++ b/app/controllers/blazer/checks_controller.rb
@@ -0,0 +1,52 @@
+module Blazer
+ class ChecksController < BaseController
+ before_action :set_check, only: [:show, :edit, :update, :destroy, :run]
+
+ def index
+ @checks = Blazer::Check.joins(:blazer_query).includes(:blazer_query).order("state, blazer_queries.name, blazer_checks.id").to_a
+ @checks.select! { |c| "#{c.blazer_query.name} #{c.emails}".downcase.include?(params[:q]) } if params[:q]
+ end
+
+ def new
+ @check = Blazer::Check.new
+ end
+
+ def create
+ @check = Blazer::Check.new(check_params)
+ @check.creator = current_user if respond_to?(:current_user) && Blazer.user_class
+
+ if @check.save
+ redirect_to run_check_path(@check)
+ else
+ render :new
+ end
+ end
+
+ def update
+ if @check.update(check_params)
+ redirect_to run_check_path(@check)
+ else
+ render :edit
+ end
+ end
+
+ def destroy
+ @check.destroy
+ redirect_to checks_path
+ end
+
+ def run
+ @query = @check.blazer_query
+ end
+
+ private
+
+ def check_params
+ params.require(:check).permit(:blazer_query_id, :emails)
+ end
+
+ def set_check
+ @check = Blazer::Check.find(params[:id])
+ end
+ end
+end
diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rb
index ad0cf9d2..0098b6f4 100644
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@ -1,23 +1,12 @@
module Blazer
- class QueriesController < ApplicationController
- # skip all filters
- skip_filter *_process_action_callbacks.map(&:filter)
-
- protect_from_forgery with: :exception
-
- if ENV["BLAZER_PASSWORD"]
- http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
- end
-
- layout "blazer/application"
-
- before_action :ensure_database_url
+ class QueriesController < BaseController
before_action :set_query, only: [:show, :edit, :update, :destroy]
def index
@queries = Blazer::Query.order(:name)
@queries = @queries.includes(:creator) if Blazer.user_class
@trending_queries = Blazer::Audit.group(:query_id).where("created_at > ?", 2.days.ago).having("COUNT(DISTINCT user_id) >= 3").uniq.count(:user_id)
+ @checks = Blazer::Check.group(:blazer_query_id).count
end
def new
@@ -42,9 +31,9 @@ module Blazer
@smart_vars = {}
@sql_errors = []
@bind_vars.each do |var|
- query = smart_variables[var]
+ query = Blazer.smart_variables[var]
if query
- rows, error = run_statement(query)
+ rows, error = Blazer.run_statement(query)
@smart_vars[var] = rows.map { |v| v.values.reverse }
@sql_errors << error if error
end
@@ -69,7 +58,13 @@ module Blazer
audit.save!
end
- @rows, @error = run_statement(@statement)
+ @rows, @error = Blazer.run_statement(@statement)
+
+ if @query
+ @query.blazer_checks.each do |check|
+ check.update_state(@rows, @error)
+ end
+ end
@columns = {}
if @rows.any?
@@ -88,19 +83,19 @@ module Blazer
@filename = @query.name.parameterize if @query
- @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || smart_columns[k] }.keys
+ @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || Blazer.smart_columns[k] }.keys
@boom = {}
@columns.keys.each do |key|
- query = smart_columns[key]
+ query = Blazer.smart_columns[key]
if query
values = @rows.map { |r| r[key] }.compact.uniq
- rows, error = run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
+ rows, error = Blazer.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
@boom[key] = Hash[rows.map(&:values)]
end
end
- @linked_columns = linked_columns
+ @linked_columns = Blazer.linked_columns
end
respond_to do |format|
@@ -128,10 +123,6 @@ module Blazer
private
- def ensure_database_url
- render text: "BLAZER_DATABASE_URL required" if !ENV["BLAZER_DATABASE_URL"] && !Rails.env.development?
- end
-
def set_query
@query = Blazer::Query.find(params[:id].to_s.split("-").first)
end
@@ -151,28 +142,6 @@ module Blazer
end
end
- def run_statement(statement)
- rows = []
- error = nil
- begin
- Blazer::Connection.transaction do
- Blazer::Connection.connection.execute("SET statement_timeout = #{Blazer.timeout * 1000}") if Blazer.timeout && postgresql?
- result = Blazer::Connection.connection.select_all(statement)
- result.each do |untyped_row|
- row = {}
- untyped_row.each do |k, v|
- row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v)
- end
- rows << row
- end
- raise ActiveRecord::Rollback
- end
- rescue ActiveRecord::StatementInvalid => e
- error = e.message.sub(/.+ERROR: /, "")
- end
- [rows, error]
- end
-
def extract_vars(statement)
statement.scan(/\{.*?\}/).map { |v| v[1...-1] }.uniq
end
@@ -197,33 +166,5 @@ module Blazer
params.except(:controller, :action, :id, :host, :query, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement)
end
helper_method :variable_params
-
- def settings
- YAML.load(File.read(Rails.root.join("config", "blazer.yml")))
- end
-
- def linked_columns
- settings["linked_columns"] || {}
- end
-
- def smart_columns
- settings["smart_columns"] || {}
- end
-
- def smart_variables
- settings["smart_variables"] || {}
- end
-
- def tables
- default_schema = postgresql? ? "public" : Blazer::Connection.connection_config[:database]
- schema = Blazer::Connection.connection_config[:schema] || default_schema
- rows, error = run_statement(Blazer::Connection.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))
- Hash[rows.group_by { |r| r["table_name"] }.map { |t, f| [t, f.sort_by { |f| f["ordinal_position"] }.map { |f| f.slice("column_name", "data_type") }] }.sort_by { |t, _f| t }]
- end
- helper_method :tables
-
- def postgresql?
- Blazer::Connection.connection.adapter_name == "PostgreSQL"
- end
end
end
diff --git a/app/helpers/blazer/queries_helper.rb b/app/helpers/blazer/base_helper.rb
similarity index 94%
rename from app/helpers/blazer/queries_helper.rb
rename to app/helpers/blazer/base_helper.rb
index b681f7a6..cdd49d52 100644
--- a/app/helpers/blazer/queries_helper.rb
+++ b/app/helpers/blazer/base_helper.rb
@@ -1,5 +1,5 @@
module Blazer
- module QueriesHelper
+ module BaseHelper
def title(title = nil)
if title
content_for(:title) { title }
diff --git a/app/mailers/blazer/check_mailer.rb b/app/mailers/blazer/check_mailer.rb
new file mode 100644
index 00000000..253b0393
--- /dev/null
+++ b/app/mailers/blazer/check_mailer.rb
@@ -0,0 +1,21 @@
+module Blazer
+ class CheckMailer < ActionMailer::Base
+ include ActionView::Helpers::TextHelper
+
+ default from: Blazer.from_email if Blazer.from_email
+
+ def state_change(check, state, state_was, rows, error)
+ @check = check
+ @state = state
+ @state_was = state_was
+ @rows = rows
+ @error = error
+ mail to: check.emails, subject: "Check #{state.titleize}: #{check.blazer_query.name}"
+ end
+
+ def failing_checks(email, checks)
+ @checks = checks
+ mail to: email, subject: "#{pluralize(checks.size, "Check")} Failing"
+ end
+ end
+end
diff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rb
new file mode 100644
index 00000000..13e98bea
--- /dev/null
+++ b/app/models/blazer/check.rb
@@ -0,0 +1,22 @@
+module Blazer
+ class Check < ActiveRecord::Base
+ belongs_to :blazer_query, class_name: "Blazer::Query"
+
+ def update_state(rows, error)
+ self.state =
+ if error
+ "error"
+ elsif rows.any?
+ "failing"
+ else
+ "passing"
+ end
+
+ # do not notify on creation, except when not passing
+ if (state_was || state != "passing") && state != state_was && emails.present?
+ Blazer::CheckMailer.state_change(self, state, state_was, rows, error).deliver_now
+ end
+ save!
+ end
+ end
+end
diff --git a/app/models/blazer/query.rb b/app/models/blazer/query.rb
index d455df1e..9abce275 100644
--- a/app/models/blazer/query.rb
+++ b/app/models/blazer/query.rb
@@ -1,6 +1,7 @@
module Blazer
class Query < ActiveRecord::Base
belongs_to :creator, class_name: Blazer.user_class.to_s if Blazer.user_class
+ has_many :blazer_checks, class_name: "Blazer::Check", foreign_key: "blazer_query_id", dependent: :destroy
validates :name, presence: true
validates :statement, presence: true
diff --git a/app/views/blazer/check_mailer/failing_checks.html.erb b/app/views/blazer/check_mailer/failing_checks.html.erb
new file mode 100644
index 00000000..05bde140
--- /dev/null
+++ b/app/views/blazer/check_mailer/failing_checks.html.erb
@@ -0,0 +1,6 @@
+<ul>
+ <% @checks.each do |check| %>
+ <li><%= link_to check.blazer_query.name, query_url(check.blazer_query_id) %></li>
+ <% end %>
+</ul>
+<p><%= link_to "Manage checks", checks_url %></p>
diff --git a/app/views/blazer/check_mailer/state_change.html.erb b/app/views/blazer/check_mailer/state_change.html.erb
new file mode 100644
index 00000000..db6e6027
--- /dev/null
+++ b/app/views/blazer/check_mailer/state_change.html.erb
@@ -0,0 +1,6 @@
+<p><%= link_to "View", query_url(@check.blazer_query_id) %></p>
+<% if @error %>
+ <p><%= @error %></p>
+<% elsif @rows.any? %>
+ <p><%= pluralize(@rows.size, "row") %></p>
+<% end %>
diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erb
new file mode 100644
index 00000000..ddcd733d
--- /dev/null
+++ b/app/views/blazer/checks/_form.html.erb
@@ -0,0 +1,23 @@
+<p class="text-muted">Checks are designed to identify bad data. A check fails if there are any results.</p>
+<%= form_for @check do |f| %>
+ <div class="form-group">
+ <%= f.label :blazer_query_id, "Query" %>
+ <%= f.select :blazer_query_id, Blazer::Query.order(:name).map { |q| [q.name, q.id] } %>
+ </div>
+ <div class="form-group">
+ <%= f.label :emails %>
+ <%= f.text_field :emails, placeholder: "Optional, comma separated", class: "form-control" %>
+ </div>
+ <p class="text-muted">Emails are sent when a check starts failing, and when it starts passing again.
+ <p>
+ <% if @check.persisted? %>
+ <%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
+ <% end %>
+ <%= f.submit "Save", class: "btn btn-success" %>
+ </p>
+<% end %>
+
+<script>
+ $("#check_blazer_query_id").selectize();
+ $("#check_schedule").selectize({"allowEmptyOption": true});
+</script>
diff --git a/app/views/blazer/checks/edit.html.erb b/app/views/blazer/checks/edit.html.erb
new file mode 100644
index 00000000..d44c608b
--- /dev/null
+++ b/app/views/blazer/checks/edit.html.erb
@@ -0,0 +1 @@
+<%= render partial: "form" %>
diff --git a/app/views/blazer/checks/index.html.erb b/app/views/blazer/checks/index.html.erb
new file mode 100644
index 00000000..51962918
--- /dev/null
+++ b/app/views/blazer/checks/index.html.erb
@@ -0,0 +1,41 @@
+<% title "Checks" %>
+
+<p style="float: right;"><%= link_to "New Check", new_check_path, class: "btn btn-info" %></p>
+<p>
+ <%= link_to "Home", root_path, class: "btn btn-primary", style: "margin-right: 10px;" %>
+</p>
+
+<% colors = {failing: "red", passing: "#5cb85c", error: "#666"} %>
+<table class="table">
+ <thead>
+ <tr>
+ <th>Query</th>
+ <th style="width: 15%;">State</th>
+ <th style="width: 20%;">Emails</th>
+ <th style="width: 15%;"></th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @checks.each do |check| %>
+ <tr>
+ <td><%= link_to check.blazer_query.name, check.blazer_query %></td>
+ <td>
+ <% if check.state %>
+ <small style="font-weight: bold; color: <%= colors[check.state.to_sym] %>;"><%= check.state.upcase %></small>
+ <% end %>
+ </td>
+ <td>
+ <ul class="list-unstyled" style="margin-bottom: 0;">
+ <% check.emails.to_s.split(",").each do |email| %>
+ <li><%= email %></li>
+ <% end %>
+ </ul>
+ </td>
+ <td style="text-align: right; padding: 1px;">
+ <%= link_to "Edit", edit_check_path(check), class: "btn btn-info" %>
+ <%= link_to "Run Now", run_check_path(check), class: "btn btn-primary" %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
diff --git a/app/views/blazer/checks/new.html.erb b/app/views/blazer/checks/new.html.erb
new file mode 100644
index 00000000..d44c608b
--- /dev/null
+++ b/app/views/blazer/checks/new.html.erb
@@ -0,0 +1 @@
+<%= render partial: "form" %>
diff --git a/app/views/blazer/checks/run.html.erb b/app/views/blazer/checks/run.html.erb
new file mode 100644
index 00000000..e7a277dd
--- /dev/null
+++ b/app/views/blazer/checks/run.html.erb
@@ -0,0 +1,9 @@
+<p style="text-muted">Running check...</p>
+
+<script>
+ $.post("<%= run_queries_path %>", <%= json_escape({statement: @query.statement, query_id: @query.id}.to_json).html_safe %>, function (data) {
+ setTimeout( function () {
+ window.location.href = "<%= checks_path %>";
+ }, 200);
+ });
+</script>
diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erb
index 7e606a72..e9884621 100644
--- a/app/views/blazer/queries/_form.html.erb
+++ b/app/views/blazer/queries/_form.html.erb
@@ -16,7 +16,7 @@
<%= link_to "Back", :back %>
<span class="text-muted" style="margin-left: 20px;"> Use {start_time} and {end_time} for time ranges</span>
</div>
- <%= select_tag :table_names, options_for_select([["Preview table", nil]] + tables.keys), style: "margin-right: 20px; width: 240px;" %>
+ <%= select_tag :table_names, options_for_select([["Preview table", nil]] + Blazer.tables.keys), style: "margin-right: 20px; width: 240px;" %>
<%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top;" %>
</div>
</div>
diff --git a/app/views/blazer/queries/index.html.erb b/app/views/blazer/queries/index.html.erb
index e285f20c..4b6f0647 100644
--- a/app/views/blazer/queries/index.html.erb
+++ b/app/views/blazer/queries/index.html.erb
@@ -2,6 +2,7 @@
<div id="header" style="margin-bottom: 20px;">
<div class="pull-right">
<%= link_to "New Query", new_query_path, class: "btn btn-info" %>
+ <%= link_to "Checks", checks_path, class: "btn btn-primary" %>
</div>
<input type="text" placeholder="Start typing a query or person" style="width: 300px; display: inline-block;" autofocus=true class="search form-control" />
</div>
@@ -24,6 +25,9 @@
<% if @trending_queries[query.id] %>
<small style="font-weight: bold; color: #f60;">TRENDING</small>
<% end %>
+ <% if @checks[query.id] %>
+ <small style="font-weight: bold; color: #f60;">CHECK</small>
+ <% end %>
<div class="hide"><%= query.name.gsub(/\s+/, "") %></div>
</td>
<td class="creator text-right text-muted">
diff --git a/config/routes.rb b/config/routes.rb
index 3669441c..9bcc7a4a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,5 +2,8 @@ Blazer::Engine.routes.draw do
resources :queries, except: [:index] do
post :run, on: :collection # err on the side of caution
end
+ resources :checks, except: [:show] do
+ get :run, on: :member
+ end
root to: "queries#index"
end
diff --git a/lib/blazer.rb b/lib/blazer.rb
index ccd5786d..c2eb3365 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -2,6 +2,7 @@ require "csv"
require "chartkick"
require "blazer/version"
require "blazer/engine"
+require "blazer/tasks"
module Blazer
class << self
@@ -10,6 +11,7 @@ module Blazer
attr_accessor :user_name
attr_accessor :user_class
attr_accessor :timeout
+ attr_accessor :from_email
end
self.audit = true
self.user_name = :name
@@ -18,4 +20,73 @@ module Blazer
def self.time_zone=(time_zone)
@time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
end
+
+ def self.settings
+ @settings ||= YAML.load(File.read(Rails.root.join("config", "blazer.yml")))
+ end
+
+ def self.linked_columns
+ settings["linked_columns"] || {}
+ end
+
+ def self.smart_columns
+ settings["smart_columns"] || {}
+ end
+
+ def self.smart_variables
+ settings["smart_variables"] || {}
+ end
+
+ def self.run_statement(statement)
+ rows = []
+ error = nil
+ begin
+ Blazer::Connection.transaction do
+ Blazer::Connection.connection.execute("SET statement_timeout = #{Blazer.timeout * 1000}") if Blazer.timeout && postgresql?
+ result = Blazer::Connection.connection.select_all(statement)
+ result.each do |untyped_row|
+ row = {}
+ untyped_row.each do |k, v|
+ row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v)
+ end
+ rows << row
+ end
+ raise ActiveRecord::Rollback
+ end
+ rescue ActiveRecord::StatementInvalid => e
+ error = e.message.sub(/.+ERROR: /, "")
+ end
+ [rows, error]
+ end
+
+ def self.tables
+ default_schema = postgresql? ? "public" : Blazer::Connection.connection_config[:database]
+ schema = Blazer::Connection.connection_config[:schema] || default_schema
+ rows, error = run_statement(Blazer::Connection.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))
+ Hash[rows.group_by { |r| r["table_name"] }.map { |t, f| [t, f.sort_by { |f| f["ordinal_position"] }.map { |f| f.slice("column_name", "data_type") }] }.sort_by { |t, _f| t }]
+ end
+
+ def self.postgresql?
+ Blazer::Connection.connection.adapter_name == "PostgreSQL"
+ end
+
+ def self.run_checks
+ Blazer::Check.includes(:blazer_query).find_each do |check|
+ rows, error = run_statement(check.blazer_query.statement)
+ check.update_state(rows, error)
+ end
+ end
+
+ def self.send_failing_checks
+ emails = {}
+ Blazer::Check.includes(:blazer_query).where(state: %w[failing error]).find_each do |check|
+ check.emails.to_s.split(",").each do |email|
+ (emails[email] ||= []) << check
+ end
+ end
+
+ emails.each do |email, checks|
+ Blazer::CheckMailer.failing_checks(email, checks).deliver_now
+ end
+ end
end
diff --git a/lib/blazer/tasks.rb b/lib/blazer/tasks.rb
new file mode 100644
index 00000000..cd45b661
--- /dev/null
+++ b/lib/blazer/tasks.rb
@@ -0,0 +1,12 @@
+require "rake"
+
+namespace :blazer do
+ desc "run checks"
+ task run_checks: :environment do
+ Blazer.run_checks
+ end
+
+ task send_failing_checks: :environment do
+ Blazer.send_failing_checks
+ end
+end
diff --git a/lib/generators/blazer/templates/install.rb b/lib/generators/blazer/templates/install.rb
index 3582c405..a1abce32 100644
--- a/lib/generators/blazer/templates/install.rb
+++ b/lib/generators/blazer/templates/install.rb
@@ -14,5 +14,12 @@ class <%= migration_class_name %> < ActiveRecord::Migration
t.text :statement
t.timestamp :created_at
end
+
+ create_table :blazer_checks do |t|
+ t.references :blazer_query
+ t.string :state
+ t.text :emails
+ t.timestamps
+ end
end
end