View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.panels;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.button.Buttons;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.client.console.rest.SRARouteRestClient;
28  import org.apache.syncope.client.console.rest.SRAStatistics;
29  import org.apache.syncope.client.console.rest.SRAStatisticsRestClient;
30  import org.apache.syncope.client.console.widgets.NumberWidget;
31  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
32  import org.apache.syncope.common.lib.to.SRARouteTO;
33  import org.apache.wicket.ajax.AjaxRequestTarget;
34  import org.apache.wicket.ajax.markup.html.AjaxLink;
35  import org.apache.wicket.markup.ComponentTag;
36  import org.apache.wicket.markup.html.basic.Label;
37  import org.apache.wicket.markup.html.list.ListItem;
38  import org.apache.wicket.markup.html.list.ListView;
39  import org.apache.wicket.markup.html.panel.Panel;
40  import org.apache.wicket.model.IModel;
41  import org.apache.wicket.model.LoadableDetachableModel;
42  import org.apache.wicket.model.Model;
43  import org.apache.wicket.spring.injection.annot.SpringBean;
44  
45  public class SRAStatisticsPanel extends Panel {
46  
47      private static final long serialVersionUID = 23816535591360L;
48  
49      protected static final List<Buttons.Type> TYPES = List.of(
50              Buttons.Type.Info, Buttons.Type.Success, Buttons.Type.Warning, Buttons.Type.Danger, Buttons.Type.Dark);
51  
52      @SpringBean
53      protected SRARouteRestClient sraRouteRestClient;
54  
55      @SpringBean
56      protected SRAStatisticsRestClient sraStatisticsRestClient;
57  
58      protected final NumberWidget count;
59  
60      protected final NumberWidget totalTime;
61  
62      protected final NumberWidget max;
63  
64      protected final List<Pair<String, String>> selected = new ArrayList<>();
65  
66      protected final LoadableDetachableModel<Map<String, String>> routes = new LoadableDetachableModel<>() {
67  
68          private static final long serialVersionUID = 9089911876466472133L;
69  
70          @Override
71          protected Map<String, String> load() {
72              return sraRouteRestClient.list().stream().
73                      collect(Collectors.toMap(SRARouteTO::getKey, SRARouteTO::getName));
74          }
75      };
76  
77      protected int current;
78  
79      public SRAStatisticsPanel(final String id, final List<NetworkService> instances) {
80          super(id);
81  
82          SRAStatistics stats = sraStatisticsRestClient.get(instances, selected);
83  
84          count = new NumberWidget("count", "bg-green", stats.getMeasurement("COUNT").orElse(0F),
85                  "count", "fas fa-pen-nib");
86          add(count);
87  
88          totalTime = new NumberWidget("totalTime", "bg-info", stats.getMeasurement("TOTAL_TIME").orElse(0F),
89                  "total time", "fas fa-stopwatch");
90          add(totalTime);
91  
92          max = new NumberWidget("max", "bg-yellow", stats.getMeasurement("MAX").orElse(0F),
93                  "max", "fas fa-greater-than");
94          add(max);
95  
96          ListView<SRAStatistics.Tag> availableTags = new ListView<>("availableTags", stats.getAvailableTags()) {
97  
98              private static final long serialVersionUID = -9112553137618363167L;
99  
100             @Override
101             protected void populateItem(final ListItem<SRAStatistics.Tag> tag) {
102                 String btnCss = next().cssClassName();
103                 tag.add(new Label("label", tag.getModelObject().getTag()));
104                 tag.add(new ListView<>("tag", tag.getModelObject().getValues()) {
105 
106                     private static final long serialVersionUID = -9112553137618363167L;
107 
108                     @Override
109                     protected void populateItem(final ListItem<String> value) {
110                         AjaxLink<String> valueLink = new AjaxLink<>("valueLink") {
111 
112                             private static final long serialVersionUID = 6250423506463465679L;
113 
114                             @Override
115                             public void onClick(final AjaxRequestTarget target) {
116                                 Pair<String, String> selection =
117                                         Pair.of(tag.getModelObject().getTag(), value.getModelObject());
118                                 if (selected.contains(selection)) {
119                                     selected.remove(selection);
120                                 } else {
121                                     selected.add(selection);
122                                 }
123 
124                                 SRAStatistics refresh = sraStatisticsRestClient.get(instances, selected);
125 
126                                 count.refresh(refresh.getMeasurement("COUNT").orElse(0F));
127                                 totalTime.refresh(refresh.getMeasurement("TOTAL_TIME").orElse(0F));
128                                 max.refresh(refresh.getMeasurement("MAX").orElse(0F));
129 
130                                 target.add(count);
131                                 target.add(totalTime);
132                                 target.add(max);
133                             }
134 
135                             @Override
136                             protected void onComponentTag(final ComponentTag tag) {
137                                 super.onComponentTag(tag);
138                                 tag.append("class", btnCss, " ");
139                             }
140                         };
141 
142                         IModel<String> valueLabel = routes.getObject().containsKey(value.getModelObject())
143                                 ? Model.of(routes.getObject().get(value.getModelObject()))
144                                 : value.getModel();
145                         valueLink.add(new Label("valueLabel", valueLabel));
146                         value.add(valueLink);
147                     }
148                 });
149             }
150         };
151         add(availableTags);
152     }
153 
154     protected Buttons.Type next() {
155         if (current < TYPES.size()) {
156             Buttons.Type type = TYPES.get(current);
157             current++;
158             return type;
159         }
160 
161         current = 0;
162         return next();
163     }
164 }