forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunReportsService.cs
More file actions
164 lines (143 loc) · 4.56 KB
/
RunReportsService.cs
File metadata and controls
164 lines (143 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.Serialization;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.OrmLite;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.WebHost.Endpoints.Support;
namespace MasterHost
{
[DataContract]
[Description("RunType=[all|pathsonly|portsonly], Filter=[PathHostNameFilter|PortFilter], e.g [handler.wildcard35|5000]")]
[RestService("/runreports")]
[RestService("/runreports/{RunType}")]
[RestService("/runreports/{RunType}/{Filter}")]
public class RunReports
{
[DataMember]
public string RunType { get; set; }
[DataMember]
public string Filter { get; set; }
}
[DataContract]
public class RunReportsResponse : IHasResponseStatus
{
public RunReportsResponse()
{
this.ResponseStatus = new ResponseStatus();
}
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
public class RunReportsService : RestServiceBase<RunReports>
{
public AppConfig Config { get; set; }
public IDbConnectionFactory DbFactory { get; set; }
public override object OnGet(RunReports request)
{
if (!request.RunType.IsNullOrEmpty())
{
var runType = request.RunType.ToLower();
if (runType != "all" && runType != "pathsonly" && runType != "portsonly")
throw new ArgumentException("RunType=[all|pathsonly|portsonly]", "RunType");
var runPaths = runType == "all" || runType == "pathsonly";
var runPorts = runType == "all" || runType == "portsonly";
for (var i = 0; i < Config.HandlerHosts.Count; i++)
{
var hostPath = Config.HandlerHosts[i];
var hostName = Config.HandlerHostNames[i];
if (!request.Filter.IsNullOrEmpty() && hostPath != request.Filter) continue;
var isPort = hostPath.Contains(":");
var isPath = !isPort;
if (isPort && !runPorts) continue;
if (isPath && !runPaths) continue;
var baseUrl = Config.RunOnBaseUrl + hostPath;
DoRequestInfo(new Report
{
Id = hostName + "-" + hostPath,
HostEnvironment = hostName,
BaseUrl = baseUrl,
},
Config.TestPaths);
}
}
return base.ResolveService<ReportsService>().Get(
new Reports { FilterHost = request.Filter });
}
private void DoRequestInfo(Report report, IEnumerable<string> testPaths)
{
report.LastModified = DateTime.UtcNow;
var restClient = new JsonServiceClient(report.BaseUrl);
report.MaxStatusCode = 0;
foreach (var testPath in testPaths)
{
var test = new ReportTest { RequestPath = testPath };
report.Tests.Add(test);
try
{
if (testPath.Contains("_requestinfo"))
{
var requestInfo = restClient.Get<RequestInfoResponse>(testPath);
if (report.ServiceName == null)
{
report.Host = requestInfo.Host;
report.ServiceName = requestInfo.ServiceName;
report.UserHostAddress = requestInfo.UserHostAddress;
}
test.StatusCode = 200;
test.AbsoluteUri = requestInfo.AbsoluteUri;
test.PathInfo = requestInfo.PathInfo;
test.RawUrl = requestInfo.RawUrl;
test.ResponseContentType = requestInfo.ResponseContentType;
}
else
{
var webReq = (HttpWebRequest)WebRequest.Create(report.BaseUrl + testPath);
webReq.Accept = ContentType.Json;
var webRes = (HttpWebResponse)webReq.GetResponse();
test.ResponseContentType = webRes.ContentType;
test.StatusCode = (int)webRes.StatusCode;
}
report.MaxStatusCode = Math.Max(report.MaxStatusCode, (int)HttpStatusCode.OK);
}
catch (WebServiceException webEx)
{
report.MaxStatusCode = Math.Max(report.MaxStatusCode, webEx.StatusCode);
test.StatusCode = webEx.StatusCode;
test.ErrorCode = webEx.ErrorCode;
test.ErrorMessage = webEx.ErrorMessage;
//report.StackTrace = webEx.ServerStackTrace;
}
catch (Exception ex)
{
var webEx = ex as WebException;
if (webEx != null)
{
report.MaxStatusCode = Math.Max(report.MaxStatusCode,
(int)webEx.Status == default(int) ? 600 : (int)webEx.Status);
test.ErrorCode = ex.GetType().Name;
test.ErrorMessage = ex.Message;
}
else
{
report.MaxStatusCode = Math.Max(report.MaxStatusCode, 600);
test.ErrorCode = ex.GetType().Name;
test.ErrorMessage = ex.Message;
}
//report.StackTrace = ex.StackTrace;
}
finally
{
DbFactory.Exec(dbCmd => dbCmd.Save(report));
}
}
}
}
}