Class: History

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ActiveRecord::Serialization
Defined in:
app/models/history.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period = nil) ⇒ History

Returns a new instance of History.


7
8
9
10
# File 'app/models/history.rb', line 7

def initialize(period = nil)
  period = 'today' if period.nil?
  @period = period
end

Instance Attribute Details

#periodObject

Returns the value of attribute period


5
6
7
# File 'app/models/history.rb', line 5

def period
  @period
end

Instance Method Details

#average_processing_timeObject


59
60
61
62
63
64
65
66
67
# File 'app/models/history.rb', line 59

def average_processing_time
  return 0 unless completed_jobs.any?

  total_time = completed_jobs.reduce(0) do |a, e|
    a + (e.completed_at - e.transcoding_started_at)
  end

  total_time / completed_jobs.size
end

#average_queue_timeObject


69
70
71
72
73
74
75
76
77
78
# File 'app/models/history.rb', line 69

def average_queue_time
  return 0 unless completed_jobs.any?

  total_time = completed_jobs.reduce(0) do |a, e|
    a + (e.transcoding_started_at - e.created_at)
  end

  num = total_time / completed_jobs.size
  num < 1 ? 1 : num
end

#betweenObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/history.rb', line 16

def between
  case period
  when 'today'
    Time.current.at_beginning_of_day..Time.current
  when 'yesterday'
    (Time.current.at_beginning_of_day - 1.day)..Time.current.at_beginning_of_day
  when 'week'
    7.days.ago.at_beginning_of_day..Time.current
  when 'month'
    30.days.ago.at_beginning_of_day..Time.current
  when 'all'
    Time.new(1970, 1, 1)..Time.current
  end
end

#completed_jobsObject


31
32
33
# File 'app/models/history.rb', line 31

def completed_jobs
  @success ||= jobs.where(state: Job::Success)
end

#failed_jobsObject


39
40
41
# File 'app/models/history.rb', line 39

def failed_jobs
  @failed ||= Job.where(created_at: between).where(state: Job::Failed)
end

#jobsObject


12
13
14
# File 'app/models/history.rb', line 12

def jobs
  @jobs ||= Job.where(created_at: between)
end

#number_of_completed_jobsObject


35
36
37
# File 'app/models/history.rb', line 35

def number_of_completed_jobs
  completed_jobs.size
end

#number_of_failed_jobsObject


43
44
45
# File 'app/models/history.rb', line 43

def number_of_failed_jobs
  failed_jobs.size
end

#number_of_processing_jobsObject


51
52
53
# File 'app/models/history.rb', line 51

def number_of_processing_jobs
  processing_jobs.size
end

#processing_jobsObject


47
48
49
# File 'app/models/history.rb', line 47

def processing_jobs
  @transcoding ||= jobs.where(state: Job::Processing)
end

#seconds_encodedObject


55
56
57
# File 'app/models/history.rb', line 55

def seconds_encoded
  completed_jobs.reduce(0) { |a, e| a + e.duration.to_i }
end

#serializable_hash(_opts = {}) ⇒ Object


80
81
82
83
84
85
# File 'app/models/history.rb', line 80

def serializable_hash(_opts = {})
  super({
    methods: [:seconds_encoded, :average_processing_time, :average_queue_time,
              :number_of_completed_jobs, :number_of_failed_jobs, :number_of_processing_jobs]
  })
end

#to_json(opts = {}) ⇒ Object


87
88
89
# File 'app/models/history.rb', line 87

def to_json(opts = {})
  serializable_hash.to_json(opts)
end

#to_xml(opts = {}) ⇒ Object


91
92
93
# File 'app/models/history.rb', line 91

def to_xml(opts = {})
  serializable_hash(opts).to_xml(root: 'statistics')
end