diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-06-30 20:01:01 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-07-09 19:40:12 +0200 |
| commit | fd3bba535d0200374dad3bd872650a4ceb075cf2 (patch) | |
| tree | b9151e2694710b65cb0e209b192b7fb9d9d1abc3 /dashboard/app/cache.go | |
| parent | a1aebcca7f3ce0215d6ef3981014d04707c50a60 (diff) | |
dashboard/app: cache per-namespace bug stats
We used to show number of fixed bugs at the top of the main page.
However, now with the button nagivation, "fixed" is shown on every page.
Fetching and processing all bugs on every page would be unwise.
Cache these stats in memcache. It will be useful to show more stats in future.
Diffstat (limited to 'dashboard/app/cache.go')
| -rw-r--r-- | dashboard/app/cache.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/dashboard/app/cache.go b/dashboard/app/cache.go new file mode 100644 index 000000000..574324518 --- /dev/null +++ b/dashboard/app/cache.go @@ -0,0 +1,70 @@ +// Copyright 2020 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package main + +import ( + "fmt" + "net/http" + "time" + + "golang.org/x/net/context" + "google.golang.org/appengine/memcache" +) + +type Cached struct { + Open int + Fixed int + Invalid int +} + +func CacheGet(c context.Context, r *http.Request, ns string) (*Cached, error) { + accessLevel := accessLevel(c, r) + key := fmt.Sprintf("%v-%v", ns, accessLevel) + v := new(Cached) + _, err := memcache.Gob.Get(c, key, v) + if err != nil && err != memcache.ErrCacheMiss { + return nil, err + } + if err == nil { + return v, nil + } + if v, err = buildCached(c, ns, accessLevel); err != nil { + return nil, err + } + item := &memcache.Item{ + Key: key, + Object: v, + Expiration: time.Hour, + } + if err := memcache.Gob.Set(c, item); err != nil { + return nil, err + } + return v, nil +} + +func buildCached(c context.Context, ns string, accessLevel AccessLevel) (*Cached, error) { + v := &Cached{} + bugs, _, err := loadNamespaceBugs(c, ns) + if err != nil { + return nil, err + } + for _, bug := range bugs { + switch bug.Status { + case BugStatusOpen: + if accessLevel < bug.sanitizeAccess(accessLevel) { + continue + } + if len(bug.Commits) == 0 { + v.Open++ + } else { + v.Fixed++ + } + case BugStatusFixed: + v.Fixed++ + case BugStatusInvalid: + v.Invalid++ + } + } + return v, nil +} |
