anchor.diff:  -define(MB, (1024*1024)).
   -define(GB, (1024*1024*1024)).
   -include_lib("stdlib/include/qlc.hrl").
  +-include_lib("stdlib/include/ms_transform.hrl").
   
   -import(proplists, [get_value/2, get_value/3]).
   
  
positive.code: %%clients api
  api(clients, Req) ->
      ClientQry = Req:parse_post(),
      CurrentPage = int(get_value("curr_page", ClientQry, "1")),
      PageSize = int(get_value("page_size", ClientQry, "10")),
      Username = get_value("user_key", ClientQry, ""),
      Clients = clientcount(mqtt_client, Username), 
      TotalNum = length(Clients),
      %% boundary
      Result = lists:sublist(Clients, (CurrentPage-1)*PageSize+1, PageSize),
      TotalPage = case TotalNum rem PageSize of
  		    0 -> TotalNum div PageSize;
  		    _ -> (TotalNum div PageSize) + 1
  		end,
      api_respond(Req, [{currentPage, CurrentPage},{pageSize, PageSize},{totalNum, TotalNum}, {totalPage, TotalPage},{result, Result}]);
      
  %%-----------------------------------session--------------------------------------
  %%sessin check api
  
negative.code: 	_ -> false
      end.
  
  clientcount(Tab) ->
      MScount = ets:fun2ms(fun(#mqtt_client{client_id = ClientId, peername = {IpAddr, Port},
                           username = Username, clean_sess = CleanSess,
                           proto_ver = ProtoVer, keepalive = KeepAlvie,
  			 connected_at = ConnectedAt}) -> true end),
      ets:select_count(Tab, MScount).
  
  
  queryclient(Tab, 'end')->
      {[],[]};
  queryclient(Tab, [])->
      result_match(ets:match_object(Tab, #mqtt_client{_='_'}, 3));
  
  queryclient(_Tab, Continue)->
      result_match(ets:match_object(Continue)).
  
  int(S) ->
      list_to_integer(S).
  
  str(A) when is_atom(A) -> atom_to_list(A);
  str(B) when is_binary(B) -> binary_to_list(B);
  str(L) when is_list(L) -> L.
  
  formatclient(#mqtt_client{client_id = ClientId, peername = {IpAddr, Port},
                           username = Username, clean_sess = CleanSess,
                           proto_ver = ProtoVer, keepalive = KeepAlvie,
  			 connected_at = ConnectedAt})->
  	[{clientId, ClientId}, 
         	 {username, Username}, 
   	 {ipaddress, list_to_binary(emqttd_net:ntoa(IpAddr))}, 
  	 {port, Port},
           {clean_sess, CleanSess},
           {proto_ver, ProtoVer},
           {keepalive, KeepAlvie},
           {connected_at, list_to_binary(connected_at_format(ConnectedAt))}].
  
  result_match(Result)->
       case Result of
  	'$end_of_table' ->
  		{[], 'end'};
  	{Matchs, '$end_of_table'}	->
  		Matchlist = [formatclient(Match)|| Match <- Matchs],
  		{Matchlist, 'end'};
  	{Matchs, Continuation}	->
  		Matchlist = [formatclient(Match)|| Match <- Matchs],
  		{Matchlist, Continuation}
      end.
  