Add today time info

This commit is contained in:
2019-11-13 22:56:20 +03:00
parent a505b23bb1
commit b40e3827b4
7 changed files with 97 additions and 24 deletions

View File

@ -6,6 +6,9 @@ base_path = ENV["BASE_PATH"]
puts "Set storage base path: " + base_path
STATUS_UPTIME = "uptime"
STATUS_OVERTIME = "overtime"
app = Dayoff::App.new base_path
add_handler CheckProfileHandler.new(app)
@ -14,6 +17,16 @@ def now
Time.local(Time::Location.load("Europe/Moscow"))
end
def date_status(profile, date)
span = profile.date_status date
{
date: date.to_s("%Y-%m-%d"),
status: span < Time::Span.zero ? STATUS_OVERTIME : STATUS_UPTIME,
hours: span.abs.total_hours.to_i32,
minutes: span.abs.minutes.to_i32,
}
end
post "/api/start" do |env|
profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s)
profile.start now
@ -29,7 +42,15 @@ end
get "/api/status" do |env|
profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s)
rem_span = profile.remaining_time now
data = Dayoff::StatusResponse.new(profile.started?, rem_span)
data = {
started: profile.started?,
total: {
status: rem_span < Time::Span.zero ? STATUS_OVERTIME : STATUS_UPTIME,
hours: rem_span.abs.total_hours.to_i32,
minutes: rem_span.abs.minutes.to_i32,
},
today: date_status(profile, now),
}
env.response.content_type = "application/json"
data.to_json
end

View File

@ -26,6 +26,10 @@ module Dayoff
def time_span : Time::Span
Time::Span.new(hours: hours, minutes: 0, seconds: 0)
end
def same_date?(d : Time) : Bool
@date.date == d.date
end
end
class WorkRecord
@ -78,24 +82,14 @@ module Dayoff
end
end
end
end
STATUS_UPTIME = "uptime"
STATUS_OVERTIME = "overtime"
class StatusResponse
JSON.mapping(
started: Bool,
status: String,
hours: Int32,
minutes: Int32,
)
def initialize(@started : Bool, ts : Time::Span)
zero = Time::Span.zero
@status = ts < zero ? STATUS_OVERTIME : STATUS_UPTIME
@hours = ts.abs.total_hours.to_i32
@minutes = ts.abs.minutes.to_i32
def on_date(d : Time) : Time::Span
if @start.date == d.date
fin = @finish || d
fin - start
else
Time::Span.zero
end
end
end
end

View File

@ -71,6 +71,28 @@ module Dayoff
planned - worked
end
def date_status(d : Time) : Time::Span
planned = get_planned_hours_on_date d
worked = get_work_hours_on_date d
planned - worked
end
private def get_planned_hours_on_date(d : Time) : Time::Span
@pdates.reduce(Time::Span.zero) do |acc, wd|
if wd.same_date? d
acc + wd.time_span
else
acc
end
end
end
private def get_work_hours_on_date(d : Time) : Time::Span
@wrecords.reduce(Time::Span.zero) do |acc, wr|
acc + wr.on_date d
end
end
private def started_point
@wrecords.find { |x| x.started? }
end