duffy_log 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df1ca47085a6b83d1df4421f75264659cfd0aaa2dea7228685a1ff1cc553a8cb
4
- data.tar.gz: 1c99dc9adfe6f145e79f94d6028bc26d634523b4878a41eccee3de3dda39e863
3
+ metadata.gz: 0f67328fc570d1f2b751c9db0e63d69e63c2c57bc7f63f845b926efd289b5260
4
+ data.tar.gz: fb9c10e330b375637ffb13f16a22b84f3fb2f62d5fdad806db7a3a487edcc2ba
5
5
  SHA512:
6
- metadata.gz: b7f8448737deff737c64222318278e694c904d91bc4fa2cd2da23005ea08c0e82784d231a5f43603dfc05e3caafc9f7f2ed92c15feee1beb3cc7c6cec1050f1b
7
- data.tar.gz: b09cfff025a1b672b9a4e1e53c28bb4425a335096cf24efdaff929c61dede12c25625ad3cccb446bd8c5ca1c5bd80fedcb6046597710efd9a553e8ced2d3f845
6
+ metadata.gz: 5540d51483c31bee05e07e2ea08ca4bbd470f67eeb3281e599fc30e0873252e5481e9da335318b0f3cd38b75ab09bebb8d07f909e32701387676712fed3ace2e
7
+ data.tar.gz: ea375f339f35df1364f4277e16869c70d45ad2d043c7aa16ce14b9120c0a6b2bc05fc61cd978d80b0de6e7e2d9b37523f370e174c3ae3dc8b61aada1ee3f2f1c
@@ -0,0 +1,109 @@
1
+ module ProcessLogBase
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+
6
+ # Validations
7
+ validates :key, presence: true
8
+ validates :start_time, presence: true
9
+ validates :status, presence: true
10
+
11
+ # Callbacks
12
+ after_initialize :defaults
13
+ before_save { self.elapsed = end_time && end_time - start_time }
14
+ before_save { self.average_elapsed = siblings.completed.sorted.limit(10).average(:elapsed) }
15
+
16
+
17
+ # Scopes
18
+ scope :completed, -> { where.not(elapsed: nil) }
19
+ scope :failed, -> { where(status: 'Fail') }
20
+ scope :sorted, -> { order(start_time: :desc) } # Newest first
21
+
22
+
23
+ # Methods
24
+
25
+
26
+ def completed?
27
+ !!read_attribute(:elapsed)
28
+ end
29
+
30
+ def elapsed
31
+ super || [(Time.now - start_time).to_i, 86400].min
32
+ rescue
33
+ 0 # Prevent an invalid record from causing problems.
34
+ end
35
+
36
+ # Fail the Process Log.
37
+ # Pass in nothing for no comment or backtrace
38
+ # Pass in the Exception itself to automatically extract.
39
+ # Pass in comment and/or backtrace to set/overwrite.
40
+ def fail!(exception = nil, comment: nil, backtrace: nil)
41
+ self.status = "Fail"
42
+ self.end_time = Time.now
43
+
44
+ unless exception.blank?
45
+ self.comment = exception.to_s
46
+ self.backtrace = exception.backtrace.join("\n")
47
+ end
48
+
49
+ # Manually passed named arguments overwrite exception if that was also provided.
50
+ self.comment = comment unless comment.blank?
51
+ self.comment ||= 'Fail'
52
+ self.backtrace = Array(backtrace).join("\n") unless backtrace.blank?
53
+
54
+ save
55
+ end
56
+
57
+ def fail?
58
+ status == "Fail"
59
+ end
60
+
61
+ def local_end
62
+ end_time&.localtime&.strftime("%F %-l:%M%P")
63
+ end
64
+
65
+ def local_start
66
+ start_time&.localtime&.strftime("%F %-l:%M%P")
67
+ end
68
+
69
+ def progress
70
+ success? ? 100 : [99, (100 * elapsed / average_elapsed).to_i].min rescue "?"
71
+ end
72
+
73
+ def siblings
74
+ ProcessLog.where(key: key)
75
+ end
76
+
77
+ def started?
78
+ status == "Started"
79
+ end
80
+
81
+ def status
82
+ elapsed >= 86400 ? "Fail" : super
83
+ end
84
+
85
+ def success!
86
+ self.status = "Success" unless status == "Fail"
87
+ self.end_time = Time.now
88
+ save!
89
+ end
90
+
91
+ def success?
92
+ status == "Success"
93
+ end
94
+
95
+ def to_s
96
+ key.to_s
97
+ end
98
+
99
+ private
100
+
101
+ # Initialize defaults only on create
102
+ def defaults
103
+ return if persisted?
104
+ self.start_time ||= Time.now
105
+ self.status ||= 'Started'
106
+ end
107
+
108
+ end
109
+ end
@@ -1,91 +1,3 @@
1
1
  class ProcessLog < ApplicationRecord
