NTP Analyzer  0.8.2
Analyze the operation of time servers
BootstrapHostTableRender.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.Text;
26 using Ntp.Analyzer.Import;
27 
28 namespace Ntp.Analyzer.Render.Host
29 {
31  {
34  string ip,
35  DateTime now,
36  DateTime next)
37  : base(page.WebPath)
38  {
39  this.page = page;
40  this.ip = ip;
41  this.now = now;
42  this.next = next;
43 
44  columns = new List<BootstrapTableColumn>
45  {
47  new BootstrapTableColumn("Name"),
48  new BootstrapTableColumn("st", "aright"),
49  new BootstrapTableColumn("when", "aright"),
50  new BootstrapTableColumn("poll", "aright2"),
51  page.ServerType == ServerType.Ntpctl
52  ? new BootstrapTableColumn("trust", "aright2")
53  : new BootstrapTableColumn("reach", "aright2"),
54  new BootstrapTableColumn("delay", "aright"),
55  new BootstrapTableColumn("offset", "aright"),
56  new BootstrapTableColumn("jitter", "aright"),
57  new BootstrapTableColumn("iso&nbsp;"),
58  new BootstrapTableColumn("location")
59  }.ToArray();
60  }
61 
62  private readonly BootstrapTableColumn[] columns;
63  private readonly string ip;
64  private readonly DateTime next;
65  private readonly DateTime now;
66  private readonly HostPageConfiguration page;
67 
68  public override string Render()
69  {
70  return string.Empty;
71  }
72 
73  public override string RenderFooter()
74  {
75  var builder = new StringBuilder();
76 
77  builder.Append(MakeSpacer());
78 
79  builder.Append("<tr>");
80 
81  foreach (var column in columns)
82  {
83  builder.Append(column.FooterString);
84  }
85 
86  builder.AppendLine("</tr>");
87  builder.AppendLine("</table>");
88  builder.AppendLine("</div>");
89 
90  if (page.PoolMember)
91  {
92  builder.Append(@"<p class=""pool""><a class=""btn btn-lg btn-primary"" ");
93  builder.Append($@"href=""{GetPoolMemberLink()}"">View info on pool.ntp.org &raquo;</a></p>");
94  builder.AppendLine();
95  }
96 
97  builder.AppendLine("</div>");
98  builder.AppendLine("</div>");
99 
100  return builder.ToString();
101  }
102 
103  public override string RenderHead()
104  {
105  var builder = new StringBuilder();
106 
107  builder.AppendLine(@"<div class=""container theme-showcase"">");
108  builder.AppendLine(@"<div class=""jumbotron"">");
109  builder.Append("<h1>");
110  builder.Append(page.Title);
111  builder.AppendLine("</h1>");
112 
113  builder.Append("<p>");
114  builder.Append(now.ToLongDateString() + " ");
115  if (page.ShowUtc == DateTimeKind.Utc)
116  {
117  builder.Append(TimeZoneInfo.Utc.StandardName);
118  }
119  else
120  {
121  builder.Append(TimeZoneInfo.Local.IsDaylightSavingTime(now)
122  ? TimeZoneInfo.Local.DaylightName
123  : TimeZoneInfo.Local.StandardName);
124  }
125  builder.Append(" " + now.ToShortTimeString() + ". ");
126  builder.Append("Next refresh scheduled at ");
127  builder.Append(next.ToShortTimeString());
128  builder.AppendLine(".</p>");
129 
130  builder.AppendLine(@"<div class=""table-responsive"">");
131  builder.Append("<table><tr>");
132 
133  foreach (var column in columns)
134  {
135  builder.Append(column);
136  }
137 
138  builder.AppendLine("</tr>");
139 
140  builder.Append(MakeSpacer());
141 
142  return builder.ToString();
143  }
144 
145  private string GetPoolMemberLink()
146  {
147  return "http://www.pool.ntp.org/scores/" + ip;
148  }
149 
150  private string MakeSpacer()
151  {
152  var builder = new StringBuilder();
153 
154  builder.Append("<tr>");
155 
156  foreach (var column in columns)
157  {
158  builder.Append(column.SpacerString);
159  }
160 
161  builder.AppendLine("</tr>");
162 
163  return builder.ToString();
164  }
165 
166  private sealed class BootstrapTableColumn
167  {
168  public BootstrapTableColumn(string name, string cssClass)
169  {
170  this.name = name;
171  this.cssClass = cssClass;
172  }
173 
174  public BootstrapTableColumn(string name)
175  {
176  this.name = name;
177  cssClass = null;
178  }
179 
181  {
182  name = "&nbsp;";
183  cssClass = null;
184  }
185 
186  private readonly string cssClass;
187  private readonly string name;
188 
189  public string SpacerString =>
190  $@"<td class=""spc{(cssClass != null ? " " + cssClass : string.Empty)}"">&nbsp;</td>";
191 
192  public string FooterString =>
193  $@"<td class=""but{(cssClass != null ? " " + cssClass : string.Empty)}"">&nbsp;</td>";
194 
195  public override string ToString()
196  {
197  return cssClass != null
198  ? $@"<th class=""{cssClass}"">{name}</th>"
199  : $@"<th>{name}</th>";
200  }
201  }
202  }
203 }
BootstrapHostTableRender(HostPageConfiguration page, string ip, DateTime now, DateTime next)
ServerType ServerType
Gets the type of the server in the HostSubConfiguration.
Configuration for a HTML page showing status and graphs for a hosted NTP server.