[api] add support for logging to the Drupal watchdog table
This commit is contained in:
parent
1cba075938
commit
304dbbb803
|
@ -30,6 +30,7 @@ ensure_started(App) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
mysql_prepare() ->
|
mysql_prepare() ->
|
||||||
|
mysql:prepare(watchdog, <<"INSERT INTO watchdog (uid, type, message, variables, severity, location, hostname, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)">>),
|
||||||
mysql:prepare(permissions, <<"SELECT permissions FROM logger_tokens WHERE meter = ? AND token = ?">>),
|
mysql:prepare(permissions, <<"SELECT permissions FROM logger_tokens WHERE meter = ? AND token = ?">>),
|
||||||
mysql:prepare(device_key, <<"SELECT sha FROM (logger_devices ld INNER JOIN logger_meters lm ON ld.device = lm.device) WHERE lm.meter = ?">>),
|
mysql:prepare(device_key, <<"SELECT sha FROM (logger_devices ld INNER JOIN logger_meters lm ON ld.device = lm.device) WHERE lm.meter = ?">>),
|
||||||
mysql:prepare(sensor_props, <<"SELECT uid, device, night FROM logger_meters WHERE meter = ?">>),
|
mysql:prepare(sensor_props, <<"SELECT uid, device, night FROM logger_meters WHERE meter = ?">>),
|
||||||
|
|
|
@ -165,3 +165,26 @@ time_to_seconds(Time) ->
|
||||||
false -> false;
|
false -> false;
|
||||||
{_Time, TimeSec} -> TimeSec
|
{_Time, TimeSec} -> TimeSec
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
% severity levels
|
||||||
|
-define(EMERGENCY, 0).
|
||||||
|
-define(ALERT, 1).
|
||||||
|
-define(CRITICAL, 2).
|
||||||
|
-define(ERROR, 3).
|
||||||
|
-define(WARNING, 4).
|
||||||
|
-define(NOTICE, 5).
|
||||||
|
-define(INFO, 6).
|
||||||
|
-define(DEBUG, 7).
|
||||||
|
|
||||||
|
% log to Drupal's watchdog table
|
||||||
|
logger(Uid, Type, Message, Severity, ReqData) ->
|
||||||
|
mysql:execute(pool, watchdog,
|
||||||
|
[Uid,
|
||||||
|
Type,
|
||||||
|
Message,
|
||||||
|
<<"a:0:{}">>,
|
||||||
|
Severity,
|
||||||
|
list_to_binary(wrq:raw_path(ReqData)),
|
||||||
|
list_to_binary(wrq:peer(ReqData)),
|
||||||
|
unix_time()
|
||||||
|
]).
|
||||||
|
|
|
@ -153,17 +153,17 @@ process_post(ReqData, #state{rrdSensor = RrdSensor} = State) ->
|
||||||
case erlrrd:update([?BASE_PATH, [RrdSensor|".rrd"], " ", RrdData]) of
|
case erlrrd:update([?BASE_PATH, [RrdSensor|".rrd"], " ", RrdData]) of
|
||||||
{ok, _RrdResponse} ->
|
{ok, _RrdResponse} ->
|
||||||
RrdResponse = "ok",
|
RrdResponse = "ok",
|
||||||
NewMidnight = update_night(RrdSensor, Uid, Midnight, LastTimestamp),
|
NewMidnight = update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData),
|
||||||
mysql:execute(pool, sensor_update, [unix_time(), NewMidnight, LastValue, RrdSensor]);
|
mysql:execute(pool, sensor_update, [unix_time(), NewMidnight, LastValue, RrdSensor]);
|
||||||
|
|
||||||
{error, RrdResponse} ->
|
{error, RrdResponse} ->
|
||||||
true
|
logger(Uid, <<"rrdupdate.base">>, list_to_binary(RrdResponse), ?ERROR, ReqData)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
JsonResponse = mochijson2:encode({struct, [{<<"response">>, list_to_binary(RrdResponse)}]}),
|
JsonResponse = mochijson2:encode({struct, [{<<"response">>, list_to_binary(RrdResponse)}]}),
|
||||||
{true , wrq:set_resp_body(JsonResponse, ReqData), State}.
|
{true , wrq:set_resp_body(JsonResponse, ReqData), State}.
|
||||||
|
|
||||||
update_night(RrdSensor, Uid, Midnight, LastTimestamp) when LastTimestamp > Midnight + 6 * ?HOUR ->
|
update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData) when LastTimestamp > Midnight + 6 * ?HOUR ->
|
||||||
LastMidnight = calculate_midnight(unix_time(), Uid),
|
LastMidnight = calculate_midnight(unix_time(), Uid),
|
||||||
|
|
||||||
case erlrrd:fetch(erlrrd:c([[?BASE_PATH, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", integer_to_list(LastMidnight + 2 * ?HOUR)], ["-e ", integer_to_list(LastMidnight + 5 * ?HOUR)], ["-r ", integer_to_list(?QUARTER)]])) of
|
case erlrrd:fetch(erlrrd:c([[?BASE_PATH, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", integer_to_list(LastMidnight + 2 * ?HOUR)], ["-e ", integer_to_list(LastMidnight + 5 * ?HOUR)], ["-r ", integer_to_list(?QUARTER)]])) of
|
||||||
|
@ -175,12 +175,12 @@ update_night(RrdSensor, Uid, Midnight, LastTimestamp) when LastTimestamp > Midni
|
||||||
%debugging: io:format("~s~n", [[?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]]),
|
%debugging: io:format("~s~n", [[?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]]),
|
||||||
|
|
||||||
erlrrd:update([?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]);
|
erlrrd:update([?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]);
|
||||||
{error, _Reason} ->
|
{error, Reason} ->
|
||||||
true
|
logger(Uid, <<"rrdupdate.night">>, list_to_binary(Reason), ?ERROR, ReqData)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
LastMidnight + ?DAY;
|
LastMidnight + ?DAY;
|
||||||
update_night(_RrdSensor, _Uid, Midnight, _LastTimestamp) ->
|
update_night(_RrdSensor, _Uid, Midnight, _LastTimestamp, _ReqData) ->
|
||||||
Midnight.
|
Midnight.
|
||||||
|
|
||||||
calculate_midnight(Timestamp, Uid) ->
|
calculate_midnight(Timestamp, Uid) ->
|
||||||
|
|
Loading…
Reference in New Issue