Guest User

Untitled

a guest
Nov 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # 1. pip install google-cloud-bigquery
  4. # 2. set up your default credentials
  5. # 3. profit
  6.  
  7. import uuid
  8. from google.cloud import bigquery
  9. client = bigquery.Client()
  10.  
  11. project = "YOUR_PROJECT"
  12. table = "YOUR_BILLING_EXPORT_TABLE_v1"
  13.  
  14. query_job = client.run_async_query(str(uuid.uuid4()), """
  15. SELECT sku.description, service.description, usage_start_time, usage_end_time, project.id, project.name, cost, labels.value, credits.name, credits.amount
  16. FROM flatten([%s:%s], credits)
  17. WHERE (labels.key = 'host' OR labels.key is null)
  18. ORDER BY usage_start_time
  19. """ % (project, table))
  20.  
  21. query_job.begin()
  22. query_job.result()
  23.  
  24. destination_table = query_job.destination
  25. destination_table.reload()
  26.  
  27. total = 0
  28. for row in destination_table.fetch_data():
  29. sku, service, start_time, end_time, project_id, project_name, cost, host, credits_name, credits_amount = row
  30. cost = float(cost)
  31. if credits_amount is None:
  32. credits_amount = 0
  33. else:
  34. credits_amount = float(credits_amount)
  35. start_time = start_time.strftime("%Y-%m-%d %H:%M:%S")
  36. end_time = end_time.strftime("%Y-%m-%d %H:%M:%S")
  37. print("%-20s\t%-20s\t%s\t%s\t%-70s\t%s\t%15.4f\t%s\t%15.4f" % (start_time, end_time, project_name, host, service, sku, cost, credits_name, credits_amount))
Add Comment
Please, Sign In to add comment