Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 Provides roll-up statuses and alerting for Skia build/test/perf. | 2 Provides roll-up statuses and alerting for Skia build/test/perf. |
| 3 */ | 3 */ |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "net/http" | 12 "net/http" |
| 13 "os" | 13 "os" |
| 14 "os/user" | 14 "os/user" |
| 15 "path/filepath" | 15 "path/filepath" |
| 16 "runtime" | 16 "runtime" |
| 17 "strconv" | |
| 17 "strings" | 18 "strings" |
| 18 "time" | 19 "time" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 import ( | 22 import ( |
| 22 "github.com/fiorix/go-web/autogzip" | 23 "github.com/fiorix/go-web/autogzip" |
| 23 "github.com/golang/glog" | 24 "github.com/golang/glog" |
| 24 "github.com/influxdb/influxdb/client" | 25 "github.com/influxdb/influxdb/client" |
| 25 ) | 26 ) |
| 26 | 27 |
| 27 import ( | 28 import ( |
| 28 "skia.googlesource.com/buildbot.git/go/common" | 29 "skia.googlesource.com/buildbot.git/go/common" |
| 29 "skia.googlesource.com/buildbot.git/go/email" | 30 "skia.googlesource.com/buildbot.git/go/email" |
| 30 "skia.googlesource.com/buildbot.git/go/gitinfo" | 31 "skia.googlesource.com/buildbot.git/go/gitinfo" |
| 31 "skia.googlesource.com/buildbot.git/go/login" | 32 "skia.googlesource.com/buildbot.git/go/login" |
| 32 "skia.googlesource.com/buildbot.git/go/metadata" | 33 "skia.googlesource.com/buildbot.git/go/metadata" |
| 33 "skia.googlesource.com/buildbot.git/go/skiaversion" | 34 "skia.googlesource.com/buildbot.git/go/skiaversion" |
| 34 "skia.googlesource.com/buildbot.git/go/util" | 35 "skia.googlesource.com/buildbot.git/go/util" |
| 35 "skia.googlesource.com/buildbot.git/monitoring/go/alerting" | 36 "skia.googlesource.com/buildbot.git/monitoring/go/alerting" |
| 37 "skia.googlesource.com/buildbot.git/monitoring/go/commit_cache" | |
| 36 ) | 38 ) |
| 37 | 39 |
| 38 const ( | 40 const ( |
| 39 COOKIESALT_METADATA_KEY = "cookiesalt" | 41 COOKIESALT_METADATA_KEY = "cookiesalt" |
| 40 CLIENT_ID_METADATA_KEY = "client_id" | 42 CLIENT_ID_METADATA_KEY = "client_id" |
| 41 CLIENT_SECRET_METADATA_KEY = "client_secret" | 43 CLIENT_SECRET_METADATA_KEY = "client_secret" |
| 44 DEFAULT_COMMITS_TO_LOAD = 35 | |
| 42 INFLUXDB_NAME_METADATA_KEY = "influxdb_name" | 45 INFLUXDB_NAME_METADATA_KEY = "influxdb_name" |
| 43 INFLUXDB_PASSWORD_METADATA_KEY = "influxdb_password" | 46 INFLUXDB_PASSWORD_METADATA_KEY = "influxdb_password" |
| 44 GMAIL_CLIENT_ID_METADATA_KEY = "gmail_clientid" | 47 GMAIL_CLIENT_ID_METADATA_KEY = "gmail_clientid" |
| 45 GMAIL_CLIENT_SECRET_METADATA_KEY = "gmail_clientsecret" | 48 GMAIL_CLIENT_SECRET_METADATA_KEY = "gmail_clientsecret" |
| 46 GMAIL_CACHED_TOKEN_METADATA_KEY = "gmail_cached_token" | 49 GMAIL_CACHED_TOKEN_METADATA_KEY = "gmail_cached_token" |
| 47 GMAIL_TOKEN_CACHE_FILE = "google_email_token.data" | 50 GMAIL_TOKEN_CACHE_FILE = "google_email_token.data" |
| 48 ) | 51 ) |
| 49 | 52 |
| 50 var ( | 53 var ( |
| 51 » alertManager *alerting.AlertManager = nil | 54 » alertManager *alerting.AlertManager = nil |
| 52 » gitInfo *gitinfo.GitInfo = nil | 55 » gitInfo *gitinfo.GitInfo = nil |
| 56 » commitCache *commit_cache.CommitCache = nil | |
| 53 ) | 57 ) |
| 54 | 58 |
| 55 // flags | 59 // flags |
| 56 var ( | 60 var ( |
| 57 graphiteServer = flag.String("graphite_server", "localhost:2003", "Where is Graphite metrics ingestion server running.") | 61 graphiteServer = flag.String("graphite_server", "localhost:2003", "Where is Graphite metrics ingestion server running.") |
| 58 host = flag.String("host", "localhost", "HTTP service h ost") | 62 host = flag.String("host", "localhost", "HTTP service h ost") |
| 59 port = flag.String("port", ":8001", "HTTP service port (e.g., ':8001')") | 63 port = flag.String("port", ":8001", "HTTP service port (e.g., ':8001')") |
| 60 useMetadata = flag.Bool("use_metadata", true, "Load sensitive values from metadata not from flags.") | 64 useMetadata = flag.Bool("use_metadata", true, "Load sensitive values from metadata not from flags.") |
| 61 influxDbHost = flag.String("influxdb_host", "localhost:8086", " The InfluxDB hostname.") | 65 influxDbHost = flag.String("influxdb_host", "localhost:8086", " The InfluxDB hostname.") |
| 62 influxDbName = flag.String("influxdb_name", "root", "The Influx DB username.") | 66 influxDbName = flag.String("influxdb_name", "root", "The Influx DB username.") |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 func makeResourceHandler() func(http.ResponseWriter, *http.Request) { | 171 func makeResourceHandler() func(http.ResponseWriter, *http.Request) { |
| 168 fileServer := http.FileServer(http.Dir("./")) | 172 fileServer := http.FileServer(http.Dir("./")) |
| 169 return func(w http.ResponseWriter, r *http.Request) { | 173 return func(w http.ResponseWriter, r *http.Request) { |
| 170 w.Header().Add("Cache-Control", string(300)) | 174 w.Header().Add("Cache-Control", string(300)) |
| 171 fileServer.ServeHTTP(w, r) | 175 fileServer.ServeHTTP(w, r) |
| 172 } | 176 } |
| 173 } | 177 } |
| 174 | 178 |
| 175 func commitsJsonHandler(w http.ResponseWriter, r *http.Request) { | 179 func commitsJsonHandler(w http.ResponseWriter, r *http.Request) { |
| 176 w.Header().Set("Content-Type", "application/json") | 180 w.Header().Set("Content-Type", "application/json") |
| 177 » gitInfo.Update(true, true) | 181 » q := r.URL.Query() |
| 178 » commitHashes := gitInfo.From(time.Now().AddDate(0, 0, -45)) | 182 » // Case 1: Requesting specific commit range by index. |
| 179 » branchHeads, err := gitInfo.GetBranches() | 183 » if start, ok := q["start"]; ok { |
| 180 » if err != nil { | 184 » » startInt, err := strconv.ParseInt(start[0], 10, 32) |
| 181 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to read branch i nformation from the repo: %s", err)) | 185 » » if err != nil { |
| 186 » » » util.ReportError(w, r, err, fmt.Sprintf("Invalid value f or parameter 'start' (%s): %v", start, err)) | |
| 187 » » » return | |
| 188 » » } | |
| 189 » » startIdx := int(startInt) | |
| 190 » » endIdx := commitCache.NumCommits() | |
| 191 » » if end, ok := q["end"]; ok { | |
|
jcgregorio
2014/12/09 16:30:05
This seems like a repeated pattern:
if end, ok :
borenet
2014/12/09 20:17:12
Done.
| |
| 192 » » » endInt, err := strconv.ParseInt(end[0], 10, 32) | |
| 193 » » » if err != nil { | |
| 194 » » » » util.ReportError(w, r, err, fmt.Sprintf("Invalid value for parameter 'end' (%s): %v", end, err)) | |
| 195 » » » » return | |
| 196 » » » } | |
| 197 » » » endIdx = int(endInt) | |
| 198 » » } | |
| 199 » » if err := commitCache.RangeAsJson(w, startIdx, endIdx); err != n il { | |
| 200 » » » util.ReportError(w, r, err, fmt.Sprintf("Failed to load commit range from cache: %v", err)) | |
| 201 » » » return | |
| 202 » » } | |
| 182 return | 203 return |
| 183 } | 204 } |
| 184 » commits := make([]*gitinfo.LongCommit, len(commitHashes)) | 205 » // Case 2: Requesting N (or the default number) commits. |
| 185 » for i, h := range commitHashes { | 206 » commitsToLoad := DEFAULT_COMMITS_TO_LOAD |
| 186 » » c, err := gitInfo.Details(h) | 207 » if n, ok := q["n"]; ok { |
| 208 » » intN, err := strconv.ParseInt(n[0], 10, 32) | |
| 187 if err != nil { | 209 if err != nil { |
| 188 » » » util.ReportError(w, r, err, fmt.Sprintf("Failed to obtai n commit details for %s: %s", h, err)) | 210 » » » util.ReportError(w, r, err, fmt.Sprintf("Invalid value f or parameter 'n' (%s): %v", n, err)) |
| 189 return | 211 return |
| 190 } | 212 } |
| 191 » » commits[i] = c | 213 » » commitsToLoad = int(intN) |
| 192 } | 214 } |
| 193 » data := struct { | 215 » if err := commitCache.LastNAsJson(w, commitsToLoad); err != nil { |
| 194 » » Commits []*gitinfo.LongCommit `json:"commits"` | 216 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to load commits from cache: %v", err)) |
| 195 » » BranchHeads []*gitinfo.GitBranch `json:"branch_heads"` | |
| 196 » }{ | |
| 197 » » Commits: commits, | |
| 198 » » BranchHeads: branchHeads, | |
| 199 » } | |
| 200 » if err := json.NewEncoder(w).Encode(data); err != nil { | |
| 201 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to encode commit data as JSON: %s", err)) | |
| 202 return | 217 return |
| 203 } | 218 } |
| 204 } | 219 } |
| 205 | 220 |
| 206 func commitsHandler(w http.ResponseWriter, r *http.Request) { | 221 func commitsHandler(w http.ResponseWriter, r *http.Request) { |
| 207 http.ServeFile(w, r, "res/html/commits.html") | 222 http.ServeFile(w, r, "res/html/commits.html") |
| 208 } | 223 } |
| 209 | 224 |
| 210 func runServer(serverURL string) { | 225 func runServer(serverURL string) { |
| 211 _, filename, _, _ := runtime.Caller(0) | 226 _, filename, _, _ := runtime.Caller(0) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 } | 308 } |
| 294 alertManager, err = alerting.NewAlertManager(dbClient, *alertsFile, pars edPollInterval, emailAuth, *testing) | 309 alertManager, err = alerting.NewAlertManager(dbClient, *alertsFile, pars edPollInterval, emailAuth, *testing) |
| 295 if err != nil { | 310 if err != nil { |
| 296 glog.Fatalf("Failed to create AlertManager: %v", err) | 311 glog.Fatalf("Failed to create AlertManager: %v", err) |
| 297 } | 312 } |
| 298 | 313 |
| 299 gitInfo, err = gitinfo.CloneOrUpdate("https://skia.googlesource.com/skia .git", *workdir, true) | 314 gitInfo, err = gitinfo.CloneOrUpdate("https://skia.googlesource.com/skia .git", *workdir, true) |
| 300 if err != nil { | 315 if err != nil { |
| 301 glog.Fatalf("Failed to check out Skia: %v", err) | 316 glog.Fatalf("Failed to check out Skia: %v", err) |
| 302 } | 317 } |
| 318 commitCache, err = commit_cache.New(gitInfo) | |
| 319 if err != nil { | |
| 320 glog.Fatalf("Failed to create commit cache: %v", err) | |
| 321 } | |
| 322 go func() { | |
| 323 for _ = range time.Tick(time.Minute) { | |
| 324 if err := commitCache.Update(); err != nil { | |
| 325 glog.Errorf("Failed to update commit cache: %v", err) | |
| 326 } | |
| 327 } | |
| 328 }() | |
| 303 runServer(serverURL) | 329 runServer(serverURL) |
| 304 } | 330 } |
| OLD | NEW |