Permalink
Browse files

Live updating of charts

  • Loading branch information...
1 parent 97fcad6 commit 95d550cde244087843b38f566510c44793db5d02 @wardbekker committed Apr 18, 2012
View
@@ -4,3 +4,4 @@ config/database.config
.DS_Store
deps
ebin
+logs
File renamed without changes.
@@ -0,0 +1,3 @@
+-- Remove all double quotes from name
+UPDATE `esense` e SET e.`name` = REPLACE(e.`name`, '"','');
+
View
@@ -8,7 +8,7 @@
%% API Function Exports
%% ------------------------------------------------------------------
--export([init/1, to_html/2]).
+-export([init/1, to_html/2, to_json/2, content_types_provided/2]).
%% ------------------------------------------------------------------
%% Header includes
@@ -22,33 +22,57 @@
init([]) -> {ok, undefined}.
+content_types_provided(ReqData, State) ->
+ Types = [{"application/json", to_json},{"text/html", to_html}],
+ {Types, ReqData, State}.
+
+
+%% @doc Provides fresh datapoints for the specified chart type
+to_json(ReqData, State) ->
+ LastMaxTimestamp = erlang:list_to_integer(
+ wrq:get_qs_value("maxValue", ReqData)
+ ),
+ StartTimestamp = LastMaxTimestamp + 1,
+ EndTimestamp = db_queries:maxESenseTimestamp(),
+ ChartDataType = wrq:get_qs_value("chartDataType", ReqData),
+ Data = chartData(ChartDataType, StartTimestamp, EndTimestamp),
+ Result = proplists:get_value("rows", Data),
+ {mochijson2:encode(Result), ReqData, State}.
+
+%% @doc Render the charts with existing datapoints
to_html(ReqData, State) ->
EndTimestamp = db_queries:maxESenseTimestamp(),
StartTimestamp = EndTimestamp - utils:timestamp({0,1000,0}),
Charts = [
- "\"meditation\"", "\"theta\"", "\"highBeta\"",
- "\"highGamma\"", "\"attention\"", "\"lowBeta\"",
- "\"delta\"", "\"highAlpha\"", "\"lowGamma\"",
- "\"lowAlpha\""
+ "meditation", "theta", "highBeta",
+ "highGamma", "attention", "lowBeta",
+ "delta", "highAlpha", "lowGamma",
+ "lowAlpha", "combined", "poorsignal", "blink"
],
ChartsData = lists:map(
- fun(C) -> eSenseData(C, StartTimestamp, EndTimestamp) end,
+ fun(C) -> chartData(C, StartTimestamp, EndTimestamp) end,
Charts),
- Data = [{charts, [
- eSenseData(StartTimestamp, EndTimestamp),
- blinkData(StartTimestamp, EndTimestamp),
- poorSignalData(StartTimestamp, EndTimestamp)
- ]
- ++
- ChartsData
- } ],
+ Data = [{charts, ChartsData} ],
{ok, Content} = linechart_dtl:render(Data),
{Content, ReqData, State}.
%% ------------------------------------------------------------------
%% private Function Definitions
%% ------------------------------------------------------------------
+-spec chartData(iolist(), integer(), integer()) -> list().
+chartData(Type, StartTimestamp, EndTimestamp) ->
+ case Type of
+ "blink" ->
+ blinkData(StartTimestamp, EndTimestamp);
+ "poorsignal" ->
+ poorSignalData(StartTimestamp, EndTimestamp);
+ "combined" ->
+ eSenseData(StartTimestamp, EndTimestamp);
+ ESense ->
+ eSenseData(ESense, StartTimestamp, EndTimestamp)
+ end.
+
-spec blinkData(integer(), integer()) -> list().
blinkData(StartTimestamp, EndTimestamp) ->
DataRows = lists:map(
@@ -66,6 +90,7 @@ blinkData(StartTimestamp, EndTimestamp) ->
{"width", "1012"},
{"height", "200"},
{"title", "Blink"},
+ {"chartDataType", "blink"},
{"columns", [{ "number", "Timestamp"}, { "number", "strength" }]},
{"rows", DataRows }
],
@@ -88,6 +113,7 @@ poorSignalData(StartTimestamp, EndTimestamp) ->
{"width", "1024"},
{"height", "200"},
{"title", "Poor Signal"},
+ {"chartDataType", "poorsignal"},
{"columns", [{ "number", "Timestamp"}, { "number", "level" }]},
{"rows", DataRows }
],
@@ -114,6 +140,7 @@ eSenseData(StartTimestamp, EndTimestamp) ->
{"width", "1024"},
{"height", "200"},
{"title", "Combined eSenses"},
+ {"chartDataType", "combined"},
{"columns", DataColumns2},
{"rows", DataRows }
],
@@ -122,22 +149,29 @@ eSenseData(StartTimestamp, EndTimestamp) ->
-spec eSenseData(iolist(), integer(), integer()) -> list().
eSenseData(Esense, StartTimestamp, EndTimestamp) ->
Res = db_queries:eSense(Esense, StartTimestamp, EndTimestamp),
- DataColumns1 = re:split(lists:nth(2,lists:nth(1, Res)), ","),
- DataColumns2 = [{"number", "Timestamp"}] ++ lists:map(fun(H) -> { "number", H } end, DataColumns1),
- DataRows = lists:map(
- fun( R ) ->
- {
- erlang:integer_to_list(lists:nth(1, R)),
- erlang:list_to_integer(erlang:binary_to_list(lists:nth(3, R)))
- }
- end, Res),
+ case Res of
+ [] ->
+ DataColumns = [{"number", "Timestamp"}],
+ DataRows = [];
+ _ ->
+ DataCol = re:split(lists:nth(2,lists:nth(1, Res)), ","),
+ DataColumns = [{"number", "Timestamp"}] ++ lists:map(fun(H) -> { "number", H } end, DataCol),
+ DataRows = lists:map(
+ fun( R ) ->
+ {
+ erlang:integer_to_list(lists:nth(1, R)),
+ erlang:list_to_integer(erlang:binary_to_list(lists:nth(3, R)))
+ }
+ end, Res)
+ end,
Data = [
{"hAxis_minValue", StartTimestamp},
{"hAxis_maxValue", EndTimestamp},
{"width", "1024"},
{"height", "200"},
{"title", Esense},
- {"columns", DataColumns2},
+ {"chartDataType", Esense},
+ {"columns", DataColumns},
{"rows", DataRows }
],
Data.
View
@@ -118,11 +118,11 @@ encode(Param) when erlang:is_list(Param) ->
mysql:encode(Param);
false ->
string:join(
- lists:map(fun(P) -> encode(printable(P)) end, Param), ", "
+ lists:map(fun(P) -> encode(P) end, Param), ", "
)
end;
encode(Param) ->
- mysql:encode(Param).
+ mysql:encode(printable(Param)).
-spec seperator(any()) -> iolist().
seperator(Param) when erlang:is_list(Param) ->
View
@@ -3,6 +3,7 @@
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
+ google.load("jquery", "1.4.2");
google.setOnLoadCallback(drawCharts);
function drawCharts() {
@@ -30,6 +31,20 @@
var chart = new google.visualization.LineChart(document.getElementById('chart_div{{forloop.counter0 }}'));
chart.draw(data, options);
+ var maxValue = {{ chart.hAxis_maxValue }};
+
+ setInterval(function() {
+ $.getJSON('/chart?esense={{ chart.chartDataType }}&maxValue=' + maxValue, function(rows) {
+ for(var index in rows) {
+ data.addRow([parseInt(index), rows[index]]);
+ maxValue = [parseInt(index)];
+ }
+ chart.draw(data);
+ }
+ )
+ },
+ 200);
+
{% endfor %}
}
</script>
View
@@ -0,0 +1,6 @@
+-module(test_SUITE).
+-include_lib("common_test/include/ct.hrl").
+-export([all/0]).
+-export([]).
+
+all() -> [].

0 comments on commit 95d550c

Please sign in to comment.