/
AuthenticatedServices.cs
369 lines (295 loc) · 12.9 KB
/
AuthenticatedServices.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BenchmarksAnalyzer.ServiceModel;
using BenchmarksAnalyzer.ServiceModel.Types;
using Ionic.Zip;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.OrmLite;
namespace BenchmarksAnalyzer.ServiceInterface
{
[Authenticate]
public class AuthenticatedServices : Service
{
public IUserAuthRepository UserAuthRepository { get; set; }
private int GetSignedInUserId()
{
var authSession = base.GetSession();
if (authSession == null || authSession.UserAuthId == null)
throw new UnauthorizedAccessException("Must be signed in to use this service");
return int.Parse(authSession.UserAuthId);
}
public UserInfo Get(MyInfo request)
{
var userId = GetSignedInUserId();
var userAuth = UserAuthRepository.GetUserAuth(userId.ToString());
var userInfo = userAuth.ConvertTo<UserInfo>();
userInfo.UserAuthId = userId;
if (userInfo.DisplayName == null)
userInfo.DisplayName = userAuth.UserName
?? userAuth.FullName
?? "{0} {1}".Fmt(userAuth.FirstName, userAuth.LastName);
if (userInfo.ProfileUrl64 == null)
userInfo.ProfileUrl64 = this.SessionAs<UserSession>().ProfileUrl64
?? "/Content/img/no-profile-pic-64.png";
return userInfo;
}
public object Get(FindTestPlans request)
{
var userId = GetSignedInUserId();
return Db.Select<TestPlan>(q => q.UserAuthId == userId)
.OrderByDescending(x => x.Id);
}
public object Get(FindTestRuns request)
{
var userId = GetSignedInUserId();
var planId = request.TestPlanId;
var testRuns = Db.Select<TestRun>(q =>
q.UserAuthId == userId && q.TestPlanId == planId)
.OrderByDescending(x => x.Id);
var testRunCounts = Db.Dictionary<int, int>(
Db.From<TestResult>()
.Select(x => new { x.TestRunId, count = Sql.Count("*") })
.Where(q => q.UserAuthId == userId && q.TestPlanId == planId)
.GroupBy(x => x.TestRunId));
testRuns
.Where(x => testRunCounts.ContainsKey(x.Id))
.Each(x => x.TestResultsCount = testRunCounts[x.Id]);
return testRuns;
}
public object Post(CreateTestPlan request)
{
var plan = request.ConvertTo<TestPlan>();
plan.CreatedDate = DateTime.UtcNow;
if (plan.Name.IsNullOrEmpty())
throw new ArgumentNullException("Name");
if (plan.Slug.IsNullOrEmpty())
plan.Slug = plan.Name.SafeVarName().ToLower();
if (Db.Count<TestPlan>(q => q.Slug == plan.Slug) > 0)
throw new ArgumentException("Slug already exists", "Slug");
plan.UserAuthId = GetSignedInUserId();
Db.Save(plan);
return HttpResult.SoftRedirect(new EditTestPlan { Id = plan.Id }.ToGetUrl(), plan);
}
public object Any(UpdateTestPlanLabels request)
{
var testPlan = GetTestPlan(request.Id);
testPlan.ServerLabels = request.ServerLabels.ParseKeyValueText(delimiter:" ");
testPlan.TestLabels = request.TestLabels.ParseKeyValueText(delimiter: " ");
Db.Save(testPlan);
return AddMissingLabels(testPlan);
}
public object Any(DeleteTestPlan request)
{
var testPlan = GetTestPlan(request.Id);
using (var trans = Db.OpenTransaction())
{
var userId = GetSignedInUserId();
Db.Delete<TestResult>(q => q.UserAuthId == userId && q.TestPlanId == testPlan.Id);
Db.Delete<TestRun>(q => q.UserAuthId == userId && q.TestPlanId == testPlan.Id);
Db.Delete<TestPlan>(q => q.UserAuthId == userId && q.Id == testPlan.Id);
trans.Commit();
}
return HttpResult.SoftRedirect("/");
}
private TestPlan GetTestPlan(int testPlanId)
{
var testPlan = Db.SingleById<TestPlan>(testPlanId);
if (testPlan == null)
throw HttpError.NotFound("Test Plan {0} does not exist".Fmt(testPlanId));
return testPlan;
}
//[AddHeader(ContentType = MimeTypes.Html)]
public object Any(EditTestPlan request)
{
//If no test run exists, create one then redirect to it
var testRun = (request.TestRunId != null
? Db.SingleById<TestRun>(request.TestRunId.Value)
: null)
?? GetLatestOrCreateTestRun(GetSignedInUserId(), request.Id);
if (request.TestRunId == null)
{
request.TestRunId = testRun.Id;
return HttpResult.Redirect(request.ToGetUrl());
}
using (var service = ResolveService<WebServices>())
{
var testPlan = service.Any(request.ConvertTo<GetTestPlan>());
var response = AddMissingLabels(testPlan).ConvertTo<EditTestPlanResponse>();
response.TestRunId = request.TestRunId;
return response;
}
}
private TestPlan AddMissingLabels(TestPlan testPlan)
{
var distinctResults = Db.Select(Db.From<TestResult>()
.SelectDistinct(x => new { x.Hostname, x.Port, x.RequestPath })
.Where(q => q.TestPlanId == testPlan.Id));
if (testPlan.ServerLabels == null)
testPlan.ServerLabels = new Dictionary<string, string>();
if (testPlan.TestLabels == null)
testPlan.TestLabels = new Dictionary<string, string>();
var serverLabels = new HashSet<string>(distinctResults.Select(x => x.Hostname + ":" + x.Port));
serverLabels.Each(x => {
if (!testPlan.ServerLabels.ContainsKey(x))
testPlan.ServerLabels[x] = "";
});
var testLabels = new HashSet<string>(distinctResults.Select(x => x.RequestPath));
testLabels.Each(x =>
{
if (!testPlan.TestLabels.ContainsKey(x))
testPlan.TestLabels[x] = "";
});
return testPlan;
}
public object Post(CreateTestRun request)
{
var testRun = CreateTestRun(request.TestPlanId, request.SeriesId);
return HttpResult.SoftRedirect(
new EditTestPlan { Id = request.TestPlanId, TestRunId = testRun.Id }.ToGetUrl(),
testRun);
}
public object Any(DeleteTestRun request)
{
var testRun = Db.SingleById<TestRun>(request.Id);
if (testRun == null)
throw HttpError.NotFound("Test Run {0} does not exist".Fmt(request.Id));
using (var trans = Db.OpenTransaction())
{
var userId = GetSignedInUserId();
Db.Delete<TestResult>(q => q.UserAuthId == userId && q.TestRunId == request.Id);
Db.Delete<TestRun>(q => q.UserAuthId == userId && q.Id == request.Id);
trans.Commit();
}
return HttpResult.SoftRedirect(new EditTestPlan { Id = testRun.TestPlanId }.ToGetUrl());
}
public object Post(AddTestResults request)
{
var testPlanId = request.TestPlanId;
var testRunId = request.TestRunId.GetValueOrDefault();
if (request.Contents.IsNullOrEmpty())
throw new ArgumentNullException("Contents");
var testResult = request.Contents.ToTestResult();
if (testResult.Software.IsNullOrEmpty())
throw new ArgumentException("Invalid Apache Benchmark File", "Contents");
var results = new[] { testResult }.ToList();
var testRun = AddTestResults(GetSignedInUserId(), testPlanId, testRunId, results);
return new AddTestResultsResponse
{
TestRun = testRun,
Results = results,
};
}
public object Post(UploadTestResults request)
{
var testPlanId = request.TestPlanId;
var testRunId = request.TestRunId.GetValueOrDefault();
TestRun testRun = null;
if (base.Request.Files.Length == 0)
throw new ArgumentException("No Test Result supplied");
var processedResults = new List<TestResult>();
var newResults = new List<TestResult>();
foreach (var httpFile in base.Request.Files)
{
if (httpFile.FileName.ToLower().EndsWith(".zip"))
{
using (var zip = ZipFile.Read(httpFile.InputStream))
{
var zipResults = new List<TestResult>();
foreach (var zipEntry in zip)
{
using (var ms = new MemoryStream())
{
zipEntry.Extract(ms);
var bytes = ms.ToArray();
var result = new MemoryStream(bytes).ToTestResult();
zipResults.Add(result);
}
}
if (request.CreateNewTestRuns)
{
var seriesId = httpFile.FileName.Substring(0, httpFile.FileName.Length - ".zip".Length);
testRun = CreateTestRun(testPlanId, seriesId);
testRun = AddTestResults(GetSignedInUserId(), testPlanId, testRun.Id, zipResults);
processedResults.AddRange(zipResults);
}
else
{
newResults.AddRange(zipResults);
}
}
}
else
{
var result = httpFile.InputStream.ToTestResult();
newResults.Add(result);
}
}
if (newResults.Count > 0)
{
testRun = AddTestResults(GetSignedInUserId(), testPlanId, testRunId, newResults);
processedResults.AddRange(newResults);
}
return new UploadTestResultsResponse
{
success = true,
TestRun = testRun,
Results = processedResults,
};
}
private TestRun AddTestResults(int userId, int testPlanId, int testRunId, List<TestResult> testResults)
{
if (testPlanId == default(int))
throw new ArgumentNullException("TestPlanId");
var testPlan = Db.SingleById<TestPlan>(testPlanId);
if (testPlan == null)
throw HttpError.NotFound("Test Plan {0} does not exist".Fmt(testPlanId));
if (testPlan.UserAuthId != userId)
throw HttpError.Unauthorized("You do not have permission to write to this plan");
TestRun testRun = null;
if (testRunId > 0)
{
testRun = Db.Single<TestRun>(q =>
q.Id == testRunId
&& q.UserAuthId == userId);
if (testRun == null)
throw HttpError.NotFound("Test Run {0} does not exist".Fmt(testRunId));
}
else
{
testRun = GetLatestOrCreateTestRun(userId, testPlanId);
}
testResults.RemoveAll(x => x.Hostname == null || x.Port == 0); //clear bogus records
testResults.Each(x =>
{
x.UserAuthId = userId;
x.TestPlanId = testPlanId;
x.TestRunId = testRun.Id;
});
Db.SaveAll(testResults);
return testRun;
}
private TestRun GetLatestOrCreateTestRun(int userId, int testPlanId)
{
var testRun = Db.Single(Db.From<TestRun>()
.Where(x => x.UserAuthId == userId &&
x.TestPlanId == testPlanId)
.OrderByDescending(x => x.Id));
return testRun ?? CreateTestRun(testPlanId);
}
private TestRun CreateTestRun(int planId, string seriesId = null)
{
var testRun = new TestRun
{
UserAuthId = GetSignedInUserId(),
TestPlanId = planId,
SeriesId = seriesId ?? "Created on {0}".Fmt(DateTime.UtcNow.ToString("yyyy-MM-dd")),
CreatedDate = DateTime.UtcNow,
};
Db.Save(testRun);
return testRun;
}
}
}