go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/rpc/metrics.go (about)

     1  // Copyright 2022 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 rpc
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/analysis/internal/analysis/metrics"
    21  	"go.chromium.org/luci/analysis/internal/perms"
    22  	pb "go.chromium.org/luci/analysis/proto/v1"
    23  )
    24  
    25  type metricsServer struct {
    26  }
    27  
    28  func NewMetricsServer() *pb.DecoratedMetrics {
    29  	return &pb.DecoratedMetrics{
    30  		Prelude:  checkAllowedPrelude,
    31  		Service:  &metricsServer{},
    32  		Postlude: gRPCifyAndLogPostlude,
    33  	}
    34  }
    35  
    36  // ListForProject lists the metrics for a given LUCI Project. See proto definition for more.
    37  func (*metricsServer) ListForProject(ctx context.Context, req *pb.ListProjectMetricsRequest) (*pb.ListProjectMetricsResponse, error) {
    38  	project, err := parseProjectName(req.Parent)
    39  	if err != nil {
    40  		return nil, invalidArgumentError(err)
    41  	}
    42  	if err := perms.VerifyProjectPermissions(ctx, project, perms.PermGetConfig); err != nil {
    43  		return nil, err
    44  	}
    45  	cfg, err := readProjectConfig(ctx, project)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	result := &pb.ListProjectMetricsResponse{}
    50  	for _, m := range metrics.ComputedMetrics {
    51  		projectMetric := m.AdaptToProject(project, cfg.Config.Metrics)
    52  		result.Metrics = append(result.Metrics, toProjectMetricPB(projectMetric))
    53  	}
    54  	return result, nil
    55  }
    56  
    57  func toProjectMetricPB(metric metrics.Definition) *pb.ProjectMetric {
    58  	return &pb.ProjectMetric{
    59  		Name:              metric.Name,
    60  		MetricId:          metric.ID.String(),
    61  		HumanReadableName: metric.HumanReadableName,
    62  		Description:       metric.Description,
    63  		IsDefault:         metric.Config.IsDefault,
    64  		SortPriority:      int32(metric.Config.SortPriority),
    65  	}
    66  }