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.widgets;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.commons.lang3.time.DateFormatUtils;
24  import org.apache.syncope.client.console.chartjs.ChartJSPanel;
25  import org.apache.syncope.client.console.chartjs.ChartOptions;
26  import org.apache.syncope.client.console.chartjs.Line;
27  import org.apache.syncope.client.console.chartjs.LineDataSet;
28  import org.apache.syncope.common.lib.info.SystemInfo;
29  import org.apache.wicket.model.Model;
30  
31  public class LoadWidget extends BaseWidget {
32  
33      private static final long serialVersionUID = -816175678514035085L;
34  
35      private final ChartJSPanel chart;
36  
37      public LoadWidget(final String id, final SystemInfo systeminfo) {
38          super(id);
39          setOutputMarkupId(true);
40  
41          chart = new ChartJSPanel("chart", Model.of(build(systeminfo)));
42          add(chart);
43      }
44  
45      private static Line build(final SystemInfo systeminfo) {
46          List<Double> cpuValues = new ArrayList<>();
47          List<Long> memValues = new ArrayList<>();
48  
49          Line line = new Line();
50          line.getOptions().setPointDot(false);
51          line.getOptions().setDatasetFill(false);
52          line.getOptions().setResponsive(true);
53          line.getOptions().setMaintainAspectRatio(true);
54          line.getOptions().setTension(0.4);
55          line.getOptions().setMultiTooltipTemplate("<%= datasetLabel %>");
56  
57          ChartOptions.Axis x = new ChartOptions.Axis();
58          x.setDisplay(false);
59          ChartOptions.Axis y = new ChartOptions.Axis();
60          y.setDisplay(false);
61          ChartOptions.Scales scales = new ChartOptions.Scales();
62          scales.setX(x);
63          scales.setY(y);
64          line.getOptions().setScales(scales);
65  
66          systeminfo.getLoad().forEach(instant -> {
67              line.getData().getLabels().add(
68                      DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(
69                              systeminfo.getStartTime() + instant.getUptime()));
70  
71              cpuValues.add(instant.getSystemLoadAverage() * 1000);
72              memValues.add(instant.getTotalMemory());
73          });
74  
75          LineDataSet cpuDataSet = new LineDataSet(cpuValues);
76          cpuDataSet.setLabel("CPU");
77          cpuDataSet.setPointColor("purple");
78          cpuDataSet.setBorderColor("purple");
79          line.getData().getDatasets().add(cpuDataSet);
80  
81          LineDataSet memDataSet = new LineDataSet(memValues);
82          memDataSet.setLabel("MEM");
83          memDataSet.setPointColor("grey");
84          memDataSet.setBorderColor("grey");
85          line.getData().getDatasets().add(memDataSet);
86  
87          return line;
88      }
89  
90      public void refresh(final SystemInfo systeminfo) {
91          chart.setDefaultModelObject(build(systeminfo));
92      }
93  }