go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/portal/index.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package portal 16 17 import ( 18 "sort" 19 "time" 20 21 "github.com/dustin/go-humanize" 22 23 "go.chromium.org/luci/common/clock" 24 25 "go.chromium.org/luci/server/router" 26 "go.chromium.org/luci/server/settings" 27 "go.chromium.org/luci/server/templates" 28 ) 29 30 type pageIndexEntry struct { 31 ID string 32 Title string 33 } 34 35 type pageIndexEntries []pageIndexEntry 36 37 func (a pageIndexEntries) Len() int { return len(a) } 38 func (a pageIndexEntries) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 39 func (a pageIndexEntries) Less(i, j int) bool { return a[i].Title < a[j].Title } 40 41 func indexPage(ctx *router.Context) { 42 c, rw := ctx.Request.Context(), ctx.Writer 43 44 entries := pageIndexEntries{} 45 for id, p := range GetPages() { 46 title, err := p.Title(c) 47 if err != nil { 48 replyError(c, rw, err) 49 return 50 } 51 entries = append(entries, pageIndexEntry{ 52 ID: id, 53 Title: title, 54 }) 55 } 56 sort.Sort(entries) 57 58 // Grab timestamp when last settings change hits all instances. 59 consistencyTime := time.Time{} 60 if s := settings.GetSettings(c); s != nil { 61 if storage, _ := s.GetStorage().(settings.EventualConsistentStorage); storage != nil { 62 var err error 63 if consistencyTime, err = storage.GetConsistencyTime(c); err != nil { 64 replyError(c, rw, err) 65 return 66 } 67 } 68 } 69 70 now := clock.Now(c).UTC() 71 templates.MustRender(c, rw, "pages/index.html", templates.Args{ 72 "Entries": entries, 73 "WaitingForConsistency": !consistencyTime.IsZero() && now.Before(consistencyTime), 74 "TimeToConsistency": humanize.RelTime(consistencyTime, now, "", ""), 75 }) 76 }