anchor.diff:  -include_lib("radius/include/radius.hrl").
   -include("ocs_log.hrl").
   
  +%% support deprecated_time_unit()
  +-define(MILLISECOND, milli_seconds).
  +%-define(MILLISECOND, millisecond).
  +
   -spec content_types_accepted() -> ContentTypes
   	when
   		ContentTypes :: list().
  
positive.code: %% 	`GET /usageManagement/v1/usage/{id}'
  %% 	requests.
  get_usage("auth-" ++ _ = Id, [] = _Query) ->
  	try
  		["auth", TimeStamp, Serial] = string:tokens(Id, [$-]),
  		TS = list_to_integer(TimeStamp),
  		N = list_to_integer(Serial),
  		Events = ocs_log:auth_query(TS, TS, '_', '_', '_', '_'),
  		case lists:keyfind(N, 2, Events) of
  			Event when is_tuple(Event) ->
  				JsonObj = usage_aaa_auth(Event, []),
  				{struct, Attr} = JsonObj,
  				{_, Date} = lists:keyfind("date", 1, Attr),
  				Body = mochijson:encode(JsonObj),
  				Headers = [{content_type, "application/json"}, {last_modified, Date}],
  				{ok, Headers, Body};
  			_ ->
  				{error, 404}
  		end
  	catch
  		_:_Reason ->
  			{error, 404}
  	end;
  get_usage("acct-" ++ _ = Id, [] = _Query) ->
  	try
  		["acct", TimeStamp, Serial] = string:tokens(Id, [$-]),
  		TS = list_to_integer(TimeStamp),
  		N = list_to_integer(Serial),
  		Events = ocs_log:acct_query(TS, TS, '_', '_', '_'),
  		case lists:keyfind(N, 2, Events) of
  			Event when is_tuple(Event) ->
  				JsonObj = usage_aaa_acct(Event, []),
  				{struct, Attr} = JsonObj,
  				{_, Date} = lists:keyfind("date", 1, Attr),
  				Body = mochijson:encode(JsonObj),
  				Headers = [{content_type, "application/json"}, {last_modified, Date}],
  				{ok, Headers, Body};
  			_ ->
  				{error, 404}
  		end
  	catch
  		_:_Reason ->
  
negative.code: get_acct_last(_Query, _Filters) ->
  	{error, 400}.
  
  %% @hidden
  filter(Keys, KeyValuePairs) ->
  	filter(Keys, KeyValuePairs, []).
  %% @hidden
  filter([H | T], L, Acc) ->
  	case lists:keyfind(H, 1, L) of
  		{H, V} ->
  			filter(T, L, [{H, V} | Acc]);
  		false ->
  			filter(T, L, Acc)
  	end;
  filter([], _, Acc) ->
  	lists:reverse(Acc).
  
  %% @hidden
  query_auth(Start, End, Protocol, Types, ReqAttrs, ResAttrs, PageSize) ->
  	query_auth(start, Start, End, Protocol, Types,
  			ReqAttrs, ResAttrs, PageSize, 0, []).
  %% @hidden
  query_auth(Cont, Start, End, Protocol, Types,
  		ReqAttrs, ResAttrs, PageSize, Count, Acc) ->
  	case ocs_log:auth_query(Cont, Start, End,
  			Protocol, Types, ReqAttrs, ResAttrs) of
  		{error, Reason} ->
  			{error, Reason};
  		{Cont1, Events} ->
  			{NewCount, NewAcc} = case length(Events) + Count of
  				Total when Total =< PageSize ->
  					{Total, [Events | Acc]};
  				Total ->
  					{Total, [lists:sublist(Events, PageSize - Count) | Acc]}
  			end,
  			case Cont1 of
  				eof ->
  					{NewCount, lists:flatten(lists:reverse(NewAcc))};
  				_ when NewCount >= PageSize ->
  					{NewCount, lists:flatten(lists:reverse(NewAcc))};
  				_ ->
  					query_auth(Cont1, Start, End, Protocol, Types,
  							ReqAttrs, ResAttrs, PageSize, NewCount, NewAcc)
  			end
  	end.
  
  %% @hidden
  query_acct(Start, End, Protocol, Types, Attrs, PageSize) ->
  	query_acct(start, Start, End, Protocol, Types, Attrs, PageSize, 0, []).
  %% @hidden
  query_acct(Cont, Start, End, Protocol, Types, Attrs, PageSize, Count, Acc) ->
  	case ocs_log:acct_query(Cont, Start, End, Protocol, Types, Attrs) of
  		{error, Reason} ->
  			{error, Reason};
  		{Cont1, Events} ->
  			{NewCount, NewAcc} = case length(Events) + Count of
  				Total when Total =< PageSize ->
  					{Total, [Events | Acc]};
  				Total ->
  					{Total, [lists:sublist(Events, PageSize - Count) | Acc]}
  			end,
  			case Cont1 of
  				eof ->
  					{NewCount, lists:flatten(lists:reverse(NewAcc))};
  				_ when NewCount >= PageSize ->
  					{NewCount, lists:flatten(lists:reverse(NewAcc))};
  				_ ->
  					query_acct(Cont1, Start, End, Protocol, Types,
  							Attrs, PageSize, NewCount, NewAcc)
  			end
  	end.
  
  