Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google Trends example #40

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions metrics/examples/gtrends/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Google Trends

Example pulling some daily [Google Trends](https://support.google.com/trends/answer/12764470?hl=en) metrics from `bigquery-public-data.google_trends.top_terms`.
68 changes: 68 additions & 0 deletions metrics/examples/gtrends/gtrends.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
with

-- get the latest data
latest_data as
(
select
*
FROM
`bigquery-public-data.google_trends.top_terms`
WHERE
-- usually the data is updated every 3 days
refresh_date = date_sub(current_date(), INTERVAL 3 DAY)
),

-- get the latest week
max_week as
(
SELECT
max(week) as week_max
FROM
latest_data
),

-- get the latest data for the latest week
most_recent_week as
(
select
latest_data.*
from
latest_data
join
max_week
on
latest_data.week = max_week.week_max
),

-- calculate the metrics
metrics as
(
select
cast(avg(score) over() as float64) as score_avg,
cast(sum(score) over() as float64) as score_sum,
cast(min(score) over() as float64) as score_min,
cast(max(score) over() as float64) as score_max,
cast(percentile_disc(score, 0.5) over() as float64) as score_p50,
cast(percentile_disc(score, 0.9) over() as float64) as score_p90
from
most_recent_week
limit 1
)

-- unpivot the metrics onto a long format expected by Anomstack
select
current_timestamp() as metric_timestamp,
concat('gtrends_',metric_name) as metric_name,
metric_value,
from
metrics
unpivot(metric_value for metric_name IN (
score_avg,
score_min,
score_max,
score_sum,
score_p50,
score_p90
)
)
;
14 changes: 14 additions & 0 deletions metrics/examples/gtrends/gtrends.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
db: "bigquery"
table_key: "andrewm4894.metrics.metrics"
disable_batch: False
metric_batch: "gtrends"
model_path: "gs://andrewm4894-tmp/models"
ingest_cron_schedule: "30 17 * * *"
train_cron_schedule: "35 17 * * *"
score_cron_schedule: "35 17 * * *"
alert_cron_schedule: "40 17 * * *"
plot_cron_schedule: "45 17 * * *"
alert_methods: "email"
alert_always: False
ingest_sql: >
{% include "./examples/gtrends/gtrends.sql" %}
Loading