Module: Work

Defined in:
lib/work.rb

Overview

A module to (roughly) estimate productivity. Uses the 'updated_at' timestamp as a proxy.

Class Method Summary collapse

Class Method Details

.new_session(current_time) ⇒ Hash

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
# File 'lib/work.rb', line 50

def self.new_session(current_time)
  return {
    start: current_time, # Time of session start
    end: nil,            # Time of session end
    count: 1,            # Number of records updated
    efficiency: nil      # Records updated per minute
  }
end

.sessions(records) ⇒ Array

Parameters:

  • records (An ActiveRecord scope)

    A session is a contiguous period of time in which the gap between updated records is no longer than 60 minutes

Returns:

  • (Array)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/work.rb', line 8

def self.sessions(records)
  return [] if records.none?

  r = records.to_a

  a = r.shift
  last_time = a.updated_at

  sessions = [ ]
  current_session = new_session(last_time)

  while r.count != 0 
    a = r.shift

    current_time = a.updated_at

    # more than one hour between subsequent specimens
    # sessions ends
    if current_time - last_time > 3600.0

      current_session[:end] = last_time

      if current_session[:count] == 1
        current_session[:efficiency] = 0.0 
      else
        current_session[:efficiency] = current_session[:count].to_f / ((current_session[:end] - current_session[:start]) / 60.0)
      end

      sessions.push current_session
      current_session = new_session(current_time)

      # session continues
    else
      current_session[:count] += 1 
    end
    last_time = current_time
  end

  sessions
end

.total_time(sessions) ⇒ Integer?

Returns in seconds

Returns:

  • (Integer, nil)

    in seconds



61
62
63
64
65
66
67
68
# File 'lib/work.rb', line 61

def self.total_time(sessions)
  return 0 if sessions.nil?
  t = 0
  sessions.each do |s|
    t += s[:end] - s[:start]
  end
  t 
end