Skip to content

Business intelligence made simple

Revert checks for now

Andrew Kane90686e36b423
diff --git a/README.md b/README.md
index dc8ba831..4b739c03 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,6 @@ 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
@@ -68,7 +67,7 @@ ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO blazer;
COMMIT;
```
-It is recommended to protect sensitive information with views. Documentation coming soon.
+It is **highly, highly recommended** to protect sensitive information with views. Documentation coming soon.
### MySQL
@@ -79,7 +78,7 @@ GRANT SELECT, SHOW VIEW ON database_name.* TO blazer@’127.0.0.1′ IDENTIFIED
FLUSH PRIVILEGES;
```
-It is recommended to protect sensitive information with views. Documentation coming soon.
+It is **highly, highly recommended** to protect sensitive information with views. Documentation coming soon.
## Authentication
@@ -97,31 +96,11 @@ ENV["BLAZER_PASSWORD"] = "secret"
### Devise
```ruby
-authenticate :user, lambda { |user| user.admin? } do
+authenticate :user, lambda{|user| user.admin? } do
mount Blazer::Engine, at: "blazer"
end
```
-## Checks [master]
-
-Set up checks to run every hour.
-
-```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
@@ -166,27 +145,6 @@ 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 63ec321f..882a5891 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 = asciifold(String(a || ''));
- b = asciifold(String(b || ''));
+ a = String(a || '').toLowerCase();
+ b = String(b || '').toLowerCase();
if (a > b) return 1;
if (b > a) return -1;
return 0;
@@ -425,44 +425,21 @@
};
var DIACRITICS = {
- 'a': '[aÀÁÂÃÄÅàáâãäåĀāąĄ]',
+ 'a': '[aÀÁÂÃÄÅàáâãäå]',
'c': '[cÇçćĆčČ]',
'd': '[dđĐďĎ]',
- 'e': '[eÈÉÊËèéêëěĚĒēęĘ]',
- 'i': '[iÌÍÎÏìíîïĪī]',
- 'l': '[lłŁ]',
- 'n': '[nÑñňŇńŃ]',
- 'o': '[oÒÓÔÕÕÖØòóôõöøŌō]',
+ 'e': '[eÈÉÊËèéêëěĚ]',
+ 'i': '[iÌÍÎÏìíîï]',
+ '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
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -608,8 +585,8 @@
}));
/**
- * selectize.js (v0.12.1)
- * Copyright (c) 2013–2015 Brian Reavis & contributors
+ * selectize.js (v0.10.1)
+ * Copyright (c) 2013 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:
@@ -730,8 +707,6 @@
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';
@@ -751,10 +726,10 @@
* 1 -> '1'
*
* @param {string} value
- * @returns {string|null}
+ * @returns {string}
*/
var hash_key = function(value) {
- if (typeof value === 'undefined' || value === null) return null;
+ if (typeof value === 'undefined' || value === null) return '';
if (typeof value === 'boolean') return value ? '1' : '0';
return value + '';
};
@@ -818,6 +793,25 @@
};
};
+ /**
+ * 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.
*
@@ -1059,16 +1053,13 @@
input.selectize = self;
// detect rtl environment
- var computedStyle = window.getComputedStyle && window.getComputedStyle(input, null);
- dir = computedStyle ? computedStyle.getPropertyValue('direction') : input.currentStyle && input.currentStyle.direction;
+ dir = window.getComputedStyle ? window.getComputedStyle(input, null).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),
@@ -1110,20 +1101,12 @@
self.sifter = new Sifter(this.options, {diacritics: settings.diacritics});
// build options table
- 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;
- }
+ $.extend(self.options, build_hash_table(settings.valueField, settings.options));
+ delete self.settings.options;
// build optgroup table
- 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;
- }
+ $.extend(self.optgroups, build_hash_table(settings.optgroupValueField, settings.optgroups));
+ delete self.settings.optgroups;
// option-dependent defaults
self.settings.mode = self.settings.mode || (self.settings.maxItems === 1 ? 'single' : 'multi');
@@ -1131,6 +1114,16 @@
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();
@@ -1168,23 +1161,21 @@
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', $input.is(':disabled') ? '-1' : self.tabIndex);
+ $control_input = $('<input type="text" autocomplete="off" />').appendTo($control).attr('tabindex', tab_index);
$dropdown_parent = $(settings.dropdownParent || $wrapper);
- $dropdown = $('<div>').addClass(settings.dropdownClass).addClass(inputMode).hide().appendTo($dropdown_parent);
+ $dropdown = $('<div>').addClass(settings.dropdownClass).addClass(classes).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
});
@@ -1203,12 +1194,6 @@
$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'));
}
@@ -1224,7 +1209,7 @@
self.$dropdown_content = $dropdown_content;
$dropdown.on('mouseenter', '[data-selectable]', function() { return self.onOptionHover.apply(self, arguments); });
- $dropdown.on('mousedown click', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); });
+ $dropdown.on('mousedown', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); });
watchChildEvent($control, 'mousedown', '*:not(input)', function() { return self.onItemSelect.apply(self, arguments); });
autoGrow($control_input);
@@ -1264,7 +1249,7 @@
}
// blur on click outside
if (!self.$control.has(e.target).length && e.target !== self.$control[0]) {
- self.blur(e.target);
+ self.blur();
}
}
});
@@ -1293,7 +1278,7 @@
}
// feature detect for the validation API
- if (SUPPORTS_VALIDITY_API) {
+ if ($input[0].validity) {
$input.on('invalid' + eventNS, function(e) {
e.preventDefault();
self.isInvalid = true;
@@ -1359,23 +1344,17 @@
*/
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',
- 'optgroup_add' : 'onOptionGroupAdd',
- 'optgroup_remove' : 'onOptionGroupRemove',
- 'optgroup_clear' : 'onOptionGroupClear',
- 'dropdown_open' : 'onDropdownOpen',
- 'dropdown_close' : 'onDropdownClose',
- 'type' : 'onType',
- 'load' : 'onLoad',
- 'focus' : 'onFocus',
- 'blur' : 'onBlur'
+ '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'
};
for (key in callbacks) {
@@ -1448,6 +1427,7 @@
this.$input.trigger('change');
},
+
/**
* Triggered on <input> paste.
*
@@ -1458,17 +1438,6 @@
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);
- }
}
},
@@ -1481,7 +1450,7 @@
onKeyPress: function(e) {
if (this.isLocked) return e && e.preventDefault();
var character = String.fromCharCode(e.keyCode || e.which);
- if (this.settings.create && this.settings.mode === 'multi' && character === this.settings.delimiter) {
+ if (this.settings.create && character === this.settings.delimiter) {
this.createItem();
e.preventDefault();
return false;
@@ -1513,11 +1482,7 @@
}
break;
case KEY_ESC:
- if (self.isOpen) {
- e.preventDefault();
- e.stopPropagation();
- self.close();
- }
+ self.close();
return;
case KEY_N:
if (!e.ctrlKey || e.altKey) break;
@@ -1544,8 +1509,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);
@@ -1556,12 +1521,7 @@
case KEY_TAB:
if (self.settings.selectOnTab && self.isOpen && self.$activeOption) {
self.onOptionSelect({currentTarget: self.$activeOption});
-
- // 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();
- }
+ e.preventDefault();
}
if (self.settings.create && self.createItem()) {
e.preventDefault();
@@ -1625,8 +1585,8 @@
*/
onFocus: function(e) {
var self = this;
- var wasFocused = self.isFocused;
+ self.isFocused = true;
if (self.isDisabled) {
self.blur();
e && e.preventDefault();
@@ -1634,11 +1594,8 @@
}
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);
@@ -1652,43 +1609,31 @@
* Triggered on <input> blur.
*
* @param {object} e
- * @param {Element} dest
+ * @returns {boolean}
*/
- onBlur: function(e, dest) {
+ onBlur: function(e) {
var self = this;
- if (!self.isFocused) return;
self.isFocused = false;
+ if (self.ignoreFocus) return;
- 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
+ // necessary to prevent IE closing the dropdown when the scrollbar is clicked
+ if (!self.ignoreBlur && document.activeElement === self.$dropdown_content[0]) {
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(null, false, deactivate);
- } else {
- deactivate();
+ self.createItem(false);
}
+
+ self.close();
+ self.setTextboxValue('');
+ self.setActiveItem(null);
+ self.setActiveOption(null);
+ self.setCaret(self.items.length);
+ self.refreshState();
},
/**
@@ -1720,20 +1665,14 @@
$target = $(e.currentTarget);
if ($target.hasClass('create')) {
- self.createItem(null, function() {
- if (self.settings.closeAfterSelect) {
- self.close();
- }
- });
+ self.createItem();
} else {
value = $target.attr('data-value');
- if (typeof value !== 'undefined') {
+ if (value) {
self.lastQuery = null;
self.setTextboxValue('');
self.addItem(value);
- if (self.settings.closeAfterSelect) {
- self.close();
- } else if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
+ if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
self.setActiveOption(self.getOption(value));
}
}
@@ -1766,7 +1705,7 @@
*/
load: function(fn) {
var self = this;
- var $wrapper = self.$wrapper.addClass(self.settings.loadingClass);
+ var $wrapper = self.$wrapper.addClass('loading');
self.loading++;
fn.apply(self, [function(results) {
@@ -1776,7 +1715,7 @@
self.refreshOptions(self.isFocused && !self.isInputHidden);
}
if (!self.loading) {
- $wrapper.removeClass(self.settings.loadingClass);
+ $wrapper.removeClass('loading');
}
self.trigger('load', results);
}]);
@@ -1817,12 +1756,10 @@
*
* @param {mixed} value
*/
- setValue: function(value, silent) {
- var events = silent ? [] : ['change'];
-
- debounce_events(this, events, function() {
- this.clear(silent);
- this.addItems(value, silent);
+ setValue: function(value) {
+ debounce_events(this, ['change'], function() {
+ this.clear();
+ this.addItems(value);
});
},
@@ -1966,7 +1903,11 @@
},
/**
- * Gives the control focus.
+ * 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
*/
focus: function() {
var self = this;
@@ -1982,12 +1923,9 @@
/**
* Forces the control out of focus.
- *
- * @param {Element} dest
*/
- blur: function(dest) {
- this.$control_input[0].blur();
- this.onBlur(null, dest);
+ blur: function() {
+ this.$control_input.trigger('blur');
},
/**
@@ -2014,7 +1952,7 @@
var settings = this.settings;
var sort = settings.sortField;
if (typeof sort === 'string') {
- sort = [{field: sort}];
+ sort = {field: sort};
}
return {
@@ -2088,7 +2026,7 @@
}
var self = this;
- var query = $.trim(self.$control_input.val());
+ var query = 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'));
@@ -2101,7 +2039,15 @@
// render and group available options individually
groups = {};
- groups_order = [];
+
+ if (self.settings.optgroupOrder) {
+ groups_order = self.settings.optgroupOrder;
+ for (i = 0; i < groups_order.length; i++) {
+ groups[groups_order[i]] = [];
+ }
+ } else {
+ groups_order = [];
+ }
for (i = 0; i < n; i++) {
option = self.options[results.items[i].id];
@@ -2122,15 +2068,6 @@
}
}
- // 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++) {
@@ -2165,7 +2102,7 @@
}
// add create option
- has_create_option = self.canCreate(query);
+ has_create_option = self.settings.create && self.canCreate(results.query);
if (has_create_option) {
$dropdown_content.prepend(self.render('option_create', {input: query}));
$create = $($dropdown_content[0].childNodes[0]);
@@ -2209,10 +2146,10 @@
*
* this.addOption(data)
*
- * @param {object|array} data
+ * @param {object} data
*/
addOption: function(data) {
- var i, n, value, self = this;
+ var i, n, optgroup, value, self = this;
if ($.isArray(data)) {
for (i = 0, n = data.length; i < n; i++) {
@@ -2221,40 +2158,13 @@
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;
- },
-
- /**
- * 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;
+ value = hash_key(data[self.settings.valueField]);
+ if (!value || self.options.hasOwnProperty(value)) return;
- data.$order = data.$order || ++this.order;
- this.optgroups[key] = data;
- return key;
+ self.userOptions[value] = true;
+ self.options[value] = data;
+ self.lastQuery = null;
+ self.trigger('option_add', value, data);
},
/**
@@ -2265,32 +2175,8 @@
* @param {object} data
*/
addOptionGroup: function(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');
+ this.optgroups[id] = data;
+ this.trigger('optgroup_add', id, data);
},
/**
@@ -2304,17 +2190,14 @@
updateOption: function(value, data) {
var self = this;
var $item, $item_new;
- var value_new, index_item, cache_items, cache_options, order_old;
+ var value_new, index_item, cache_items, cache_options;
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 (typeof value_new !== 'string') throw new Error('Value must be set in option data');
-
- order_old = self.options[value].$order;
+ if (!value_new) throw new Error('Value must be set in option data');
// update references
if (value_new !== value) {
@@ -2324,7 +2207,6 @@
self.items.splice(index_item, 1, value_new);
}
}
- data.$order = data.$order || order_old;
self.options[value_new] = data;
// invalidate render cache
@@ -2348,9 +2230,6 @@
$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);
@@ -2361,9 +2240,8 @@
* Removes a single option.
*
* @param {string} value
- * @param {boolean} silent
*/
- removeOption: function(value, silent) {
+ removeOption: function(value) {
var self = this;
value = hash_key(value);
@@ -2376,7 +2254,7 @@
delete self.options[value];
self.lastQuery = null;
self.trigger('option_remove', value);
- self.removeItem(value, silent);
+ self.removeItem(value);
},
/**
@@ -2431,7 +2309,7 @@
getElementWithValue: function(value, $els) {
value = hash_key(value);
- if (typeof value !== 'undefined' && value !== null) {
+ if (value) {
for (var i = 0, n = $els.length; i < n; i++) {
if ($els[i].getAttribute('data-value') === value) {
return $($els[i]);
@@ -2458,13 +2336,12 @@
* at the current caret position.
*
* @param {string} value
- * @param {boolean} silent
*/
- addItems: function(values, silent) {
+ addItems: function(values) {
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], silent);
+ this.addItem(items[i]);
}
},
@@ -2473,12 +2350,9 @@
* at the current caret position.
*
* @param {string} value
- * @param {boolean} silent
*/
- addItem: function(value, silent) {
- var events = silent ? [] : ['change'];
-
- debounce_events(this, events, function() {
+ addItem: function(value) {
+ debounce_events(this, ['change'], function() {
var $item, $option, $options;
var self = this;
var inputMode = self.settings.mode;
@@ -2491,7 +2365,7 @@
}
if (!self.options.hasOwnProperty(value)) return;
- if (inputMode === 'single') self.clear(silent);
+ if (inputMode === 'single') self.clear();
if (inputMode === 'multi' && self.isFull()) return;
$item = $(self.render('item', self.options[value]));
@@ -2524,7 +2398,7 @@
self.updatePlaceholder();
self.trigger('item_add', value, $item);
- self.updateOriginalInput({silent: silent});
+ self.updateOriginalInput();
}
});
},
@@ -2535,7 +2409,7 @@
*
* @param {string} value
*/
- removeItem: function(value, silent) {
+ removeItem: function(value) {
var self = this;
var $item, i, idx;
@@ -2553,7 +2427,7 @@
self.items.splice(i, 1);
self.lastQuery = null;
if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) {
- self.removeOption(value, silent);
+ self.removeOption(value);
}
if (i < self.caretPos) {
@@ -2562,9 +2436,9 @@
self.refreshState();
self.updatePlaceholder();
- self.updateOriginalInput({silent: silent});
+ self.updateOriginalInput();
self.positionDropdown();
- self.trigger('item_remove', value, $item);
+ self.trigger('item_remove', value);
}
},
@@ -2576,30 +2450,19 @@
* Once this completes, it will be added
* to the item list.
*
- * @param {string} value
- * @param {boolean} [triggerDropdown]
- * @param {function} [callback]
* @return {boolean}
*/
- createItem: function(input, triggerDropdown) {
+ createItem: function(triggerDropdown) {
var self = this;
+ var input = $.trim(self.$control_input.val() || '');
var caret = self.caretPos;
- input = input || $.trim(self.$control_input.val() || '');
-
- var callback = arguments[arguments.length - 1];
- if (typeof callback !== 'function') callback = function() {};
+ if (!self.canCreate(input)) return false;
+ self.lock();
- if (typeof triggerDropdown !== 'boolean') {
+ if (typeof triggerDropdown === 'undefined') {
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;
@@ -2610,16 +2473,15 @@
var create = once(function(data) {
self.unlock();
- if (!data || typeof data !== 'object') return callback();
+ if (!data || typeof data !== 'object') return;
var value = hash_key(data[self.settings.valueField]);
- if (typeof value !== 'string') return callback();
+ if (!value) return;
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]);
@@ -2637,7 +2499,9 @@
this.lastQuery = null;
if (this.isSetup) {
- this.addItem(this.items);
+ for (var i = 0; i < this.items.length; i++) {
+ this.addItem(this.items);
+ }
}
this.refreshState();
@@ -2697,15 +2561,13 @@
* Refreshes the original <select> or <input>
* element to reflect the current state.
*/
- updateOriginalInput: function(opts) {
- var i, n, options, label, self = this;
- opts = opts || {};
+ updateOriginalInput: function() {
+ var i, n, options, self = this;
if (self.tagType === TAG_SELECT) {
options = [];
for (i = 0, n = self.items.length; i < n; i++) {
- label = self.options[self.items[i]][self.settings.labelField] || '';
- options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected">' + escape_html(label) + '</option>');
+ options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected"></option>');
}
if (!options.length && !this.$input.attr('multiple')) {
options.push('<option value="" selected="selected"></option>');
@@ -2717,9 +2579,7 @@
}
if (self.isSetup) {
- if (!opts.silent) {
- self.trigger('change', self.$input.val());
- }
+ self.trigger('change', self.$input.val());
}
},
@@ -2794,10 +2654,8 @@
/**
* Resets / clears all selected items
* from the control.
- *
- * @param {boolean} silent
*/
- clear: function(silent) {
+ clear: function() {
var self = this;
if (!self.items.length) return;
@@ -2807,7 +2665,7 @@
self.setCaret(0);
self.setActiveItem(null);
self.updatePlaceholder();
- self.updateOriginalInput({silent: silent});
+ self.updateOriginalInput();
self.refreshState();
self.showInput();
self.trigger('clear');
@@ -3018,7 +2876,6 @@
disable: function() {
var self = this;
self.$input.prop('disabled', true);
- self.$control_input.prop('disabled', true).prop('tabindex', -1);
self.isDisabled = true;
self.lock();
},
@@ -3030,7 +2887,6 @@
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();
},
@@ -3081,7 +2937,7 @@
var html = '';
var cache = false;
var self = this;
- var regex_tag = /^[\t \r\n]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;
+ var regex_tag = /^[\t ]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;
if (templateName === 'option' || templateName === 'item') {
value = hash_key(data[self.settings.valueField]);
@@ -3135,36 +2991,16 @@
} 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,
@@ -3178,12 +3014,9 @@
addPrecedence: false,
selectOnTab: false,
preload: false,
- allowEmptyOption: false,
- closeAfterSelect: false,
scrollDuration: 60,
loadThrottle: 300,
- loadingClass: 'loading',
dataAttr: 'data-data',
optgroupField: 'optgroup',
@@ -3191,7 +3024,7 @@
labelField: 'text',
optgroupLabelField: 'label',
optgroupValueField: 'value',
- lockOptgroupOrder: false,
+ optgroupOrder: null,
sortField: '$order',
searchField: ['text'],
@@ -3205,26 +3038,21 @@
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() { ... }
- 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) { ... }
+ 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) { ... }
*/
render: {
@@ -3238,7 +3066,6 @@
}
};
-
$.fn.selectize = function(settings_user) {
var defaults = $.fn.selectize.defaults;
var settings = $.extend({}, defaults, settings_user);
@@ -3256,27 +3083,19 @@
* @param {object} settings_element
*/
var init_textbox = function($input, settings_element) {
- var i, n, values, option;
+ var i, n, values, option, value = $.trim($input.val() || '');
+ if (!value.length) return;
- var data_raw = $input.attr(attr_data);
+ 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];
- 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.options[values[i]] = option;
}
+
+ settings_element.items = values;
};
/**
@@ -3288,7 +3107,6 @@
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);
@@ -3299,36 +3117,37 @@
};
var addOption = function($option, group) {
+ var value, option;
+
$option = $($option);
- var value = hash_key($option.attr('value'));
- if (!value && !settings.allowEmptyOption) return;
+ value = $option.attr('value') || '';
+ if (!value.length) 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 (optionsMap.hasOwnProperty(value)) {
+ if (options.hasOwnProperty(value)) {
if (group) {
- var arr = optionsMap[value][field_optgroup];
- if (!arr) {
- optionsMap[value][field_optgroup] = group;
- } else if (!$.isArray(arr)) {
- optionsMap[value][field_optgroup] = [arr, group];
+ if (!options[value].optgroup) {
+ options[value].optgroup = group;
+ } else if (!$.isArray(options[value].optgroup)) {
+ options[value].optgroup = [options[value].optgroup, group];
} else {
- arr.push(group);
+ options[value].optgroup.push(group);
}
}
return;
}
- var option = readData($option) || {};
+ 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;
- optionsMap[value] = option;
- options.push(option);
+ option.$order = ++order;
+ options[value] = option;
if ($option.is(':selected')) {
settings_element.items.push(value);
@@ -3345,7 +3164,7 @@
optgroup = readData($optgroup) || {};
optgroup[field_optgroup_label] = id;
optgroup[field_optgroup_value] = id;
- settings_element.optgroups.push(optgroup);
+ settings_element.optgroups[id] = optgroup;
}
$options = $('option', $optgroup);
@@ -3373,15 +3192,10 @@
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' : placeholder,
- 'options' : [],
- 'optgroups' : [],
+ 'placeholder' : $input.children('option[value=""]').text() || $input.attr('placeholder'),
+ 'options' : {},
+ 'optgroups' : {},
'items' : []
};
@@ -3396,9 +3210,6 @@
};
$.fn.selectize.defaults = Selectize.defaults;
- $.fn.selectize.support = {
- validity: SUPPORTS_VALIDITY_API
- };
Selectize.define('drag_drop', function(options) {
@@ -3641,7 +3452,7 @@
return option[this.settings.labelField];
};
- this.onKeyDown = (function() {
+ this.onKeyDown = (function(e) {
var original = self.onKeyDown;
return function(e) {
var index, option;
@@ -3662,6 +3473,5 @@
})();
});
-
return Selectize;
}));
diff --git a/app/assets/stylesheets/blazer/selectize.default.css b/app/assets/stylesheets/blazer/selectize.default.css
index ffd219c4..06d39647 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.12.1) - Default Theme
- * Copyright (c) 2013–2015 Brian Reavis & contributors
+ * selectize.default.css (v0.10.1) - Default Theme
+ * Copyright (c) 2013 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,7 +189,6 @@
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
deleted file mode 100644
index 965ab09d..00000000
--- a/app/controllers/blazer/base_controller.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-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
deleted file mode 100644
index af6858a1..00000000
--- a/app/controllers/blazer/checks_controller.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-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)
-
- 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 7a5e0503..ad0cf9d2 100644
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@ -1,12 +1,23 @@
module Blazer
- class QueriesController < BaseController
+ 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
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
@@ -31,9 +42,9 @@ module Blazer
@smart_vars = {}
@sql_errors = []
@bind_vars.each do |var|
- query = Blazer.smart_variables[var]
+ query = smart_variables[var]
if query
- rows, error = Blazer.run_statement(query)
+ rows, error = run_statement(query)
@smart_vars[var] = rows.map { |v| v.values.reverse }
@sql_errors << error if error
end
@@ -58,13 +69,7 @@ module Blazer
audit.save!
end
- @rows, @error = Blazer.run_statement(@statement)
-
- if @query && [email protected]_s.include?("canceling statement due to statement timeout")
- @query.blazer_checks.each do |check|
- check.update_state(@rows, @error)
- end
- end
+ @rows, @error = run_statement(@statement)
@columns = {}
if @rows.any?
@@ -83,19 +88,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) || Blazer.smart_columns[k] }.keys
+ @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || smart_columns[k] }.keys
@boom = {}
@columns.keys.each do |key|
- query = Blazer.smart_columns[key]
+ query = smart_columns[key]
if query
values = @rows.map { |r| r[key] }.compact.uniq
- rows, error = Blazer.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
+ rows, error = run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
@boom[key] = Hash[rows.map(&:values)]
end
end
- @linked_columns = Blazer.linked_columns
+ @linked_columns = linked_columns
end
respond_to do |format|
@@ -123,6 +128,10 @@ 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
@@ -142,6 +151,28 @@ 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
@@ -166,5 +197,33 @@ 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/base_helper.rb b/app/helpers/blazer/queries_helper.rb
similarity index 94%
rename from app/helpers/blazer/base_helper.rb
rename to app/helpers/blazer/queries_helper.rb
index cdd49d52..b681f7a6 100644
--- a/app/helpers/blazer/base_helper.rb
+++ b/app/helpers/blazer/queries_helper.rb
@@ -1,5 +1,5 @@
module Blazer
- module BaseHelper
+ module QueriesHelper
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
deleted file mode 100644
index e6a31b4e..00000000
--- a/app/mailers/blazer/check_mailer.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-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_count, error)
- @check = check
- @state = state
- @state_was = state_was
- @rows_count = rows_count
- @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
deleted file mode 100644
index 2ca3f43e..00000000
--- a/app/models/blazer/check.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-module Blazer
- class Check < ActiveRecord::Base
- belongs_to :blazer_query, class_name: "Blazer::Query"
-
- validates :blazer_query_id, presence: true
-
- def split_emails
- emails.to_s.split(",").map(&:strip)
- end
-
- 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.size, error).deliver_later
- end
- save!
- end
- end
-end
diff --git a/app/models/blazer/query.rb b/app/models/blazer/query.rb
index 9abce275..d455df1e 100644
--- a/app/models/blazer/query.rb
+++ b/app/models/blazer/query.rb
@@ -1,7 +1,6 @@
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
deleted file mode 100644
index 05bde140..00000000
--- a/app/views/blazer/check_mailer/failing_checks.html.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-<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
deleted file mode 100644
index da513626..00000000
--- a/app/views/blazer/check_mailer/state_change.html.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-<p><%= link_to "View", query_url(@check.blazer_query_id) %></p>
-<% if @error %>
- <p><%= @error %></p>
-<% elsif @rows_count > 0 %>
- <p><%= pluralize(@rows_count, "row") %></p>
-<% end %>
diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erb
deleted file mode 100644
index 27e3864a..00000000
--- a/app/views/blazer/checks/_form.html.erb
+++ /dev/null
@@ -1,28 +0,0 @@
-<p class="text-muted">Checks are designed to identify bad data. A check fails if there are any results.</p>
-
-<% if @check.errors.any? %>
- <div class="alert alert-danger"><%= @check.errors.full_messages.first %></div>
-<% end %>
-
-<%= form_for @check do |f| %>
- <div class="form-group">
- <%= f.label :blazer_query_id, "Query" %>
- <div class="hide">
- <%= f.select :blazer_query_id, Blazer::Query.order(:name).map { |q| [q.name, q.id] }, {include_blank: true} %>
- </div>
- <script>
- $("#check_blazer_query_id").selectize().parents(".hide").removeClass("hide");
- </script>
- </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 %>
diff --git a/app/views/blazer/checks/edit.html.erb b/app/views/blazer/checks/edit.html.erb
deleted file mode 100644
index d44c608b..00000000
--- a/app/views/blazer/checks/edit.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render partial: "form" %>
diff --git a/app/views/blazer/checks/index.html.erb b/app/views/blazer/checks/index.html.erb
deleted file mode 100644
index 9016c8f2..00000000
--- a/app/views/blazer/checks/index.html.erb
+++ /dev/null
@@ -1,41 +0,0 @@
-<% 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.split_emails.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
deleted file mode 100644
index d44c608b..00000000
--- a/app/views/blazer/checks/new.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render partial: "form" %>
diff --git a/app/views/blazer/checks/run.html.erb b/app/views/blazer/checks/run.html.erb
deleted file mode 100644
index e7a277dd..00000000
--- a/app/views/blazer/checks/run.html.erb
+++ /dev/null
@@ -1,9 +0,0 @@
-<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 e9884621..7e606a72 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]] + Blazer.tables.keys), style: "margin-right: 20px; width: 240px;" %>
+ <%= select_tag :table_names, options_for_select([["Preview table", nil]] + 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 4b6f0647..e285f20c 100644
--- a/app/views/blazer/queries/index.html.erb
+++ b/app/views/blazer/queries/index.html.erb
@@ -2,7 +2,6 @@
<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>
@@ -25,9 +24,6 @@
<% 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/app/views/blazer/queries/run.html.erb b/app/views/blazer/queries/run.html.erb
index 88cacf14..d3d44dfb 100644
--- a/app/views/blazer/queries/run.html.erb
+++ b/app/views/blazer/queries/run.html.erb
@@ -6,12 +6,12 @@
<p class="text-muted"><%= pluralize(@rows.size, "row") %></p>
<% if @rows.any? %>
<% values = @rows.first.values %>
- <% if values.size >= 2 && (values.first.is_a?(Time) || values.first.is_a?(Date)) && values[1..-1].all?{|v| v.is_a?(Numeric) } %>
+ <% if values.size >= 2 && values.first.is_a?(Time) && values[1..-1].all?{|v| v.is_a?(Numeric) } %>
<% time_k = @columns.keys.first %>
- <%= line_chart @columns.keys[1..-1].map{|k| {name: k, data: @rows.map{|r| [r[time_k].in_time_zone(Blazer.time_zone), r[k]] }} } %>
- <% elsif values.size == 3 && (values.first.is_a?(Time) || values.first.is_a?(Date)) && values[1].is_a?(String) && values[2].is_a?(Numeric) %>
+ <%= line_chart @columns.keys[1..-1].map{|k| {name: k, data: @rows.map{|r| [r[time_k], r[k]] }} } %>
+ <% elsif values.size == 3 && values.first.is_a?(Time) && values[1].is_a?(String) && values[2].is_a?(Numeric) %>
<% keys = @columns.keys %>
- <%= line_chart @rows.group_by { |v| v[keys[1]] }.map { |name, v| {name: name, data: v.map { |v2| [v2[keys[0]].in_time_zone(Blazer.time_zone), v2[keys[2]]] } } } %>
+ <%= line_chart @rows.group_by { |v| v[keys[1]] }.map { |name, v| {name: name, data: v.map { |v2| [v2[keys[0]], v2[keys[2]]] } } } %>
<% elsif values.size == 2 && values.first.is_a?(String) && values.last.is_a?(Numeric) %>
<%= pie_chart @rows.map(&:values), library: {sliceVisibilityThreshold: 1 / 40.0} %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 9bcc7a4a..3669441c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,8 +2,5 @@ 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 50ab367a..ccd5786d 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -2,7 +2,6 @@ require "csv"
require "chartkick"
require "blazer/version"
require "blazer/engine"
-require "blazer/tasks"
module Blazer
class << self
@@ -11,7 +10,6 @@ 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
@@ -20,80 +18,4 @@ 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 = nil
- error = nil
- tries = 0
- # try 3 times on timeout errors
- begin
- rows, error = run_statement(check.blazer_query.statement)
- tries += 1
- end while error && error.include?("canceling statement due to statement timeout") && tries < 3
- 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.split_emails.each do |email|
- (emails[email] ||= []) << check
- end
- end
-
- emails.each do |email, checks|
- Blazer::CheckMailer.failing_checks(email, checks).deliver_later
- end
- end
end
diff --git a/lib/blazer/tasks.rb b/lib/blazer/tasks.rb
deleted file mode 100644
index cd45b661..00000000
--- a/lib/blazer/tasks.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-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 a1abce32..3582c405 100644
--- a/lib/generators/blazer/templates/install.rb
+++ b/lib/generators/blazer/templates/install.rb
@@ -14,12 +14,5 @@ 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