2010-02-25 17:45:11 +00:00
|
|
|
%% @author icarus75 <bart.vandermeerssche@flukso.net>
|
|
|
|
%% @copyright 2009-2010 flukso.net
|
|
|
|
%% @doc Flukso webmachine_resource.
|
|
|
|
|
|
|
|
-module(flukso_resource).
|
2010-03-14 20:31:10 +00:00
|
|
|
-export([init/1, allowed_methods/2, malformed_request/2, is_authorized/2, content_types_provided/2, to_json/2]).
|
2010-02-25 17:45:11 +00:00
|
|
|
|
|
|
|
-include_lib("webmachine/include/webmachine.hrl").
|
|
|
|
|
2010-03-14 13:47:28 +00:00
|
|
|
-record(state,
|
|
|
|
{rrdSensor,
|
2010-05-21 14:25:01 +00:00
|
|
|
rrdStart,
|
|
|
|
rrdEnd,
|
2010-05-22 11:21:31 +00:00
|
|
|
rrdResolution,
|
2010-03-14 20:31:10 +00:00
|
|
|
rrdFactor,
|
2010-04-23 23:20:47 +00:00
|
|
|
token,
|
|
|
|
jsonpCallback}).
|
2010-03-14 13:47:28 +00:00
|
|
|
|
2010-02-25 17:45:11 +00:00
|
|
|
init([]) ->
|
|
|
|
{ok, undefined}.
|
|
|
|
|
|
|
|
allowed_methods(ReqData, State) ->
|
|
|
|
{['GET'], ReqData, State}.
|
|
|
|
|
2010-04-23 23:20:47 +00:00
|
|
|
malformed_request(ReqData, _State) ->
|
2010-04-27 22:08:41 +00:00
|
|
|
{_Version, ValidVersion} = check_version(wrq:get_req_header("X-Version", ReqData), wrq:get_qs_value("version", ReqData)),
|
2010-04-27 21:23:27 +00:00
|
|
|
{RrdSensor, ValidSensor} = check_sensor(wrq:path_info(sensor, ReqData)),
|
2010-05-22 11:21:31 +00:00
|
|
|
{RrdStart, RrdEnd, RrdResolution, ValidTime} = check_time(wrq:get_qs_value("interval", ReqData), wrq:get_qs_value("start", ReqData), wrq:get_qs_value("end", ReqData), wrq:get_qs_value("resolution", ReqData)),
|
2010-04-27 21:47:01 +00:00
|
|
|
{RrdFactor, ValidUnit} = check_unit(wrq:get_qs_value("unit", ReqData)),
|
2010-04-27 21:23:27 +00:00
|
|
|
{Token, ValidToken} = check_token(wrq:get_req_header("X-Token", ReqData), wrq:get_qs_value("token", ReqData)),
|
|
|
|
{JsonpCallback, ValidJsonpCallback} = check_jsonp_callback(wrq:get_qs_value("jsonp_callback", ReqData)),
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
State = #state{rrdSensor = RrdSensor,
|
|
|
|
rrdStart = RrdStart,
|
|
|
|
rrdEnd = RrdEnd,
|
|
|
|
rrdResolution = RrdResolution,
|
|
|
|
rrdFactor = RrdFactor,
|
|
|
|
token = Token,
|
|
|
|
jsonpCallback = JsonpCallback},
|
2010-03-14 13:47:28 +00:00
|
|
|
|
2010-05-21 14:25:01 +00:00
|
|
|
{case {ValidVersion, ValidSensor, ValidTime, ValidUnit, ValidToken, ValidJsonpCallback} of
|
2010-04-27 22:08:41 +00:00
|
|
|
{true, true, true, true, true, true} -> false;
|
2010-02-25 17:45:11 +00:00
|
|
|
_ -> true
|
|
|
|
end,
|
2010-03-14 13:47:28 +00:00
|
|
|
ReqData, State}.
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2010-03-14 20:31:10 +00:00
|
|
|
is_authorized(ReqData, #state{rrdSensor = RrdSensor, token = Token} = State) ->
|
|
|
|
{data, Result} = mysql:execute(pool, permissions, [RrdSensor, Token]),
|
|
|
|
|
|
|
|
{case mysql:get_result_rows(Result) of
|
|
|
|
[[62]] -> true;
|
2010-04-23 23:20:47 +00:00
|
|
|
_Permission -> "access refused"
|
2010-03-14 20:31:10 +00:00
|
|
|
end,
|
|
|
|
ReqData, State}.
|
|
|
|
|
2010-02-25 17:45:11 +00:00
|
|
|
content_types_provided(ReqData, State) ->
|
|
|
|
{[{"application/json", to_json}], ReqData, State}.
|
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
to_json(ReqData, #state{rrdSensor = RrdSensor, rrdStart = RrdStart, rrdEnd = RrdEnd, rrdResolution = RrdResolution, rrdFactor = RrdFactor, jsonpCallback = JsonpCallback} = State) ->
|
2010-04-27 12:22:25 +00:00
|
|
|
case wrq:get_qs_value("interval", ReqData) of
|
2010-02-25 17:45:11 +00:00
|
|
|
"night" -> Path = "var/data/night/";
|
|
|
|
_Interval -> Path = "var/data/base/"
|
|
|
|
end,
|
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
%% debugging: io:format("~s~n", [erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])]),
|
2010-05-21 14:25:01 +00:00
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
case erlrrd:fetch(erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])) of
|
2010-02-25 17:45:11 +00:00
|
|
|
{ok, Response} ->
|
|
|
|
Filtered = [re:split(X, "[:][ ]", [{return,list}]) || [X] <- Response, string:str(X, ":") == 11],
|
2010-03-14 14:12:35 +00:00
|
|
|
Datapoints = [[list_to_integer(X), round(list_to_float(Y) * RrdFactor)] || [X, Y] <- Filtered, string:len(Y) /= 3],
|
2010-02-25 17:45:11 +00:00
|
|
|
Nans = [[list_to_integer(X), list_to_binary(Y)] || [X, Y] <- Filtered, string:len(Y) == 3],
|
2010-04-23 23:20:47 +00:00
|
|
|
Final = mochijson2:encode(lists:merge(Datapoints, Nans)),
|
|
|
|
{case JsonpCallback of
|
|
|
|
undefined -> Final;
|
|
|
|
_ -> [JsonpCallback, "(", Final, ");"]
|
|
|
|
end,
|
|
|
|
ReqData, State};
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2010-03-14 20:31:10 +00:00
|
|
|
{error, _Reason} ->
|
2010-02-25 17:45:11 +00:00
|
|
|
{{halt, 404}, ReqData, State}
|
|
|
|
end.
|
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
%% checks
|
2010-04-27 22:08:41 +00:00
|
|
|
check_version(undefined, undefined) ->
|
|
|
|
{false, false};
|
|
|
|
check_version(Version, undefined) ->
|
|
|
|
case Version of
|
|
|
|
"1.0" -> {Version, true};
|
|
|
|
_ -> {false, false}
|
|
|
|
end;
|
|
|
|
check_version(undefined, Version) ->
|
|
|
|
check_version(Version, undefined);
|
|
|
|
check_version(_, _) ->
|
|
|
|
{false, false}.
|
|
|
|
|
2010-04-27 21:23:27 +00:00
|
|
|
check_sensor(Sensor) ->
|
2010-02-25 17:45:11 +00:00
|
|
|
case re:run(Sensor, "[0-9a-f]+", []) of
|
|
|
|
{match, [{0,32}]} -> {Sensor, true};
|
|
|
|
_ -> {false, false}
|
|
|
|
end.
|
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
check_time(undefined, undefined, _End, _Resolution) ->
|
|
|
|
{false, false, false, false};
|
|
|
|
check_time(Interval, undefined, undefined, undefined) ->
|
|
|
|
check_time(Interval, undefined, undefined, "");
|
|
|
|
check_time(Interval, undefined, undefined, Resolution) ->
|
|
|
|
Now = unix_time(),
|
|
|
|
|
|
|
|
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};
|
|
|
|
{{_Interval, Start, _DefResolution}, {match, [{0,_}]}} ->
|
|
|
|
AlignedEnd = integer_to_list(time_align(Now, list_to_integer(Resolution))),
|
|
|
|
{Start, AlignedEnd, Resolution, true};
|
|
|
|
{{_Interval, Start, DefResolution}, _} ->
|
|
|
|
AlignedEnd = integer_to_list(time_align(Now, DefResolution)),
|
|
|
|
{Start, AlignedEnd, integer_to_list(DefResolution), true}
|
2010-05-21 14:25:01 +00:00
|
|
|
end;
|
2010-05-22 11:21:31 +00:00
|
|
|
check_time(undefined, Start, undefined, Resolution) ->
|
|
|
|
check_time(undefined, Start, integer_to_list(unix_time()), Resolution);
|
|
|
|
check_time(undefined, Start, End, undefined) ->
|
|
|
|
check_time(undefined, Start, End, "60");
|
|
|
|
check_time(undefined, Start, End, Resolution) ->
|
|
|
|
case {re:run(Start, "[0-9]+", []), re:run(End, "[0-9]+", []), re:run(Resolution, "[0-9]+", [])} of
|
|
|
|
{{match, [{0,_}]}, {match, [{0,_}]}, {match, [{0,_}]}} ->
|
|
|
|
AlignedStart = integer_to_list(time_align(list_to_integer(Start), list_to_integer(Resolution))),
|
|
|
|
AlignedEnd = integer_to_list(time_align(list_to_integer(End), list_to_integer(Resolution))),
|
|
|
|
{AlignedStart, AlignedEnd, Resolution, true};
|
|
|
|
_ -> {false, false, false, false}
|
2010-05-21 14:25:01 +00:00
|
|
|
end;
|
2010-05-22 11:21:31 +00:00
|
|
|
check_time(_, _, _, _) ->
|
|
|
|
{false, false, false, false}.
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2010-04-27 21:47:01 +00:00
|
|
|
check_unit(Unit) ->
|
2010-02-25 17:45:11 +00:00
|
|
|
Units = [{"watt", 3600},
|
|
|
|
{"kwhperyear", 31536},
|
|
|
|
{"eurperyear", 5676},
|
|
|
|
{"audperyear", 5991}],
|
|
|
|
|
|
|
|
case lists:keyfind(Unit, 1, Units) of
|
|
|
|
false -> {false, false};
|
|
|
|
{_Unit, RrdFactor} -> {RrdFactor, true}
|
|
|
|
end.
|
|
|
|
|
2010-04-27 21:23:27 +00:00
|
|
|
check_token(undefined, undefined) ->
|
2010-04-27 21:14:30 +00:00
|
|
|
{false, false};
|
2010-04-27 21:23:27 +00:00
|
|
|
check_token(Token, undefined) ->
|
|
|
|
check_sensor(Token);
|
|
|
|
check_token(undefined, Token) ->
|
|
|
|
check_sensor(Token);
|
|
|
|
check_token(_, _) ->
|
2010-04-27 21:14:30 +00:00
|
|
|
{false, false}.
|
|
|
|
|
2010-04-27 21:23:27 +00:00
|
|
|
check_jsonp_callback(undefined) ->
|
2010-04-23 23:20:47 +00:00
|
|
|
{undefined, true};
|
2010-04-27 21:23:27 +00:00
|
|
|
check_jsonp_callback(JsonpCallback) ->
|
2010-04-23 23:20:47 +00:00
|
|
|
Length = string:len(JsonpCallback),
|
|
|
|
|
|
|
|
case re:run(JsonpCallback, "[0-9a-zA-Z_]+", []) of
|
|
|
|
{match, [{0, Length}]} -> {JsonpCallback, true};
|
|
|
|
_ -> {false, false}
|
|
|
|
end.
|
2010-05-22 11:21:31 +00:00
|
|
|
|
|
|
|
%% helper functions
|
|
|
|
unix_time() ->
|
|
|
|
{Megaseconds, Seconds, _Microseconds} = erlang:now(),
|
|
|
|
Megaseconds*1000000 + Seconds.
|
|
|
|
|
|
|
|
time_align(Time, Resolution) ->
|
|
|
|
(Time div Resolution) * Resolution.
|