Skip to content

Instantly share code, notes, and snippets.

View vinoaj's full-sized avatar

Vinoaj (Vinny) Vijeyakumaar vinoaj

  • Sydney, Australia
  • 09:22 (UTC +11:00)
  • X @vinoaj
View GitHub Profile
@vinoaj
vinoaj / bq-auth-with-service-account.py
Created April 2, 2019 21:27
Connect to Google BigQuery using a Google Cloud service account
import os
from google.cloud import bigquery
# Set this to the path to your credentials file
os.environ[
'GOOGLE_APPLICATION_CREDENTIALS'] = 'drd-cloud-sandbox-e21f6cf4f15b.json'
def main():
# Instantiate a Google BigQuery client
WITH avg_runtimes AS (
SELECT
resource.labels.function_name AS function_name
, AVG(CAST(REGEXP_EXTRACT(textPayload, r"\d+") AS INT64)) AS avg_runtime
FROM `<dataset_id>.logging.cloudfunctions_googleapis_com_cloud_functions_*`
WHERE _TABLE_SUFFIX BETWEEN '20190101' AND '20190315'
AND REGEXP_CONTAINS(textPayload, r"^Function execution took \d+ ms.*")
GROUP BY resource.labels.function_name
)
SELECT
WITH error_stats AS (
SELECT
labels.execution_id as execution_id
, MIN(resource.labels.function_name) as name
, MAX(IF(severity="ERROR", 1, 0)) as error
FROM `<dataset_id>.logging.cloudfunctions_googleapis_com_cloud_functions_*`
WHERE _TABLE_SUFFIX BETWEEN '20190101' AND '20190315'
GROUP BY labels.execution_id
)
SELECT
SELECT
labels.execution_id
, timestamp
, CAST(REGEXP_EXTRACT(textPayload, r"\d+") AS INT64) as runtime
FROM `<dataset_id>.logging.cloudfunctions_googleapis_com_cloud_functions_*`
WHERE _TABLE_SUFFIX BETWEEN '20190101' AND '20190315'
AND resource.labels.function_name = "<function_name>"
AND REGEXP_CONTAINS(textPayload, r"^Function execution took \d+ ms.*")
for VARIABLE in $(gsutil ls)
do
echo $(gsutil du $VARIABLE | grep -v /$ | wc -l) $VARIABLE
done
import base64
import json
import logging
from google.cloud import bigquery
# Insert name of Google Cloud Storage bucket
BUCKET_NAME = '<your_bucket_name>'
# Insert Google Cloud project ID
PROJECT = '<your_project_id>'
# Insert required file format, either 'NEWLINE_DELIMITED_JSON', 'CSV' or 'AVRO'
@vinoaj
vinoaj / gcp_billing_costs_total_bigquery_costs.sql
Created March 7, 2019 23:35
BigQuery SQL for GCP Billing Reports: Total BigQuery Costs over the last year
#standardSQL
SELECT
project.id
, service.description
, ROUND(SUM(cost),2) AS total_cost
FROM `<project_id>.<dataset_id>.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE _PARTITIONTIME BETWEEN TIMESTAMP(DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY)) AND TIMESTAMP(CURRENT_DATE())
AND STARTS_WITH(service.description, 'BigQuery')
GROUP BY project.id, service.description
ORDER BY total_cost DESC
@vinoaj
vinoaj / gcp_billing_costs_services_by_project.sql
Created March 7, 2019 23:29
BigQuery SQL for GCP Billing Reports: Total Costs by Service for a specific Project
#standardSQL
SELECT
service.description
, ROUND(SUM(cost),2) AS total_cost
FROM `<project_id>.<dataset_id>.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE _PARTITIONTIME BETWEEN TIMESTAMP(DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY)) AND TIMESTAMP(CURRENT_DATE())
AND project.id = '<project_name>'
GROUP BY service.description
ORDER BY total_cost DESC
@vinoaj
vinoaj / gcp_billing_costs_by_project.sql
Created March 7, 2019 23:21
BigQuery SQL for GCP Billing Reports: Getting Total Costs by Project for the last 4 weeks
#standardSQL
SELECT
project.id AS project_id
, project.name AS project_name
, currency
, ROUND(SUM(cost),2) AS total_cost
FROM `<project_id>.<dataset_id>.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE _PARTITIONTIME BETWEEN TIMESTAMP(DATE_SUB(CURRENT_DATE(), INTERVAL 28 DAY)) AND TIMESTAMP(CURRENT_DATE())
AND project.id IS NOT NULL
GROUP BY project_id, project_name, currency
@vinoaj
vinoaj / ga360_daily_tables.sql
Created January 11, 2019 00:26
BQ SQL GA360: Report on when daily tables were created
# standardSQL
SELECT
*
, TIMESTAMP_MILLIS(CAST(creation_time AS INT64)) AS date_creation
, DATETIME(TIMESTAMP_MILLIS(CAST(creation_time AS INT64)), "Australia/Sydney") AS date_creation_au
FROM `project_id.dataset_id.__TABLES__`
WHERE STARTS_WITH(table_id, "ga_sessions_201901")
ORDER BY table_id DESC