| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package buildbot |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "strings" |
| 10 |
| 11 "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/appengine/cmd/milo/resp" |
| 13 "github.com/luci/luci-go/common/clock" |
| 14 log "github.com/luci/luci-go/common/logging" |
| 15 "github.com/luci/luci-go/common/sync/parallel" |
| 16 "golang.org/x/net/context" |
| 17 ) |
| 18 |
| 19 // getFullBuilds fetches all of the recent builds from the datastore. |
| 20 func getFullBuilds(c context.Context, masterName, builderName string, finished b
ool) ([]*buildbotBuild, error) { |
| 21 // TODO(hinoka): Builder specific structs. |
| 22 ds := datastore.Get(c) |
| 23 q := datastore.NewQuery("buildbotBuild") |
| 24 q = q.Eq("finished", finished) |
| 25 q = q.Eq("master", masterName) |
| 26 q = q.Eq("builder", builderName) |
| 27 q = q.Limit(25) // TODO(hinoka): This should be adjustable |
| 28 q = q.Order("-number") |
| 29 q.Finalize() |
| 30 buildbots := make([]*buildbotBuild, 0, 25) |
| 31 err := ds.GetAll(q, &buildbots) |
| 32 return buildbots, err |
| 33 } |
| 34 |
| 35 // GetConsoleBuilds takes commits and builders and returns a matrix of |
| 36 // resp.ConsoleBuild objects. The expected format of the result |
| 37 // is [len(commits)][len(builders)]*resp.ConsoleBuild. The first level matches |
| 38 // 1:1 to a commit, and the second level matches 1:1 to a builder. |
| 39 func GetConsoleBuilds( |
| 40 c context.Context, builders []resp.BuilderRef, commits []string) ( |
| 41 [][]*resp.ConsoleBuild, error) { |
| 42 |
| 43 results := make([][]*resp.ConsoleBuild, len(commits)) |
| 44 for i := range results { |
| 45 results[i] = make([]*resp.ConsoleBuild, len(builders)) |
| 46 } |
| 47 // HACK(hinoka): This fetches 25 full builds and then filters them. Repl
ace this |
| 48 // with something more reasonable. |
| 49 // This is kind of a hack but it's okay for now. |
| 50 err := parallel.FanOutIn(func(taskC chan<- func() error) { |
| 51 for i, builder := range builders { |
| 52 i := i |
| 53 builder := builder |
| 54 builderComponents := strings.SplitN(builder.Name, "/", 2
) |
| 55 if len(builderComponents) != 2 { |
| 56 taskC <- func() error { |
| 57 return fmt.Errorf("%s is an invalid buil
der name", builder.Name) |
| 58 } |
| 59 return |
| 60 } |
| 61 master := builderComponents[0] |
| 62 builderName := builderComponents[1] |
| 63 taskC <- func() error { |
| 64 t1 := clock.Now(c) |
| 65 builds, err := getFullBuilds(c, master, builderN
ame, true) |
| 66 if err != nil { |
| 67 return err |
| 68 } |
| 69 t2 := clock.Now(c) |
| 70 var currentStatus *resp.Status |
| 71 for j, commit := range commits { |
| 72 for _, build := range builds { |
| 73 if build.Sourcestamp.Revision ==
commit { |
| 74 results[j][i] = &resp.Co
nsoleBuild{ |
| 75 Link: &resp.Link
{ |
| 76 Label: s
trings.Join(build.Text, " "), |
| 77 URL: fmt
.Sprintf( |
| 78
"/buildbot/%s/%s/%d", master, builderName, build.Number), |
| 79 }, |
| 80 Status: build.to
Status(), |
| 81 } |
| 82 currentStatus = &results
[j][i].Status |
| 83 } |
| 84 } |
| 85 if currentStatus != nil && results[j][i]
== nil { |
| 86 results[j][i] = &resp.ConsoleBui
ld{Status: *currentStatus} |
| 87 } |
| 88 } |
| 89 log.Debugf(c, |
| 90 "Builder %s took %s to query, %s to comp
ute.", builderName, |
| 91 t2.Sub(t1), clock.Since(c, t2)) |
| 92 return nil |
| 93 } |
| 94 } |
| 95 }) |
| 96 return results, err |
| 97 } |
| OLD | NEW |