Skip to content

Commit

Permalink
chore: housekeeping dbt project (et/somenergia-jardiner!156)
Browse files Browse the repository at this point in the history
Merge branch 'chore/housekeeping-dbt-updates' into 'main'
  • Loading branch information
diegoquintanav committed Sep 10, 2024
2 parents de12acc + dfab8b8 commit 09ef1c2
Show file tree
Hide file tree
Showing 26 changed files with 837 additions and 827 deletions.
20 changes: 11 additions & 9 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ stages: # List of stages for jobs, and their order of execution
- deploy

variables:
CI_PIPELINE_DEBUG_FLAG: $CI_PIPELINE_DEBUG_FLAG
CI_PIPELINE_DEBUG_FLAG: ${CI_PIPELINE_DEBUG_FLAG-false}
GIT_CLONE_PATH: $CI_BUILDS_DIR/$CI_CONCURRENT_ID/$CI_PROJECT_PATH # https://docs.gitlab.com/ee/ci/runners/configure_runners.html#handling-concurrency

SOMENERGIA_REGISTRY: $SOM_HARBOR_DADES_URL
Expand Down Expand Up @@ -96,7 +96,7 @@ build-builder-image-pre-release:
stage: build
before_script: *docker-login-before-script
script:
- docker build --pull -t $IMAGE_NAME_MAIN_PRE_RELEASE --progress=plain --target=main -f docker/main/Dockerfile .
- docker build --pull -t $IMAGE_NAME_MAIN_PRE_RELEASE --progress=plain --target=builder --no-cache -f docker/main/Dockerfile .
- docker push $IMAGE_NAME_MAIN_PRE_RELEASE
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
Expand All @@ -118,7 +118,7 @@ build-main-image-pre-release:
stage: build
before_script: *docker-login-before-script
script:
- docker build --pull -t $IMAGE_NAME_MAIN_PRE_RELEASE --progress=plain --target=main -f docker/main/Dockerfile .
- docker build --pull -t $IMAGE_NAME_MAIN_PRE_RELEASE --progress=plain --target=main --no-cache -f docker/main/Dockerfile .
- docker push $IMAGE_NAME_MAIN_PRE_RELEASE
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
Expand All @@ -140,7 +140,7 @@ build-dbt-deps-image-pre-release:
stage: build
before_script: *docker-login-before-script
script:
- docker build --pull -t $IMAGE_NAME_DBT_DEPS_PRE_RELEASE --progress=plain --target=dbt-deps -f docker/main/Dockerfile .
- docker build --pull -t $IMAGE_NAME_DBT_DEPS_PRE_RELEASE --progress=plain --target=dbt-deps --no-cache -f docker/main/Dockerfile .
- docker push $IMAGE_NAME_DBT_DEPS_PRE_RELEASE
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
Expand Down Expand Up @@ -178,6 +178,11 @@ dbt-build:
- echo "retrieving dbt artifacts from previous succesful job"
# gitlab sometimes fails silently when artifact is invalid or not found, https://gitlab.com/gitlab-org/gitlab/-/issues/29118
- export RESULT=$(curl --output /dev/null --silent --write-out "%{content_type}" "$DBT_MANIFEST_ARTIFACT_URL")
- |
if [ "$CI_PIPELINE_DEBUG_FLAG" = "true" ]; then
echo "Sleeping for 1 hour to allow debugging"
sleep 3600
fi
- |
echo "Artifact content type: ${RESULT}"
if [ "$RESULT" = "application/zip" ]; then
Expand Down Expand Up @@ -219,13 +224,12 @@ release-main-image:
script:
- docker pull $IMAGE_NAME_MAIN_PRE_RELEASE
- docker tag $IMAGE_NAME_MAIN_PRE_RELEASE $IMAGE_NAME_MAIN_RELEASE
# https://blog.container-solutions.com/tagging-docker-images-the-right-way
- docker tag $IMAGE_NAME_MAIN_RELEASE $IMAGE_NAME_MAIN_SHA_RELEASE
- docker push $IMAGE_NAME_MAIN_SHA_RELEASE
- docker push $IMAGE_NAME_MAIN_RELEASE
rules:
# run only on main branch after build-main-image-pre-release is successful
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
changes:
paths: *main-build-changes-paths
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: manual
allow_failure: true
Expand Down Expand Up @@ -343,8 +347,6 @@ release-dbt-deps-image:
script:
- docker pull $IMAGE_NAME_DBT_DEPS_PRE_RELEASE
- docker tag $IMAGE_NAME_DBT_DEPS_PRE_RELEASE $IMAGE_NAME_DBT_DEPS_RELEASE
- docker tag $IMAGE_NAME_DBT_DEPS_PRE_RELEASE $IMAGE_NAME_DBT_DEPS_SHA_RELEASE
- docker push $IMAGE_NAME_DBT_DEPS_SHA_RELEASE
- docker push $IMAGE_NAME_DBT_DEPS_RELEASE
tags:
- somenergia-et
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: check-yaml
args: ["--unsafe"]
Expand All @@ -10,22 +10,22 @@ repos:
- id: name-tests-test
args: ["--pytest-test-first"]
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.0.283"
rev: "v0.6.3"
hooks:
- id: ruff
- repo: https://github.com/sqlfluff/sqlfluff
rev: 2.3.5
rev: 3.1.1
hooks:
# enable sqlfluff-fix will modify sql files.
- id: sqlfluff-fix
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ mkdocs_compose_file := docker-compose.mkdocs.yml
help: ## Print this help
@grep -E '^[0-9a-zA-Z_\-\.]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

