Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: monitoring/go/alertserver/main.go

Issue 784103004: Improvements to commits page (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: wrap up getIntParam Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | monitoring/go/commit_cache/commit_cache.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.")
63 influxDbPassword = flag.String("influxdb_password", "root", "The In fluxDB password.") 67 influxDbPassword = flag.String("influxdb_password", "root", "The In fluxDB password.")
64 influxDbDatabase = flag.String("influxdb_database", "", "The Influx DB database.") 68 influxDbDatabase = flag.String("influxdb_database", "", "The Influx DB database.")
65 emailClientIdFlag = flag.String("email_clientid", "", "OAuth Client ID for sending email.") 69 emailClientIdFlag = flag.String("email_clientid", "", "OAuth Client ID for sending email.")
66 emailClientSecretFlag = flag.String("email_clientsecret", "", "OAuth Cli ent Secret for sending email.") 70 emailClientSecretFlag = flag.String("email_clientsecret", "", "OAuth Cli ent Secret for sending email.")
67 alertPollInterval = flag.String("alert_poll_interval", "1s", "How of ten to check for new alerts.") 71 alertPollInterval = flag.String("alert_poll_interval", "1s", "How of ten to check for new alerts.")
68 alertsFile = flag.String("alerts_file", "alerts.cfg", "Config file containing alert rules.") 72 alertsFile = flag.String("alerts_file", "alerts.cfg", "Config file containing alert rules.")
69 testing = flag.Bool("testing", false, "Set to true for loc ally testing rules. No email will be sent.") 73 testing = flag.Bool("testing", false, "Set to true for loc ally testing rules. No email will be sent.")
70 workdir = flag.String("workdir", ".", "Directory to use fo r scratch work.") 74 workdir = flag.String("workdir", ".", "Directory to use fo r scratch work.")
71 ) 75 )
72 76
73 func userHasEditRights(email string) bool { 77 func userHasEditRights(email string) bool {
74 if strings.HasSuffix(email, "@google.com") { 78 if strings.HasSuffix(email, "@google.com") {
75 return true 79 return true
76 } 80 }
77 return false 81 return false
78 } 82 }
79 83
84 func getIntParam(name string, r *http.Request) (*int, error) {
85 raw, ok := r.URL.Query()[name]
86 if !ok {
87 return nil, nil
88 }
89 v64, err := strconv.ParseInt(raw[0], 10, 32)
90 if err != nil {
91 return nil, fmt.Errorf("Invalid value for parameter %q: %s -- %v ", name, raw, err)
92 }
93 v32 := int(v64)
94 return &v32, nil
95 }
96
80 func alertJsonHandler(w http.ResponseWriter, r *http.Request) { 97 func alertJsonHandler(w http.ResponseWriter, r *http.Request) {
81 w.Header().Set("Content-Type", "application/json") 98 w.Header().Set("Content-Type", "application/json")
82 type displayAlert struct { 99 type displayAlert struct {
83 Id string `json:"id"` 100 Id string `json:"id"`
84 Name string `json:"name"` 101 Name string `json:"name"`
85 Query string `json:"query"` 102 Query string `json:"query"`
86 Condition string `json:"condition"` 103 Condition string `json:"condition"`
87 Message string `json:"message"` 104 Message string `json:"message"`
88 Active bool `json:"active"` 105 Active bool `json:"active"`
89 Snoozed bool `json:"snoozed"` 106 Snoozed bool `json:"snoozed"`
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 func makeResourceHandler() func(http.ResponseWriter, *http.Request) { 184 func makeResourceHandler() func(http.ResponseWriter, *http.Request) {
168 fileServer := http.FileServer(http.Dir("./")) 185 fileServer := http.FileServer(http.Dir("./"))
169 return func(w http.ResponseWriter, r *http.Request) { 186 return func(w http.ResponseWriter, r *http.Request) {
170 w.Header().Add("Cache-Control", string(300)) 187 w.Header().Add("Cache-Control", string(300))
171 fileServer.ServeHTTP(w, r) 188 fileServer.ServeHTTP(w, r)
172 } 189 }
173 } 190 }
174 191
175 func commitsJsonHandler(w http.ResponseWriter, r *http.Request) { 192 func commitsJsonHandler(w http.ResponseWriter, r *http.Request) {
176 w.Header().Set("Content-Type", "application/json") 193 w.Header().Set("Content-Type", "application/json")
177 » gitInfo.Update(true, true) 194 » // Case 1: Requesting specific commit range by index.
178 » commitHashes := gitInfo.From(time.Now().AddDate(0, 0, -45)) 195 » startIdx, err := getIntParam("start", r)
179 » branchHeads, err := gitInfo.GetBranches()
180 if err != nil { 196 if err != nil {
181 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to read branch i nformation from the repo: %s", err)) 197 » » util.ReportError(w, r, err, fmt.Sprintf("Invalid parameter: %v", err))
182 return 198 return
183 } 199 }
184 » commits := make([]*gitinfo.LongCommit, len(commitHashes)) 200 » if startIdx != nil {
185 » for i, h := range commitHashes { 201 » » endIdx := commitCache.NumCommits()
186 » » c, err := gitInfo.Details(h) 202 » » end, err := getIntParam("end", r)
187 if err != nil { 203 if err != nil {
188 » » » util.ReportError(w, r, err, fmt.Sprintf("Failed to obtai n commit details for %s: %s", h, err)) 204 » » » util.ReportError(w, r, err, fmt.Sprintf("Invalid paramet er: %v", err))
189 return 205 return
190 } 206 }
191 » » commits[i] = c 207 » » if end != nil {
208 » » » endIdx = *end
209 » » }
210 » » if err := commitCache.RangeAsJson(w, *startIdx, endIdx); err != nil {
211 » » » util.ReportError(w, r, err, fmt.Sprintf("Failed to load commit range from cache: %v", err))
212 » » » return
213 » » }
214 » » return
192 } 215 }
193 » data := struct { 216 » // Case 2: Requesting N (or the default number) commits.
194 » » Commits []*gitinfo.LongCommit `json:"commits"` 217 » commitsToLoad := DEFAULT_COMMITS_TO_LOAD
195 » » BranchHeads []*gitinfo.GitBranch `json:"branch_heads"` 218 » n, err := getIntParam("n", r)
196 » }{ 219 » if err != nil {
197 » » Commits: commits, 220 » » util.ReportError(w, r, err, fmt.Sprintf("Invalid parameter: %v", err))
198 » » BranchHeads: branchHeads, 221 » » return
199 } 222 }
200 » if err := json.NewEncoder(w).Encode(data); err != nil { 223 » if n != nil {
201 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to encode commit data as JSON: %s", err)) 224 » » commitsToLoad = *n
225 » }
226 » if err := commitCache.LastNAsJson(w, commitsToLoad); err != nil {
227 » » util.ReportError(w, r, err, fmt.Sprintf("Failed to load commits from cache: %v", err))
202 return 228 return
203 } 229 }
204 } 230 }
205 231
206 func commitsHandler(w http.ResponseWriter, r *http.Request) { 232 func commitsHandler(w http.ResponseWriter, r *http.Request) {
207 http.ServeFile(w, r, "res/html/commits.html") 233 http.ServeFile(w, r, "res/html/commits.html")
208 } 234 }
209 235
210 func runServer(serverURL string) { 236 func runServer(serverURL string) {
211 _, filename, _, _ := runtime.Caller(0) 237 _, filename, _, _ := runtime.Caller(0)
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 319 }
294 alertManager, err = alerting.NewAlertManager(dbClient, *alertsFile, pars edPollInterval, emailAuth, *testing) 320 alertManager, err = alerting.NewAlertManager(dbClient, *alertsFile, pars edPollInterval, emailAuth, *testing)
295 if err != nil { 321 if err != nil {
296 glog.Fatalf("Failed to create AlertManager: %v", err) 322 glog.Fatalf("Failed to create AlertManager: %v", err)
297 } 323 }
298 324
299 gitInfo, err = gitinfo.CloneOrUpdate("https://skia.googlesource.com/skia .git", *workdir, true) 325 gitInfo, err = gitinfo.CloneOrUpdate("https://skia.googlesource.com/skia .git", *workdir, true)
300 if err != nil { 326 if err != nil {
301 glog.Fatalf("Failed to check out Skia: %v", err) 327 glog.Fatalf("Failed to check out Skia: %v", err)
302 } 328 }
329 commitCache, err = commit_cache.New(gitInfo)
330 if err != nil {
331 glog.Fatalf("Failed to create commit cache: %v", err)
332 }
333 go func() {
334 for _ = range time.Tick(time.Minute) {
335 if err := commitCache.Update(); err != nil {
336 glog.Errorf("Failed to update commit cache: %v", err)
337 }
338 }
339 }()
303 runServer(serverURL) 340 runServer(serverURL)
304 } 341 }
OLDNEW
« no previous file with comments | « no previous file | monitoring/go/commit_cache/commit_cache.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698