Skip to content

Commit dcbb29e

Browse files
authored
Merge pull request #7 from merefield/add_daily_workflow_stats
FEATURE: Add daily workflow stats
2 parents 6be7a44 + 267b818 commit dcbb29e

File tree

8 files changed

+165
-26
lines changed

8 files changed

+165
-26
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class ::Jobs::WorkflowDailyStats < ::Jobs::Scheduled
4+
sidekiq_options retry: false
5+
6+
every 24.hours
7+
8+
def execute(args)
9+
::DiscourseWorkflow::Stats.new.calculate_daily_stats
10+
end
11+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module ::DiscourseWorkflow
4+
class WorkflowStat < ActiveRecord::Base
5+
self.table_name = 'workflow_stats'
6+
has_one :workflow
7+
has_one :workflow_step
8+
validates :cob_date, presence: true
9+
validates :workflow_id, presence: true
10+
validates :workflow_step_id, presence: true
11+
validates :count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
12+
end
13+
end
14+
15+
# == Schema Information
16+
#
17+
# Table name: workflow_stats
18+
#
19+
# id :bigint not null, primary key
20+
# cob_date :datetime
21+
# count :integer
22+
# created_at :datetime not null
23+
# updated_at :datetime not null
24+
# workflow_id :bigint
25+
# workflow_step_id :bigint
26+
#
27+
# Indexes
28+
#
29+
# index_workflow_stats_on_workflow_id (workflow_id)
30+
# index_workflow_stats_on_workflow_step_id (workflow_step_id)
31+
#
32+
# Foreign Keys
33+
#
34+
# fk_rails_... (workflow_id => workflows.id)
35+
# fk_rails_... (workflow_step_id => workflow_steps.id)
36+
#

app/models/discourse_workflow/workflow_step_option.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class WorkflowStepOption < ActiveRecord::Base
1414
#
1515
# id :bigint not null, primary key
1616
# position :integer
17-
# workflow_step_id :bigint
18-
# workflow_option_id :bigint
19-
# target_step_id :bigint
2017
# created_at :datetime not null
2118
# updated_at :datetime not null
19+
# target_step_id :bigint
20+
# workflow_option_id :bigint
21+
# workflow_step_id :bigint
2222
#
2323
# Indexes
2424
#

db/fixtures/002_workflow_data_explorer_query.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# frozen_string_literal: true
3+
# rubocop:disable Discourse/NoAddReferenceOrAliasesActiveRecordMigration
4+
class CreateWorkflowStats < ActiveRecord::Migration[7.2]
5+
def change
6+
create_table :workflow_stats do |t|
7+
t.datetime :cob_date
8+
t.references :workflow, foreign_key: true
9+
t.references :workflow_step, foreign_key: true
10+
t.integer :count
11+
12+
t.timestamps
13+
end
14+
end
15+
end

lib/discourse_workflow/stats.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
module DiscourseWorkflow
3+
class Stats
4+
5+
def calculate_daily_stats
6+
return unless SiteSetting.workflow_enabled
7+
8+
current_date = Date.current
9+
10+
::DiscourseWorkflow::WorkflowStat.where(cob_date: current_date).destroy_all
11+
12+
Workflow.all.each do |workflow|
13+
workflow.workflow_state.each do |state|
14+
stat = ::DiscourseWorkflow::WorkflowStat.find_or_initialize_by(
15+
cob_date: current_date,
16+
workflow_id: workflow.id,
17+
workflow_step_id: state.workflow_step_id
18+
)
19+
stat.count ||= 0
20+
stat.count += 1
21+
stat.save!
22+
end
23+
::Rails.logger.info("Workflow Daily Stats recorded for: #{workflow.name}")
24+
end
25+
end
26+
end
27+
end

plugin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
# name: discourse-workflow
33
# about: A topic-based workflow engine for Discourse
4-
# version: 0.0.15
4+
# version: 0.0.16
55
# authors: Robert Barrow
66
# contact_emails: [email protected]
77
# url: https://github.com/merefield/discourse-workflow

0 commit comments

Comments
 (0)