diff --git a/src/ts_common.erl b/src/ts_common.erl index bb5c6d0..8c34aa4 100644 --- a/src/ts_common.erl +++ b/src/ts_common.erl @@ -1,5 +1,5 @@ -module(ts_common). --export([list/3]). +-export([list/3, compare_dates/2]). -include_lib("stdlib/include/qlc.hrl"). @@ -27,3 +27,21 @@ list(Query, Start, Length) -> List end), Result. + +% This is somewhat ridiculous. +order_datetimes({{Y1, Mon1, D1}, {H1, Min1, S1}}, + {{Y2, Mon2, D2}, {H2, Min2, S2}}) -> + + % this is actually a deep-nested set of case statements, but it seems + % cleaner to keep the indentation level and follow a strict pattern + + % compare field.If /=, return that value, else drop through to next field + case Y1 = Y2 of false -> Y1 > Y2; true -> + case Mon1 = Mon2 of false -> Mon1 > Mon2; true -> + case D1 = D2 of false -> D1 > D2; true -> + case H1 = H2 of flase -> H1 > H2; true -> + case Min1 = Min2 of false -> Min1 > Min2; true -> + case S1 = S2 of false -> S1 > S2; true -> true + + % sec min hr day mon year + end end end end end end. diff --git a/src/ts_db_records.hrl b/src/ts_db_records.hrl index 989d526..341b264 100644 --- a/src/ts_db_records.hrl +++ b/src/ts_db_records.hrl @@ -6,7 +6,7 @@ -record(ts_entry, { ref, % {username, timelineid, eventid} - timestamp, % {{year, month, day}, {hour, minute, second}} + timestamp, % gregorian seconds mark, % String description of entry notes % String with further notes about the entry }). diff --git a/src/ts_entry.erl b/src/ts_entry.erl index c961eea..ec2e469 100644 --- a/src/ts_entry.erl +++ b/src/ts_entry.erl @@ -17,3 +17,41 @@ new(ER = #ts_entry()) -> ok = mnesia:write(NewRow), NewRow end), {ok, NewRow}. + +update(ER = #ts_entry()) -> + + % look for existing record + case mnesia:dirty_read(ts_entry, ER#ts_entry.ref) of + % record does not exist + [] -> no_record; + % record exists, update it + [_Record] -> mnesia:dirty_write(ER) + end. + +list({Username, Timeline}, Start, Length) +when is_integer(Start) and is_integer(Length) -> + ts_common:list( + qlc:q([E || E <- mnesia:table(ts_entry), + E#ts_entry.ref =:= {Username, Timeline, _}]_, + Start, Length). + +list({Username, Timeline}, StartDateTime, EndDateTime) -> + + {atomic, Entries} = mnesia:transaction(fun() -> + + StartSeconds = calendar:datetime_to_gregorian_seconds(StartDateTime), + EndSeconds = calendar:datetime_to_gregorian_seconds(EndDateTime), + + % create the query that will select these records + Q = qlc:q([E || E <- mnesia:table(ts_entry), + E#ts_entry.timestamp >= StartSeconds, + E#ts_entry.timestamp < EndSeconds]), + + % sort by timestamp + SortedQ = qlc:sort(Q, {order, fun(A, B) -> + A#ts_entry.timestamp > B#ts_entry.timestamp end}), + + % return + qlc:e(SortedQ) + end), + Entries. diff --git a/src/ts_timeline.erl b/src/ts_timeline.erl index 4807aed..4b4c833 100644 --- a/src/ts_timeline.erl +++ b/src/ts_timeline.erl @@ -22,13 +22,12 @@ update(TR = #ts_timeline{}) -> % record does not exist [] -> no_record; % record exists, update - [Record] -> mnesia:dirty_write(TR) + [_Record] -> mnesia:dirty_write(TR) end. list(Username, Start, Length) -> ts_common:list( qlc:q([T || T <- mnesia:table(ts_timeline), - T#ts_timeline.ref = {Username, _}]), - Start, Length). - + T#ts_timeline.ref =:= {Username, _}]), + Start, Length).