Skip to content
Permalink
default
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : Gabriel Geise
# Created Date: Tue Nov 12 12:06:00 EST 2021
# =============================================================================
'''
Utility script to create a table and view for recieving the Azure consumption data.
Execute this script to initialize.
'''
from config import config
from google.cloud import bigquery
client = bigquery.Client()
project = client.project
dataset_ref = bigquery.DatasetReference(project, config['dataset'])
table_ref = dataset_ref.table(config['table_name'])
schema=[
bigquery.SchemaField("charge_type", "STRING", "NULLABLE"),
bigquery.SchemaField("plan_name", "STRING", "NULLABLE"),
bigquery.SchemaField("publisher_name", "STRING", "NULLABLE"),
bigquery.SchemaField("resource_group", "STRING", "NULLABLE"),
bigquery.SchemaField("invoice_section", "STRING", "NULLABLE"),
bigquery.SchemaField("service_info2", "STRING", "NULLABLE"),
bigquery.SchemaField("frequency", "STRING", "NULLABLE"),
bigquery.SchemaField("service_info1", "STRING", "NULLABLE"),
bigquery.SchemaField("reservation_id", "STRING", "NULLABLE"),
bigquery.SchemaField("resource_id", "STRING", "NULLABLE"),
bigquery.SchemaField("consumed_service", "STRING", "NULLABLE"),
bigquery.SchemaField("reservation_name", "STRING", "NULLABLE"),
bigquery.SchemaField("additional_info", "STRING", "NULLABLE"),
bigquery.SchemaField("resource_location", "STRING", "NULLABLE"),
bigquery.SchemaField("is_azure_credit_eligible", "BOOLEAN", "NULLABLE"),
bigquery.SchemaField("billing_currency", "STRING", "NULLABLE"),
bigquery.SchemaField("unit_price", "FLOAT", "NULLABLE"),
bigquery.SchemaField("cost", "FLOAT", "NULLABLE"),
bigquery.SchemaField("effective_price", "FLOAT", "NULLABLE"),
bigquery.SchemaField("quantity", "FLOAT", "NULLABLE"),
bigquery.SchemaField("meter_details", "STRING", "NULLABLE"),
bigquery.SchemaField("product_order_id", "STRING", "NULLABLE"),
bigquery.SchemaField("cost_center", "STRING", "NULLABLE"),
bigquery.SchemaField("kind", "STRING", "NULLABLE"),
bigquery.SchemaField("meter_id", "STRING", "NULLABLE"),
bigquery.SchemaField("date", "TIMESTAMP", "NULLABLE", "bq-datetime"),
bigquery.SchemaField("product", "STRING", "NULLABLE"),
bigquery.SchemaField("part_number", "STRING", "NULLABLE"),
bigquery.SchemaField("publisher_type", "STRING", "NULLABLE"),
bigquery.SchemaField("subscription_name", "STRING", "NULLABLE"),
bigquery.SchemaField("account_name", "STRING", "NULLABLE"),
bigquery.SchemaField("billing_period_end_date", "TIMESTAMP", "NULLABLE", "bq-datetime"),
bigquery.SchemaField("offer_id", "STRING", "NULLABLE"),
bigquery.SchemaField("billing_account_name", "STRING", "NULLABLE"),
bigquery.SchemaField("account_owner_id", "STRING", "NULLABLE"),
bigquery.SchemaField("billing_profile_id", "INTEGER", "NULLABLE"),
bigquery.SchemaField("subscription_id", "STRING", "NULLABLE"),
bigquery.SchemaField("resource_name", "STRING", "NULLABLE"),
bigquery.SchemaField("billing_profile_name", "STRING", "NULLABLE"),
bigquery.SchemaField("name", "STRING", "NULLABLE"),
bigquery.SchemaField("billing_account_id", "INTEGER", "NULLABLE"),
bigquery.SchemaField("billing_period_start_date", "TIMESTAMP", "NULLABLE", "bq-datetime"),
bigquery.SchemaField("billing_period", "DATE","NULLABLE"),
bigquery.SchemaField("type", "STRING", "NULLABLE"),
bigquery.SchemaField("term", "INTEGER", "NULLABLE"),
bigquery.SchemaField("id", "STRING", "NULLABLE")
]
table = bigquery.Table(table_ref, schema=schema)
table.time_partitioning = bigquery.TimePartitioning(
type_=bigquery.TimePartitioningType.DAY,
field="billing_period", # name of column to use for partitioning
) # 90 days
table = client.create_table(table, exists_ok=True)
view = bigquery.Table(dataset_ref.table("Azure_Summary_View"))
view.view_query = (
" SELECT subscription_id, subscription_name, charge_type, billing_period, date, SUM(cost) as cost FROM `"+project+"."+config['dataset']+"."+config['table_name']+"` " +
" where charge_type = \"Usage\" and (publisher_type = \"Azure\" or publisher_type is null) group by 4,1,2,3,5"+
" UNION"+
" ALL (Select subscription_id, subscription_name, \"Marketplace Purchases\" as charge_type, billing_period, date, SUM(cost) as cost FROM (Select distinct id, billing_period, subscription_id, subscription_name, date, cost from `up-eit-ce-production.CSP_Billing.Azure_Daily_Export` "+
" where publisher_type = \"Marketplace\") group by 4,1,2,3,5)"+
" UNION "+
" ALL (SELECT subscription_id, subscription_name, \"Reserved Instances\" as charge_type, billing_period, date, SUM(cost) as cost FROM `"+project+"."+config['dataset']+"."+config['table_name']+"` "+
" where charge_type = \"Purchase\" group by 4,1,2,3,5)")
view = client.create_table(view, exists_ok=True)