-
Notifications
You must be signed in to change notification settings - Fork 25
/
BuildHistoryCharts.fs
302 lines (254 loc) · 10.5 KB
/
BuildHistoryCharts.fs
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
module BuildStats.BuildHistoryCharts
open System
open System.Net
open Microsoft.FSharp.Core.Option
open Newtonsoft.Json.Linq
open Giraffe
open Giraffe.Common
open BuildStats.Common
// -------------------------------------------
// Common Types and Functions
// -------------------------------------------
type BuildStatus =
| Success
| Failed
| Cancelled
| Pending
| Unkown
type Build =
{
Id : int
BuildNumber : int
TimeTaken : TimeSpan
Status : BuildStatus
Branch : string
FromPullRequest : bool
}
let pullRequestFilter (inclFromPullRequest : bool)
(build : Build) =
inclFromPullRequest || not build.FromPullRequest
let getTimeTaken (started : Nullable<DateTime>)
(finished : Nullable<DateTime>) =
match started.HasValue with
| true ->
match finished.HasValue with
| true -> finished.Value - started.Value
| false -> TimeSpan.Zero
| false -> TimeSpan.Zero
// -------------------------------------------
// Build Metrics
// -------------------------------------------
module BuildMetrics =
let longestBuildTime (builds : Build list) =
match builds.Length with
| 0 -> TimeSpan.Zero
| _ ->
builds
|> List.maxBy (fun x -> x.TimeTaken.TotalMilliseconds)
|> fun x -> x.TimeTaken
let shortestBuildTime (builds : Build list) =
match builds.Length with
| 0 -> TimeSpan.Zero
| _ ->
builds
|> List.minBy (fun x -> x.TimeTaken.TotalMilliseconds)
|> fun x -> x.TimeTaken
let averageBuildTime (builds : Build list) =
match builds.Length with
| 0 -> TimeSpan.Zero
| _ ->
builds
|> List.averageBy (fun x -> x.TimeTaken.TotalMilliseconds)
|> TimeSpan.FromMilliseconds
// -------------------------------------------
// AppVeyor
// -------------------------------------------
module AppVeyor =
let parseToJArray (json : string) =
let obj = Json.deserialize json :?> JObject
obj.Value<JArray> "builds"
let parseStatus (status : string) =
match status with
| "success" -> Success
| "failed" -> Failed
| "cancelled" -> Cancelled
| "queued" | "running" -> Pending
| _ -> Unkown
let isPullRequest (pullRequestId : string) =
isNotNull pullRequestId
let convertToBuilds (items : JArray option) =
match items with
| None -> []
| Some items ->
items
|> Seq.map (fun x ->
let started = x.Value<Nullable<DateTime>> "started"
let finished = x.Value<Nullable<DateTime>> "finished"
{
Id = x.Value<int> "buildId"
BuildNumber = x.Value<int> "buildNumber"
Status = x.Value<string> "status" |> parseStatus
Branch = x.Value<string> "branch"
FromPullRequest = x.Value<string> "pullRequestId" |> isPullRequest
TimeTaken = getTimeTaken started finished
})
|> Seq.toList
let getBuilds (account : string)
(project : string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let additionalFilter =
match branch with
| Some b -> sprintf "&branch=%s" b
| None -> ""
let url =
sprintf "https://ci.appveyor.com/api/projects/%s/%s/history?recordsNumber=%d%s"
account project (5 * buildCount) additionalFilter
let! json = Http.getJson url
return json
|> (Str.toOption
>> map parseToJArray
>> convertToBuilds)
|> List.filter (pullRequestFilter inclFromPullRequest)
|> List.truncate buildCount
}
// -------------------------------------------
// TravisCI
// -------------------------------------------
module TravisCI =
let parseToJArray (json : string) =
Json.deserialize json :?> JArray
let parseStatus (state : string)
(result : Nullable<int>) =
match state with
| "finished" ->
match result with
| x when not x.HasValue -> Cancelled
| x when x.Value = 0 -> Success
| x when x.Value = 1 -> Failed
| _ -> Unkown
| "started" | "created" -> Pending
| _ -> Unkown
let isPullRequest eventType = eventType = "pull_request"
let convertToBuilds (items : JArray option) =
match items with
| None -> []
| Some items ->
items
|> Seq.map (fun x ->
let started = x.Value<Nullable<DateTime>> "started_at"
let finished = x.Value<Nullable<DateTime>> "finished_at"
let state = x.Value<string> "state"
let result = x.Value<Nullable<int>> "result"
{
Id = x.Value<int> "id"
BuildNumber = x.Value<int> "number"
Branch = x.Value<string> "branch"
FromPullRequest = x.Value<string> "event_type" |> isPullRequest
TimeTaken = getTimeTaken started finished
Status = parseStatus state result
})
|> Seq.toList
let numberOfBuildsPerPage = 25
let rec getBatchOfBuilds (account : string)
(project : string)
(afterBuildNumber : int option)
(maxRequests : int)
(requestCount : int) =
task {
let additionalQuery =
match afterBuildNumber with
| Some x -> sprintf "?after_number=%i" x
| None -> ""
let url = sprintf "https://api.travis-ci.org/repos/%s/%s/builds%s" account project additionalQuery
let! json = Http.getJson url
let requestCount' = requestCount + 1
let batch =
json
|> (Str.toOption
>> map parseToJArray)
|> convertToBuilds
match batch with
| x when x.IsEmpty -> return []
| x when x.Length < numberOfBuildsPerPage -> return x
| x when requestCount' = maxRequests -> return x
| _ ->
let lastBuild = batch |> Seq.last
let! nextBatch = getBatchOfBuilds account project (Some lastBuild.BuildNumber) maxRequests requestCount'
return batch @ nextBatch
}
let getBuilds (account : string)
(project : string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let maxRequests = int(Math.Ceiling((float buildCount / float numberOfBuildsPerPage) * 5.0))
let! builds = getBatchOfBuilds account project None maxRequests 0
let branchFilter build =
match branch with
| Some b -> build.Branch = b
| None -> true
return builds
|> List.filter branchFilter
|> List.filter (pullRequestFilter inclFromPullRequest)
|> List.truncate buildCount
}
// -------------------------------------------
// CircleCI
// -------------------------------------------
module CircleCI =
let parseToJArray (json : string) =
Json.deserialize json :?> JArray
let parseStatus (status : string) =
match status with
| "success" | "fixed" | "no_tests" -> Success
| "failed" | "infrastructure_fail" | "timedout" -> Failed
| "canceled" | "not_run" | "retried" | "not_running" -> Cancelled
| "scheduled" | "queued" | "running" -> Pending
| _ -> Unkown
let isPullRequest (subject : string) =
isNotNull subject && subject.ToLowerInvariant().Contains("pull request")
let convertToBuilds (items : JArray option) =
match items with
| None -> []
| Some items ->
items
|> Seq.map (fun x ->
let started = x.Value<Nullable<DateTime>> "start_time"
let finished = x.Value<Nullable<DateTime>> "stop_time"
{
Id = x.Value<int> "build_num"
BuildNumber = x.Value<int> "build_num"
Status = x.Value<string> "status" |> parseStatus
Branch = x.Value<string> "branch"
FromPullRequest = x.Value<string> "subject" |> isPullRequest
TimeTaken = getTimeTaken started finished
})
|> Seq.toList
let getBuilds (account : string)
(project : string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let additionalFilter =
match branch with
| Some b -> sprintf "/tree/%s" <| WebUtility.UrlEncode b
| None -> ""
// CircleCI has a max limit of 100 items per request
// ToDo: Refactor to pull more items when buildCount is higher
let limit = min 100 (5 * buildCount)
let url =
sprintf "https://circleci.com/api/v1/project/%s/%s%s?limit=%i"
account project additionalFilter limit
let! json = Http.getJson url
return json
|> (Str.toOption
>> map parseToJArray
>> convertToBuilds)
|> List.filter (pullRequestFilter inclFromPullRequest)
|> List.truncate buildCount
}