Skip to content

Commit

Permalink
Merge pull request #984 from Azanul/bigquery-cost-calculation
Browse files Browse the repository at this point in the history
Bigquery table cost calculation
  • Loading branch information
mlabouardy authored Sep 21, 2023
2 parents 5473f76 + 27fd2dc commit af51eb2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions providers/gcp/bigquery/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ import (
"google.golang.org/api/option"
)

const (
ACTIVE_STORAGE = "ACTIVE"
LONGTERM_STORAGE = "LONGTERM"
)

func getTableStorageClass(meta *bigquery.TableMetadata) string {
activeThreshold := time.Now().AddDate(0, 0, -90)
lastModified := meta.LastModifiedTime
if lastModified.After(activeThreshold) {
return ACTIVE_STORAGE
}
return LONGTERM_STORAGE
}

func getStoragePricingForBigQueryTable() map[string]float64 {
return map[string]float64{
ACTIVE_STORAGE: 0.02,
LONGTERM_STORAGE: 0.01,
}
}

func Tables(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

Expand Down Expand Up @@ -59,6 +80,9 @@ func Tables(ctx context.Context, client providers.ProviderClient) ([]models.Reso
})
}

storageClass := getTableStorageClass(tableMetadata)
monthlyCost := float64(tableMetadata.NumBytes) / (1024 * 1024 * 1024) * getStoragePricingForBigQueryTable()[storageClass]

resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Expand All @@ -68,6 +92,7 @@ func Tables(ctx context.Context, client providers.ProviderClient) ([]models.Reso
Name: tableMetadata.Name,
FetchedAt: time.Now(),
Tags: tags,
Cost: monthlyCost,
Link: fmt.Sprintf("https://console.cloud.google.com/bigquery?project=%s&page=dataset&p=%s&d=%s", client.GCPClient.Credentials.ProjectID, client.GCPClient.Credentials.ProjectID, dataset.DatasetID),
})
}
Expand Down

0 comments on commit af51eb2

Please sign in to comment.