|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ::Jobs::WorkflowDataExplorerQueriesCompleteness < ::Jobs::Scheduled |
| 4 | + sidekiq_options retry: false |
| 5 | + |
| 6 | + every 24.hours |
| 7 | + |
| 8 | + def execute(args) |
| 9 | + if !ActiveRecord::Base.connection.table_exists?(:data_explorer_queries) |
| 10 | + Rails.logger.warn "Skipping WorkflowDataExplorerQueriesCompleteness: doesn't look like Data Explorer plugin is properly installed" |
| 11 | + return |
| 12 | + end |
| 13 | + |
| 14 | + if !::DiscourseDataExplorer::Query.exists?(name: "Workflow Stats (default)") |
| 15 | + query_sql = <<~SQL |
| 16 | + -- [params] |
| 17 | + -- int :workflow_id = 1 |
| 18 | + -- int :num_of_days_history = 14 |
| 19 | +
|
| 20 | + SELECT cob_date as "COB DATE", |
| 21 | + w.name as "Workflow", |
| 22 | + wstp.name as "Step", |
| 23 | + wstt.count as "Count" |
| 24 | + FROM workflow_stats wstt |
| 25 | + INNER JOIN workflow_steps wstp ON wstt.workflow_step_id = wstp.id |
| 26 | + INNER JOIN workflows w ON wstt.workflow_id = w.id |
| 27 | + WHERE wstt.cob_date >= NOW() - (:num_of_days_history * INTERVAL '1 day') |
| 28 | + AND wstt.workflow_id = :workflow_id |
| 29 | + SQL |
| 30 | + |
| 31 | + DB.exec <<~SQL, now: Time.zone.now, query_sql: query_sql |
| 32 | + INSERT INTO data_explorer_queries(name, description, sql, created_at, updated_at) |
| 33 | + VALUES |
| 34 | + ('Workflow Stats (default)', |
| 35 | + 'Daily counts for each workflow step in a workflow (useful for e.g. burndown/burnup charts)', |
| 36 | + :query_sql, |
| 37 | + :now, |
| 38 | + :now) |
| 39 | + SQL |
| 40 | + end |
| 41 | + |
| 42 | + if !::DiscourseDataExplorer::Query.exists?( |
| 43 | + name: "Workflow Audit Log (default)" |
| 44 | + ) |
| 45 | + query_sql = <<~SQL |
| 46 | + -- [params] |
| 47 | + -- int :workflow_id = 1 |
| 48 | + -- int :num_of_days_history = 14 |
| 49 | +
|
| 50 | + SELECT user_id, |
| 51 | + topic_id, |
| 52 | + workflow_name, |
| 53 | + starting_step_name, |
| 54 | + step_option_name |
| 55 | + FROM |
| 56 | + workflow_audit_logs |
| 57 | + WHERE created_at >= NOW() - (:num_of_days_history * INTERVAL '1 day') |
| 58 | + AND workflow_id = :workflow_id |
| 59 | + SQL |
| 60 | + |
| 61 | + DB.exec <<~SQL, now: Time.zone.now, query_sql: query_sql |
| 62 | + INSERT INTO data_explorer_queries(name, description, sql, created_at, updated_at) |
| 63 | + VALUES |
| 64 | + ('Workflow Audit Log (default)', |
| 65 | + 'Audit log for workflow actions', |
| 66 | + :query_sql, |
| 67 | + :now, |
| 68 | + :now) |
| 69 | + SQL |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments