go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/clustering/rules/exporter/schema.go (about)

     1  // Copyright 2023 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 exporter
    16  
    17  import (
    18  	"cloud.google.com/go/bigquery"
    19  	"cloud.google.com/go/bigquery/storage/managedwriter/adapt"
    20  	"github.com/golang/protobuf/descriptor"
    21  	desc "github.com/golang/protobuf/protoc-gen-go/descriptor"
    22  	"google.golang.org/protobuf/types/descriptorpb"
    23  
    24  	"go.chromium.org/luci/analysis/internal/bqutil"
    25  	bqpb "go.chromium.org/luci/analysis/proto/bq"
    26  	pb "go.chromium.org/luci/analysis/proto/v1"
    27  )
    28  
    29  // tableName is the name of the exported BigQuery table.
    30  const tableName = "failure_association_rules_history"
    31  
    32  const rowMessage = "luci.analysis.bq.FailureAssociationRulesHistoryRow"
    33  
    34  var tableMetadata *bigquery.TableMetadata
    35  
    36  // tableSchemaDescriptor is a self-contained DescriptorProto for describing
    37  // row protocol buffers sent to the BigQuery Write API.
    38  var tableSchemaDescriptor *descriptorpb.DescriptorProto
    39  
    40  func init() {
    41  	var err error
    42  	var schema bigquery.Schema
    43  	if schema, err = generateRowSchema(); err != nil {
    44  		panic(err)
    45  	}
    46  	if tableSchemaDescriptor, err = generateRowSchemaDescriptor(); err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	tableMetadata = &bigquery.TableMetadata{
    51  		Clustering: &bigquery.Clustering{
    52  			Fields: []string{"project", "rule_id"},
    53  		},
    54  		// Relax ensures no fields are marked "required".
    55  		Schema: schema.Relax(),
    56  	}
    57  }
    58  
    59  func generateRowSchema() (schema bigquery.Schema, err error) {
    60  	fd, _ := descriptor.MessageDescriptorProto(&bqpb.FailureAssociationRulesHistoryRow{})
    61  	fdci, _ := descriptor.MessageDescriptorProto(&pb.ClusterId{})
    62  	fdbms, _ := descriptor.MessageDescriptorProto(&pb.BugManagementState{})
    63  	fdps, _ := descriptor.MessageDescriptorProto(&pb.BugManagementState_PolicyState{})
    64  	fdset := &desc.FileDescriptorSet{File: []*desc.FileDescriptorProto{fd, fdci, fdbms, fdps}}
    65  	return bqutil.GenerateSchema(fdset, rowMessage)
    66  }
    67  
    68  func generateRowSchemaDescriptor() (*desc.DescriptorProto, error) {
    69  	m := &bqpb.FailureAssociationRulesHistoryRow{}
    70  	descriptorProto, err := adapt.NormalizeDescriptor(m.ProtoReflect().Descriptor())
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return descriptorProto, nil
    75  }