Class: Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/zentool/metrics.rb

Overview

Represents an aggregate of metrics from multiple tickets

Constant Summary collapse

MINUTES_IN_DAY =
1440
MINUTES_IN_HOUR =
60
PLOT_WIDTH =
200
CURRENT_DATE =
Date.today

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tickets) ⇒ Metrics

Returns a new instance of Metrics.



14
15
16
17
18
19
20
21
22
23
# File 'lib/zentool/metrics.rb', line 14

def initialize(tickets)
  @tickets = tickets
  log_scale = {'0' => 0, '1' => 0, '2' => 0, '3' => 0, '4' => 0,
    '5' => 0, '6-10' => 0, '11-20' => 0, '21-50' => 0, '51-100' => 0, '101+' => 0}
  log_scale2 = log_scale.clone
  @unsolved_tickets_by_age_log_scale = unsolved_age(log_scale)
  @solved_tickets_by_age_log_scale = solved_age(log_scale2)
  @avg_user_priority = avg_priority('user_priority')
  @avg_development_priority = avg_priority('development_priority')
end

Instance Attribute Details

#avg_development_priorityObject

Returns the value of attribute avg_development_priority.



11
12
13
# File 'lib/zentool/metrics.rb', line 11

def avg_development_priority
  @avg_development_priority
end

#avg_user_priorityObject

Returns the value of attribute avg_user_priority.



11
12
13
# File 'lib/zentool/metrics.rb', line 11

def avg_user_priority
  @avg_user_priority
end

#solved_tickets_by_age_log_scaleObject

Returns the value of attribute solved_tickets_by_age_log_scale.



11
12
13
# File 'lib/zentool/metrics.rb', line 11

def solved_tickets_by_age_log_scale
  @solved_tickets_by_age_log_scale
end

#ticketsObject

Returns the value of attribute tickets.



11
12
13
# File 'lib/zentool/metrics.rb', line 11

def tickets
  @tickets
end

#unsolved_tickets_by_age_log_scaleObject

Returns the value of attribute unsolved_tickets_by_age_log_scale.



11
12
13
# File 'lib/zentool/metrics.rb', line 11

def unsolved_tickets_by_age_log_scale
  @unsolved_tickets_by_age_log_scale
end

Instance Method Details

#avg_priority(priority_type) ⇒ Object

Creates plot data for average first reply time by priority



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zentool/metrics.rb', line 74

def avg_priority(priority_type)
  # Hash with key => user_priority, and the value => array of reply times from tickets with that priority
  tickets_by_priority = Hash.new {|h,k| h[k] = []}

  # Hash with key => user_priority, and the value => average reply time of that priority
  avg_priority = Hash.new(0)

  tickets.each do |ticket|

    priority = ticket.info[priority_type]
    reply_time = ticket.metrics['reply_time_in_minutes']

    if priority == nil
        priority = 'None'
    end

    if reply_time
      tickets_by_priority[priority] << reply_time
    end
  end

  tickets_by_priority.each do |key, value|
      if key == nil
          key = 'None'
      end
      avg = value.inject(:+).to_f / value.length
      avg_priority[key] = avg / MINUTES_IN_HOUR
  end
  avg_priority
end

#graphObject

draws command line graph based on ticket metrics



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zentool/metrics.rb', line 106

