2014-09-24 07:28:17 +00:00
|
|
|
|
|
|
|
module TimeCost
|
|
|
|
class Range
|
2014-09-24 17:46:02 +00:00
|
|
|
attr_accessor :time_start, :time_stop, :commits, :author
|
2014-09-24 07:28:17 +00:00
|
|
|
|
2014-09-26 07:42:09 +00:00
|
|
|
GRANULARITY_DEFAULT = 0.5
|
|
|
|
|
|
|
|
def initialize commit, options = {}
|
|
|
|
@granularity = options[:granularity] || GRANULARITY_DEFAULT
|
2014-09-24 17:46:02 +00:00
|
|
|
|
|
|
|
# FIXME: First approximation for users
|
|
|
|
# later, we'll replace with @user = User.parse(commit.author)
|
|
|
|
@author = commit.author
|
|
|
|
|
2014-09-24 07:28:17 +00:00
|
|
|
@time_stop = DateTime.parse(commit.date)
|
2014-09-26 07:42:09 +00:00
|
|
|
@time_start = @time_stop - (@granularity * 3 / 24.0)
|
2014-09-24 07:28:17 +00:00
|
|
|
@commits = [commit]
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge range
|
|
|
|
# B -----[----]----
|
|
|
|
# A --[----]------
|
|
|
|
# = ---[------]----
|
|
|
|
|
|
|
|
# minimum of both
|
|
|
|
new_start = if range.time_start < @time_start then range.time_start
|
|
|
|
else @time_start
|
|
|
|
end
|
|
|
|
|
|
|
|
new_end = if range.time_stop >= @time_stop then range.time_stop
|
|
|
|
else @time_stop
|
|
|
|
end
|
|
|
|
|
|
|
|
@time_start = new_start
|
|
|
|
@time_stop = new_end
|
|
|
|
@commits.concat range.commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def overlap? range
|
|
|
|
result = false
|
|
|
|
|
2014-09-24 17:46:02 +00:00
|
|
|
# return early result if ranges come from different authors
|
|
|
|
return false if (@author != range.author)
|
|
|
|
|
2014-09-24 07:28:17 +00:00
|
|
|
# Ref ----[----]-----
|
|
|
|
# overlapping :
|
|
|
|
# A -[----]--------
|
|
|
|
# B -------[----]--
|
|
|
|
# C -[----------]--
|
|
|
|
# D ------[]-------
|
|
|
|
# non-overlapping :
|
|
|
|
# E -[]------------
|
|
|
|
# F -----------[]--
|
|
|
|
|
|
|
|
start_before_start = (range.time_start < @time_start)
|
|
|
|
start_after_start = (range.time_start >= @time_start)
|
|
|
|
start_after_stop = (range.time_start >= @time_stop)
|
|
|
|
start_before_stop = (range.time_start < @time_stop)
|
|
|
|
|
|
|
|
stop_before_stop = (range.time_stop < @time_stop)
|
|
|
|
stop_after_stop = (range.time_stop >= @time_stop)
|
|
|
|
stop_before_start = (range.time_stop < @time_start)
|
|
|
|
stop_after_start = (range.time_stop >= @time_start)
|
|
|
|
|
|
|
|
# A case
|
|
|
|
if start_before_start and start_before_stop and
|
|
|
|
stop_after_start and stop_before_stop then
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# B case
|
|
|
|
if start_after_start and start_before_stop and
|
|
|
|
stop_after_start and stop_after_stop then
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# C case
|
|
|
|
if start_before_start and start_before_stop and
|
|
|
|
stop_after_start and stop_after_stop then
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# D case
|
|
|
|
if start_after_start and start_before_stop and
|
|
|
|
stop_after_start and stop_before_stop then
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
def fixed_start
|
2014-09-26 07:42:09 +00:00
|
|
|
return @time_start + (@granularity/24.0)
|
2014-09-24 07:28:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
|
|
|
return ("%.2f" % ((@time_stop - fixed_start).to_f * 24)).to_f
|
|
|
|
end
|
|
|
|
|
2014-09-26 07:42:09 +00:00
|
|
|
def to_s show_authors = true
|
2014-09-25 07:13:05 +00:00
|
|
|
val = "(%s)\t%s - %s\n" % [diff, fixed_start, @time_stop]
|
2014-09-26 07:42:09 +00:00
|
|
|
if show_authors then
|
2014-09-25 07:13:05 +00:00
|
|
|
val += "\tby %s\n" % @commits.first.author
|
|
|
|
end
|
2014-09-24 07:28:17 +00:00
|
|
|
@commits.each do |commit|
|
|
|
|
lines = []
|
|
|
|
lines.concat commit.note.split(/\n/)
|
|
|
|
r = lines.map{ |s| "\t %s" % s }.join "\n"
|
|
|
|
r[1] = '*'
|
|
|
|
val += r + "\n"
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-24 17:46:02 +00:00
|
|
|
|