Class: Desiru::Persistence::Repositories::ModuleExecutionRepository
- Inherits:
-
BaseRepository
- Object
- BaseRepository
- Desiru::Persistence::Repositories::ModuleExecutionRepository
show all
- Defined in:
- lib/desiru/persistence/repositories/module_execution_repository.rb
Overview
Repository for module execution records
Instance Attribute Summary
#model_class
Instance Method Summary
collapse
#all, #count, #create, #delete?, #exists?, #find, #find_by, #paginate, #update, #where
Constructor Details
Returns a new instance of ModuleExecutionRepository.
10
11
12
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 10
def initialize
super(Models::ModuleExecution)
end
|
Instance Method Details
#average_duration(module_name = nil) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 29
def average_duration(module_name = nil)
scope = dataset
scope = scope.where(module_name: module_name) if module_name
scope = scope.where(status: 'completed')
.exclude(finished_at: nil)
records = scope.all
return nil if records.empty?
durations = records.map(&:duration).compact
return nil if durations.empty?
durations.sum.to_f / durations.length
end
|
#by_status(status) ⇒ Object
25
26
27
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 25
def by_status(status)
dataset.where(status: status).all
end
|
#complete(id, outputs, metadata = {}) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 65
def complete(id, outputs, metadata = {})
update(id, {
outputs: outputs,
metadata: metadata,
status: 'completed',
finished_at: Time.now
})
end
|
#create_for_module(module_name, inputs, api_request_id: nil) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 55
def create_for_module(module_name, inputs, api_request_id: nil)
create(
module_name: module_name,
inputs: inputs,
status: 'pending',
started_at: Time.now,
api_request_id: api_request_id
)
end
|
#fail(id, error_message, error_backtrace = nil) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 74
def fail(id, error_message, error_backtrace = nil)
update(id, {
error_message: error_message,
error_backtrace: error_backtrace,
status: 'failed',
finished_at: Time.now
})
end
|
#find_by_module(module_name) ⇒ Object
14
15
16
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 14
def find_by_module(module_name)
dataset.where(module_name: module_name).all
end
|
#recent(limit = 10) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 18
def recent(limit = 10)
dataset
.order(Sequel.desc(:started_at))
.limit(limit)
.all
end
|
#success_rate(module_name = nil) ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/desiru/persistence/repositories/module_execution_repository.rb', line 44
def success_rate(module_name = nil)
scope = dataset
scope = scope.where(module_name: module_name) if module_name
total = scope.count
return 0.0 if total.zero?
successful = scope.where(status: 'completed').count
(successful.to_f / total * 100).round(2)
end
|