Class: FtgStats

Inherits:
Object
  • Object
show all
Defined in:
lib/ftg/ftg_stats.rb

Constant Summary collapse

IDLE_THRESHOLD =
5 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(only_last_day) ⇒ FtgStats

Returns a new instance of FtgStats.



6
7
8
9
10
# File 'lib/ftg/ftg_stats.rb', line 6

def initialize(only_last_day)
  load_data(only_last_day)
  crunch
  group
end

Instance Attribute Details

#statsObject

Returns the value of attribute stats.



4
5
6
# File 'lib/ftg/ftg_stats.rb', line 4

def stats
  @stats
end

Instance Method Details

#crunchObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ftg/ftg_stats.rb', line 57

def crunch
  # tagging branches in idle_parts
  @commands.each do |timestamp, command_info|
    if (key = search_idle_key(timestamp))
      @idle_parts[key][:branch] = command_info[:branch]
    end
  end

  # filling branches in idle_parts
  # tagging thresholds in idle_parts
  last_branch = 'unknown'
  @idle_parts.each do |timestamp, part|
    if part[:branch] && part[:branch] != '' && part[:branch] != 'no_branch'
      last_branch = part[:branch]
    end
    # puts "setting to #{last_branch} (#{Time.at(timestamp).strftime('%Y/%m/%d at %I:%M%p')})"
    @idle_parts[timestamp][:branch] = last_branch
    @idle_parts[timestamp][:idle] = part[:time_elapsed].to_i > IDLE_THRESHOLD
  end
end

#displayObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/ftg/ftg_stats.rb', line 92

def display
  Hash[@stats].each do |day, by_day|
    puts "#{day}:"
    Hash[by_day].each do |branch, by_branch|
      by_idle = Hash[by_branch]
      idle_str = by_idle[true] ? "(and #{Utils.format_time(by_idle[true])} idle)" : ''
      puts "  #{branch}: #{Utils.format_time(by_idle[false]) || '00:00:00'} #{idle_str}"
    end
  end
end

#groupObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ftg/ftg_stats.rb', line 78

def group
  @stats = @idle_parts.group_by { |ts, _| Time.at(ts).strftime('%F') }.map do |day, parts_by_day|
    [
      day,
      parts_by_day.group_by { |_, v| v[:branch] }.map do |branch, parts_by_branch|
        [
          branch,
          parts_by_branch.group_by { |_, v| v[:idle] }.map { |k, v| [k, v.count*10] }
        ]
      end
    ]
  end
end

#load_data(only_last_day) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ftg/ftg_stats.rb', line 27

def load_data(only_last_day)
  home = `echo $HOME`.strip
  ftg_dir = "#{home}/.ftg"
  commands_log_path = "#{ftg_dir}/log/commands.log"
  idle_log_path = "#{ftg_dir}/log/idle.log"
  records_to_load = only_last_day ? 24 * 360 : 0
  @commands = {}
  @idle_parts = {}

  # sample row:
  # pinouchon       fg      no_alias        /Users/pinouchon/.ftg   no_branch       1438867098
  (only_last_day ?
    `tail -n #{records_to_load} #{commands_log_path}`.split("\n") :
    File.foreach(commands_log_path)).each do |line|
    parts = line.split("\t")
    next if !parts[5] || parts[5].empty?
    @commands[parts[5].strip.to_i] = { :user => parts[0],
                                       :command => parts[1],
                                       :alias => parts[2], :dir => parts[3], :branch => parts[4] }
  end

  (only_last_day ?
    `tail -n #{records_to_load} #{idle_log_path}`.split("\n") :
    File.foreach(idle_log_path)).each do |line|
    parts = line.split("\t")
    next if !parts[1] || parts[1].empty?
    @idle_parts[parts[1].strip.to_i] = { :time_elapsed => parts[0] }
  end
end

#runObject



12
13
14
15
16
# File 'lib/ftg/ftg_stats.rb', line 12

def run

  display
  # sync_toggl
end

#search_idle_key(timestamp) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/ftg/ftg_stats.rb', line 18

def search_idle_key(timestamp)
  (0..10).each do |k|
    key = timestamp + k
    return key if @idle_parts[key]
  end
  # puts("not found #{Utils.format_time(timestamp)}")
  nil
end

#sync_togglObject



103
104
105
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
# File 'lib/ftg/ftg_stats.rb', line 103

def sync_toggl
  require 'pry'
  sync = FtgSync.new
  i = 0

  Hash[@stats].each do |day, by_day|
    puts "#{day}:"
    Hash[by_day].each do |branch, by_branch|
      by_idle = Hash[by_branch]
      idle_str = by_idle[true] ? "(and #{by_idle[true]} idle)" : ''
      puts "  #{branch}: #{by_idle[false] || '00:00:00'} #{idle_str}"

      if branch =~ /jt-/ && by_idle[false]
        ps = day.split('-')
        time = Time.new(ps[0], ps[1], ps[2], 12,0,0)
        begining_of_day = Time.new(ps[0], ps[1], ps[2], 0,0,0)
        end_of_day = begining_of_day + (24*3600)

        jt = branch[/(jt-[0-9]+)/]
        duration_parts = by_idle[false].split(':')
        duration = duration_parts[0].to_i * 3600 + duration_parts[1].to_i * 60 + duration_parts[2].to_i
        type = sync.maintenance?(jt) ? :maintenance : :sprint
        sync.create_entry("#{branch} [via FTG]", duration, time, type)
        i += 1

        puts "logging #{branch}: #{by_idle[false]}"
      end
    end
  end
  puts "total: #{i}"
end