source: misc/tools/HeuristicLab.HiveDrain/HiveStatus/Program.cs @ 16667

Last change on this file since 16667 was 16667, checked in by abeham, 3 years ago

Added further projects useful for interfacing with irace

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using CommandLine;
27using HeuristicLab.Clients.Hive;
28
29namespace HiveStatus {
30  class Program {
31    public const int EXIT_OK = 0;
32    public const int EXIT_ERR = 1;
33    public const int EXIT_FAILED = 2;
34    public const int EXIT_NOTFOUND = 3;
35
36    static void Main(string[] args) {
37      //AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "HiveStatus.exe.config");
38      var result = Parser.Default.ParseArguments<Options>(args)
39        .WithParsed(x => {
40          try {
41            Environment.Exit(Run(x));
42          } catch { Environment.Exit(EXIT_ERR); }
43        })
44        .WithNotParsed(x => Environment.Exit(EXIT_ERR));
45    }
46
47    private static int Run(Options opts) {
48#if __DRY_RUN__
49      Console.WriteLine("DONE");
50      return EXIT_OK;
51#endif
52      if (string.IsNullOrEmpty(opts.Username) && HeuristicLab.Clients.Common.Properties.Settings.Default.UserName.ToLower() == "anonymous") {
53        Console.Write("Username: ");
54        opts.Username = Console.ReadLine();
55      }
56      if (!string.IsNullOrEmpty(opts.Username)) {
57        HiveServiceLocator.Instance.Username = opts.Username;
58        var password = opts.Password;
59        if (string.IsNullOrEmpty(password)) {
60          Console.Error.Write("Password for " + opts.Username + ": ");
61          password = ReadPassword();
62        }
63        HiveServiceLocator.Instance.Password = password;
64      }
65      return CheckStatus(opts);
66    }
67
68    private static int CheckStatus(Options opt) {
69      if (opt.JobId == default(Guid)) return CheckStatusAll(opt);
70
71      var jobId = opt.JobId;
72
73      if (opt.Verbose) {
74
75        List<LightweightTask> tasks = null;
76        try {
77          tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(jobId));
78        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
79          Console.WriteLine("?");
80          return EXIT_NOTFOUND;
81        }
82
83        int completed, failed, running, paused;
84        GetStats(tasks, out completed, out failed, out running, out paused);
85        Console.WriteLine(FormatStateInfo(tasks.Count, completed, failed, running, paused));
86        return failed > 0 ? EXIT_FAILED : EXIT_OK;
87
88      } else {
89
90        Job job = null;
91        try {
92          job = HiveServiceLocator.Instance.CallHiveService(x => x.GetJob(jobId));
93        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
94          Console.WriteLine("?");
95          return EXIT_NOTFOUND;
96        }
97        Console.WriteLine(FormatStateInfo(job.JobCount, job.FinishedCount, job.CalculatingCount));
98        return EXIT_OK;
99      }
100    }
101
102    private static int CheckStatusAll(Options opt) {
103      List<Job> jobs = null;
104      try {
105        jobs = HiveServiceLocator.Instance.CallHiveService(x => x.GetJobs());
106      } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
107        Console.WriteLine("?");
108        return EXIT_NOTFOUND;
109      }
110
111      if (opt.Verbose) {
112        List<LightweightTask> tasks = null;
113        int totFinished, totFailed, totCalculating, totPaused, total;
114        totFinished = totFailed = totCalculating = totPaused = total = 0;
115        try {
116
117          foreach (var j in jobs) {
118            tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(j.Id));
119
120            int finished, failed, calculating, paused;
121            GetStats(tasks, out finished, out failed, out calculating, out paused);
122            totFinished += finished;
123            totFailed += failed;
124            totCalculating += calculating;
125            totPaused += paused;
126            total += tasks.Count;
127
128            Console.WriteLine(FormatStateInfo(tasks.Count, finished, failed, calculating, paused) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
129          }
130        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
131          Console.WriteLine("?");
132          return EXIT_NOTFOUND;
133        }
134        Console.WriteLine();
135        Console.WriteLine(FormatStateInfo(total, totFinished, totFailed, totCalculating, totPaused) + " | ALL");
136        return totFailed > 0 ? EXIT_FAILED : EXIT_OK;
137      } else {
138        int totFinished, totCalculating, total;
139        totFinished = totCalculating = total = 0;
140        foreach (var j in jobs) {
141          Console.WriteLine(FormatStateInfo(j.JobCount, j.FinishedCount, j.CalculatingCount) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
142          totFinished += j.FinishedCount;
143          totCalculating += j.CalculatingCount;
144          total += j.JobCount;
145        }
146        Console.WriteLine();
147        Console.WriteLine(FormatStateInfo(total, totFinished, totCalculating) + " | ALL");
148        return EXIT_OK;
149      }
150    }
151
152    private static void GetStats(List<LightweightTask> tasks, out int completed, out int failed, out int running, out int paused) {
153      completed = tasks.Count(x => x.State == TaskState.Finished);
154      failed = tasks.Count(x => x.State == TaskState.Failed || x.State == TaskState.Aborted);
155      running = tasks.Count(x => x.State == TaskState.Calculating || x.State == TaskState.Transferring
156|| x.State == TaskState.Waiting);
157      paused = tasks.Count(x => x.State == TaskState.Paused);
158    }
159
160    private static string FormatStateInfo(int total, int finished, int calculating) {
161      var state = finished == total ? "DONE" : "CALC";
162      return string.Join(" ", new object[] { state
163        , "CALC=" + calculating.ToString().PadRight(4)
164        , "DONE=" + finished.ToString().PadRight(4) });
165    }
166
167    private static string FormatStateInfo(int total, int finished, int failed, int calculating, int paused) {
168      var state = finished == total ? "DONE" : failed > 0 ? "FAIL" : calculating > 0 ? "CALC" : paused > 0 ? "PAUS" : "NDEF";
169      return string.Join(" ", new object[] { state
170        , "CALC=" + calculating.ToString().PadRight(4)
171        , "DONE=" + finished.ToString().PadRight(4)
172        , "FAIL=" + failed.ToString().PadRight(4)
173        , "PAUS=" + paused.ToString().PadRight(4) });
174    }
175
176    #region Helpers
177    private static string Truncate(string text, int length) {
178      if (text.Length <= length) return text;
179      return text.Substring(0, length - 3) + "...";
180    }
181
182    private static string ReadPassword() {
183      ConsoleKeyInfo info;
184      var sb = new StringBuilder();
185      var index = 0;
186      while (true) {
187        info = Console.ReadKey(true);
188        if (info.Key == ConsoleKey.Escape) Environment.Exit(EXIT_ERR);
189        if (info.Key == ConsoleKey.Enter) break;
190
191        if (info.Key == ConsoleKey.Backspace) {
192          if (index > 0) {
193            sb.Remove(index - 1, 1);
194            //else sb.Remove(index, 1);
195            index--;
196            Console.CursorLeft--;
197            for (var i = index; i < sb.Length; i++)
198              Console.Write('*');
199            Console.Write(' ');
200            Console.CursorLeft -= sb.Length - index + 1;
201          } else Console.Write((char)7);
202        } else if (info.Key == ConsoleKey.LeftArrow) {
203          if (index > 0) {
204            index--;
205            Console.CursorLeft--;
206          }
207        } else if (info.Key == ConsoleKey.RightArrow) {
208          if (index < sb.Length) {
209            index++;
210            Console.CursorLeft++;
211          }
212        } else if (info.KeyChar < 32) {
213          Console.Write((char)7);
214        } else {
215          if (sb.Length == index)
216            sb.Append(info.KeyChar);
217          else sb.Insert(index, info.KeyChar);
218          for (var i = index; i < sb.Length; i++)
219            Console.Write('*');
220          index++;
221          Console.CursorLeft -= sb.Length - index;
222        }
223      }
224      Console.WriteLine();
225      return sb.ToString();
226    }
227    #endregion
228  }
229}
Note: See TracBrowser for help on using the repository browser.