This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracker.rb
191 lines (161 loc) · 4.84 KB
/
tracker.rb
1
2
3
4
5
6
7
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Not good to pollute the global classes
# But supersexy code!
class Fixnum
def hours
return sprintf("%.2f", self.to_f/60/60).to_f
end
end
class Tracker
@root = File.expand_path(File.dirname(__FILE__))
# Time lock
@@lock_file = "#{@root}/data/clock.lock"
# Database
@@data_file = "#{@root}/data/timetracker.db"
# Workday in seconds
@@workday = 28800
class << self
def stop(comment="")
self.write_tracked_time(comment)
return self.unlock_clock if self.locked?
return false
end
def start
return self.lock_clock unless self.locked?
return false
end
def today
return self.day(self.date_string)
end
def running?
return self.locked?
end
def day(date)
return false unless File.readable? @@data_file
file = File.new(@@data_file,"r:utf-8")
today = Array.new
file.each do |line|
data = line.chomp.split(",")
if(data.first == date)
today << data
end
end
file.close
return today
end
def summary(date=nil)
if date.nil?
date = self.date_string
end
if day = self.day(date)
time = 0
day.each do |d|
time += (d[2].to_i - d[1].to_i)
end
if self.locked?
time += (Time.now.to_i - self.locked_at?)
end
{:worked => time, :worked_hours => time.hours, :work_diff => (time - @@workday), :work_diff_hours => (time - @@workday).hours}
else
{:worked => 0, :worked_hours => 0, :work_diff => (0 - @@workday), :work_diff_hours => (0 - @@workday).hours}
end
end
def history
history = Hash.new
meta = Hash.new
return false unless File.exists? @@data_file
file = File.open(@@data_file)
days = Array.new
file.each_with_index do |line, i|
date, start, stop, comment = line.split(",")
# How long is the session
diff = stop.to_i - start.to_i
# Clean-up
%w{date start stop comment}.each do |clean|
clean.chomp!
end
# Since we have the date we only need hour, minute and second
start_s = Time.at(start.to_i).strftime("%H:%M:%S")
stop_s = Time.at(stop.to_i).strftime("%H:%M:%S")
# Remove the quotes
comment.gsub!(/"/, '')
# Pure chaos! Embrace!
history[:days] = Hash.new unless history[:days].is_a?(Hash)
history[:days][date] = Hash.new if !history[:days][date].is_a?(Hash)
history[:days][date][:runs] = Array.new unless history[:days][date][:runs].is_a?(Array)
history[:days][date][:runs] << {:start => start_s, :stop => stop_s, :comment => comment, :id => i}
history[:days][date][:worked] = (history[:days][date][:worked] || 0) + (stop.to_i - start.to_i)
history[:days][date][:worked_hours] = history[:days][date][:worked].hours
history[:days][date][:work_diff] = history[:days][date][:worked] - @@workday
history[:days][date][:work_diff_hours] = history[:days][date][:work_diff].hours
end
history[:days].each do |day, data|
history[:total] = (history[:total] || 0) + data[:worked]
history[:total_hours] = history[:total].hours
history[:total_diff] = (history[:total_diff] || 0) + data[:work_diff]
history[:total_diff_hours] = history[:total_diff].hours
end
history[:days] = history[:days].sort
return history
end
protected
def lock_clock
if locked?
return false
else
lock = File.new(@@lock_file, "w:utf-8")
lock.puts Time.now.to_i
lock.close
return true
end
end
def unlock_clock
if locked?
File.delete(@@lock_file)
return true
end
return false
end
def locked?
return File.exists?(@@lock_file);
end
def locked_at?
return false unless File.readable? @@lock_file
file = File.open(@@lock_file);
lock_time = file.readline
file.close
return lock_time.to_i
end
def last_entry
if File.exists?(@@data_file)
file = File.open(@@data_file)
if entry = file.gets
file.close
return entry.split(",")
end
else
return false
end
end
def write_tracked_time(comment="")
if self.locked?
last_entry = self.last_entry
start_time = self.locked_at?
file = File.new(@@data_file, "a+:utf-8")
end_time = Time.now.to_i
work_time = end_time - start_time
file.puts("#{self.date_string(start_time)}, #{start_time}, #{end_time}, \"#{comment}\"")
file.close
return true
end
return false
end
def date_string(time=nil)
format="%Y-%m-%d"
if time.nil?
return Time.new.strftime(format)
else
return Time.at(time).strftime(format)
end
end
end
end