[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[8051] | 23 | using System.Collections.Generic;
|
---|
[16117] | 24 | using System.ComponentModel;
|
---|
[6976] | 25 | using System.Drawing;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
[8051] | 28 | using HeuristicLab.Clients.Access;
|
---|
[6976] | 29 | using HeuristicLab.Clients.Hive.Views;
|
---|
[16117] | 30 | using HeuristicLab.Collections;
|
---|
| 31 | using HeuristicLab.Common.Resources;
|
---|
[6976] | 32 | using HeuristicLab.Core;
|
---|
| 33 | using HeuristicLab.Core.Views;
|
---|
| 34 | using HeuristicLab.MainForm;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Clients.Hive.Administrator.Views {
|
---|
| 37 | [View("Resources View")]
|
---|
[7928] | 38 | [Content(typeof(IItemList<Resource>), false)]
|
---|
[6976] | 39 | public partial class ResourcesView : ItemView, IDisposable {
|
---|
| 40 | private const int slaveImageIndex = 0;
|
---|
| 41 | private const int slaveGroupImageIndex = 1;
|
---|
[16117] | 42 | public const string UNGROUPED_GROUP_NAME = "UNGROUPED";
|
---|
| 43 | public const string UNGROUPED_GROUP_DESCRIPTION = "Contains slaves that are not assigned to any group.";
|
---|
| 44 | private const string SELECTED_TAG = ""; // " [selected]";
|
---|
| 45 | private const string NOT_STORED_TAG = "*"; // " [not stored]";
|
---|
| 46 | private const string CHANGES_NOT_STORED_TAG = "*"; // " [changes not stored]";
|
---|
[6976] | 47 |
|
---|
[16117] | 48 | private readonly Color changedColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
|
---|
| 49 | private readonly Color selectedBackColor = Color.DodgerBlue;
|
---|
| 50 | private readonly Color selectedForeColor = Color.White;
|
---|
| 51 | private readonly Color calculatingColor = Color.FromArgb(255, 58, 114, 35); // #3a7223
|
---|
| 52 | private readonly Color offlineColor = Color.FromArgb(255, 187, 36, 36); // #bb2424
|
---|
| 53 | private readonly Color grayTextColor = SystemColors.GrayText;
|
---|
[6976] | 54 |
|
---|
| 55 |
|
---|
[16117] | 56 | private TreeNode ungroupedGroupNode;
|
---|
| 57 |
|
---|
| 58 | private Resource selectedResource = null;
|
---|
| 59 | public Resource SelectedResource {
|
---|
| 60 | get { return selectedResource; }
|
---|
| 61 | set { if (selectedResource != value) ChangeSelectedResource(value); }
|
---|
[6976] | 62 | }
|
---|
| 63 |
|
---|
[16117] | 64 | private readonly object locker = new object();
|
---|
[16446] | 65 | private bool refreshingInternal = false;
|
---|
| 66 | private bool refreshingExternal = false;
|
---|
[6976] | 67 |
|
---|
[16117] | 68 | public new IItemList<Resource> Content {
|
---|
| 69 | get { return (IItemList<Resource>)base.Content; }
|
---|
| 70 | set { base.Content = value; }
|
---|
[6976] | 71 | }
|
---|
| 72 |
|
---|
[16117] | 73 | public ResourcesView() {
|
---|
| 74 | InitializeComponent();
|
---|
[6976] | 75 |
|
---|
[16117] | 76 | treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
|
---|
| 77 | treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
|
---|
| 78 |
|
---|
| 79 | HiveAdminClient.Instance.Refreshing += HiveAdminClient_Instance_Refreshing;
|
---|
| 80 | HiveAdminClient.Instance.Refreshed += HiveAdminClient_Instance_Refreshed;
|
---|
[6976] | 81 | }
|
---|
| 82 |
|
---|
[16117] | 83 | #region Overrides
|
---|
| 84 | protected override void RegisterContentEvents() {
|
---|
| 85 | base.RegisterContentEvents();
|
---|
| 86 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
| 87 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
[8051] | 88 | }
|
---|
| 89 |
|
---|
[6976] | 90 | protected override void DeregisterContentEvents() {
|
---|
[16117] | 91 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
| 92 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
[6976] | 93 | base.DeregisterContentEvents();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected override void OnContentChanged() {
|
---|
| 97 | base.OnContentChanged();
|
---|
| 98 | if (Content == null) {
|
---|
[16117] | 99 | treeView.Nodes.Clear();
|
---|
| 100 | viewHost.Content = null;
|
---|
[8051] | 101 | scheduleView.Content = null;
|
---|
[6976] | 102 | } else {
|
---|
[16117] | 103 | BuildResourceTree(Content);
|
---|
| 104 | }
|
---|
[16122] | 105 | SetEnabledStateOfControls();
|
---|
[16117] | 106 | }
|
---|
[6976] | 107 |
|
---|
[16117] | 108 | protected override void SetEnabledStateOfControls() {
|
---|
| 109 | base.SetEnabledStateOfControls();
|
---|
[6976] | 110 |
|
---|
[16122] | 111 | bool locked = Content == null || Locked || ReadOnly;
|
---|
| 112 | bool addLocked = locked
|
---|
| 113 | || !IsAdmin()
|
---|
| 114 | || (selectedResource is Slave && selectedResource.ParentResourceId != null)
|
---|
| 115 | || (selectedResource != null && selectedResource.Id == Guid.Empty);
|
---|
[6976] | 116 |
|
---|
[16122] | 117 | HashSet<Guid> descendantResources = null;
|
---|
[16185] | 118 | bool selectedRDeleteLocked = selectedResource == null
|
---|
| 119 | || (selectedResource.Id != Guid.Empty && (!HiveAdminClient.Instance.ResourceDescendants.TryGetValue(selectedResource.Id, out descendantResources) || descendantResources.Any()));
|
---|
[16122] | 120 |
|
---|
[16185] | 121 | var nodes = GetCheckedNodes(treeView.Nodes).ToList();
|
---|
| 122 | var checkedResources = nodes.Select(x => x.Tag).OfType<Resource>().ToList();
|
---|
| 123 | bool checkedRDeleteLocked = false;
|
---|
| 124 | for (int i = 0; !checkedRDeleteLocked && i < checkedResources.Count; i++) {
|
---|
| 125 | if (checkedResources[i].Id != Guid.Empty &&
|
---|
| 126 | (!HiveAdminClient.Instance.ResourceDescendants.TryGetValue(checkedResources[i].Id, out descendantResources) ||
|
---|
| 127 | descendantResources.Any()))
|
---|
| 128 | checkedRDeleteLocked = true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[16122] | 131 | bool deleteLocked = locked
|
---|
[16185] | 132 | || !IsAdmin()
|
---|
| 133 | || !Content.Any()
|
---|
| 134 | || checkedResources.Any() && checkedRDeleteLocked
|
---|
| 135 | || !checkedResources.Any() && selectedRDeleteLocked;
|
---|
[16122] | 136 |
|
---|
| 137 | bool saveLocked = locked
|
---|
| 138 | || !IsAdmin()
|
---|
| 139 | || !Content.Any()
|
---|
| 140 | || selectedResource == null;
|
---|
| 141 |
|
---|
[16117] | 142 | btnAddGroup.Enabled = !addLocked;
|
---|
| 143 | btnRemoveGroup.Enabled = !deleteLocked;
|
---|
[16122] | 144 | btnSave.Enabled = !saveLocked;
|
---|
| 145 | viewHost.Locked = locked || !IsAdmin();
|
---|
| 146 | scheduleView.Locked = locked || !IsAdmin();
|
---|
[16117] | 147 | }
|
---|
| 148 | #endregion
|
---|
[6976] | 149 |
|
---|
[16117] | 150 | #region Event Handlers
|
---|
| 151 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
|
---|
| 152 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsAdded, sender, e);
|
---|
| 153 | else {
|
---|
| 154 | OnContentChanged();
|
---|
[6976] | 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[16117] | 158 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
|
---|
| 159 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsRemoved, sender, e);
|
---|
| 160 | else {
|
---|
| 161 | OnContentChanged();
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | private void SlaveViewContent_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 166 | if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)SlaveViewContent_PropertyChanged, sender, e);
|
---|
| 167 | else {
|
---|
| 168 | OnContentChanged();
|
---|
| 169 | if (e.PropertyName == "HbInterval") {
|
---|
| 170 | UpdateChildHbIntervall((Resource)viewHost.Content);
|
---|
[6976] | 171 | }
|
---|
[16117] | 172 | }
|
---|
| 173 | }
|
---|
[6976] | 174 |
|
---|
[16117] | 175 | private void HiveAdminClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
| 176 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshing, sender, e);
|
---|
| 177 | else {
|
---|
[16446] | 178 | lock (locker) {
|
---|
| 179 | if (refreshingExternal) return;
|
---|
| 180 | if (!refreshingInternal) refreshingExternal = true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[16430] | 183 | Progress.Show(this, "Refreshing ...", ProgressMode.Indeterminate);
|
---|
[16117] | 184 | SetEnabledStateOfControls();
|
---|
[6976] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[16117] | 188 | private void HiveAdminClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
| 189 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshed, sender, e);
|
---|
| 190 | else {
|
---|
[16446] | 191 | if (refreshingExternal) refreshingExternal = false;
|
---|
| 192 | Content = HiveAdminClient.Instance.Resources;
|
---|
| 193 |
|
---|
[16430] | 194 | Progress.Hide(this);
|
---|
[16117] | 195 | SetEnabledStateOfControls();
|
---|
[7250] | 196 | }
|
---|
[6976] | 197 | }
|
---|
| 198 |
|
---|
[16446] | 199 | private async void ResourcesView_Load(object sender, EventArgs e) {
|
---|
| 200 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(() => UpdateResources());
|
---|
[8051] | 201 | }
|
---|
[6976] | 202 |
|
---|
[16446] | 203 | private void ResourcesView_Disposed(object sender, EventArgs e) {
|
---|
| 204 | HiveAdminClient.Instance.Refreshed -= HiveAdminClient_Instance_Refreshed;
|
---|
| 205 | HiveAdminClient.Instance.Refreshing -= HiveAdminClient_Instance_Refreshing;
|
---|
[16117] | 206 | }
|
---|
[6976] | 207 |
|
---|
[16117] | 208 | private async void btnRefresh_Click(object sender, EventArgs e) {
|
---|
| 209 | lock (locker) {
|
---|
| 210 | if (!btnRefresh.Enabled) return;
|
---|
| 211 | btnRefresh.Enabled = false;
|
---|
| 212 | }
|
---|
[8051] | 213 |
|
---|
[16117] | 214 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 215 | action: () => UpdateResources(),
|
---|
| 216 | finallyCallback: () => btnRefresh.Enabled = true);
|
---|
| 217 | }
|
---|
[8051] | 218 |
|
---|
[16117] | 219 | private void btnAddGroup_Click(object sender, EventArgs e) {
|
---|
[16122] | 220 | var parentResourceId = selectedResource is SlaveGroup ? selectedResource.Id : (Guid?)null;
|
---|
[16117] | 221 |
|
---|
| 222 | var group = new SlaveGroup {
|
---|
| 223 | Name = "New Group",
|
---|
| 224 | OwnerUserId = UserInformation.Instance.User.Id,
|
---|
| 225 | ParentResourceId = parentResourceId
|
---|
| 226 | };
|
---|
| 227 |
|
---|
| 228 | SelectedResource = group;
|
---|
| 229 | Content.Add(group);
|
---|
[6976] | 230 | }
|
---|
| 231 |
|
---|
[16430] | 232 | private async void btnRemoveGroup_Click(object sender, EventArgs e) {
|
---|
[16185] | 233 | var nodes = GetCheckedNodes(treeView.Nodes).ToList();
|
---|
| 234 | var checkedResources = nodes.Select(x => x.Tag).OfType<Resource>().ToList();
|
---|
| 235 | if (selectedResource == null && !checkedResources.Any()) return;
|
---|
[16117] | 236 |
|
---|
| 237 | lock (locker) {
|
---|
| 238 | if (!btnRemoveGroup.Enabled) return;
|
---|
| 239 | btnRemoveGroup.Enabled = false;
|
---|
[6976] | 240 | }
|
---|
[16117] | 241 |
|
---|
[16185] | 242 | if (checkedResources.Count > 0) {
|
---|
| 243 | var result = MessageBox.Show(
|
---|
| 244 | "Do you really want to delete all " + checkedResources.Count + " checked resources?",
|
---|
| 245 | "HeuristicLab Hive Administrator",
|
---|
| 246 | MessageBoxButtons.YesNo,
|
---|
| 247 | MessageBoxIcon.Question);
|
---|
| 248 | if (result == DialogResult.Yes) {
|
---|
| 249 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 250 | action: () => {
|
---|
| 251 | RemoveResource(checkedResources);
|
---|
| 252 | });
|
---|
| 253 | }
|
---|
| 254 | } else {
|
---|
| 255 | var res = checkedResources.Any() ? checkedResources.First() : selectedResource;
|
---|
| 256 | var result = MessageBox.Show(
|
---|
| 257 | "Do you really want to delete the selected resource " + res.Name + "?",
|
---|
| 258 | "HeuristicLab Hive Administrator",
|
---|
| 259 | MessageBoxButtons.YesNo,
|
---|
| 260 | MessageBoxIcon.Question);
|
---|
| 261 | if (result == DialogResult.Yes) {
|
---|
| 262 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 263 | action: () => {
|
---|
| 264 | RemoveResource(res);
|
---|
| 265 | });
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
[16122] | 268 |
|
---|
[16185] | 269 | OnContentChanged();
|
---|
| 270 | SetEnabledStateOfControls();
|
---|
[6976] | 271 | }
|
---|
| 272 |
|
---|
[16117] | 273 | private async void btnSave_Click(object sender, EventArgs e) {
|
---|
| 274 | lock (locker) {
|
---|
| 275 | if (!btnSave.Enabled) return;
|
---|
| 276 | btnSave.Enabled = false;
|
---|
[6976] | 277 | }
|
---|
[16117] | 278 |
|
---|
| 279 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 280 | action: () => {
|
---|
| 281 | var resourcesToSave = Content.Where(x => x.Id == Guid.Empty || x.Modified);
|
---|
| 282 | foreach (var resource in resourcesToSave)
|
---|
| 283 | resource.Store();
|
---|
| 284 | UpdateResources();
|
---|
[16122] | 285 | });
|
---|
[16117] | 286 |
|
---|
| 287 | OnContentChanged();
|
---|
[16122] | 288 | SetEnabledStateOfControls();
|
---|
[6976] | 289 | }
|
---|
| 290 |
|
---|
[16117] | 291 | private void treeSlaveGroup_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 292 | var node = treeView.GetNodeAt(e.Location);
|
---|
| 293 | if (node == null || node == ungroupedGroupNode) return;
|
---|
| 294 | var r = (Resource)node.Tag;
|
---|
| 295 | if (!HiveAdminClient.Instance.DisabledParentResources.Contains(r)) ChangeSelectedResourceNode(node);
|
---|
[6976] | 296 | }
|
---|
| 297 |
|
---|
[16117] | 298 | private void treeSlaveGroup_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
|
---|
| 299 | e.Cancel = true;
|
---|
[6976] | 300 | }
|
---|
| 301 |
|
---|
[16117] | 302 | private void treeSlaveGroup_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
|
---|
[16209] | 303 | if (!IsAdmin() || e.Node == ungroupedGroupNode) {
|
---|
[16117] | 304 | e.Cancel = true;
|
---|
| 305 | } else {
|
---|
| 306 | var r = (Resource)e.Node.Tag;
|
---|
| 307 | if (HiveAdminClient.Instance.DisabledParentResources.Contains(r)) {
|
---|
| 308 | e.Cancel = true;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[16185] | 313 | private void treeSlaveGroup_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
| 314 | SetEnabledStateOfControls();
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[16117] | 317 | private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
|
---|
| 318 | if (e.Effect == DragDropEffects.None) return;
|
---|
| 319 |
|
---|
| 320 | var targetNode = treeView.GetNodeAt(treeView.PointToClient(new Point(e.X, e.Y)));
|
---|
| 321 | var targetResource = (targetNode != null) ? (Resource)targetNode.Tag : null;
|
---|
| 322 | var resources = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IEnumerable<Resource>;
|
---|
| 323 |
|
---|
| 324 | foreach (var r in resources) {
|
---|
| 325 | r.ParentResourceId = targetResource != null ? targetResource.Id : (Guid?)null;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | // TODO
|
---|
| 329 | //HiveAdminClient.Instance.UpdateResourceGenealogy(Content);
|
---|
[6976] | 330 | OnContentChanged();
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[16117] | 333 | private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 334 | if (!IsAdmin()) return;
|
---|
[6976] | 335 |
|
---|
[16117] | 336 | var nodes = GetCheckedNodes(treeView.Nodes).ToList();
|
---|
| 337 | TreeNode sourceNode = (TreeNode)e.Item;
|
---|
| 338 | if (!sourceNode.Checked) nodes.Add(sourceNode);
|
---|
| 339 | nodes.Remove(ungroupedGroupNode);
|
---|
| 340 | ungroupedGroupNode.Checked = false;
|
---|
| 341 | var resources = nodes.Select(x => x.Tag).OfType<Resource>().ToList();
|
---|
| 342 |
|
---|
| 343 | if (resources.Count > 0) {
|
---|
| 344 | DataObject data = new DataObject();
|
---|
| 345 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, resources);
|
---|
| 346 | var action = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 347 | if (action.HasFlag(DragDropEffects.Move)) {
|
---|
| 348 | foreach (var node in nodes) node.Remove();
|
---|
| 349 | StyleTreeNode(ungroupedGroupNode, (Resource)ungroupedGroupNode.Tag, resources);
|
---|
[6976] | 350 | }
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[16117] | 354 | private IEnumerable<TreeNode> GetCheckedNodes(TreeNodeCollection nodes) {
|
---|
| 355 | if (nodes != null) {
|
---|
| 356 | foreach (var node in nodes.OfType<TreeNode>()) {
|
---|
| 357 | if (node.Checked && node != ungroupedGroupNode) yield return node;
|
---|
| 358 | foreach (var child in GetCheckedNodes(node.Nodes))
|
---|
| 359 | yield return child;
|
---|
[6976] | 360 | }
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[16117] | 364 | private void treeSlaveGroup_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 365 | e.Effect = DragDropEffects.Move;
|
---|
| 366 | var resources = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IEnumerable<Resource>;
|
---|
| 367 | var targetNode = treeView.GetNodeAt(treeView.PointToClient(new Point(e.X, e.Y)));
|
---|
| 368 | var targetResource = (targetNode != null ? targetNode.Tag : null) as Resource;
|
---|
[6976] | 369 |
|
---|
[16117] | 370 | if (!IsAdmin()
|
---|
| 371 | || resources == null
|
---|
| 372 | || !resources.Any()
|
---|
| 373 | || resources.Any(x => !HiveAdminClient.Instance.CheckParentChange(x, targetResource))
|
---|
| 374 | || (targetNode != null && (targetNode == ungroupedGroupNode || targetNode.Parent == ungroupedGroupNode))) {
|
---|
| 375 | e.Effect = DragDropEffects.None;
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
[6976] | 378 |
|
---|
[16117] | 379 | private void TabSlaveGroup_TabIndexChanged(object sender, EventArgs e) {
|
---|
| 380 | throw new NotImplementedException();
|
---|
| 381 | }
|
---|
[6976] | 382 |
|
---|
[16117] | 383 | private async void TabSlaveGroup_Selected(object sender, System.Windows.Forms.TabControlEventArgs e) {
|
---|
| 384 | if (e.TabPage == tabSchedule) {
|
---|
| 385 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 386 | action: () => UpdateSchedule(),
|
---|
| 387 | finallyCallback: () => scheduleView.Content = HiveAdminClient.Instance.Downtimes);
|
---|
| 388 | }
|
---|
| 389 | SetEnabledStateOfControls();
|
---|
| 390 | }
|
---|
| 391 | #endregion
|
---|
[6976] | 392 |
|
---|
[16117] | 393 | #region Helpers
|
---|
| 394 | private void BuildResourceTree(IEnumerable<Resource> resources) {
|
---|
| 395 | treeView.Nodes.Clear();
|
---|
| 396 | if (!resources.Any()) return;
|
---|
[8051] | 397 |
|
---|
[16117] | 398 | var disabledParentResources = HiveAdminClient.Instance.DisabledParentResources;
|
---|
| 399 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
| 400 | .Where(x => x.ParentResourceId == null));
|
---|
| 401 | //var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
| 402 | // .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
| 403 | //mainResources.UnionWith(parentedMainResources);
|
---|
| 404 | var mainDisabledParentResources = new HashSet<Resource>(disabledParentResources.Where(x => x.ParentResourceId == null || x.ParentResourceId == Guid.Empty));
|
---|
| 405 | mainResources.UnionWith(mainDisabledParentResources);
|
---|
| 406 | var subResources = new HashSet<Resource>(resources.Union(disabledParentResources).Except(mainResources).OrderByDescending(x => x.Name));
|
---|
[8051] | 407 |
|
---|
[16117] | 408 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
| 409 | if (selectedResource != null) SelectedResource = resources.Where(x => x.Id == selectedResource.Id).FirstOrDefault();
|
---|
| 410 | bool nodeSelected = false;
|
---|
[6976] | 411 |
|
---|
[16117] | 412 | TreeNode currentNode = null;
|
---|
| 413 | Resource currentResource = null;
|
---|
| 414 |
|
---|
| 415 | while (stack.Any()) {
|
---|
| 416 | var newResource = stack.Pop();
|
---|
| 417 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
| 418 | StyleTreeNode(newNode, newResource, resources);
|
---|
| 419 |
|
---|
| 420 | if (selectedResource == null && !disabledParentResources.Contains(newResource)) {
|
---|
| 421 | SelectedResource = newResource;
|
---|
| 422 | }
|
---|
| 423 | if (!nodeSelected && selectedResource != null && newResource.Id == selectedResource.Id) {
|
---|
| 424 | newNode.BackColor = selectedBackColor;
|
---|
| 425 | newNode.ForeColor = selectedForeColor;
|
---|
| 426 | newNode.Text += SELECTED_TAG;
|
---|
| 427 | nodeSelected = true;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if (disabledParentResources.Contains(newResource)) {
|
---|
| 431 | newNode.Checked = false;
|
---|
| 432 | newNode.ForeColor = grayTextColor;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | // search for parent node of newNode and save in currentNode
|
---|
| 436 | // necessary since newNodes (stack top items) might be siblings
|
---|
| 437 | // or grand..grandparents of previous node (currentNode)
|
---|
| 438 | while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
|
---|
| 439 | currentNode = currentNode.Parent;
|
---|
| 440 | currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | if (currentNode == null) {
|
---|
| 444 | treeView.Nodes.Add(newNode);
|
---|
| 445 | } else {
|
---|
| 446 | currentNode.Nodes.Add(newNode);
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | if (newResource is SlaveGroup) {
|
---|
| 450 | var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
|
---|
| 451 | if (childResources.Any()) {
|
---|
| 452 | foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
|
---|
| 453 | subResources.Remove(resource);
|
---|
| 454 | stack.Push(resource);
|
---|
[6976] | 455 | }
|
---|
[16117] | 456 | currentNode = newNode;
|
---|
| 457 | currentResource = newResource;
|
---|
[6976] | 458 | }
|
---|
| 459 | }
|
---|
[16117] | 460 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
[6976] | 461 | }
|
---|
| 462 |
|
---|
[16117] | 463 | // collapse slave-only nodes
|
---|
| 464 | foreach (TreeNode n in treeView.Nodes) {
|
---|
| 465 | CollapseSlaveOnlyNodes(n);
|
---|
| 466 | }
|
---|
[6976] | 467 |
|
---|
[16117] | 468 | ungroupedGroupNode = new TreeNode(UNGROUPED_GROUP_NAME) {
|
---|
| 469 | ForeColor = SystemColors.GrayText,
|
---|
[16122] | 470 | ImageIndex = slaveGroupImageIndex,
|
---|
[16117] | 471 | Tag = new SlaveGroup() {
|
---|
| 472 | Name = UNGROUPED_GROUP_NAME,
|
---|
| 473 | Description = UNGROUPED_GROUP_DESCRIPTION
|
---|
[6976] | 474 | }
|
---|
[16117] | 475 | };
|
---|
| 476 |
|
---|
| 477 | foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
|
---|
| 478 | var slaveNode = new TreeNode(slave.Name) { Tag = slave };
|
---|
| 479 | StyleTreeNode(slaveNode, slave, resources);
|
---|
| 480 | ungroupedGroupNode.Nodes.Add(slaveNode);
|
---|
| 481 | if (selectedResource == null) {
|
---|
| 482 | SelectedResource = slave;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | if (slave.Id == selectedResource.Id && !nodeSelected) {
|
---|
| 486 | slaveNode.BackColor = selectedBackColor;
|
---|
| 487 | slaveNode.ForeColor = selectedForeColor;
|
---|
| 488 | slaveNode.Text += SELECTED_TAG;
|
---|
| 489 | nodeSelected = true;
|
---|
| 490 | }
|
---|
[6976] | 491 | }
|
---|
| 492 |
|
---|
[16117] | 493 | if (ungroupedGroupNode.Nodes.Count > 0) {
|
---|
| 494 | ungroupedGroupNode.Text += " [" + ungroupedGroupNode.Nodes.Count.ToString() + "]";
|
---|
| 495 | ungroupedGroupNode.Expand();
|
---|
[16122] | 496 | treeView.Nodes.Add(ungroupedGroupNode);
|
---|
[16117] | 497 | }
|
---|
[6976] | 498 | }
|
---|
| 499 |
|
---|
[16117] | 500 | private void CollapseSlaveOnlyNodes(TreeNode tn) {
|
---|
| 501 | Resource r = (Resource)tn.Tag;
|
---|
| 502 | var descendants = GetResourceDescendants();
|
---|
| 503 | if (descendants.ContainsKey(r.Id)) {
|
---|
| 504 | if (descendants[r.Id].OfType<SlaveGroup>().Any()) {
|
---|
| 505 | tn.Expand();
|
---|
| 506 | foreach (TreeNode n in tn.Nodes) CollapseSlaveOnlyNodes(n);
|
---|
| 507 | } else {
|
---|
| 508 | tn.Collapse();
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
[6976] | 511 | }
|
---|
| 512 |
|
---|
[16117] | 513 | private void ExpandResourceNodesOfInterest(TreeNodeCollection nodes) {
|
---|
| 514 | foreach (TreeNode n in nodes) {
|
---|
| 515 | Resource r = (Resource)n.Tag;
|
---|
| 516 | if (n.Nodes.Count > 0) {
|
---|
| 517 | if (HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id).OfType<SlaveGroup>().Any()) {
|
---|
| 518 | n.Expand();
|
---|
| 519 | ExpandResourceNodesOfInterest(n.Nodes);
|
---|
| 520 | } else {
|
---|
| 521 | n.Collapse();
|
---|
| 522 | }
|
---|
| 523 | } else {
|
---|
| 524 | n.Collapse();
|
---|
| 525 | }
|
---|
| 526 | }
|
---|
[6976] | 527 | }
|
---|
| 528 |
|
---|
[16117] | 529 | private void UpdateChildHbIntervall(Resource resource) {
|
---|
| 530 | foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
|
---|
| 531 | r.HbInterval = resource.HbInterval;
|
---|
| 532 | if (r is SlaveGroup) {
|
---|
| 533 | UpdateChildHbIntervall(r);
|
---|
[7256] | 534 | }
|
---|
[6976] | 535 | }
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | private void UpdateResources() {
|
---|
[16446] | 539 | lock (locker) {
|
---|
| 540 | if (refreshingInternal || refreshingExternal) return;
|
---|
| 541 | refreshingInternal = true;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[6976] | 544 | try {
|
---|
| 545 | HiveAdminClient.Instance.Refresh();
|
---|
[16117] | 546 | } catch (AnonymousUserException) {
|
---|
| 547 | ShowHiveInformationDialog();
|
---|
[16446] | 548 | } finally {
|
---|
| 549 | refreshingInternal = false;
|
---|
[6976] | 550 | }
|
---|
[16117] | 551 | }
|
---|
| 552 |
|
---|
| 553 | private void RemoveResource(Resource resource) {
|
---|
| 554 | if (resource == null) return;
|
---|
| 555 |
|
---|
| 556 | try {
|
---|
| 557 | if (resource.Id != Guid.Empty) {
|
---|
| 558 | SelectedResource = HiveAdminClient.Instance.GetAvailableResourceAncestors(resource.Id).LastOrDefault();
|
---|
[16185] | 559 |
|
---|
| 560 | // deal with all new, but not yet saved resources
|
---|
| 561 | var newResources = Content.Where(x => x.ParentResourceId == resource.Id).ToList();
|
---|
| 562 | if (newResources.Any(x => x.Id != Guid.Empty)) return;
|
---|
| 563 | foreach (var nr in newResources) Content.Remove(nr);
|
---|
| 564 |
|
---|
[16430] | 565 | HiveAdminClient.Delete(resource);
|
---|
[16117] | 566 | UpdateResources();
|
---|
| 567 | } else {
|
---|
[16185] | 568 | SelectedResource = Content.FirstOrDefault(x => x.Id == resource.ParentResourceId);
|
---|
[16122] | 569 | Content.Remove(resource);
|
---|
[16117] | 570 | }
|
---|
| 571 | } catch (AnonymousUserException) {
|
---|
| 572 | ShowHiveInformationDialog();
|
---|
[6976] | 573 | }
|
---|
[16117] | 574 | }
|
---|
| 575 |
|
---|
[16185] | 576 | private void RemoveResource(IEnumerable<Resource> resources) {
|
---|
| 577 | if (resources == null || !resources.Any()) return;
|
---|
| 578 |
|
---|
[16430] | 579 | var ids = resources.Select(x => x.Id).ToList();
|
---|
[16185] | 580 | try {
|
---|
| 581 | bool update = false;
|
---|
[16430] | 582 | foreach (var r in resources) {
|
---|
| 583 | if (r.Id != Guid.Empty) {
|
---|
| 584 | if (r.Id == SelectedResource.Id)
|
---|
[16185] | 585 | SelectedResource = HiveAdminClient.Instance.GetAvailableResourceAncestors(r.Id).LastOrDefault();
|
---|
| 586 |
|
---|
| 587 | // deal with all new, but not yet saved resources
|
---|
| 588 | var newResources = Content.Where(x => x.ParentResourceId == r.Id).ToList();
|
---|
| 589 | if (newResources.Any(x => x.Id != Guid.Empty)) return;
|
---|
| 590 | foreach (var nr in newResources) Content.Remove(nr);
|
---|
| 591 |
|
---|
| 592 | HiveAdminClient.Delete(r);
|
---|
| 593 | update = true;
|
---|
| 594 | } else {
|
---|
| 595 | if (r.Id == SelectedResource.Id)
|
---|
| 596 | SelectedResource = Content.FirstOrDefault(x => x.Id == r.ParentResourceId);
|
---|
| 597 | Content.Remove(r);
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | if (update) UpdateResources();
|
---|
| 601 | } catch (AnonymousUserException) {
|
---|
| 602 | ShowHiveInformationDialog();
|
---|
| 603 | }
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[16117] | 606 | private void UpdateSchedule() {
|
---|
| 607 | try {
|
---|
| 608 | HiveAdminClient.Instance.RefreshCalendar();
|
---|
| 609 | } catch (AnonymousUserException) {
|
---|
[7256] | 610 | ShowHiveInformationDialog();
|
---|
[7249] | 611 | }
|
---|
[6976] | 612 | }
|
---|
| 613 |
|
---|
[16117] | 614 | private bool IsAdmin() {
|
---|
| 615 | return HiveRoles.CheckAdminUserPermissions();
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | private void StyleTreeNode(TreeNode n, Resource r, IEnumerable<Resource> resources) {
|
---|
| 619 | n.Text = r.Name;
|
---|
| 620 | n.BackColor = Color.Transparent;
|
---|
| 621 | n.ForeColor = Color.Black;
|
---|
| 622 |
|
---|
| 623 | if (HiveAdminClient.Instance.DisabledParentResources.Select(x => x.Id).Contains(r.Id)) {
|
---|
| 624 | n.ForeColor = grayTextColor;
|
---|
| 625 | } else if (r.Id == Guid.Empty && n != ungroupedGroupNode /*!r.Name.StartsWith(UNGROUPED_GROUP_NAME)*/) {
|
---|
| 626 | // not stored (i.e. new)
|
---|
| 627 | n.Text += NOT_STORED_TAG;
|
---|
| 628 | } else if (r.Modified && n != ungroupedGroupNode /*!r.Name.StartsWith(UNGROUPED_GROUP_NAME)*/) {
|
---|
| 629 | // changed
|
---|
| 630 | n.Text += CHANGES_NOT_STORED_TAG;
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | // slave count
|
---|
| 634 | int childSlavesCount = 0;
|
---|
| 635 | if (r.Id != Guid.Empty && r is SlaveGroup) {
|
---|
| 636 | var descendants = GetResourceDescendants();
|
---|
| 637 | if (descendants.ContainsKey(r.Id)) {
|
---|
| 638 | childSlavesCount = resources
|
---|
| 639 | .OfType<Slave>()
|
---|
| 640 | .Where(x => descendants[r.Id].Select(y => y.Id)
|
---|
| 641 | .Contains(x.Id))
|
---|
| 642 | .Count();
|
---|
| 643 | }
|
---|
| 644 | } else if (n == ungroupedGroupNode /*|| r.Name.StartsWith(UNGROUPED_GROUP_NAME)*/) {
|
---|
| 645 | childSlavesCount = resources
|
---|
| 646 | .OfType<Slave>()
|
---|
| 647 | .Where(x => x.ParentResourceId == null
|
---|
| 648 | || (x.ParentResourceId.HasValue && x.ParentResourceId.Value == Guid.Empty))
|
---|
| 649 | .Count();
|
---|
| 650 | }
|
---|
| 651 | if (childSlavesCount > 0)
|
---|
| 652 | n.Text += " [" + childSlavesCount.ToString() + "]";
|
---|
| 653 |
|
---|
| 654 | // slave image index, state, utilization
|
---|
| 655 | if (r is Slave) {
|
---|
| 656 | n.ImageIndex = slaveImageIndex;
|
---|
| 657 | var s = r as Slave;
|
---|
| 658 | if (s.SlaveState == SlaveState.Calculating) {
|
---|
| 659 | n.ForeColor = calculatingColor;
|
---|
| 660 | n.Text += " [" + s.CpuUtilization.ToString("N2") + "%]";
|
---|
| 661 | } else if (s.SlaveState == SlaveState.Offline) {
|
---|
| 662 | n.ForeColor = offlineColor;
|
---|
| 663 | if (s.LastHeartbeat.HasValue)
|
---|
| 664 | n.Text += " [" + (s.LastHeartbeat != null ? s.LastHeartbeat.Value.ToString("g") : null) + "]";
|
---|
| 665 | }
|
---|
[7256] | 666 | } else {
|
---|
[16117] | 667 | n.ImageIndex = slaveGroupImageIndex;
|
---|
[7256] | 668 | }
|
---|
[16117] | 669 |
|
---|
| 670 | // ungrouped
|
---|
| 671 | if (n == ungroupedGroupNode /*r.Name.StartsWith(UNGROUPED_GROUP_NAME)*/) {
|
---|
| 672 | n.ForeColor = SystemColors.GrayText;
|
---|
| 673 | }
|
---|
[7256] | 674 | }
|
---|
| 675 |
|
---|
[16117] | 676 | private void ResetTreeNodes(TreeNodeCollection nodes, IEnumerable<Resource> resources) {
|
---|
| 677 | foreach (TreeNode n in nodes) {
|
---|
| 678 | StyleTreeNode(n, (Resource)n.Tag, resources);
|
---|
| 679 | if (n.Nodes.Count > 0) {
|
---|
| 680 | ResetTreeNodes(n.Nodes, resources);
|
---|
[7256] | 681 | }
|
---|
| 682 | }
|
---|
| 683 | }
|
---|
| 684 |
|
---|
[16117] | 685 | private async void ChangeSelectedResource(Resource resource) {
|
---|
| 686 | selectedResource = resource;
|
---|
| 687 | viewHost.Content = selectedResource;
|
---|
[6976] | 688 |
|
---|
[16117] | 689 | HiveAdminClient.Instance.DowntimeForResourceId = selectedResource != null ? selectedResource.Id : Guid.Empty;
|
---|
| 690 | if (tabSlaveGroup.SelectedTab == tabSchedule) {
|
---|
| 691 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 692 | action: () => UpdateSchedule(),
|
---|
| 693 | finallyCallback: () => scheduleView.Content = HiveAdminClient.Instance.Downtimes);
|
---|
| 694 | }
|
---|
[6976] | 695 |
|
---|
[16117] | 696 | SetEnabledStateOfControls();
|
---|
[6976] | 697 | }
|
---|
| 698 |
|
---|
[16117] | 699 | private void ChangeSelectedResourceNode(TreeNode resourceNode) {
|
---|
| 700 | if (resourceNode == null) return;
|
---|
| 701 | SelectedResource = (Resource)resourceNode.Tag;
|
---|
| 702 | ResetTreeNodes(treeView.Nodes, Content);
|
---|
| 703 | resourceNode.BackColor = selectedBackColor;
|
---|
| 704 | resourceNode.ForeColor = selectedForeColor;
|
---|
| 705 | resourceNode.Text += SELECTED_TAG;
|
---|
[8051] | 706 | }
|
---|
| 707 |
|
---|
[16117] | 708 | private void ShowHiveInformationDialog() {
|
---|
| 709 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
| 710 | else {
|
---|
| 711 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
| 712 | dialog.ShowDialog(this);
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
[8051] | 715 | }
|
---|
| 716 |
|
---|
[16117] | 717 | private void ResetView() {
|
---|
| 718 | if (InvokeRequired) Invoke((Action)ResetView);
|
---|
| 719 | else {
|
---|
| 720 | treeView.Nodes.Clear();
|
---|
[8051] | 721 |
|
---|
[16117] | 722 | if (viewHost.Content != null && viewHost.Content is SlaveGroup) {
|
---|
| 723 | ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
|
---|
| 724 | }
|
---|
[6976] | 725 |
|
---|
[16117] | 726 | viewHost.Content = null;
|
---|
| 727 | if (scheduleView.Content != null) {
|
---|
| 728 | scheduleView.Content.Clear();
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | HiveAdminClient.Instance.ResetDowntime();
|
---|
[6976] | 732 | }
|
---|
| 733 | }
|
---|
| 734 |
|
---|
[6994] | 735 |
|
---|
[16117] | 736 | private Dictionary<Guid, HashSet<Resource>> GetResourceDescendants() {
|
---|
| 737 | var resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
|
---|
[16185] | 738 | //var resources = Content.Where(x => x.Id != Guid.Empty).Union(HiveAdminClient.Instance.DisabledParentResources).ToList();
|
---|
[16117] | 739 | var resources = Content.Union(HiveAdminClient.Instance.DisabledParentResources).ToList();
|
---|
| 740 |
|
---|
| 741 | foreach (var r in resources) {
|
---|
[16430] | 742 | if (!resourceDescendants.ContainsKey(r.Id))
|
---|
[16185] | 743 | resourceDescendants.Add(r.Id, new HashSet<Resource>());
|
---|
| 744 | }
|
---|
| 745 | foreach (var r in resources) {
|
---|
[16117] | 746 | var parentResourceId = r.ParentResourceId;
|
---|
| 747 | while (parentResourceId != null) {
|
---|
| 748 | var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
|
---|
| 749 | if (parent != null) {
|
---|
| 750 | resourceDescendants[parent.Id].Add(r);
|
---|
| 751 | parentResourceId = parent.ParentResourceId;
|
---|
| 752 | } else {
|
---|
| 753 | parentResourceId = null;
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 | return resourceDescendants;
|
---|
[6994] | 758 | }
|
---|
[8051] | 759 |
|
---|
[16117] | 760 | #endregion
|
---|
[6976] | 761 | }
|
---|
| 762 | }
|
---|