Class: Clockout

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, author = nil, num = 1) ⇒ Clockout

Returns a new instance of Clockout.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/clockout.rb', line 237

def initialize(path = nil, author = nil, num = 1)
    @time_per_day = Hash.new(0)

    # Default options
    $opts = {time_cutoff:120, my_files:"/.*/", estimation_factor:1.0}

    if path
        repo, root_path = Clockout.get_repo(path) || exit

        # Parse config options
        clock_opts = Clockout.parse_clockfile(Clockout.clock_path(root_path))

        # Merge with config override options
        $opts.merge!(clock_opts) if clock_opts

        commits = repo.commits('master', num).reverse
        commit_stats = repo.commit_stats('master', num).reverse #much faster if retrieved in batch

        @maxed_out = (commits.size == num)

        prepare_blocks(commits, commit_stats, author)
    end
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



20
21
22
# File 'lib/clockout.rb', line 20

def blocks
  @blocks
end

#maxed_outObject

Returns the value of attribute maxed_out.



20
21
22
# File 'lib/clockout.rb', line 20

def maxed_out
  @maxed_out
end

#time_per_dayObject

Returns the value of attribute time_per_day.



20
21
22
# File 'lib/clockout.rb', line 20

def time_per_day
  @time_per_day
end

Class Method Details

.clock_path(path) ⇒ Object



227
228
229
230
# File 'lib/clockout.rb', line 227

def self.clock_path(path)
    return nil if !path
    path+"/clock.yml"
end

