go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/aggrmetrics/active_projects.go (about)

     1  // Copyright 2021 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 aggrmetrics
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/data/stringset"
    23  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    24  	"go.chromium.org/luci/server/caching/layered"
    25  )
    26  
    27  const (
    28  	activeProjectsTTL = 20 * time.Minute
    29  	activeProjectsKey = "v1"
    30  )
    31  
    32  // activeProjects returns LUCI projects currently active in CV.
    33  func activeProjects(ctx context.Context) ([]string, error) {
    34  	out, err := activeProjectsCache.GetOrCreate(ctx, activeProjectsKey, func() ([]string, time.Duration, error) {
    35  		out, err := prjcfg.GetAllProjectIDs(ctx, true /* enabled only*/)
    36  		return out, activeProjectsTTL, err
    37  	})
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return stringset.NewFromSlice(out...).ToSortedSlice(), nil
    42  }
    43  
    44  // activeProjectsCache caches active projects to avoid hammering Datastore on
    45  // something which doesn't change very often.
    46  var activeProjectsCache = layered.RegisterCache(layered.Parameters[[]string]{
    47  	ProcessCacheCapacity: 1,
    48  	GlobalNamespace:      "aggrmetrics_active_projects",
    49  	Marshal: func(item []string) ([]byte, error) {
    50  		return json.Marshal(item)
    51  	},
    52  	Unmarshal: func(blob []byte) ([]string, error) {
    53  		// There are ~100 active LUCI projects.
    54  		out := make([]string, 0, 100)
    55  		err := json.Unmarshal(blob, &out)
    56  		return out, err
    57  	},
    58  })