2
-
3
- validates :key, presence: true
4
- validates :start_time, presence: true
5
- validates :status, presence: true
6
-
7
- before_validation { self.start_time ||= Time.now }
8
- before_validation { self.status ||= "Started" }
9
- before_save { self.elapsed = end_time && end_time - start_time }
10
- before_save { self.average_elapsed = siblings.completed.sorted.limit(10).average(:elapsed) }
11
-
12
-
13
- # Scopes
14
- scope :completed, -> { where.not(elapsed: nil) }
15
- scope :sorted, -> { order(start_time: :desc) } # Newest first
16
-
17
-
18
- # Methods
19
-
20
- def completed?
21
- !!elapsed
22
- end
23
-
24
- def elapsed
25
- super || [(Time.now - start_time).to_i, 86400].min
26
- end
27
-
28
- # Fail the Process Log.
29
- # Pass in nothing for no comment or backtrace
30
- # Pass in the Exception itself to automatically extract.
31
- # Pass in comment and/or backtrace to set/overwrite.
32
- def fail!(exception = nil, comment: nil, backtrace: nil)
33
- self.status = "Fail"
34
- self.end_time = Time.now
35
-
36
- unless exception.blank?
37
- self.comment = exception.to_s
38
- self.backtrace = exception.backtrace.join("\n")
39
- end
40
-
41
- # Manually passed named arguments overwrite exception if that was also provided.
42
- self.comment = comment unless comment.blank?
43
- self.comment ||= 'Fail'
44
- self.backtrace = Array(backtrace).join("\n") unless backtrace.blank?
45
-
46
- save
47
- end
48
-
49
- def fail?
50
- status == "Fail"
51
- end
52
-
53
- def local_end
54
- end_time&.localtime&.strftime("%F %-l:%M%P")
55
- end
56
-
57
- def local_start
58
- start_time&.localtime&.strftime("%F %-l:%M%P")
59
- end
60
-
61
- def progress
62
- success? ? 100 : [99, (100 * elapsed / average_elapsed).to_i].min rescue "?"
63
- end
64
-
65
- def siblings
66
- ProcessLog.where(key: key)
67
- end
68
-
69
- def started?
70
- status == "Started"
71
- end
72
-
73
- def status
74
- elapsed >= 86400 ? "Fail" : super
75
- end
76
-
77
- def success!
78
- self.status = "Success" unless status == "Fail"
79
- self.end_time = Time.now
80
- save!
81
- end
82
-
83
- def success?
84
- status == "Success"
85
- end
86
-
87
- def to_s
88
- key
89
- end
90
-
2
+ include ProcessLogBase
91
3
  end
@@ -1,3 +1,8 @@
1
1
  module DuffyLog
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
4
+
5
+ # CHANGELOG
6
+ #
7
+ # 1.1.0 Non-breaking refactor of ProcessLog logic into Concern for easier host_app extension.
8
+ # 1.0.0 Initial Release
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duffy_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Duffy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-22 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -105,6 +105,7 @@ files:
105
105
  - README.md
106
106
  - Rakefile
107
107
  - app/controllers/process_logs_controller.rb
108
+ - app/models/concerns/process_log_base.rb
108
109
  - app/models/process_log.rb
109
110
  - app/views/process_logs/_form.html.erb
110
111
  - app/views/process_logs/edit.html.erb