| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package hierarchy | 5 package hierarchy |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sort" |
| 9 |
| 8 "github.com/luci/luci-go/appengine/logdog/coordinator" | 10 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 9 » "github.com/luci/luci-go/appengine/logdog/coordinator/config" | 11 » "github.com/luci/luci-go/common/config" |
| 10 » luciConfig "github.com/luci/luci-go/common/config" | |
| 11 log "github.com/luci/luci-go/common/logging" | 12 log "github.com/luci/luci-go/common/logging" |
| 12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 func getProjects(c context.Context, r *Request) (*List, error) { | 16 func getProjects(c context.Context, r *Request) (*List, error) { |
| 16 // None of the projects are streams. | 17 // None of the projects are streams. |
| 17 var l List | 18 var l List |
| 18 if r.StreamOnly { | 19 if r.StreamOnly { |
| 19 return &l, nil | 20 return &l, nil |
| 20 } | 21 } |
| 21 | 22 |
| 22 » projects, err := config.UserProjects(c) | 23 » // Get all user-accessible active projects. |
| 24 » allPcfgs, err := coordinator.ActiveUserProjects(c) |
| 23 if err != nil { | 25 if err != nil { |
| 24 » » log.WithError(err).Errorf(c, "Failed to get user projects.") | 26 » » // If there is an error, we will refrain from filtering projects
. |
| 27 » » log.WithError(err).Warningf(c, "Failed to get user project list.
") |
| 25 return nil, err | 28 return nil, err |
| 26 } | 29 } |
| 27 | 30 |
| 28 » // Get all current datastore namespaces. | 31 » projects := make(projectNameSlice, 0, len(allPcfgs)) |
| 29 » nsProjects, err := coordinator.AllProjectsWithNamespaces(c) | 32 » for project := range allPcfgs { |
| 30 » if err != nil { | 33 » » projects = append(projects, project) |
| 31 » » // If there is an error, we will refrain from filtering projects
. | 34 » } |
| 32 » » log.WithError(err).Warningf(c, "Failed to get namespace project
list.") | 35 » sort.Sort(projects) |
| 33 » } else { | |
| 34 » » // Only list projects that have datastore namespaces. | |
| 35 » » lookup := make(map[luciConfig.ProjectName]struct{}, len(nsProjec
ts)) | |
| 36 » » for _, proj := range nsProjects { | |
| 37 » » » lookup[proj] = struct{}{} | |
| 38 » » } | |
| 39 | 36 |
| 40 » » pos := 0 | 37 » next := config.ProjectName(r.Next) |
| 41 » » for _, proj := range projects { | |
| 42 » » » if _, ok := lookup[proj]; ok { | |
| 43 » » » » projects[pos] = proj | |
| 44 » » » » pos++ | |
| 45 » » » } | |
| 46 » » } | |
| 47 » » projects = projects[:pos] | |
| 48 » } | |
| 49 | |
| 50 » next := luciConfig.ProjectName(r.Next) | |
| 51 skip := r.Skip | 38 skip := r.Skip |
| 52 for _, proj := range projects { | 39 for _, proj := range projects { |
| 53 // Implement "Next" cursor. If set, don't do anything until we'v
e seen it. | 40 // Implement "Next" cursor. If set, don't do anything until we'v
e seen it. |
| 54 if next != "" { | 41 if next != "" { |
| 55 if proj == next { | 42 if proj == next { |
| 56 next = "" | 43 next = "" |
| 57 } | 44 } |
| 58 continue | 45 continue |
| 59 } | 46 } |
| 60 | 47 |
| 61 // Implement skip. | 48 // Implement skip. |
| 62 if skip > 0 { | 49 if skip > 0 { |
| 63 skip-- | 50 skip-- |
| 64 continue | 51 continue |
| 65 } | 52 } |
| 66 | 53 |
| 67 l.Comp = append(l.Comp, &ListComponent{ | 54 l.Comp = append(l.Comp, &ListComponent{ |
| 68 Name: string(proj), | 55 Name: string(proj), |
| 69 }) | 56 }) |
| 70 | 57 |
| 71 // Implement limit. | 58 // Implement limit. |
| 72 if r.Limit > 0 && len(l.Comp) >= r.Limit { | 59 if r.Limit > 0 && len(l.Comp) >= r.Limit { |
| 73 l.Next = string(proj) | 60 l.Next = string(proj) |
| 74 break | 61 break |
| 75 } | 62 } |
| 76 } | 63 } |
| 77 | 64 |
| 78 return &l, nil | 65 return &l, nil |
| 79 } | 66 } |
| 67 |
| 68 // projectNameSlice is a sortable slice of config.ProjectName. |
| 69 type projectNameSlice []config.ProjectName |
| 70 |
| 71 func (s projectNameSlice) Len() int { return len(s) } |
| 72 func (s projectNameSlice) Less(i, j int) bool { return s[i] < s[j] } |
| 73 func (s projectNameSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| OLD | NEW |