summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/views/VolumesSummaryView.java
blob: 9581600b417d7cd046ac1eae09a432f3b5fba3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.console.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;
import org.gluster.storage.management.console.GlusterDataModelManager;
import org.gluster.storage.management.console.IImageKeys;
import org.gluster.storage.management.console.utils.ChartViewerComposite;
import org.gluster.storage.management.console.utils.GUIHelper;
import org.gluster.storage.management.core.constants.CoreConstants;
import org.gluster.storage.management.core.model.Alert;
import org.gluster.storage.management.core.model.Cluster;
import org.gluster.storage.management.core.model.ClusterListener;
import org.gluster.storage.management.core.model.DefaultClusterListener;
import org.gluster.storage.management.core.model.EntityGroup;
import org.gluster.storage.management.core.model.Event;
import org.gluster.storage.management.core.model.Status;
import org.gluster.storage.management.core.model.TaskInfo;
import org.gluster.storage.management.core.model.Volume;
import org.gluster.storage.management.core.model.TaskInfo.TASK_TYPE;
import org.gluster.storage.management.core.model.Volume.VOLUME_STATUS;


/**
 * 
 */
public class VolumesSummaryView extends ViewPart {
	public static final String ID = VolumesSummaryView.class.getName();
	private static final GUIHelper guiHelper = GUIHelper.getInstance();
	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private ScrolledForm form;
	private EntityGroup<Volume> volumes;
	private Cluster cluster = GlusterDataModelManager.getInstance().getModel().getCluster();
	private ClusterListener clusterListener;

	private static final String ALERTS = "Alerts";
	private static final String VOLUMES_SUMMARY = "Volumes - Summary";
	private static final String AVAILABILITY = "Availability";
	private Composite alertsSection;
	private Composite tasksSection;
	private Composite summarySection;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void createPartControl(Composite parent) {
		if (volumes == null) {
			Object selectedObj = guiHelper.getSelectedEntity(getSite(), EntityGroup.class);
			if (selectedObj != null && ((EntityGroup) selectedObj).getEntityType() == Volume.class) {
				volumes = (EntityGroup<Volume>) selectedObj;
			}
		}
		
		setPartName("Summary");
		createSections(parent);

		clusterListener = new DefaultClusterListener() {
			@Override
			public void volumeCreated(Volume volume) {
				super.volumeCreated(volume);
				updateSummarySection();
			}

			@Override
			public void volumeDeleted(Volume volume) {
				super.volumeDeleted(volume);
				updateSummarySection();
			}

			@Override
			public void volumeChanged(Volume volume, Event event) {
				super.volumeChanged(volume, event);
				updateSummarySection();
			}
			
			private void updateAlertSection() {
				guiHelper.clearSection(alertsSection);
				populateAlertSection();
			}
			
			private void updateSummarySection() {
				guiHelper.clearSection(summarySection);
				populateSummarySection();
				summarySection.layout();
				form.reflow(true);
			}

			@Override
			public void alertsGenerated() {
				super.alertsGenerated();
				guiHelper.clearSection(alertsSection);
				populateAlertSection();
			}
			
			@Override
			public void alertRemoved(Alert alert) {
				super.alertRemoved(alert);
				updateAlertSection();
			}
			
			@Override
			public void alertCreated(Alert alert) {
				super.alertCreated(alert);
				updateAlertSection();
			}

			@Override
			public void taskAdded(TaskInfo taskInfo) {
				super.taskAdded(taskInfo);
				updateTasksSection();
			}

			@Override
			public void taskRemoved(TaskInfo taskInfo) {
				super.taskRemoved(taskInfo);
				updateTasksSection();
			}

			@Override
			public void taskUpdated(TaskInfo taskInfo) {
				super.taskUpdated(taskInfo);
				updateTasksSection();
			}
			
			private void updateTasksSection() {
				guiHelper.clearSection(tasksSection);
				populateTasks();
			}
		};
		GlusterDataModelManager.getInstance().addClusterListener(clusterListener);
	}
	
	@Override
	public void dispose() {
		super.dispose();
		GlusterDataModelManager.getInstance().removeClusterListener(clusterListener);
	}

	private void createSections(Composite parent) {
		form = guiHelper.setupForm(parent, toolkit, VOLUMES_SUMMARY);
		createSummarySection();
		createTasksSection();
		createAlertsSection();

		parent.layout(); // IMP: lays out the form properly
	}

