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

Last change on this file since 15921 was 15921, checked in by abeham, 4 years ago

#2829:

  • Updated project's references
  • Renamed solution file
  • Added HiveStatus console application to query the job status
File size: 8.5 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_WAITING = 2;
34    public const int EXIT_FAILED = 3;
35    public const int EXIT_NOTFOUND = 4;
36
37    static void Main(string[] args) {
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 opt) {
48      if (!string.IsNullOrEmpty(opt.Username)) {
49        HiveServiceLocator.Instance.Username = opt.Username;
50        var password = opt.Password;
51        if (string.IsNullOrEmpty(password)) {
52          Console.Error.Write("Password for " + opt.Username + ": ");
53          password = ReadPassword();
54        }
55        HiveServiceLocator.Instance.Password = password;
56      }
57      return CheckStatus(opt);
58    }
59
60    private static string ReadPassword() {
61      ConsoleKeyInfo info;
62      var sb = new StringBuilder();
63      var index = 0;
64      while (true) {
65        info = Console.ReadKey(true);
66        if (info.Key == ConsoleKey.Escape) Environment.Exit(EXIT_ERR);
67        if (info.Key == ConsoleKey.Enter) break;
68
69        if (info.Key == ConsoleKey.Backspace) {
70          if (index > 0) {
71            sb.Remove(index - 1, 1);
72            //else sb.Remove(index, 1);
73            index--;
74            Console.CursorLeft--;
75            for (var i = index; i < sb.Length; i++)
76              Console.Write('*');
77            Console.Write(' ');
78            Console.CursorLeft -= sb.Length - index + 1;
79          } else Console.Write((char)7);
80        } else if (info.Key == ConsoleKey.LeftArrow) {
81          if (index > 0) {
82            index--;
83            Console.CursorLeft--;
84          }
85        } else if (info.Key == ConsoleKey.RightArrow) {
86          if (index < sb.Length) {
87            index++;
88            Console.CursorLeft++;
89          }
90        } else if (info.KeyChar < 32) {
91          Console.Write((char)7);
92        } else {
93          if (sb.Length == index)
94            sb.Append(info.KeyChar);
95          else sb.Insert(index, info.KeyChar);
96          for (var i = index; i < sb.Length; i++)
97            Console.Write('*');
98          index++;
99          Console.CursorLeft -= sb.Length - index;
100        }
101      }
102      Console.WriteLine();
103      return sb.ToString();
104    }
105
106    private static int CheckStatus(Options opt) {
107      if (opt.All) return CheckStatusAll(opt);
108
109      var jobId = opt.JobId;
110
111      if (opt.Verbose) {
112
113        List<LightweightTask> tasks = null;
114        try {
115          tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(jobId));
116        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
117          Console.WriteLine("?");
118          return EXIT_NOTFOUND;
119        }
120
121        int completed, failed, running, paused;
122        GetStats(tasks, out completed, out failed, out running, out paused);
123        Console.WriteLine(FormatStateInfo(tasks.Count, completed, failed, running, paused));
124        return completed == tasks.Count ? EXIT_OK : failed > 0 ? EXIT_FAILED : EXIT_WAITING;
125
126      } else {
127
128        Job job = null;
129        try {
130          job = HiveServiceLocator.Instance.CallHiveService(x => x.GetJob(jobId));
131        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
132          Console.WriteLine("?");
133          return EXIT_NOTFOUND;
134        }
135        Console.WriteLine(FormatStateInfo(job.JobCount, job.FinishedCount, job.CalculatingCount));
136        return job.FinishedCount == job.JobCount ? EXIT_OK : EXIT_WAITING;
137      }
138    }
139
140    private static int CheckStatusAll(Options opt) {
141      List<Job> jobs = null;
142      try {
143        jobs = HiveServiceLocator.Instance.CallHiveService(x => x.GetJobs());
144      } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
145        Console.WriteLine("?");
146        return EXIT_NOTFOUND;
147      }
148
149      if (opt.Verbose) {
150        List<LightweightTask> tasks = null;
151        int totFinished, totFailed, totCalculating, totPaused, total;
152        totFinished = totFailed = totCalculating = totPaused = total = 0;
153        try {
154
155          foreach (var j in jobs) {
156            tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(j.Id));
157
158            int finished, failed, calculating, paused;
159            GetStats(tasks, out finished, out failed, out calculating, out paused);
160            totFinished += finished;
161            totFailed += failed;
162            totCalculating += calculating;
163            totPaused += paused;
164            total += tasks.Count;
165
166            Console.WriteLine(FormatStateInfo(tasks.Count, finished, failed, calculating, paused) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
167          }
168        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
169          Console.WriteLine("?");
170          return EXIT_NOTFOUND;
171        }
172        Console.WriteLine();
173        Console.WriteLine(FormatStateInfo(total, totFinished, totFailed, totCalculating, totPaused) + " | ALL");
174        return totFinished == total ? EXIT_OK : totFailed > 0 ? EXIT_FAILED : EXIT_WAITING;
175      } else {
176        int totFinished, totCalculating, total;
177        totFinished = totCalculating = total = 0;
178        foreach (var j in jobs) {
179          Console.WriteLine(FormatStateInfo(j.JobCount, j.FinishedCount, j.CalculatingCount) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
180          totFinished += j.FinishedCount;
181          totCalculating += j.CalculatingCount;
182          total += j.JobCount;
183        }
184        Console.WriteLine();
185        Console.WriteLine(FormatStateInfo(total, totFinished, totCalculating) + " | ALL");
186        return totFinished == total ? EXIT_OK : EXIT_WAITING;
187      }
188    }
189
190    private static void GetStats(List<LightweightTask> tasks, out int completed, out int failed, out int running, out int paused) {
191      completed = tasks.Count(x => x.State == TaskState.Finished);
192      failed = tasks.Count(x => x.State == TaskState.Failed || x.State == TaskState.Aborted);
193      running = tasks.Count(x => x.State == TaskState.Calculating || x.State == TaskState.Transferring
194|| x.State == TaskState.Waiting);
195      paused = tasks.Count(x => x.State == TaskState.Paused);
196    }
197
198    private static string FormatStateInfo(int total, int finished, int calculating) {
199      var state = finished == total ? "DONE" : "CALC";
200      return string.Join(" ", new object[] { state
201        , "CALC=" + calculating.ToString().PadRight(4)
202        , "DONE=" + finished.ToString().PadRight(4) });
203    }
204
205    private static string FormatStateInfo(int total, int finished, int failed, int calculating, int paused) {
206      var state = finished == total ? "DONE" : failed > 0 ? "FAIL" : calculating > 0 ? "CALC" : paused > 0 ? "PAUS" : "NDEF";
207      return string.Join(" ", new object[] { state
208        , "CALC=" + calculating.ToString().PadRight(4)
209        , "DONE=" + finished.ToString().PadRight(4)
210        , "FAIL=" + failed.ToString().PadRight(4)
211        , "PAUS=" + paused.ToString().PadRight(4) });
212    }
213
214    private static string Truncate(string text, int length) {
215      if (text.Length <= length) return text;
216      return text.Substring(0, length - 3) + "...";
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.