From e341d5a6faf969c56cc891978f4b97b2a03c50e6 Mon Sep 17 00:00:00 2001 From: Rishabh Nambiar <5862206+rishabhnambiar@users.noreply.github.com> Date: Sun, 12 Aug 2018 21:38:25 +0530 Subject: [PATCH 1/4] List all previous queries on Data Explorer homepage --- .../controllers/admin-plugins-explorer.js.es6 | 10 +++++ .../templates/admin/plugins-explorer.hbs | 27 ++++++++++++ assets/stylesheets/explorer.scss | 41 +++++++++++++++++++ config/locales/client.en.yml | 4 ++ plugin.rb | 11 +++-- 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index 42a774b..a281235 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -20,6 +20,7 @@ export default Ember.Controller.extend({ editing: false, everEditing: false, + showRecentQueries: true, createDisabled: function() { return (this.get('newQueryName') || "").trim().length === 0; @@ -28,6 +29,9 @@ export default Ember.Controller.extend({ selectedItem: function() { const id = parseInt(this.get('selectedQueryId')); const item = this.get('content').find(q => q.get('id') === id); + if (!isNaN(id)) { + this.set('showRecentQueries', false); + } return item || NoQuery; }.property('selectedQueryId'), @@ -76,6 +80,11 @@ export default Ember.Controller.extend({ showCreate() { this.set('showCreate', true); + this.set('showRecentQueries', false); + }, + + showRecentQueries() { + this.set('showRecentQueries', true); }, editName() { @@ -106,6 +115,7 @@ export default Ember.Controller.extend({ const name = this.get("newQueryName").trim(); this.set('loading', true); this.set('showCreate', false); + this.set('showRecentQueries', false); this.store .createRecord('query', { name }) .save() diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs index b0847c4..24d229c 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs @@ -133,6 +133,33 @@ {{/if}} {{/unless}} + {{#if showRecentQueries}} + {{#if model.length}} + + + + + + + + {{#each model as |query|}} + + + + + + {{/each}} +
{{i18n 'explorer.query_name'}}{{i18n 'explorer.query_user'}}{{i18n 'explorer.query_time'}}
+ + {{query.name}} +
+ {{query.description}} +
+
+
{{query.user}}{{query.time}}
+ {{/if}} + {{/if}} +
{{else}} diff --git a/assets/stylesheets/explorer.scss b/assets/stylesheets/explorer.scss index 9c88adf..7bef968 100644 --- a/assets/stylesheets/explorer.scss +++ b/assets/stylesheets/explorer.scss @@ -210,6 +210,47 @@ } } +.recent-queries { + tr a { + display:block; + width:100%; + height: 100%; + color: inherit; + } + th { + .time{ + width: 15%; + } + .user { + width: 15%; + } + } + td{ + .query-time { + font-weight: bold; + color: dark-light-diff($primary, $secondary, 40%, -20%); + } + .query-user { + color: dark-light-diff($primary, $secondary, 25%, -20%); + } + } + hr { + height: 2px; + width: 100%; + color: dark-light-diff($primary, $secondary, 35%, -20%); + } + .heading { + padding-top: 30px; + color: dark-light-diff($primary, $secondary, 30%, -20%); + } + .query-name { + color: dark-light-diff($primary, $secondary, 15%, -20%); + } + .query-desc { + color: dark-light-diff($primary, $secondary, 25%, -20%); + } +} + .explorer-pad-bottom { margin-bottom: 200px; } diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 32a0f9e..1bf2ba0 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -52,6 +52,10 @@ en: download_csv: "CSV" others_dirty: "A query has unsaved changes that will be lost if you navigate away." run_time: "Query completed in {{value}} ms." + query_name: "Query" + query_description: "Description" + query_time: "Date created" + query_user: "Created by" column: "Column {{number}}" explain_label: "Include query plan?" save_params: "Set Defaults" diff --git a/plugin.rb b/plugin.rb index de25af4..bd446ba 100644 --- a/plugin.rb +++ b/plugin.rb @@ -567,12 +567,15 @@ SQL # Reimplement a couple ActiveRecord methods, but use PluginStore for storage instead class DataExplorer::Query - attr_accessor :id, :name, :description, :sql + attr_accessor :id, :name, :description, :sql, :user, :time def initialize @name = 'Unnamed Query' @description = 'Enter a description here' @sql = 'SELECT 1' + #TODO: Figure out where to assign current user for storage + @created_by = 'Admin' + @created_at = Time.now.strftime("%b %e, %Y") end def slug @@ -609,7 +612,7 @@ SQL def self.from_hash(h) query = DataExplorer::Query.new - [:name, :description, :sql].each do |sym| + [:name, :description, :sql, :created_by, :created_at].each do |sym| query.send("#{sym}=", h[sym].strip) if h[sym] end query.id = h[:id].to_i if h[:id] @@ -622,6 +625,8 @@ SQL name: @name, description: @description, sql: @sql, + created_by: @created_by, + created_at: @created_at } end @@ -1073,7 +1078,7 @@ SQL end class DataExplorer::QuerySerializer < ActiveModel::Serializer - attributes :id, :sql, :name, :description, :param_info + attributes :id, :sql, :name, :description, :param_info, :created_by, :created_at def param_info object.params.map(&:to_hash) rescue nil From 6e806cc1dfaa035c1f7ed502e0b4ac9b20e3c5f8 Mon Sep 17 00:00:00 2001 From: Rishabh Nambiar <5862206+rishabhnambiar@users.noreply.github.com> Date: Mon, 13 Aug 2018 17:04:01 +0530 Subject: [PATCH 2/4] Added clickable rows, default query sorting by id and clickable usernames --- .../controllers/admin-plugins-explorer.js.es6 | 11 +++-------- .../templates/admin/plugins-explorer.hbs | 14 +++++++++----- assets/stylesheets/explorer.scss | 12 +++++------- plugin.rb | 15 ++++++++------- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index a281235..236de82 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -21,6 +21,8 @@ export default Ember.Controller.extend({ editing: false, everEditing: false, showRecentQueries: true, + sortBy: ['id:desc'], + sortedQueries: Em.computed.sort('model', 'sortBy'), createDisabled: function() { return (this.get('newQueryName') || "").trim().length === 0; @@ -29,9 +31,7 @@ export default Ember.Controller.extend({ selectedItem: function() { const id = parseInt(this.get('selectedQueryId')); const item = this.get('content').find(q => q.get('id') === id); - if (!isNaN(id)) { - this.set('showRecentQueries', false); - } + !isNaN(id) ? this.set('showRecentQueries', false) : this.set('showRecentQueries', true); return item || NoQuery; }.property('selectedQueryId'), @@ -80,11 +80,6 @@ export default Ember.Controller.extend({ showCreate() { this.set('showCreate', true); - this.set('showRecentQueries', false); - }, - - showRecentQueries() { - this.set('showRecentQueries', true); }, editName() { diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs index 24d229c..f04f685 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs @@ -134,7 +134,7 @@ {{/unless}} {{#if showRecentQueries}} - {{#if model.length}} + {{#if sortedQueries.length}} @@ -142,18 +142,22 @@ - {{#each model as |query|}} + {{#each sortedQueries as |query|}} - - + + {{/each}}
{{i18n 'explorer.query_name'}}{{i18n 'explorer.query_time'}}
- + {{query.name}}
{{query.description}}
{{query.user}}{{query.time}} + + {{query.username}} + + {{query.created_at}}
diff --git a/assets/stylesheets/explorer.scss b/assets/stylesheets/explorer.scss index 7bef968..08f2c67 100644 --- a/assets/stylesheets/explorer.scss +++ b/assets/stylesheets/explorer.scss @@ -217,20 +217,18 @@ height: 100%; color: inherit; } - th { - .time{ - width: 15%; - } - .user { + .time { width: 15%; } + .user { + width: 20%; } td{ - .query-time { + .query-created-at { font-weight: bold; color: dark-light-diff($primary, $secondary, 40%, -20%); } - .query-user { + .query-created-by { color: dark-light-diff($primary, $secondary, 25%, -20%); } } diff --git a/plugin.rb b/plugin.rb index bd446ba..2c73607 100644 --- a/plugin.rb +++ b/plugin.rb @@ -567,15 +567,12 @@ SQL # Reimplement a couple ActiveRecord methods, but use PluginStore for storage instead class DataExplorer::Query - attr_accessor :id, :name, :description, :sql, :user, :time + attr_accessor :id, :name, :description, :sql, :created_by, :created_at, :username def initialize @name = 'Unnamed Query' @description = 'Enter a description here' @sql = 'SELECT 1' - #TODO: Figure out where to assign current user for storage - @created_by = 'Admin' - @created_at = Time.now.strftime("%b %e, %Y") end def slug @@ -612,7 +609,7 @@ SQL def self.from_hash(h) query = DataExplorer::Query.new - [:name, :description, :sql, :created_by, :created_at].each do |sym| + [:name, :description, :sql, :created_by, :created_at, :username].each do |sym| query.send("#{sym}=", h[sym].strip) if h[sym] end query.id = h[:id].to_i if h[:id] @@ -626,7 +623,8 @@ SQL description: @description, sql: @sql, created_by: @created_by, - created_at: @created_at + created_at: @created_at, + username: @username } end @@ -943,6 +941,9 @@ SQL # guardian.ensure_can_create_explorer_query! query = DataExplorer::Query.from_hash params.require(:query) + query.created_by = current_user.id.to_s + query.created_at = Time.now.strftime("%b %e, %Y") + query.username = current_user.username query.id = nil # json import will assign an id, which is wrong query.save @@ -1078,7 +1079,7 @@ SQL end class DataExplorer::QuerySerializer < ActiveModel::Serializer - attributes :id, :sql, :name, :description, :param_info, :created_by, :created_at + attributes :id, :sql, :name, :description, :param_info, :created_by, :created_at, :username def param_info object.params.map(&:to_hash) rescue nil From 83b1fcb54be0c984dc0090e2587bd827af9627e9 Mon Sep 17 00:00:00 2001 From: Rishabh Nambiar <5862206+rishabhnambiar@users.noreply.github.com> Date: Tue, 14 Aug 2018 16:36:28 +0530 Subject: [PATCH 3/4] Added User.find() & fixed scroll history bug --- .../discourse/controllers/admin-plugins-explorer.js.es6 | 4 ++++ .../discourse/templates/admin/plugins-explorer.hbs | 2 +- plugin.rb | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index 236de82..e3db48f 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -90,6 +90,10 @@ export default Ember.Controller.extend({ window.open(this.get('selectedItem.downloadUrl'), "_blank"); }, + scrollTop() { + window.scrollTo(0,0); + }, + resetParams() { this.get('selectedItem').resetParams(); }, diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs index f04f685..47cb510 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs @@ -145,7 +145,7 @@ {{#each sortedQueries as |query|}} - + {{query.name}}
{{query.description}} diff --git a/plugin.rb b/plugin.rb index 2c73607..2596d7a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -941,9 +941,9 @@ SQL # guardian.ensure_can_create_explorer_query! query = DataExplorer::Query.from_hash params.require(:query) - query.created_by = current_user.id.to_s query.created_at = Time.now.strftime("%b %e, %Y") - query.username = current_user.username + query.created_by = current_user.id.to_s + query.username = User.find(query.created_by).username query.id = nil # json import will assign an id, which is wrong query.save From d9e7b438b43fca4f75a6667c51c4392646972f18 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Tue, 14 Aug 2018 17:21:59 +0530 Subject: [PATCH 4/4] do not save username since we are already saving user_id --- plugin.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugin.rb b/plugin.rb index 2596d7a..7aa0f7a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -567,7 +567,7 @@ SQL # Reimplement a couple ActiveRecord methods, but use PluginStore for storage instead class DataExplorer::Query - attr_accessor :id, :name, :description, :sql, :created_by, :created_at, :username + attr_accessor :id, :name, :description, :sql, :created_by, :created_at def initialize @name = 'Unnamed Query' @@ -609,7 +609,7 @@ SQL def self.from_hash(h) query = DataExplorer::Query.new - [:name, :description, :sql, :created_by, :created_at, :username].each do |sym| + [:name, :description, :sql, :created_by, :created_at].each do |sym| query.send("#{sym}=", h[sym].strip) if h[sym] end query.id = h[:id].to_i if h[:id] @@ -623,8 +623,7 @@ SQL description: @description, sql: @sql, created_by: @created_by, - created_at: @created_at, - username: @username + created_at: @created_at } end @@ -943,7 +942,6 @@ SQL query = DataExplorer::Query.from_hash params.require(:query) query.created_at = Time.now.strftime("%b %e, %Y") query.created_by = current_user.id.to_s - query.username = User.find(query.created_by).username query.id = nil # json import will assign an id, which is wrong query.save @@ -1084,6 +1082,10 @@ SQL def param_info object.params.map(&:to_hash) rescue nil end + + def username + User.find(created_by).username rescue nil + end end DataExplorer::Engine.routes.draw do