b0a8464c2d70b9ebdcfd8aa99fccb81fb746b478
[public/netxms.git] / src / java / netxms-eclipse / 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(false);
87 chart.setLogScaleEnabled(settings.isLogScaleEnabled());
88 chart.setStacked(settings.isStacked());
89
90 timeInterval = settings.getTimeRangeMillis();
91 final Date from = new Date(System.currentTimeMillis() - settings.getTimeRangeMillis());
92 final Date to = new Date(System.currentTimeMillis());
93 chart.setTimeRange(from, to);
94
95 GraphItemStyle style = new GraphItemStyle(settings.getType(), settings.getColorAsInt(), 2, 0);
96 chart.setItemStyles(Arrays.asList(new GraphItemStyle[] { style }));
97 if(!settings.isAutoScale())
98 {
99 chart.setYAxisRange(settings.getMinYScaleValue(), settings.getMaxYScaleValue());
100 }
101 chart.addParameter(new GraphItem(nodeId, dci.getId(), 0, 0, "", settings.getRuntimeName(), "%s")); //$NON-NLS-1$ //$NON-NLS-2$
102
103 addDisposeListener(new DisposeListener() {
104 @Override
105 public void widgetDisposed(DisposeEvent e)
106 {
107 if (refreshController != null)
108 refreshController.dispose();
109 }
110 });
111 }
112
113 /**
114 * Add another item to graph
115 *
116 * @param dci
117 * @param settings
118 */
119 public void addItem(PerfTabDci dci, PerfTabGraphSettings settings)
120 {
121 chart.setLegendVisible(true);
122 synchronized(items)
123 {
124 items.add(dci);
125 GraphItemStyle style = new GraphItemStyle(settings.getType(), settings.getColorAsInt(), 2, 0);
126 List<GraphItemStyle> styles = new ArrayList<GraphItemStyle>(chart.getItemStyles());
127 if (styles.size() < items.size())
128 styles.add(style);
129 else
130 styles.set(items.size() - 1, style);
131 chart.setItemStyles(styles);
132 chart.addParameter(new GraphItem(nodeId, dci.getId(), 0, 0, "", settings.getRuntimeName(), "%s")); //$NON-NLS-1$ //$NON-NLS-2$
133 }
134 }
135
136 /**
137 * Start chart update
138 */
139 public void start()
140 {
141 refreshController = new ViewRefreshController(viewPart, 30, new Runnable() {
142 @Override
143 public void run()
144 {
145 if (PerfTabGraph.this.isDisposed())
146 return;
147
148 refreshData();
149 }
150 }, validator);
151 if (validator.isVisible())
152 refreshData();
153 }
154
155 /**
156 * Refresh graph's data
157 */
158 public void refreshData()
159 {
160 if (updateInProgress)
161 return;
162
163 updateInProgress = true;
164 chart.clearErrors();
165
166 ConsoleJob job = new ConsoleJob(Messages.get().PerfTabGraph_JobTitle, null, Activator.PLUGIN_ID, Activator.PLUGIN_ID) {
167 private PerfTabDci currentDci;
168
169 @Override
170 protected void runInternal(IProgressMonitor monitor) throws Exception
171 {
172 final Date from = new Date(System.currentTimeMillis() - timeInterval);
173 final Date to = new Date(System.currentTimeMillis());
174 synchronized(items)
175 {
176 final DciData[] data = new DciData[items.size()];
177 for(int i = 0; i < data.length; i++)
178 {
179 currentDci = items.get(i);
180 data[i] = session.getCollectedData(nodeId, currentDci.getId(), from, to, 0);
181 }
182 runInUIThread(new Runnable() {
183 @Override
184 public void run()
185 {
186 if (!((Widget)chart).isDisposed())
187 {
188 chart.setTimeRange(from, to);
189 for(int i = 0; i < data.length; i++)
190 chart.updateParameter(i, data[i], true);
191 }
192 updateInProgress = false;
193 }
194 });
195 }
196 }
197
198 @Override
199 protected String getErrorMessage()
200 {
201 return String.format(Messages.get().PerfTabGraph_JobError, currentDci.getId(), currentDci.getDescription());
202 }
203
204 @Override
205 protected void jobFailureHandler()
206 {
207 updateInProgress = false;
208 super.jobFailureHandler();
209 }
210
211 @Override
212 protected IStatus createFailureStatus(Exception e)
213 {
214 runInUIThread(new Runnable() {
215 @Override
216 public void run()
217 {
218 chart.addError(getErrorMessage());
219 }
220 });
221 return Status.OK_STATUS;
222 }
223 };
224 job.setUser(false);
225 job.start();
226 }
227 }