def graph
  puts
  puts 'Age of Solved Tickets'
  puts '_____________________'
	puts 'Days     Ticket-Count'
	@solved_tickets_by_age_log_scale.keys.each do |age|
    printf "%-10s %5d %s \n" % [age, @solved_tickets_by_age_log_scale[age],
      '#' * (Math.log10(@solved_tickets_by_age_log_scale[age]+1)*5)]
	end

  puts
  puts 'Age of Unsolved Tickets'
  puts '_______________________'
  puts 'Days     Ticket-Count'
  @unsolved_tickets_by_age_log_scale.keys.each do |age|
    printf "%-10s %5d %s \n" % [age, @unsolved_tickets_by_age_log_scale[age],
      '#' * (Math.log10(@unsolved_tickets_by_age_log_scale[age]+1)*5)]
  end

  puts
  puts 'Average First Reply Time by Development Priority'
  puts '___________________________________________'
	puts 'Priority   Reply-Time-Hours'
	@avg_development_priority.keys.sort.each do |development_priority|
    printf "%-10s %5d %s \n" % [development_priority, @avg_development_priority[development_priority],
      '#' * (@avg_development_priority[development_priority] / PLOT_WIDTH)]
	end

  puts
  puts 'Average First Reply Time by User Priority'
  puts '___________________________________________'
  puts 'Priority   Reply-Time-Hours'
  @avg_user_priority.keys.sort.each do |user_priority|
    printf "%-10s %5d %s \n" % [user_priority, @avg_user_priority[user_priority],
      '#' * (@avg_user_priority[user_priority] / PLOT_WIDTH)]
  end
end

#plot_as_log(tickets_by_log_scale, age) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zentool/metrics.rb', line 25

def plot_as_log(tickets_by_log_scale, age)
  if age <= 5
    tickets_by_log_scale[age.to_s] += 1
  elsif age <= 10
    tickets_by_log_scale['6-10'] += 1
  elsif age <= 20
    tickets_by_log_scale['11-20'] += 1
  elsif age <= 50
    tickets_by_log_scale['21-50'] += 1
  elsif age <= 100
    tickets_by_log_scale['51-100'] += 1
  else
    tickets_by_log_scale['101+'] += 1
  end
  tickets_by_log_scale
end

#saveObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/zentool/metrics.rb', line 144

def save
  CSV.open('solved_tickets_by_age.csv', 'wb') do |csv|
    csv << ['Days', 'Ticket-Count']
  end
  @solved_tickets_by_age_log_scale.keys.each do |age|
    CSV.open('solved_tickets_by_age.csv', 'a') do |csv|
      csv << [age, @solved_tickets_by_age_log_scale[age]]
    end
  end

  CSV.open('unsolved_tickets_by_age.csv', 'wb') do |csv|
    csv << ['Days', 'Ticket-Count']
  end
  @unsolved_tickets_by_age_log_scale.keys.each do |age|
    CSV.open('unsolved_tickets_by_age.csv', 'a') do |csv|
      csv << [age, @unsolved_tickets_by_age_log_scale[age]]
    end
  end

  CSV.open('avg_reply_time_priority.csv', 'wb') do |csv|
    csv << ['Priority', 'Average-Reply-Time']
  end
  @avg_development_priority.keys.sort.each do |age|
    CSV.open('avg_reply_time_priority.csv', 'a') do |csv|
      csv << [age, @avg_development_priority[age]]
    end
  end
end

#solved_age(log_scale) ⇒ Object

Creates plot data for number of solved tickets by age



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zentool/metrics.rb', line 60

def solved_age(log_scale)
  out_scale = Hash.new
  @tickets.each do |ticket|
    metrics = ticket.metrics
    # Rounds the age of the ticket down to the nearest day
    if metrics['full_resolution_time_in_minutes']
      age = metrics['full_resolution_time_in_minutes'] / MINUTES_IN_DAY
      log_scale = plot_as_log(log_scale, age)
    end
  end
  log_scale
end

#unsolved_age(log_scale) ⇒ Object

Creates plot data for number of solved tickets by age



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zentool/metrics.rb', line 43

def unsolved_age(log_scale)
  out_scale = Hash.new
  @tickets.each do |ticket|
    metrics = ticket.metrics
    # Rounds the age of the ticket down to the nearest day
    unless metrics['full_resolution_time_in_minutes']
      if metrics['initially_assigned_at']
        start_date = Date.parse metrics['initially_assigned_at']
        age = (CURRENT_DATE - start_date).to_i
        log_scale = plot_as_log(log_scale, age)
      end
    end
  end
  log_scale
end