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.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/clockout.rb', line 209

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

    # Default options
    $opts = {time_cutoff:120, 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 = get_commits(repo, num)

        @maxed_out = (commits.size == num)

        prepare_blocks(commits, author)
    end
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#maxed_outObject

Returns the value of attribute maxed_out.



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

def maxed_out
  @maxed_out
end

#time_per_dayObject

Returns the value of attribute time_per_day.



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

def time_per_day
  @time_per_day
end

Class Method Details

.clock_path(path) ⇒ Object



193
194
195
196
# File 'lib/clockout.rb', line 193

def self.clock_path(path)
    return nil if !path
    File.join(path,"clock.yml")
end

.get_repo(path) ⇒ Object



174
175
176
177
# File 'lib/clockout.rb', line 174

def self.get_repo(path)
    repo = Rugged::Repository.discover(path)
    return repo, repo.workdir
end

.parse_clockfile(file) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/clockout.rb', line 179

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



198
199
200
201
# File 'lib/clockout.rb', line 198

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

Instance Method Details

#clocks_to_records(clocks, in_out) ⇒ Object



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

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(git_commits) ⇒ Object



13
14
15
16
17
# File 'lib/clockout.rb', line 13

def commits_to_records(git_commits)
    git_commits.each_with_index.map do |commit, i| 
        Commit.new(commit)
    end
end

#get_commits(repo, num) ⇒ Object



203
204
205
206
207
# File 'lib/clockout.rb', line 203

def get_commits(repo, num)
    walker = Rugged::Walker.new(repo)
    walker.push(repo.head.target_id)
    walker.each.take(num)
end

#lastObject



170
171
172
# File 'lib/clockout.rb', line 170

def last
    @blocks.last.last
end

#prepare_blocks(commits_in, author) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/clockout.rb', line 153

def prepare_blocks(commits_in, 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)

    # 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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/clockout.rb', line 38

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clockout.rb', line 25

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