api: use strings (minute, 15min, hour, day, week, month, year) for defining the requested resolution in the query string

This commit is contained in:
Bart Van Der Meerssche 2010-09-14 00:21:04 +02:00
parent fdc2e35e0f
commit a435c5109c
1 changed files with 46 additions and 21 deletions

View File

@ -102,35 +102,31 @@ check_sensor(Sensor) ->
check_time(undefined, undefined, _End, _Resolution) -> check_time(undefined, undefined, _End, _Resolution) ->
{false, false, false, false}; {false, false, false, false};
check_time(Interval, undefined, undefined, undefined) -> check_time(Interval, undefined, undefined, undefined) ->
check_time(Interval, undefined, undefined, ""); case default_resolution(Interval) of
false -> {false, false, false, false};
DefResolution -> check_time(Interval, undefined, undefined, DefResolution)
end;
check_time(Interval, undefined, undefined, Resolution) -> check_time(Interval, undefined, undefined, Resolution) ->
Now = unix_time(), Now = unix_time(),
case {time_to_seconds(Interval), time_to_seconds(Resolution)} of
Intervals = [{"hour", "end-1h", 60},
{"day", "end-1d", 900},
{"month", "end-30d", 86400},
{"year", "end-1y", 604800},
{"night", "end-30d", 86400}],
case {lists:keyfind(Interval, 1, Intervals), re:run(Resolution, "[0-9]+", [])} of
{false, _} -> {false, false, false, false}; {false, _} -> {false, false, false, false};
{{_Interval, Start, _DefResolution}, {match, [{0,_}]}} -> {_, false} -> {false, false, false, false};
AlignedEnd = integer_to_list(time_align(Now, list_to_integer(Resolution))), {IntervalSec, ResolutionSec} ->
{Start, AlignedEnd, Resolution, true}; AlignedEnd = time_align(Now, ResolutionSec),
{{_Interval, Start, DefResolution}, _} -> AlignedStart = AlignedEnd - IntervalSec,
AlignedEnd = integer_to_list(time_align(Now, DefResolution)), {integer_to_list(AlignedStart), integer_to_list(AlignedEnd), integer_to_list(ResolutionSec), true}
{Start, AlignedEnd, integer_to_list(DefResolution), true}
end; end;
check_time(undefined, Start, undefined, Resolution) -> check_time(undefined, Start, undefined, Resolution) ->
check_time(undefined, Start, integer_to_list(unix_time()), Resolution); check_time(undefined, Start, integer_to_list(unix_time()), Resolution);
check_time(undefined, Start, End, undefined) -> check_time(undefined, Start, End, undefined) ->
check_time(undefined, Start, End, "60"); check_time(undefined, Start, End, "minute");
check_time(undefined, Start, End, Resolution) -> check_time(undefined, Start, End, Resolution) ->
case {re:run(Start, "[0-9]+", []), re:run(End, "[0-9]+", []), re:run(Resolution, "[0-9]+", [])} of case {re:run(Start, "[0-9]+", []), re:run(End, "[0-9]+", []), time_to_seconds(Resolution)} of
{{match, [{0,_}]}, {match, [{0,_}]}, {match, [{0,_}]}} -> {_, _, false} -> {false, false, false, false};
AlignedStart = integer_to_list(time_align(list_to_integer(Start), list_to_integer(Resolution))), {{match, [{0,_}]}, {match, [{0,_}]}, ResolutionSec} ->
AlignedEnd = integer_to_list(time_align(list_to_integer(End), list_to_integer(Resolution))), AlignedStart = time_align(list_to_integer(Start), ResolutionSec),
{AlignedStart, AlignedEnd, Resolution, true}; AlignedEnd = time_align(list_to_integer(End), ResolutionSec),
{integer_to_list(AlignedStart), integer_to_list(AlignedEnd), integer_to_list(ResolutionSec), true};
_ -> {false, false, false, false} _ -> {false, false, false, false}
end; end;
check_time(_, _, _, _) -> check_time(_, _, _, _) ->
@ -173,3 +169,32 @@ unix_time() ->
time_align(Time, Resolution) -> time_align(Time, Resolution) ->
(Time div Resolution) * Resolution. (Time div Resolution) * Resolution.
default_resolution(Interval) ->
DefResolutions = [{"15min", "minute"},
{"hour", "minute"},
{"day", "15min"},
{"week", "day"},
{"month", "day"},
{"year", "week"},
{"night", "day"}],
case lists:keyfind(Interval, 1, DefResolutions) of
false -> false;
{_Interval, Defresolution} -> Defresolution
end.
time_to_seconds(Time) ->
Times = [{"minute", 60},
{"15min", 900},
{"hour", 3600},
{"day", 86400},
{"week", 604800},
{"month", 2419200},
{"year", 31536000},
{"night", 2419200}],
case lists:keyfind(Time, 1, Times) of
false -> false;
{_Time, TimeSec} -> TimeSec
end.