.get_repo(path, original_path = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/clockout.rb', line 188

def self.get_repo(path, original_path = nil)
    begin
        return Grit::Repo.new(path), path
    rescue Exception => e
        if e.class == Grit::NoSuchPathError
            puts_error "Path '#{path}' could not be found."
            return nil
        else
            # Must have drilled down to /
            if (path.length <= 1)
                puts_error "'#{original_path}' is not a Git repository."
                return nil
            end

            # Could be that we're in a directory inside the repo
            # Strip off last directory
            one_up = path
            while ((one_up = one_up[0..-2])[-1] != '/') do end

            # Recursively try one level higher
            return get_repo(one_up[0..-2], path)
        end
    end
end

.parse_clockfile(file) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/clockout.rb', line 213

def self.parse_clockfile(file)
    return nil if !File.exists?(file)

    begin
        opts = YAML.load_file(file)
    rescue Exception => e
        puts_error e.to_s
        exit
    end

    # Symbolizes keys
    Hash[opts.map{|k,v| [k.to_sym, v]}]
end

.root_path(path) ⇒ Object



232
233
234
235
# File 'lib/clockout.rb', line 232

def self.root_path(path)
    repo, root_path = get_repo(path)
    root_path
end

Instance Method Details

#clocks_to_records(clocks, in_out) ⇒ Object



33
34
35
36
37
# File 'lib/clockout.rb', line 33

def clocks_to_records(clocks, in_out)
    clocks.map do |c| 
        Clock.new(in_out, c.first[0], c.first[1])
    end
end

#commits_to_records(grit_commits, commit_stats) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/clockout.rb', line 22

def commits_to_records(grit_commits, commit_stats)
    my_files = eval($opts[:my_files])
    not_my_files = eval($opts[:not_my_files] || "")
    grit_commits.each_with_index.map do |commit, i| 
        c = Commit.new(commit)
        c.stats = commit_stats[i][1]
        c.calculate_diffs(my_files, not_my_files)
        c
    end
end

#lastObject



184
185
186
# File 'lib/clockout.rb', line 184

def last
    @blocks.last.last
end

#prepare_blocks(commits_in, commit_stats, author) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clockout.rb', line 167

def prepare_blocks(commits_in, commit_stats, author)
    clockins = $opts[:in] || {}
    clockouts = $opts[:out] || {}

    # Convert clock-in/-outs into Clock objs & commits into Commit objs
    clocks = clocks_to_records(clockins, :in) + clocks_to_records(clockouts, :out)
    commits = commits_to_records(commits_in, commit_stats)

    # Merge & sort everything by date
    data = (commits + clocks).sort { |a,b| a.date <=> b.date }

    # If author is specified, delete everything not by that author
    data.delete_if { |c| c.author != author } if author

    @blocks = run(data)
end

#run(data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/clockout.rb', line 52

def run(data)
    blocks = []
    total_diffs, total_mins = 0, 0

    add_commit = lambda do |commit|
        last = blocks.last
        mins = (last && last.last.minutes) ? last.last.minutes*60 : 0
        if !last || (commit.date - (last.last.date + mins))/60.0 > $opts[:time_cutoff]
            blocks << [commit]
            false
        else
            last << commit
            true
        end
    end

    add_time_to_day = lambda do |time, date|
        @time_per_day[date.strftime(DAY_FORMAT)] += time
    end

    # Now go through and coalesce Clocks into Commits, while also splitting into blocks
    i = 0
    while i < data.size
        prev_c = (i == 0) ? nil : data[i-1]
        next_c = data[i+1]
        curr_c = data[i]

        if curr_c.class == Clock
            # If next is also a clock and it's the same type, delete this & use that one instead
            if next_c && next_c.class == Clock && next_c.in == curr_c.in
                data.delete_at(i)
                next
            end

            # Clock in doesn't do anything, a commit will pick them up
            # For a clock out...
            if curr_c.out && prev_c
                # If previous is an IN, delete both and make a new commit
                if prev_c.class == Clock && prev_c.in
                    c = Commit.new
                    c.date = curr_c.date # date is "commit date", so on clockout
                    c.minutes = (curr_c.date - prev_c.date)/60.0
                    c.clocked_in, c.clocked_out = true, true

                    data.insert(i, c)
                    data.delete(prev_c)
                    data.delete(curr_c)

                    add_commit.call(c)
                    add_time_to_day.call(c.minutes, c.date)

                    #i is already incremented (we deleted 2 & added 1)
                    next
                elsif !prev_c.overriden
                    #Otherwise, append time onto the last commit (if it's time wasn't overriden)
                    addition = (curr_c.date - prev_c.date)/60.0
                    if prev_c.minutes
                        prev_c.minutes += addition
                        add_time_to_day.call(addition, prev_c.date)
                    else
                        # This means it's an estimation commit (first one)
                        # Mark how much we shoul add after we've estimated
                        prev_c.addition = addition
                    end
                    prev_c.clocked_out = true
                end
            end
        else
            # See if this commit was overriden in the config file
            if !try_overriding_record(curr_c)
                # Otherwise, if we're ignoring initial & it's initial, set minutes to 0
                if $opts[:ignore_initial] && !prev_c
                    curr_c.minutes = 0
                else
                    curr_c.clocked_in = true if prev_c && prev_c.class == Clock && prev_c.in
                    # If it added successfully into a block (or was clocked in), we can calculate based on last commit
                    if add_commit.call(curr_c) || curr_c.clocked_in
                        curr_c.minutes = (curr_c.date - prev_c.date)/60.0 # clock or commit, doesn't matter
                    end
                    # Otherwise, we'll do an estimation later, once we have more data
                end
            end

            if curr_c.minutes
                add_time_to_day.call(curr_c.minutes, curr_c.date)

                if curr_c.diffs
                    total_diffs += curr_c.diffs
                    total_mins += curr_c.minutes
                end
            end
        end

        i += 1
    end

    diffs_per_min = (1.0*total_diffs/total_mins)

    # Do estimation for all `nil` minutes.
    blocks.each do |block|
        first = block.first
        if !first.minutes
            first.estimated = true
            if diffs_per_min.nan? || diffs_per_min.infinite?
                first.minutes = first.addition
            else
                first.minutes = first.diffs/diffs_per_min * $opts[:estimation_factor] + first.addition
            end
            add_time_to_day.call(first.minutes, first.date)
        end
    end
    
    blocks
end

#try_overriding_record(record) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clockout.rb', line 39

def try_overriding_record(record)
    overrides = $opts[:overrides]
    overrides.each do |k, v|
        if record.sha.start_with? k
            record.minutes = v
            record.overriden = true
            return true
        end
    end if overrides

    false
end