-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from bqbooster/fix-project-list
Fix project list
- Loading branch information
Showing
80 changed files
with
446 additions
and
1,883 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Improve the scalability of the project approach by factoring them into a table | ||
time: 2025-01-11T14:26:13.652366+01:00 | ||
custom: | ||
Author: Kayrnt | ||
Issue: "" |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,4 @@ | ||
# Contributing to dbt-bigquery-monitoring | ||
|
||
## Install setup | ||
|
||
You're free to use the environment management tools you prefer but if you're familiar with those, you can use the following: | ||
|
||
- pipx (to isolate the global tools from your local environment) | ||
- tox (to run the tests) | ||
- pre-commit (to run the linter) | ||
- SQLFluff (to lint SQL) | ||
- changie (to generate CHANGELOG entries) | ||
|
||
### tool setup guide | ||
|
||
To install pipx: | ||
|
||
```bash | ||
pip install pipx | ||
pipx ensurepath | ||
``` | ||
|
||
Then you'll be able to install tox, pre-commit and sqlfluff with pipx: | ||
|
||
```bash | ||
pipx install tox | ||
pipx install pre-commit | ||
pipx install sqlfluff | ||
``` | ||
|
||
To install changie, there are few options depending on your OS. | ||
See the [installation guide](https://changie.dev/guide/installation/) for more details. | ||
|
||
To configure pre-commit hooks: | ||
|
||
```bash | ||
pre-commit install | ||
``` | ||
|
||
To configure your dbt profile, run following command and follow the prompts: | ||
|
||
```bash | ||
dbt init | ||
``` | ||
|
||
## Development workflow | ||
|
||
- Fork the repo | ||
- Create a branch from `main` | ||
- Make your changes | ||
- Run `tox` to run the tests | ||
- Create your changelog entry with `changie new` (don't edit directly the CHANGELOG.md) | ||
- Commit your changes (it will run the linter through pre-commit) | ||
- Push your branch and open a PR on the repository | ||
|
||
## Adding a CHANGELOG Entry | ||
|
||
We use changie to generate CHANGELOG entries. Note: Do not edit the CHANGELOG.md directly. Your modifications will be lost. | ||
|
||
Follow the steps to [install changie](https://changie.dev/guide/installation/) for your system. | ||
|
||
Once changie is installed and your PR is created, simply run `changie new` and changie will walk you through the process of creating a changelog entry. Commit the file that's created and your changelog entry is complete! | ||
|
||
### SQLFluff | ||
|
||
We use SQLFluff to keep SQL style consistent. By installing `pre-commit` per the initial setup guide above, SQLFluff will run automatically when you make a commit locally. A GitHub action automatically tests pull requests and adds annotations where there are failures. | ||
|
||
Lint all models in the /models directory: | ||
```bash | ||
tox -e lint_all | ||
``` | ||
|
||
Fix all models in the /models directory: | ||
```bash | ||
tox -e fix_all | ||
``` | ||
|
||
Lint (or subsitute lint to fix) a specific model: | ||
```bash | ||
tox -e lint -- models/path/to/model.sql | ||
``` | ||
|
||
Lint (or subsitute lint to fix) a specific directory: | ||
```bash | ||
tox -e lint -- models/path/to/directory | ||
``` | ||
|
||
#### Rules | ||
|
||
Enforced rules are defined within `tox.ini`. To view the full list of available rules and their configuration, see the [SQLFluff documentation](https://docs.sqlfluff.com/en/stable/rules.html). | ||
|
||
## Generation of dbt base google models | ||
|
||
dbt base google models are generated in another dedicated project hosted in: | ||
https://github.com/bqbooster/dbt-bigquery-monitoring-parser | ||
|
||
It was separated to ensure that users don't install the parser (and tests) when they install the dbt package. | ||
See related documentation page: | ||
https://bqbooster.github.io/dbt-bigquery-monitoring/contributing |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% macro dbt_bigquery_monitoring_materialization() %} | ||
{% set projects = project_list() %} | ||
{#- If the user has set the materialization in the config that's different from the default -#} | ||
{% if var('google_information_schema_model_materialization') != 'placeholder' %} | ||
{% set materialization = var('google_information_schema_model_materialization') %} | ||
{% elif projects|length == 0 %} | ||
{% set materialization = 'ephemeral' %} | ||
{% else %} | ||
{% set materialization = 'project_by_project_table' %} | ||
{% endif %} | ||
{{ return(materialization) }} | ||
{% endmacro %} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{%- materialization project_by_project_table, adapter='bigquery' -%} | ||
|
||
{% set target_relation = this %} | ||
{% set existing_relation = load_relation(this) %} | ||
{% set projects = project_list() %} | ||
{%- set raw_partition_by = config.get('partition_by', none) -%} | ||
{%- set partition_config = adapter.parse_partition_by(raw_partition_by) -%} | ||
{%- set full_refresh_mode = (should_full_refresh()) -%} | ||
|
||
{{ run_hooks(pre_hooks) }} | ||
|
||
{%- set sql_no_data = sql + " LIMIT 0" %} | ||
|
||
-- Create the table if it doesn't exist or if we're in full-refresh mode | ||
{% if existing_relation is none or full_refresh_mode %} | ||
{% call statement('main') -%} | ||
{% if partition_config is not none %} | ||
{% set build_sql = create_table_as(False, target_relation, sql_no_data) %} | ||
{% else %} | ||
{% set build_sql = create_table_as(False, target_relation, sql_no_data) %} | ||
{% endif %} | ||
{{ build_sql }} | ||
{%- endcall %} | ||
{% else %} | ||
{% call statement('main') -%} | ||
SELECT 1 | ||
{%- endcall %} | ||
{% if partition_config is not none %} | ||
-- Get the maximum partition value | ||
{% set max_partition_sql %} | ||
SELECT FORMAT_TIMESTAMP("%F %T", MAX({{ partition_config.field }})) as max_partition | ||
FROM {{ target_relation }} | ||
WHERE {{ partition_config.field }} IS NOT NULL | ||
{% endset %} | ||
{% else %} | ||
-- Truncate the table if partition_by is not defined | ||
{% set truncate_sql %} | ||
TRUNCATE TABLE {{ target_relation }} | ||
{% endset %} | ||
{{ truncate_sql }} | ||
{% do run_query(truncate_sql) %} | ||
{% endif %} | ||
{% if partition_config is not none %} | ||
{% set max_partition_result = run_query(max_partition_sql) %} | ||
{% if max_partition_result|length > 0 %} | ||
{% set max_partition_value = max_partition_result.columns[0].values()[0] %} | ||
{% endif %} | ||
{% endif %} | ||
{% endif %} | ||
|
||
-- If we have projects, process them one by one | ||
{% if projects|length > 0 %} | ||
{% set all_insert_sql = [] %} | ||
{% for project in projects %} | ||
{% set project_sql = sql | replace('`region-', '`' ~ project | trim ~ '`.`region-') %} | ||
{% if existing_relation is not none and partition_config is not none and max_partition_value is not none and max_partition_value | length > 0 %} | ||
{% set where_condition = 'WHERE ' ~ partition_config.field ~ ' >= TIMESTAMP_TRUNC("' ~ max_partition_value ~ '", HOUR)' %} | ||
{% set insert_sql %} | ||
DELETE FROM {{ target_relation }} | ||
{{ where_condition }}; | ||
|
||
INSERT INTO {{ target_relation }} | ||
{{ project_sql }} | ||
{{ where_condition }} | ||
{% endset %} | ||
{% else %} | ||
{#- bigquery doesn't allow more than 4000 partitions per insert so if we have hourly tables it's ~ 166 days -#} | ||
{% set project_sql = project_sql + ' WHERE ' ~ partition_config.field ~ ' >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 166 DAY)' %} | ||
{% set insert_sql %} | ||
INSERT INTO {{ target_relation }} | ||
{{ project_sql }} | ||
{% endset %} | ||
{% endif %} | ||
{% do all_insert_sql.append(insert_sql) %} | ||
{% endfor %} | ||
{% call statement('insert') -%} | ||
{{ all_insert_sql | join(';\n') }} | ||
{%- endcall %} | ||
{% endif %} | ||
|
||
{{ run_hooks(post_hooks) }} | ||
{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke) %} | ||
{% do persist_docs(target_relation, model) %} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{%- endmaterialization -%} |
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
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
22 changes: 2 additions & 20 deletions
22
models/base/google/bi_engine/information_schema_bi_capacities.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,10 @@ | ||
{{ config(materialized=dbt_bigquery_monitoring_materialization()) }} | ||
{# More details about base table in https://cloud.google.com/bigquery/docs/information-schema-bi-capacities -#} | ||
|
||
WITH base AS ( | ||
{% if project_list()|length > 0 -%} | ||
{% for project in project_list() -%} | ||
SELECT project_id, project_number, bi_capacity_name, size, preferred_tables | ||
FROM `{{ project | trim }}`.`region-{{ var('bq_region') }}`.`INFORMATION_SCHEMA`.`BI_CAPACITIES` | ||
{% if not loop.last %}UNION ALL{% endif %} | ||
{% endfor %} | ||
{%- else %} | ||
SELECT | ||
SELECT | ||
project_id, | ||
project_number, | ||
bi_capacity_name, | ||
size, | ||
preferred_tables | ||
FROM `region-{{ var('bq_region') }}`.`INFORMATION_SCHEMA`.`BI_CAPACITIES` | ||
{%- endif %} | ||
) | ||
|
||
SELECT | ||
project_id, | ||
project_number, | ||
bi_capacity_name, | ||
size, | ||
preferred_tables, | ||
FROM | ||
base |
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
Oops, something went wrong.