2bca0b75e56cc5846cc172c940525f9b41e8bdf0
[public/netxms.git] / webui / webapp / PerfView / src / org / netxms / ui / eclipse / perfview / objecttabs / internal / PerfTabGraph.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.perfview.objecttabs.internal;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.DisposeEvent;
30 import org.eclipse.swt.events.DisposeListener;
31 import org.eclipse.swt.layout.FillLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Widget;
34 import org.eclipse.ui.IViewPart;
35 import org.netxms.client.NXCSession;
36 import org.netxms.client.datacollection.DciData;
37 import org.netxms.client.datacollection.GraphItem;
38 import org.netxms.client.datacollection.GraphItemStyle;
39 import org.netxms.client.datacollection.PerfTabDci;
40 import org.netxms.ui.eclipse.charts.api.ChartFactory;
41 import org.netxms.ui.eclipse.charts.api.HistoricalDataChart;
42 import org.netxms.ui.eclipse.jobs.ConsoleJob;
43 import org.netxms.ui.eclipse.perfview.Activator;
44 import org.netxms.ui.eclipse.perfview.Messages;
45 import org.netxms.ui.eclipse.perfview.PerfTabGraphSettings;
46 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
47 import org.netxms.ui.eclipse.tools.ViewRefreshController;
48 import org.netxms.ui.eclipse.tools.VisibilityValidator;
49 import org.netxms.ui.eclipse.widgets.DashboardComposite;
50
51 /**
52 * Performance tab graph
53 *
54 */
55 public class PerfTabGraph extends DashboardComposite
56 {
57 private long nodeId;
58 private List<PerfTabDci> items = new ArrayList<PerfTabDci>(4);
59 private HistoricalDataChart chart;
60 private ViewRefreshController refreshController = null;
61 private boolean updateInProgress = false;
62 private NXCSession session;
63 private long timeInterval;
64 private IViewPart viewPart;
65 private VisibilityValidator validator;
66
67 /**
68 * @param parent
69 * @param style
70 */
71 public PerfTabGraph(Composite parent, long nodeId, PerfTabDci dci, PerfTabGraphSettings settings, IViewPart viewPart, VisibilityValidator validator)
72 {
73 super(parent, SWT.BORDER);
74 this.nodeId = nodeId;
75 this.viewPart = viewPart;
76 this.validator = validator;
77 items.add(dci);
78 session = (NXCSession)ConsoleSharedData.getSession();
79
80 setLayout(new FillLayout());
81
82 chart = ChartFactory.createLineChart(this, SWT.NONE);
83 chart.setZoomEnabled(false);
84 chart.setTitleVisible(true);
85 chart.setChartTitle(settings.getRuntimeTitle());
86 chart.setLegendVisible(settings.isShowLegendAlways());
87 chart.setExtendedLegend(settings.isExtendedLegend());
88 chart.setLogScaleEnabled(settings.isLogScaleEnabled());
89 chart.setStacked(settings.isStacked());
90
91 timeInterval = settings.getTimeRangeMillis();
92 final Date from = new Date(System.currentTimeMillis() - settings.getTimeRangeMillis());
93 final Date to = new Date(System.currentTimeMillis());
94 chart.setTimeRange(from, to);
95
96 GraphItemStyle style = new GraphItemStyle(settings.getType(), settings.getColorAsInt(), 2, 0);
97 chart.setItemStyles(Arrays.asList(new GraphItemStyle[] { style }));
98 if (!settings.isAutoScale())
99 {
100 chart.setYAxisRange(settings.getMinYScaleValue(), settings.getMaxYScaleValue());
101 }
102 chart.addParameter(new GraphItem(nodeId, dci.getId(), 0, 0, "", settings.getRuntimeName(), "%s")); //$NON-NLS-1$ //$NON-NLS-2$
103
104 addDisposeListener(new DisposeListener() {
105 @Override
106 public void widgetDisposed(DisposeEvent e)
107 {
108 if (refreshController != null)
109 refreshController.dispose();
110 }
111 });
112 }
113
114 /**
115 * Add another item to graph
116 *
117 * @param dci
118 * @param settings
119 */
120 public void addItem(PerfTabDci dci, PerfTabGraphSettings settings)
121 {
122 chart.setLegendVisible(true);
123 if (settings.isExtendedLegend())
124 chart.setExtendedLegend(true);
125 synchronized(items)
126 {
127 items.add(dci);
128 GraphItemStyle style = new GraphItemStyle(settings.getType(), settings.getColorAsInt(), 2, 0);
129 List<GraphItemStyle> styles = new ArrayList<GraphItemStyle>(chart.getItemStyles());
130 if (styles.size() < items.size())
131 styles.add(style);
132 else
133 styles.set(items.size() - 1, style);
134 chart.setItemStyles(styles);
135 chart.addParameter(new GraphItem(nodeId, dci.getId(), 0, 0, "", settings.getRuntimeName(), "%s")); //$NON-NLS-1$ //$NON-NLS-2$
136 }
137 }
138
139 /**
140 * Start chart update
141 */
142 public void start()
143 {
144 refreshController = new ViewRefreshController(viewPart, 30, new Runnable() {
145 @Override
146 public void run()
147 {
148 if (PerfTabGraph.this.isDisposed())
149 return;
150
151 refreshData();
152 }
153 }, validator);
154 if (validator.isVisible())
155 refreshData();
156 }
157
158 /**
159 * Refresh graph's data
160 */
161 public void refreshData()
162 {
163 if (updateInProgress)
164 return;
165
166 updateInProgress = true;
167 chart.clearErrors();
168
169 ConsoleJob job = new ConsoleJob(Messages.get().PerfTabGraph_JobTitle, null, Activator.PLUGIN_ID, Activator.PLUGIN_ID) {
170 private PerfTabDci currentDci;
171
172 @Override
173 protected void runInternal(IProgressMonitor monitor) throws Exception
174 {
175 final Date from = new Date(System.currentTimeMillis() - timeInterval);
176 final Date to = new Date(System.currentTimeMillis());
177 synchronized(items)
178 {
179 final DciData[] data = new DciData[items.size()];
180 for(int i = 0; i < data.length; i++)
181 {
182 currentDci = items.get(i);
183 data[i] = session.getCollectedData(nodeId, currentDci.getId(), from, to, 0);
184 }
185 runInUIThread(new Runnable() {
186 @Override
187 public void run()
188 {
189 if (!((Widget)chart).isDisposed())
190 {
191 chart.setTimeRange(from, to);
192 for(int i = 0; i < data.length; i++)
193 chart.updateParameter(i, data[i], true);
194 }
195 updateInProgress = false;
196 }
197 });
198 }
199 }
200
201 @Override
202 protected String getErrorMessage()
203 {
204 return String.format(Messages.get().PerfTabGraph_JobError, currentDci.getId(), currentDci.getDescription());
205 }
206
207 @Override
208 protected void jobFailureHandler()
209 {
210 updateInProgress = false;
211 super.jobFailureHandler();
212 }
213
214 @Override
215 protected IStatus createFailureStatus(Exception e)
216 {
217 runInUIThread(new Runnable() {
218 @Override
219 public void run()
220 {
221 chart.addError(getErrorMessage());
222 }
223 });
224 return Status.OK_STATUS;
225 }
226 };
227 job.setUser(false);
228 job.start();
229 }
230 }