forked from gotthardp/lorawan-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorawan_admin_graph_gw.erl
More file actions
96 lines (84 loc) · 3.31 KB
/
lorawan_admin_graph_gw.erl
File metadata and controls
96 lines (84 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
%
% Copyright (c) 2016-2019 Petr Gotthard <[email protected]>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
-module(lorawan_admin_graph_gw).
-export([init/2]).
-export([allowed_methods/2]).
-export([is_authorized/2]).
-export([forbidden/2]).
-export([content_types_provided/2]).
-export([resource_exists/2]).
-export([get_gateway/2]).
-include("lorawan_db.hrl").
-record(state, {format, scopes, auth_fields}).
init(Req, {Format, Scopes}) ->
{cowboy_rest, Req, #state{format=Format, scopes=Scopes}}.
allowed_methods(Req, State) ->
{[<<"OPTIONS">>, <<"GET">>], Req, State}.
is_authorized(Req, #state{scopes=Scopes}=State) ->
case lorawan_admin:handle_authorization(Req, Scopes) of
{true, AuthFields} ->
{true, Req, State#state{auth_fields=AuthFields}};
Else ->
{Else, Req, State}
end.
forbidden(Req, #state{auth_fields=AuthFields}=State) ->
{lorawan_admin:fields_empty(AuthFields), Req, State}.
content_types_provided(Req, State) ->
{[
{{<<"application">>, <<"json">>, []}, get_gateway}
], Req, State}.
get_gateway(Req, #state{format=Type}=State) ->
MAC = cowboy_req:binding(mac, Req),
[Gateway] = mnesia:dirty_read(gateway, lorawan_utils:hex_to_binary(MAC)),
{jsx:encode([{mac, MAC}, {array, get_array(Type, Gateway)}]), Req, State}.
get_array(pgraph, #gateway{delays=Delays}) when is_list(Delays) ->
% construct Google Chart DataTable
% see https://developers.google.com/chart/interactive/docs/reference#dataparam
[{cols, [
[{id, <<"timestamp">>}, {label, <<"Timestamp">>}, {type, <<"datetime">>}],
[{id, <<"avgdelay">>}, {label, <<"Average [ms]">>}, {type, <<"number">>}],
[{type, <<"number">>}, {role, <<"interval">>}],
[{type, <<"number">>}, {role, <<"interval">>}]
]},
{rows, lists:filtermap(
fun ({Date, {Min, Avg, Max}}) ->
{true, [{c, [
[{v, lorawan_admin:timestamp_to_json_date(Date)}],
[{v, Avg}],
[{v, Min}],
[{v, Max}]
]}]};
(_Else) ->
false
end, Delays)
}];
get_array(tgraph, #gateway{dwell=Dwell}) when is_list(Dwell) ->
[{cols, [
[{id, <<"timestamp">>}, {label, <<"Timestamp">>}, {type, <<"datetime">>}],
[{id, <<"dwell">>}, {label, <<"Tx Time [ms]">>}, {type, <<"number">>}],
[{id, <<"sum">>}, {label, <<"Tx in Hour [ms]">>}, {type, <<"number">>}]
]},
{rows, lists:filtermap(
fun ({Date, {_, Duration, Sum}}) ->
{true, [{c, [
[{v, lorawan_admin:timestamp_to_json_date(Date)}],
[{v, if Duration == 0 -> null; true -> round(Duration) end}],
[{v, Sum/36000}, {f, <<(integer_to_binary(round(Sum)))/binary,
" (", (float_to_binary(Sum/36000, [{decimals, 3}, compact]))/binary, "%)">>}]
]}]};
(_Else) ->
false
end, Dwell)
}];
get_array(_, _Else) ->
[].
resource_exists(Req, State) ->
case mnesia:dirty_read(gateway,
lorawan_admin:parse_field(mac, cowboy_req:binding(mac, Req))) of
[] -> {false, Req, State};
[_Stats] -> {true, Req, State}
end.
% end of file