22
33import com .google .common .collect .ImmutableList ;
44import dashboard .control .Constants ;
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
57import org .springframework .beans .factory .annotation .Value ;
68import org .springframework .http .HttpHeaders ;
79import org .springframework .web .client .RestTemplate ;
10+ import org .springframework .web .util .UriComponentsBuilder ;
811
9- import java .net .URLEncoder ;
10- import java .nio .charset .Charset ;
12+ import java .net .URI ;
1113import java .util .List ;
1214import java .util .Optional ;
1315
@@ -20,6 +22,8 @@ public class StatsImpl implements Stats, Constants {
2022 private final RestTemplate restTemplate ;
2123 private final String baseUrl ;
2224
25+ private static final Logger LOG = LoggerFactory .getLogger (StatsImpl .class );
26+
2327 public StatsImpl (@ Value ("${statsUser}" ) String user ,
2428 @ Value ("${statsPassword}" ) String password ,
2529 @ Value ("${statsBaseUrl}" ) String baseUrl ) {
@@ -32,38 +36,57 @@ public StatsImpl(@Value("${statsUser}") String user,
3236 headers .setAccept (ImmutableList .of (APPLICATION_JSON ));
3337 headers .set (AUTHORIZATION , authorizationHeaderValue (user , password ));
3438
39+ if (LOG .isDebugEnabled ()) {
40+ LOG .debug ("Outgoing request URI: " + request .getURI ());
41+ }
42+
3543 return execution .execute (request , body );
3644 }));
3745 }
3846
3947 public List <Object > loginTimeFrame (long from , long to , String scale , Optional <String > spEntityIdOptional ) {
40- String idp = currentIdPEncoded ();
41- StringBuilder url = new StringBuilder (String .format (
42- "%s/public/login_time_frame?from=%s&to=%s&include_unique=true&scale=%s&epoch=ms&idp_id=%s" ,
43- baseUrl , from , to , scale , idp ));
44- spEntityIdOptional .ifPresent (spEntityId -> url .append (String .format ("&sp_id=%s" , spEntityId )));
45- return restTemplate .getForEntity (url .toString (), List .class ).getBody ();
46- }
48+ UriComponentsBuilder builder = baseBuilder ("/public/login_time_frame" )
49+ .queryParam ("from" , from )
50+ .queryParam ("to" , to )
51+ .queryParam ("scale" , scale )
52+ .queryParam ("epoch" , "ms" );
53+ spEntityIdOptional .ifPresent (sp -> builder .queryParam ("sp_id" , sp ));
4754
48- private String currentIdPEncoded () {
49- return URLEncoder . encode ( currentUserIdp (), Charset . defaultCharset () );
55+ URI uri = builder . build (). encode (). toUri ();
56+ return restTemplate . getForEntity ( uri , List . class ). getBody ( );
5057 }
5158
5259 public List <Object > loginAggregated (String period , Optional <String > spEntityIdOptional ) {
53- String idp = currentIdPEncoded ();
54- StringBuilder url = new StringBuilder (String .format (
55- "%s/public/login_aggregated?period=%s&include_unique=true&idp_id=%s&group_by=sp_id" ,
56- baseUrl , period , idp ));
57- spEntityIdOptional .ifPresent (spEntityId -> url .append (String .format ("&sp_id=%s" , spEntityId )));
58- return restTemplate .getForEntity (url .toString (), List .class ).getBody ();
60+ UriComponentsBuilder builder = baseBuilder ("/public/login_aggregated" )
61+ .queryParam ("period" , period )
62+ .queryParam ("group_by" , "sp_id" );
63+ spEntityIdOptional .ifPresent (sp -> builder .queryParam ("sp_id" , sp ));
64+
65+ URI uri = builder .build ().encode ().toUri ();
66+ return restTemplate .getForEntity (uri , List .class ).getBody ();
5967 }
6068
6169 public List <Object > uniqueLoginCount (long from , long to , String spEntityId ) {
62- String idp = currentIdPEncoded ();
63- String url = String .format (
64- "%s/public/unique_login_count?from=%s&to=%s&include_unique=true&epoch=ms&idp_id=%s&sp_id=%s" ,
65- baseUrl , from , to , idp , spEntityId );
66- return restTemplate .getForEntity (url , List .class ).getBody ();
70+ UriComponentsBuilder builder = baseBuilder ("/public/unique_login_count" )
71+ .queryParam ("from" , from )
72+ .queryParam ("to" , to )
73+ .queryParam ("epoch" , "ms" )
74+ .queryParam ("sp_id" , spEntityId );
75+
76+ URI uri = builder .build ().encode ().toUri ();
77+ return restTemplate .getForEntity (uri , List .class ).getBody ();
78+ }
79+
80+ private UriComponentsBuilder baseBuilder (String path ) {
81+ return UriComponentsBuilder
82+ .fromHttpUrl (baseUrl )
83+ .path (path )
84+ .queryParam ("include_unique" , true )
85+ .queryParam ("idp_id" , getCurrentUserIdp ());
86+ }
87+
88+ protected String getCurrentUserIdp () {
89+ return currentUserIdp ();
6790 }
6891
6992}
0 commit comments