This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for VARIABLE in $(gsutil ls) | |
do | |
echo $(gsutil du $VARIABLE | grep -v /$ | wc -l) $VARIABLE | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |