NTP Analyzer  0.8.2
Analyze the operation of time servers
Billboard.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Globalization;
25 using System.Linq;
26 using System.Text;
27 using Ntp.Common.Process;
28 
29 namespace Ntp.Analyzer.Monitor.Server
30 {
31  public static class Billboard
32  {
33  public static string Jobs(IEnumerable<Job> jobs)
34  {
35  var builder = new StringBuilder();
36 
37  builder.Append("ID".PadRight(3));
38  builder.Append("Act".PadRight(4));
39  builder.Append("Job".PadRight(15));
40  builder.Append("Que".PadLeft(5));
41  builder.Append("Ini".PadLeft(5));
42  builder.Append("Fix".PadLeft(5));
43  builder.Append("Freq".PadLeft(6));
44  builder.Append("Prio".PadLeft(5));
45  builder.Append("Runs".PadLeft(6));
46  builder.Append(" ");
47  builder.Append("Description");
48  builder.AppendLine();
49 
50  const int size = 3 + 4 + 15 + 5 + 5 + 5 + 6 + 5 + 6 + 3 + 25;
51 
52  builder.AppendLine(string.Empty.PadLeft(size, '-'));
53 
54  IEnumerable<Job> orderedJobs = jobs.OrderBy(j => j.Description.Priority);
55 
56  foreach (Job job in orderedJobs)
57  {
58  builder.Append(job.JobId.ToString("00").PadRight(3));
59  builder.Append(job.Running ? " * " : " ");
60  builder.Append(
61  string.IsNullOrWhiteSpace(job.Description.Name)
62  ? "Unnamed".PadRight(15)
63  : job.Description.Name.
64  Substring(0, job.Description.Name.Length > 15 ? 15 : job.Description.Name.Length).
65  PadRight(15)
66  );
67  builder.Append((job.Queued ? " 1" : " 0").PadLeft(5));
68  builder.Append((job.Schedule.InitialRun ? " 1" : " 0").PadLeft(5));
69  builder.Append((job.Schedule.FixedRun ? " 1" : " 0").PadLeft(5));
70  builder.Append(job.Schedule.Frequency.ToString(CultureInfo.InvariantCulture).PadLeft(6));
71  builder.Append(job.Description.Priority.ToString(CultureInfo.InvariantCulture).PadLeft(5));
72  builder.Append(job.RunCount.ToString(CultureInfo.InvariantCulture).PadLeft(6));
73  builder.Append(" ");
74  builder.Append(job.Description.JobType);
75  builder.AppendLine();
76  }
77 
78  builder.AppendLine(string.Empty.PadLeft(size, '-'));
79 
80  return builder.ToString();
81  }
82 
83  public static string Proc(IEnumerable<Job> jobs)
84  {
85  var builder = new StringBuilder();
86 
87  builder.Append("ID".PadRight(4));
88  builder.Append("Job".PadRight(16));
89  builder.Append("Ini".PadLeft(5));
90  builder.Append("Fix".PadLeft(5));
91  builder.Append("Freq".PadLeft(7));
92  builder.Append("Prio".PadLeft(6));
93  builder.Append("Runs".PadLeft(5));
94  builder.Append(" ");
95  builder.Append("State".PadRight(12));
96  builder.Append("Time".PadRight(15));
97  builder.AppendLine();
98 
99  const int size = 4 + 16 + 5 + 5 + 7 + 6 + 5 + 3 + 12 + 15 + 2;
100 
101  builder.AppendLine(string.Empty.PadLeft(size, '-'));
102 
103  IEnumerable<Job> orderedJobs = jobs.OrderBy(j => j.JobId);
104 
105  foreach (Job job in orderedJobs)
106  {
107  builder.Append(job.JobId.ToString("00").PadRight(4));
108  builder.Append(
109  job.Description.Name.Substring(0,
110  job.Description.Name.Length > 15 ? 15 : job.Description.Name.Length).PadRight(15));
111  builder.Append((job.Schedule.InitialRun ? " 1" : " 0").PadLeft(5));
112  builder.Append((job.Schedule.FixedRun ? " 1" : " 0").PadLeft(5));
113  builder.Append(job.Schedule.Frequency.ToString(CultureInfo.InvariantCulture).PadLeft(7));
114  builder.Append(job.Description.Priority.ToString(CultureInfo.InvariantCulture).PadLeft(6));
115  builder.Append(job.RunCount.ToString(CultureInfo.InvariantCulture).PadLeft(6));
116  builder.Append(" ");
117  builder.Append(job.State.PadRight(12));
118  builder.Append(job.TotalRuntime.PadLeft(15).Substring(0, 15));
119  builder.AppendLine();
120  }
121 
122  builder.AppendLine(string.Empty.PadLeft(size, '-'));
123 
124  return builder.ToString();
125  }
126 
127  public static string Running(IEnumerable<Job> jobs)
128  {
129  var list = jobs.ToList();
130  if (list.Count(j => j.Running) == 0)
131  {
132  return "No jobs are currently running.";
133  }
134 
135  var builder = new StringBuilder();
136 
137  builder.Append("ID".PadRight(4));
138  builder.Append("Job".PadRight(16));
139  builder.Append("Ini".PadLeft(5));
140  builder.Append("Fix".PadLeft(5));
141  builder.Append("Freq".PadLeft(7));
142  builder.Append("Prio".PadLeft(6));
143  builder.Append("Runs".PadLeft(5));
144  builder.Append(" ");
145  builder.Append("State".PadRight(12));
146  builder.Append("Time".PadRight(15));
147  builder.AppendLine();
148 
149  const int size = 4 + 16 + 5 + 5 + 7 + 6 + 5 + 3 + 12 + 15 + 2;
150 
151  builder.AppendLine(string.Empty.PadLeft(size, '-'));
152 
153  IEnumerable<Job> orderedJobs = list.Where(j => j.Running).OrderBy(j => j.JobId);
154 
155  foreach (Job job in orderedJobs)
156  {
157  builder.Append(job.JobId.ToString("00").PadRight(4));
158  builder.Append(
159  job.Description.Name.Substring(0,
160  job.Description.Name.Length > 15 ? 15 : job.Description.Name.Length).PadRight(15));
161  builder.Append((job.Schedule.InitialRun ? " 1" : " 0").PadLeft(5));
162  builder.Append((job.Schedule.FixedRun ? " 1" : " 0").PadLeft(5));
163  builder.Append(job.Schedule.Frequency.ToString(CultureInfo.InvariantCulture).PadLeft(7));
164  builder.Append(job.Description.Priority.ToString(CultureInfo.InvariantCulture).PadLeft(6));
165  builder.Append(job.RunCount.ToString(CultureInfo.InvariantCulture).PadLeft(6));
166  builder.Append(" ");
167  builder.Append(job.State.PadRight(12));
168  builder.Append(job.Runtime.PadLeft(15).Substring(0, 15));
169  builder.AppendLine();
170  }
171 
172  builder.AppendLine(string.Empty.PadLeft(size, '-'));
173 
174  return builder.ToString();
175  }
176 
177  public static string Schedule(IEnumerable<ScheduledJob> jobs)
178  {
179  var builder = new StringBuilder();
180 
181  builder.AppendLine("Server time: " + DateTime.Now.ToLongTimeString());
182  builder.AppendLine();
183 
184  builder.Append("ID".PadRight(4));
185  builder.Append("Job".PadRight(15));
186  builder.Append("Ini".PadLeft(4));
187  builder.Append("Fix".PadLeft(5));
188  builder.Append("Freq".PadLeft(6));
189  builder.Append("Prio".PadLeft(5));
190  builder.Append("Runs".PadLeft(6));
191  builder.Append(" ");
192  builder.Append("Next".PadRight(8));
193  builder.AppendLine();
194 
195  const int size = 4 + 15 + 4 + 5 + 6 + 5 + 6 + 3 + 8 + 2;
196 
197  builder.AppendLine(string.Empty.PadLeft(size, '-'));
198 
199  IEnumerable<ScheduledJob> orderedJobs = jobs.OrderBy(j => j.Run);
200 
201  foreach (ScheduledJob job in orderedJobs)
202  {
203  builder.Append(job.Job.JobId.ToString("00").PadRight(4));
204  builder.Append(
205  job.Job.Description.Name.Substring(0,
206  job.Job.Description.Name.Length > 15 ? 15 : job.Job.Description.Name.Length).PadRight(15));
207  builder.Append((job.Job.Schedule.InitialRun ? " 1" : " 0").PadLeft(4));
208  builder.Append((job.Job.Schedule.FixedRun ? " 1" : " 0").PadLeft(5));
209  builder.Append(job.Job.Schedule.Frequency.ToString(CultureInfo.InvariantCulture).PadLeft(6));
210  builder.Append(job.Job.Description.Priority.ToString(CultureInfo.InvariantCulture).PadLeft(5));
211  builder.Append(job.Job.RunCount.ToString(CultureInfo.InvariantCulture).PadLeft(6));
212  builder.Append(" ");
213  builder.Append(job.Run.ToLongTimeString().PadLeft(8));
214  builder.AppendLine();
215  }
216 
217  builder.AppendLine(string.Empty.PadLeft(size, '-'));
218 
219  return builder.ToString();
220  }
221  }
222 }
bool Queued
Gets or sets a value indicating whether this Job is queued for run.
Definition: Job.cs:77
abstract int Priority
Gets the priority to use when scheduling jobs.
static string Proc(IEnumerable< Job > jobs)
Definition: Billboard.cs:83
DateTime Run
Gets the time of planned execution.
Definition: ScheduledJob.cs:52
Job Job
Gets the job to execute.
Definition: ScheduledJob.cs:46
string State
Gets the current state of this Job.
Definition: Job.cs:108
bool InitialRun
Gets a value indicating whether this JobScheduleDescription should do an initial run upon first call...
static string Schedule(IEnumerable< ScheduledJob > jobs)
Definition: Billboard.cs:177
JobScheduleDescription Schedule
Gets the schedule.
Definition: Job.cs:65
static string Jobs(IEnumerable< Job > jobs)
Definition: Billboard.cs:33
abstract string JobType
Gets the type of the job as text.
int RunCount
Gets the number of times this Job have been executed.
Definition: Job.cs:101
A job which have been scheduled for execution.
Definition: ScheduledJob.cs:29
string Runtime
Gets the time this Job have currently been running.
Definition: Job.cs:124
int Frequency
Gets the schedule frequency in minutes.
bool Running
Gets or sets a value indicating whether this Job is running.
Definition: Job.cs:89
string TotalRuntime
Gets the total time this Job have been running.
Definition: Job.cs:130
bool FixedRun
Gets a value indicating whether this JobScheduleDescription has a fixed schedule frequency, eg. cannot be moved.
static string Running(IEnumerable< Job > jobs)
Definition: Billboard.cs:127
int JobId
Gets the job identifier.
Definition: Job.cs:59
JobDescription Description
Gets the description.
Definition: Job.cs:71