More consistent api

This commit is contained in:
2019-11-17 20:48:25 +03:00
parent af95c67110
commit fbb11fbcd8
6 changed files with 48 additions and 42 deletions

View File

@ -17,10 +17,8 @@ def now
Time.local(Time::Location.load("Europe/Moscow"))
end
def date_status(profile, date)
span = profile.date_status date
def serialize_span(span : Time::Span)
{
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,
@ -41,15 +39,17 @@ end
get "/api/status" do |env|
profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s)
rem_span = profile.remaining_time now
total_span = profile.total_status now
today_span = profile.date_status now
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,
time: serialize_span total_span,
},
today: {
date: now.to_s("%Y-%m-%d"),
time: serialize_span today_span,
},
today: date_status(profile, now),
}
env.response.content_type = "application/json"
data.to_json

View File

@ -70,7 +70,8 @@ module Dayoff
end
private def in_range_finished(from_time : Time, to_time : Time) : Time::Span
if @start <= to_time && finish! >= from_time
crossed = Helpers.crossed? @start, finish!, from_time, to_time
if crossed
normalized_start = Math.max(@start, from_time)
normalized_finish = Math.min(finish!, to_time)
normalized_finish - normalized_start

12
src/dayoff/helpers.cr Normal file
View File

@ -0,0 +1,12 @@
module Dayoff::Helpers
extend self
def zero_time : Time
location = Time::Location.load("Europe/Moscow")
Time.local(1, 1, 1, 0, 0, location: location)
end
def crossed?(s1 : Time, f1 : Time, s2 : Time, f2 : Time) : Bool
s1 <= f2 && f1 >= s2
end
end

View File

@ -64,20 +64,15 @@ module Dayoff
end
end
private def zero_time : Time
location = Time::Location.load("Europe/Moscow")
Time.local(1, 1, 1, 0, 0, location: location)
end
def remaining_time(on_time : Time) : Time::Span
planned = get_planned zero_time, on_time
worked = get_worked zero_time, on_time
def total_status(on_time : Time) : Time::Span
planned = get_planned Helpers.zero_time, on_time
worked = get_worked Helpers.zero_time, on_time
planned - worked
end
def date_status(date : Time) : Time::Span
planned = get_planned date.at_beginning_of_day, date.at_end_of_day
worked = get_worked date.at_beginning_of_day, date
def date_status(on_time : Time) : Time::Span
planned = get_planned on_time.at_beginning_of_day, on_time.at_end_of_day
worked = get_worked on_time.at_beginning_of_day, on_time
planned - worked
end
end