|
| 1 | +package manual |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "cloud.google.com/go/bigquery" |
| 7 | + |
| 8 | + "github.com/overmindtech/cli/sdp-go" |
| 9 | + "github.com/overmindtech/cli/sources" |
| 10 | + gcpshared "github.com/overmindtech/cli/sources/gcp/shared" |
| 11 | + "github.com/overmindtech/cli/sources/shared" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + BigQueryModelLookupById = shared.NewItemTypeLookup("id", gcpshared.BigQueryModel) |
| 16 | +) |
| 17 | + |
| 18 | +// BigQueryModelWrapper is a wrapper for the BigQueryModelClient that implements the sources.SearchableWrapper interface |
| 19 | +type BigQueryModelWrapper struct { |
| 20 | + client gcpshared.BigQueryModelClient |
| 21 | + *gcpshared.ProjectBase |
| 22 | +} |
| 23 | + |
| 24 | +// NewBigQueryModel creates a new BigQueryModelWrapper instance |
| 25 | +func NewBigQueryModel(client gcpshared.BigQueryModelClient, projectID string) sources.SearchableWrapper { |
| 26 | + return &BigQueryModelWrapper{ |
| 27 | + client: client, |
| 28 | + ProjectBase: gcpshared.NewProjectBase( |
| 29 | + projectID, |
| 30 | + sdp.AdapterCategory_ADAPTER_CATEGORY_DATABASE, |
| 31 | + gcpshared.BigQueryModel, |
| 32 | + ), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (m BigQueryModelWrapper) GetLookups() sources.ItemTypeLookups { |
| 37 | + return sources.ItemTypeLookups{ |
| 38 | + BigQueryDatasetLookupByID, |
| 39 | + BigQueryModelLookupById, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (m BigQueryModelWrapper) Get(ctx context.Context, queryParts ...string) (*sdp.Item, *sdp.QueryError) { |
| 44 | + metadata, err := m.client.Get(ctx, m.ProjectBase.ProjectID(), queryParts[0], queryParts[1]) |
| 45 | + if err != nil { |
| 46 | + return nil, gcpshared.QueryError(err) |
| 47 | + } |
| 48 | + return m.GCPBigQueryMetadataToItem(ctx, queryParts[0], metadata) |
| 49 | +} |
| 50 | + |
| 51 | +func (m BigQueryModelWrapper) GCPBigQueryMetadataToItem(ctx context.Context, dataSetId string, metadata *bigquery.ModelMetadata) (*sdp.Item, *sdp.QueryError) { |
| 52 | + attributes, err := shared.ToAttributesWithExclude(metadata, "labels") |
| 53 | + if err != nil { |
| 54 | + return nil, gcpshared.QueryError(err) |
| 55 | + } |
| 56 | + |
| 57 | + sdpItem := &sdp.Item{ |
| 58 | + Type: gcpshared.BigQueryModel.String(), |
| 59 | + UniqueAttribute: "Name", |
| 60 | + Attributes: attributes, |
| 61 | + Scope: m.DefaultScope(), |
| 62 | + Tags: metadata.Labels, |
| 63 | + } |
| 64 | + |
| 65 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 66 | + Query: &sdp.Query{ |
| 67 | + Type: gcpshared.BigQueryDataset.String(), |
| 68 | + Method: sdp.QueryMethod_GET, |
| 69 | + Scope: m.DefaultScope(), |
| 70 | + Query: dataSetId, |
| 71 | + }, |
| 72 | + // Model is in a dataset, if dataset is deleted, model is deleted. |
| 73 | + // If the model is deleted, the dataset is not deleted. |
| 74 | + BlastPropagation: &sdp.BlastPropagation{ |
| 75 | + In: false, |
| 76 | + Out: true, |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + if metadata.EncryptionConfig != nil && metadata.EncryptionConfig.KMSKeyName != "" { |
| 81 | + values := gcpshared.ExtractPathParams(metadata.EncryptionConfig.KMSKeyName, "locations", "keyRings", "cryptoKeys") |
| 82 | + if len(values) == 3 && values[0] != "" && values[1] != "" && values[2] != "" { |
| 83 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 84 | + |
| 85 | + Query: &sdp.Query{ |
| 86 | + Type: gcpshared.CloudKMSCryptoKey.String(), |
| 87 | + Method: sdp.QueryMethod_GET, |
| 88 | + Scope: m.ProjectID(), |
| 89 | + Query: shared.CompositeLookupKey(values...), |
| 90 | + }, |
| 91 | + BlastPropagation: &sdp.BlastPropagation{ |
| 92 | + In: true, |
| 93 | + Out: false, |
| 94 | + }, |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for _, row := range metadata.RawTrainingRuns() { |
| 100 | + if row.DataSplitResult != nil && row.DataSplitResult.EvaluationTable.TableId != "" { |
| 101 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 102 | + Query: &sdp.Query{ |
| 103 | + Type: gcpshared.BigQueryTable.String(), |
| 104 | + Method: sdp.QueryMethod_GET, |
| 105 | + Scope: m.DefaultScope(), |
| 106 | + Query: shared.CompositeLookupKey(dataSetId, row.DataSplitResult.EvaluationTable.TableId), |
| 107 | + }, |
| 108 | + BlastPropagation: &sdp.BlastPropagation{ |
| 109 | + In: true, |
| 110 | + Out: false, |
| 111 | + }, |
| 112 | + }) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return sdpItem, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (m BigQueryModelWrapper) PotentialLinks() map[shared.ItemType]bool { |
| 120 | + return shared.NewItemTypesSet( |
| 121 | + gcpshared.CloudKMSCryptoKey, |
| 122 | + gcpshared.BigQueryDataset, |
| 123 | + gcpshared.BigQueryTable, |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +func (m BigQueryModelWrapper) SearchLookups() []sources.ItemTypeLookups { |
| 128 | + return []sources.ItemTypeLookups{ |
| 129 | + { |
| 130 | + BigQueryModelLookupById, |
| 131 | + }, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func (m BigQueryModelWrapper) Search(ctx context.Context, queryParts ...string) ([]*sdp.Item, *sdp.QueryError) { |
| 136 | + items, err := m.client.List(ctx, m.ProjectBase.ProjectID(), queryParts[0], func(ctx context.Context, metadata *bigquery.ModelMetadata) (*sdp.Item, *sdp.QueryError) { |
| 137 | + // Convert the dataset metadata to an SDP item |
| 138 | + attributes, err := shared.ToAttributesWithExclude(metadata, "labels") |
| 139 | + if err != nil { |
| 140 | + return nil, gcpshared.QueryError(err) |
| 141 | + } |
| 142 | + |
| 143 | + item := &sdp.Item{ |
| 144 | + Type: gcpshared.BigQueryModel.String(), |
| 145 | + UniqueAttribute: "Name", |
| 146 | + Scope: m.DefaultScope(), |
| 147 | + Attributes: attributes, |
| 148 | + Tags: metadata.Labels, |
| 149 | + } |
| 150 | + return item, nil |
| 151 | + }) |
| 152 | + if err != nil { |
| 153 | + return nil, gcpshared.QueryError(err) |
| 154 | + } |
| 155 | + return items, nil |
| 156 | +} |
0 commit comments