go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/failureattributes/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 failureattributes
    16  
    17  import (
    18  	"time"
    19  
    20  	"cloud.google.com/go/bigquery"
    21  	"github.com/golang/protobuf/descriptor"
    22  	desc "github.com/golang/protobuf/protoc-gen-go/descriptor"
    23  
    24  	"go.chromium.org/luci/common/bq"
    25  
    26  	"go.chromium.org/luci/analysis/internal/bqutil"
    27  	bqpb "go.chromium.org/luci/analysis/proto/bq"
    28  )
    29  
    30  // tableName is the name of the exported BigQuery table.
    31  const tableName = "failure_attributes"
    32  
    33  // schemaApplyer ensures BQ schema matches the row proto definitions.
    34  var schemaApplyer = bq.NewSchemaApplyer(bq.RegisterSchemaApplyerCache(50))
    35  
    36  // We keep the failures for 90 days to align with the clustered_failures table.
    37  const partitionExpirationTime = 90 * 24 * time.Hour
    38  
    39  const rowMessage = "luci.analysis.bq.FailureAttributeRow"
    40  
    41  var tableMetadata *bigquery.TableMetadata
    42  
    43  func init() {
    44  	var err error
    45  	var schema bigquery.Schema
    46  	if schema, err = generateRowSchema(); err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	tableMetadata = &bigquery.TableMetadata{
    51  		TimePartitioning: &bigquery.TimePartitioning{
    52  			Type:       bigquery.DayPartitioningType,
    53  			Expiration: partitionExpirationTime,
    54  			Field:      "partition_time",
    55  		},
    56  		Clustering: &bigquery.Clustering{
    57  			Fields: []string{"project", "test_result_system", "ingested_invocation_id", "test_result_id"},
    58  		},
    59  		// Relax ensures no fields are marked "required".
    60  		Schema: schema.Relax(),
    61  	}
    62  }
    63  
    64  func generateRowSchema() (schema bigquery.Schema, err error) {
    65  	fd, _ := descriptor.MessageDescriptorProto(&bqpb.FailureAttributeRow{})
    66  	fdset := &desc.FileDescriptorSet{File: []*desc.FileDescriptorProto{fd}}
    67  	return bqutil.GenerateSchema(fdset, rowMessage)
    68  }