1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
23 | using System.Threading;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Clients.Hive {
|
---|
28 | [Item("Hive Administrator", "Hive Administrator")]
|
---|
29 | public sealed class HiveAdminClient : IContent {
|
---|
30 | private static HiveAdminClient instance;
|
---|
31 | public static HiveAdminClient Instance {
|
---|
32 | get {
|
---|
33 | if (instance == null) instance = new HiveAdminClient();
|
---|
34 | return instance;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private IItemList<Resource> resources;
|
---|
39 | public IItemList<Resource> Resources {
|
---|
40 | get { return resources; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | private IItemList<Downtime> downtimes;
|
---|
44 | public IItemList<Downtime> Downtimes {
|
---|
45 | get { return downtimes; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private Guid downtimeForResourceId;
|
---|
49 | public Guid DowntimeForResourceId {
|
---|
50 | get { return downtimeForResourceId; }
|
---|
51 | set {
|
---|
52 | downtimeForResourceId = value;
|
---|
53 | if (downtimes != null) {
|
---|
54 | downtimes.Clear();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private IItemList<Project> projects;
|
---|
60 | public IItemList<Project> Projects {
|
---|
61 | get { return projects; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | #region Events
|
---|
65 | public event EventHandler Refreshing;
|
---|
66 | private void OnRefreshing() {
|
---|
67 | EventHandler handler = Refreshing;
|
---|
68 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
69 | }
|
---|
70 | public event EventHandler Refreshed;
|
---|
71 | private void OnRefreshed() {
|
---|
72 | var handler = Refreshed;
|
---|
73 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | private HiveAdminClient() { }
|
---|
78 |
|
---|
79 | #region Refresh
|
---|
80 | public void Refresh() {
|
---|
81 | OnRefreshing();
|
---|
82 |
|
---|
83 | try {
|
---|
84 | resources = new ItemList<Resource>();
|
---|
85 | projects = new ItemList<Project>();
|
---|
86 |
|
---|
87 | HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
88 | service.GetSlaveGroups().ForEach(g => resources.Add(g));
|
---|
89 | service.GetSlaves().ForEach(s => resources.Add(s));
|
---|
90 | service.GetProjects().ForEach(p => projects.Add(p));
|
---|
91 | });
|
---|
92 | }
|
---|
93 | catch {
|
---|
94 | throw;
|
---|
95 | }
|
---|
96 | finally {
|
---|
97 | OnRefreshed();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region Refresh downtime calendar
|
---|
103 | public void RefreshCalendar() {
|
---|
104 | if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty) {
|
---|
105 | OnRefreshing();
|
---|
106 |
|
---|
107 | try {
|
---|
108 | downtimes = new ItemList<Downtime>();
|
---|
109 |
|
---|
110 | HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
111 | service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
|
---|
112 | });
|
---|
113 | }
|
---|
114 | catch {
|
---|
115 | throw;
|
---|
116 | }
|
---|
117 | finally {
|
---|
118 | OnRefreshed();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | #region Store
|
---|
125 | public static void Store(IHiveItem item, CancellationToken cancellationToken) {
|
---|
126 | if (item.Id == Guid.Empty) {
|
---|
127 | if (item is SlaveGroup) {
|
---|
128 | item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
|
---|
129 | }
|
---|
130 | if (item is Slave) {
|
---|
131 | item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlave((Slave)item));
|
---|
132 | }
|
---|
133 | if (item is Downtime) {
|
---|
134 | item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddDowntime((Downtime)item));
|
---|
135 | }
|
---|
136 | if (item is Project) {
|
---|
137 | item.Id = HiveServiceLocator.Instance.CallHiveService(s => s.AddProject((Project)item));
|
---|
138 | }
|
---|
139 | } else {
|
---|
140 | if (item is SlaveGroup) {
|
---|
141 | HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
|
---|
142 | }
|
---|
143 | if (item is Slave) {
|
---|
144 | HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlave((Slave)item));
|
---|
145 | }
|
---|
146 | if (item is Downtime) {
|
---|
147 | HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
|
---|
148 | }
|
---|
149 | if (item is Project) {
|
---|
150 | HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateProject((Project)item));
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | #region Delete
|
---|
157 | public static void Delete(IHiveItem item) {
|
---|
158 | if (item is SlaveGroup) {
|
---|
159 | HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
|
---|
160 | } else if (item is Slave) {
|
---|
161 | HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlave(item.Id));
|
---|
162 | } else if (item is Downtime) {
|
---|
163 | HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteDowntime(item.Id));
|
---|
164 | } else if (item is Project) {
|
---|
165 | HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteProject(item.Id));
|
---|
166 | }
|
---|
167 | }
|
---|
168 | #endregion
|
---|
169 |
|
---|
170 | public void ResetDowntime() {
|
---|
171 | downtimeForResourceId = Guid.Empty;
|
---|
172 | if (downtimes != null) {
|
---|
173 | downtimes.Clear();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|