Add statistics table
This commit is contained in:
@ -19,9 +19,10 @@ end
|
||||
|
||||
def serialize_span(span : Time::Span)
|
||||
{
|
||||
status: span < Time::Span.zero ? STATUS_OVERTIME : STATUS_UPTIME,
|
||||
hours: span.abs.total_hours.to_i32,
|
||||
minutes: span.abs.minutes.to_i32,
|
||||
status: span < Time::Span.zero ? STATUS_OVERTIME : STATUS_UPTIME,
|
||||
hours: span.abs.total_hours.to_i32,
|
||||
minutes: span.abs.minutes.to_i32,
|
||||
total_minutes: span.abs.total_minutes.to_i32,
|
||||
}
|
||||
end
|
||||
|
||||
@ -55,6 +56,19 @@ get "/api/status" do |env|
|
||||
data.to_json
|
||||
end
|
||||
|
||||
get "/api/statistics" do |env|
|
||||
profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s)
|
||||
data = profile.statistics now
|
||||
mapped = data.map do |v|
|
||||
{
|
||||
date: v.date.to_s("%Y-%m-%d"),
|
||||
planned: serialize_span(v.planned),
|
||||
worked: serialize_span(v.worked),
|
||||
}
|
||||
end
|
||||
mapped.to_json
|
||||
end
|
||||
|
||||
get "/" do
|
||||
render "public/index.ecr"
|
||||
end
|
||||
|
@ -23,9 +23,13 @@ module Dayoff
|
||||
def initialize(@date : Time, @hours : Int32)
|
||||
end
|
||||
|
||||
def hours_as_span : Time::Span
|
||||
Time::Span.new(hours: @hours, minutes: 0, seconds: 0)
|
||||
end
|
||||
|
||||
def in_range(from_time : Time, to_time : Time) : Time::Span
|
||||
if @date >= from_time && @date < to_time
|
||||
Time::Span.new(hours: @hours, minutes: 0, seconds: 0)
|
||||
hours_as_span
|
||||
else
|
||||
Time::Span.zero
|
||||
end
|
||||
@ -61,6 +65,10 @@ module Dayoff
|
||||
!@finish.nil?
|
||||
end
|
||||
|
||||
def get_span(to_time : Time) : Time::Span
|
||||
in_range @start, to_time
|
||||
end
|
||||
|
||||
def in_range(from_time : Time, to_time : Time) : Time::Span
|
||||
if finished?
|
||||
in_range_finished from_time, to_time
|
||||
|
@ -12,6 +12,23 @@ module Dayoff
|
||||
end
|
||||
end
|
||||
|
||||
class DayStatRecord
|
||||
getter date
|
||||
getter planned
|
||||
getter worked
|
||||
|
||||
def initialize(@date : Time, @planned : Time::Span, @worked : Time::Span)
|
||||
end
|
||||
|
||||
def add_planned(v : Time::Span)
|
||||
@planned += v
|
||||
end
|
||||
|
||||
def add_worked(v : Time::Span)
|
||||
@worked += v
|
||||
end
|
||||
end
|
||||
|
||||
class Profile
|
||||
@pdates = [] of PlannedDate
|
||||
@wrecords = [] of WorkRecord
|
||||
@ -75,5 +92,40 @@ module Dayoff
|
||||
worked = get_worked on_time.at_beginning_of_day, on_time
|
||||
planned - worked
|
||||
end
|
||||
|
||||
def statistics(on_time : Time) : Array(DayStatRecord)
|
||||
dates = {} of String => DayStatRecord
|
||||
|
||||
@pdates.each do |pd|
|
||||
key = pd.date.to_s("%Y-%m-%d")
|
||||
dates[key] = DayStatRecord.new(
|
||||
pd.date,
|
||||
pd.hours_as_span,
|
||||
Time::Span.zero
|
||||
)
|
||||
end
|
||||
|
||||
@wrecords.each do |wr|
|
||||
key = wr.start.to_s("%Y-%m-%d")
|
||||
if dates.has_key? key
|
||||
dates[key].add_worked wr.in_range(Helpers.zero_time, on_time)
|
||||
else
|
||||
dates[key] = DayStatRecord.new(
|
||||
wr.start,
|
||||
Time::Span.zero,
|
||||
wr.in_range(Helpers.zero_time, on_time)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
date_keys = dates.keys.sort!.reverse!
|
||||
|
||||
result = [] of DayStatRecord
|
||||
date_keys.each do |k|
|
||||
result.push dates[k]
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user