go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/rpc/clusterid.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 "strings" 19 20 "go.chromium.org/luci/analysis/internal/clustering" 21 "go.chromium.org/luci/analysis/internal/clustering/algorithms/rulesalgorithm" 22 pb "go.chromium.org/luci/analysis/proto/v1" 23 ) 24 25 func createClusterIdPB(b clustering.ClusterID) *pb.ClusterId { 26 return &pb.ClusterId{ 27 Algorithm: aliasAlgorithm(b.Algorithm), 28 Id: b.ID, 29 } 30 } 31 32 func aliasAlgorithm(algorithm string) string { 33 // Drop the version number from the rules algorithm, 34 // e.g. "rules-v2" -> "rules". 35 // Clients may want to identify if the rules algorithm 36 // was used to cluster, to identify clusters for which they 37 // can lookup the corresponding rule. 38 // Hiding the version information avoids clients 39 // accidentally depending on it in their code, which would 40 // make changing the version of the rules-based clustering 41 // algorithm breaking for clients. 42 // It is anticipated that future updates to the rules-based 43 // clustering algorithm will mostly be about tweaking the 44 // failure-matching semantics, and will retain the property 45 // that the Cluster ID corresponds to the Rule ID. 46 if strings.HasPrefix(algorithm, clustering.RulesAlgorithmPrefix) { 47 return "rules" 48 } 49 return algorithm 50 } 51 52 func resolveAlgorithm(algorithm string) string { 53 // Resolve an alias to the rules algorithm to the concrete 54 // implementation, e.g. "rules" -> "rules-v2". 55 if algorithm == "rules" { 56 return rulesalgorithm.AlgorithmName 57 } 58 return algorithm 59 }