sh: ## run a shell in the container
@docker compose run --rm -it --entrypoint sh main

# ---------------------------------------------------------------------------- #
# docker image with target main #
# ---------------------------------------------------------------------------- #

main.bash: ## run a bash shell in the container main
@docker compose run --rm -it --entrypoint bash main

main.build: ## build docker image
@docker compose -f $(main_compose_file) build main --progress=plain

main.push: ## push docker image to registry
@docker compose -f $(main_compose_file) push main

main_dev.bash: ## run a bash shell in the container main_dev
@docker compose run --rm -it --entrypoint bash dev

main_dev.build: ## build docker image for development
@docker compose -f $(main_compose_file) build dev --progress=plain

Expand Down
8 changes: 5 additions & 3 deletions dags/dset_materialize_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def dbapi_to_dict(dbapi: str):
return {
"provider": parsed_string.scheme,
"user": parsed_string.username,
"password": urllib.parse.unquote(parsed_string.password)
if parsed_string.password
else None,
"password": (
urllib.parse.unquote(parsed_string.password)
if parsed_string.password
else None
),
"host": parsed_string.hostname,
"port": parsed_string.port,
"database": parsed_string.path[1:],
Expand Down
8 changes: 5 additions & 3 deletions dags/plant_production_datasets_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def dbapi_to_dict(dbapi: str):
return {
"provider": parsed_string.scheme,
"user": parsed_string.username,
"password": urllib.parse.unquote(parsed_string.password)
if parsed_string.password
else None,
"password": (
urllib.parse.unquote(parsed_string.password)
if parsed_string.password
else None
),
"host": parsed_string.hostname,
"port": parsed_string.port,
"database": parsed_string.path[1:],
Expand Down
14 changes: 8 additions & 6 deletions dbt_jardiner/config/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ dbt_jardiner:
dbname: "{{ env_var('DBNAME') }}"
schema: dbt_prod

prod: # used in production
# used in production
prod:
type: postgres
threads: 1
host: "{{ env_var('DBHOST') }}"
Expand All @@ -22,7 +23,8 @@ dbt_jardiner:
dbname: "{{ env_var('DBNAME') }}"
schema: dbt_prod

pre: # pre-production, not used at the moment
# pre-production, not used at the moment
pre:
type: postgres
threads: 1
host: "{{ env_var('DBHOST') }}"
Expand All @@ -32,7 +34,8 @@ dbt_jardiner:
dbname: "{{ env_var('DBNAME') }}"
schema: dbt_pre

test: # used during CI
# used during CI
test:
type: postgres
threads: 1
host: "{{ env_var('DBHOST') }}"
Expand All @@ -42,7 +45,8 @@ dbt_jardiner:
dbname: "{{ env_var('DBNAME') }}"
schema: dbt_test

dev: # used during local development
# used during local development
dev:
type: postgres
threads: 1
host: "{{ env_var('DBHOST') }}"
Expand Down Expand Up @@ -101,5 +105,3 @@ elementary:
keepalives_idle: 0 # default 0 seconds
connect_timeout: 10 # default 10 seconds

config:
send_anonymous_usage_stats: false
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{ config(enabled=false) }}

-- tests pairs device_uuid, plant, device, device_type, device_parent as unique
with
unique_device_per_plant as (
Expand Down
21 changes: 7 additions & 14 deletions dbt_jardiner/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"

flags:
send_anonymous_usage_stats: false
require_explicit_package_overrides_for_builtin_materializations: false

# Configuring models
# Full documentation: https://docs.getdbt.com/docs/configuring-models

Expand All @@ -45,23 +49,18 @@ models:
+tags: dset
simel:
+grants:
select: ['mercat_electric']
select: ["mercat_electric"]
intermediate:
+docs:
node_color: "#d05808"
dset:
+tags: dset
simel:
+grants:
select: ['mercat_electric']
select: ["mercat_electric"]
marts:
+docs:
node_color: "goldenrod"
dset:
+tags: dset
simel:
+grants:
select: ['mercat_electric']
observability:
+docs:
node_color: "#c3c557"
Expand All @@ -84,16 +83,11 @@ models:
## To disable elementary for dev, uncomment this:
# enabled: "{{ target.name in ['prod','analytics'] }}"

re_data:
+docs:
show: false
node_color: "lightblue"

snapshots:
+docs:
node_color: "#e7bca1"

tests:
data_tests:
+docs:
node_color: "#84b4bc"

Expand All @@ -103,4 +97,3 @@ seeds:
+tags: jardiner
legacy:
+tags: legacy

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ models:
description: energy forecast of the previous day. Discards SomRenovables plants.
config:
tags: meteologica
tests:
data_tests:
- dbt_utils.unique_combination_of_columns:
config:
severity: warn
Expand Down
16 changes: 8 additions & 8 deletions dbt_jardiner/models/jardiner/marts/_marts__models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ models:
description: Conté les lectures crues que venen de la producció horària de les plantes
columns:
- name: hora_inici
tests:
data_tests:
- not_null
- name: hora_final
tests:
data_tests:
- not_null
- name: nom_planta
tests:
data_tests:
- not_null
- name: tecnologia
tests:
data_tests:
- not_null
- name: energia_instantania_inversor_kwh
- name: energia_exportada_instantania_comptador_kwh
tests:
data_tests:
- dbt_utils.expression_is_true:
expression: ">= energia_instantania_inversor_kwh"
config:
severity: warn
where: "hora_inici > (now() at time zone 'Europe/Madrid')::date - interval '7 days'"
tags: source
- name: energia_perduda_inversor_a_comptador
tests:
data_tests:
- dbt_utils.accepted_range:
max_value: 1
min_value: 0
Expand All @@ -79,7 +79,7 @@ models:
- name: irradiation_wh_m2
- name: irradiacio_satellit_wh_m2
- name: temperatura_modul_avg_c
tests:
data_tests:
- dbt_utils.not_null_proportion:
description: //TODO get the right proportion or whatever
error_if: ">=7"
Expand All @@ -93,7 +93,7 @@ models:
enabled: false
where: "hora_inici > (now() at time zone 'Europe/Madrid')::date - interval '7 days'"
- name: pr_horari
tests:
data_tests:
- dbt_utils.accepted_range:
max_value: 1
min_value: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ models:
- name: device_uuid
- name: device_parent
- name: signal_uuid
tests:
data_tests:
- not_null:
severity: warn
config:
Expand All @@ -34,7 +34,7 @@ models:
Valor del senyal en el moment de la lectura. El valor del senyal per
les mètriques à testejat dins d'uns rangs, triat manualment observant
valors actuals Enable tests when signals are more estable
tests:
data_tests:
- dbt_utils.accepted_range:
name: accepted_range_intensity_instantaneous_last_7_days
max_value: 500
Expand Down
4 changes: 4 additions & 0 deletions dbt_jardiner/models/jardiner/raw/dset/_raw_dset__models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ models:
column_anomalies:
- null_count
- null_percent
config:
severity: warn
- name: signal_value
description: Valor del senyal observat. Camp definit per partner DSET
tests:
Expand All @@ -51,6 +53,8 @@ models:
column_anomalies:
- null_count
- null_percent
config:
severity: warn
- name: raw_dset_responses__last_signal_reading
description: >
Conté l'últim batch de dades, descartant ts i value. Es fa servir per obtenir signal_last_ts
Expand Down
4 changes: 2 additions & 2 deletions dbt_jardiner/packages.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
packages:
- package: dbt-labs/dbt_utils
version: [">=1.0.0", "<1.1.0"]
version: [">=1.3.0", "<2.0.0"]
- package: elementary-data/elementary
version: 0.14.1
version: [">=0.14.1", "<0.17.0"]
## Docs: https://docs.elementary-data.com
Loading

0 comments on commit 09ef1c2

Please sign in to comment.