	private void createAlertsSection() {
		alertsSection = guiHelper.createSection(form, toolkit, ALERTS, null, 1, false);
		populateAlertSection();
	}

	private void populateAlertSection() {
		for (Alert alert : cluster.getAlerts()) {
			if (alert.getType() == Alert.ALERT_TYPES.OFFLINE_VOLUME_BRICKS_ALERT
					|| alert.getType() == Alert.ALERT_TYPES.OFFLINE_VOLUME_ALERT) {
				addAlertLabel(alertsSection, alert);
			}
		}
		alertsSection.pack(true);
		form.reflow(true);
	}

	private void addAlertLabel(Composite section, Alert alert) {
		CLabel lblAlert = new CLabel(section, SWT.NONE);
		
		Image alertImage = null;
		switch (alert.getType()) {
			case OFFLINE_VOLUME_BRICKS_ALERT:
				alertImage = guiHelper.getImage(IImageKeys.BRICK_OFFLINE_22x22);
				break;
			case OFFLINE_VOLUME_ALERT:
				alertImage = guiHelper.getImage(IImageKeys.VOLUME_OFFLINE_22x22);
				break;
		}
		lblAlert.setImage(alertImage);
		lblAlert.setText(alert.getMessage());
		lblAlert.redraw();
	}

	private void createTasksSection() {
		tasksSection = guiHelper.createSection(form, toolkit, CoreConstants.RUNNING_TASKS, null, 1, false);
		populateTasks();
	}

	private void populateTasks() {
		for (TaskInfo taskInfo : cluster.getTaskInfoList()) {
			if ((taskInfo.getType() == TASK_TYPE.BRICK_MIGRATE || taskInfo.getType() == TASK_TYPE.VOLUME_REBALANCE)
					&& taskInfo.getStatus().getCode() != Status.STATUS_CODE_SUCCESS)
				addTaskLabel(tasksSection, taskInfo);
		}
		tasksSection.pack(true);
		form.reflow(true);
	}

	private void addTaskLabel(Composite section, TaskInfo taskInfo) {
		// Task related to Volumes context
		if (taskInfo.getStatus().isPercentageSupported()) {
			// TODO Progress bar or link to progress view
		}
		
		CLabel lblTask = new CLabel(section, SWT.NONE);
		String description = taskInfo.getDescription();
		switch (taskInfo.getStatus().getCode()) {
		case Status.STATUS_CODE_PAUSE:
			description += " (paused)";
			break;
		case Status.STATUS_CODE_COMMIT_PENDING:
			description += " (commit pending)";
			break;
		case Status.STATUS_CODE_FAILURE:
			description += " (failed)";
			break;
		}
		lblTask.setText(description);
		lblTask.setImage((taskInfo.getType() == TASK_TYPE.BRICK_MIGRATE) ? guiHelper
				.getImage(IImageKeys.BRICK_MIGRATE_32x32) : guiHelper.getImage(IImageKeys.VOLUME_REBALANCE_32x32));
		lblTask.redraw();
	}

	private void createSummarySection() {
		summarySection = guiHelper.createSection(form, toolkit, AVAILABILITY, null, 2, false);
		populateSummarySection();
	}

	private void populateSummarySection() {
		if(volumes.getEntities().size() == 0) {
			toolkit.createLabel(summarySection,
					"This section will be populated after at least" + CoreConstants.NEWLINE +"one volume is created the storage cloud.");
			return;
		}

		Double[] values = new Double[] { Double.valueOf(getVolumeCountByStatus(volumes, VOLUME_STATUS.ONLINE)),
				Double.valueOf(getVolumeCountByStatus(volumes, VOLUME_STATUS.OFFLINE)) };
		createStatusChart(toolkit, summarySection, values);
	}

	private int getVolumeCountByStatus(EntityGroup<Volume> volumes, VOLUME_STATUS status) {
		int count = 0;
		for (Volume volume : volumes.getEntities()) {
			if (volume.getStatus() == status) {
				count++;
			}
		}
		return count;
	}

	private void createStatusChart(FormToolkit toolkit, Composite section, Double[] values) {
		String[] categories = new String[] { "Online", "Offline" };
		ChartViewerComposite chartViewerComposite = new ChartViewerComposite(section, SWT.NONE, categories, values);

		GridData data = new GridData(SWT.FILL, SWT.FILL, false, false);
		data.widthHint = 300;
		data.heightHint = 150;
		chartViewerComposite.setLayoutData(data);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		if (form != null) {
			form.setFocus();
		}
	}
}