FEATURE: add support for _url to link to a url

FEATURE: add support for _url to link to a url

This new feature means that if you name a column xyz_url then the return value will be treated as a URL.

If you would like to name the link use the format name,https://cnn.com

diff --git a/assets/javascripts/discourse/components/query-row-content.js.es6 b/assets/javascripts/discourse/components/query-row-content.js.es6
index e23acf6..12c476e 100644
--- a/assets/javascripts/discourse/components/query-row-content.js.es6
+++ b/assets/javascripts/discourse/components/query-row-content.js.es6
@@ -33,6 +33,20 @@ function bound_date_replacement(str, ctx) {
 
 const esc = Handlebars.Utils.escapeExpression;
 
+// consider moving this elsewhere
+function guessUrl(t) {
+  let [dest, name] = [t, t];
+
+  const split = t.split(/,(.+)/);
+
+  if (split.length > 1) {
+    name = split[0];
+    dest = split[1];
+  }
+
+  return [dest, name];
+}
+
 const QueryRowContentComponent = Ember.Component.extend(
   bufferedRender({
     tagName: "tr",
@@ -70,6 +84,12 @@ const QueryRowContentComponent = Ember.Component.extend(
           ctx[t.name] = parentView[`lookup${t.name.capitalize()}`](id);
         }
 
+        if (t.name === "url") {
+          let [url, name] = guessUrl(value);
+          ctx["href"] = url;
+          ctx["target"] = name;
+        }
+
         if (
           t.name === "category" ||
           t.name === "badge" ||
diff --git a/assets/javascripts/discourse/templates/explorer/url.raw.hbs b/assets/javascripts/discourse/templates/explorer/url.raw.hbs
new file mode 100644
index 0000000..4cf17da
--- /dev/null
+++ b/assets/javascripts/discourse/templates/explorer/url.raw.hbs
@@ -0,0 +1 @@
+<a href="{{href}}">{{target}}</a>
diff --git a/lib/queries.rb b/lib/queries.rb
index a7878cc..ad5ad34 100644
--- a/lib/queries.rb
+++ b/lib/queries.rb
@@ -80,6 +80,11 @@ class Queries
             "id": -14,
             "name": "Group Members Reply Count",
             "description": "Number of replies by members of a group over a given time period. Requires 'group_name', 'start_date', and 'end_date' parameters. Dates need to be in the form 'yyyy-mm-dd'. Accepts an 'include_pms' parameter."
+        },
+        "total-assigned-topics-report": {
+            "id": -15,
+            "name": "Total topics assigned per user",
+            "description": "Count of assigned topis per user linking to assign list"
         }
     }.with_indifferent_access
 
@@ -396,46 +401,58 @@ class Queries
     SQL
 
     queries["group-members-reply-count"]["sql"] = <<~SQL
-    -- [params]
-    -- date :start_date
-    -- date :end_date
-    -- string :group_name
-    -- boolean :include_pms = false
+      -- [params]
+      -- date :start_date
+      -- date :end_date
+      -- string :group_name
+      -- boolean :include_pms = false
 
-    WITH target_users AS (
-    SELECT
-    u.id AS user_id
-    FROM users u
-    JOIN group_users gu
-    ON gu.user_id = u.id
-    JOIN groups g
-    ON g.id = gu.group_id
-    WHERE g.name = :group_name
-    AND gu.created_at::date <= :end_date
-    ),
-    target_posts AS (
-    SELECT
-    p.id,
-    p.user_id
-    FROM posts p
-    JOIN topics t
-    ON t.id = p.topic_id
-    WHERE CASE WHEN :include_pms THEN true ELSE t.archetype = 'regular' END
-    AND t.deleted_at IS NULL
-    AND p.deleted_at IS NULL
-    AND p.created_at::date >= :start_date
-    AND p.created_at::date <= :end_date
-    AND p.post_number > 1
-    )
+      WITH target_users AS (
+      SELECT
+      u.id AS user_id
+      FROM users u
+      JOIN group_users gu
+      ON gu.user_id = u.id
+      JOIN groups g
+      ON g.id = gu.group_id
+      WHERE g.name = :group_name
+      AND gu.created_at::date <= :end_date
+      ),
+      target_posts AS (
+      SELECT
+      p.id,
+      p.user_id
+      FROM posts p
+      JOIN topics t
+      ON t.id = p.topic_id
+      WHERE CASE WHEN :include_pms THEN true ELSE t.archetype = 'regular' END
+      AND t.deleted_at IS NULL
+      AND p.deleted_at IS NULL
+      AND p.created_at::date >= :start_date
+      AND p.created_at::date <= :end_date
+      AND p.post_number > 1
+      )
 
-    SELECT
-    tu.user_id,
-    COALESCE(COUNT(tp.id), 0) AS reply_count
-    FROM target_users tu
-    LEFT OUTER JOIN target_posts tp
-    ON tp.user_id = tu.user_id
-    GROUP BY tu.user_id
-    ORDER BY reply_count DESC, tu.user_id
+      SELECT
+      tu.user_id,
+      COALESCE(COUNT(tp.id), 0) AS reply_count
+      FROM target_users tu
+      LEFT OUTER JOIN target_posts tp
+      ON tp.user_id = tu.user_id
+      GROUP BY tu.user_id
+      ORDER BY reply_count DESC, tu.user_id
+    SQL
+
+    queries["total-assigned-topics-report"]["sql"] = <<~SQL
+      SELECT value::int user_id,
+      count(*)::varchar || ',/u/' || username_lower || '/activity/assigned' assigned_url
+      FROM topic_custom_fields tf
+      JOIN topics t on t.id = topic_id
+      JOIN users u on u.id = value::int
+      WHERE tf.name = 'assigned_to_id'
+        AND t.deleted_at IS NULL
+      GROUP BY value::int, username_lower
+      ORDER BY count(*) DESC, username_lower
     SQL
 
   # convert query ids from "mostcommonlikers" to "-1", "mostmessages" to "-2" etc.
diff --git a/plugin.rb b/plugin.rb
index 1602b61..661173e 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -177,6 +177,8 @@ SQL
 
     def self.add_extra_data(pg_result)
       needed_classes = {}
+      ret = {}
+      col_map = {}
 
       pg_result.fields.each_with_index do |col, idx|
         rgx = column_regexes.find { |r| r.match col }
@@ -188,11 +190,11 @@ SQL
           cls = $1.to_sym
           needed_classes[cls] ||= []
           needed_classes[cls] << idx
+        elsif col =~ /^\w+_url$/
+          col_map[idx] = "url"
         end
       end
 
-      ret = {}
-      col_map = {}
       needed_classes.each do |cls, column_nums|
         next unless column_nums.present?
         support_info = extra_data_pluck_fields[cls]
@@ -1126,9 +1128,9 @@ SQL
             json[:explain] = result[:explain] if opts[:explain]
 
             if !params[:download]
-              ext = DataExplorer.add_extra_data(pg_result)
-              json[:colrender] = ext[1]
-              json[:relations] = ext[0]
+              relations, colrender = DataExplorer.add_extra_data(pg_result)
+              json[:relations] = relations
+              json[:colrender] = colrender
             end
 
             json[:rows] = pg_result.values

GitHub sha: 29862def

1 Like