Skip to content

Commit

Permalink
atualiza tratamento de fare_rule e feed_info
Browse files Browse the repository at this point in the history
  • Loading branch information
vtr363 committed Jul 22, 2024
1 parent dddc6bc commit 3bb5964
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
8 changes: 8 additions & 0 deletions queries/models/gtfs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog - gtfs

## [1.1.6] - 2024-07-22

### Alterado

- Alterada a tabela `feed_info` de table para incremental e adicionada a coluna `feed_update_datetime`(https://github.com/prefeitura-rio/pipelines_rj_smtr/pull/82)

- Alterada a tabela `fare_rules` para refletir a alteração nas primary keys (https://github.com/prefeitura-rio/pipelines_rj_smtr/pull/82)

## [1.1.5] - 2024-07-15

### Adicionado
Expand Down
4 changes: 2 additions & 2 deletions queries/models/gtfs/fare_rules_gtfs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ SELECT
fi.feed_version,
SAFE_CAST(fr.data_versao AS DATE) feed_start_date,
fi.feed_end_date,
SAFE_CAST(JSON_VALUE(fr.content, '$.fare_id') AS STRING) fare_id,
SAFE_CAST(JSON_VALUE(fr.content, '$.route_id') AS STRING) route_id,
SAFE_CAST(fr.fare_id AS STRING) fare_id,
SAFE_CAST(fr.route_id AS STRING) route_id,
SAFE_CAST(JSON_VALUE(fr.content, '$.origin_id') AS STRING) origin_id,
SAFE_CAST(JSON_VALUE(fr.content, '$.destination_id') AS STRING) destination_id,
SAFE_CAST(JSON_VALUE(fr.content, '$.contains_id') AS STRING) contains_id,
Expand Down
67 changes: 48 additions & 19 deletions queries/models/gtfs/feed_info_gtfs.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
{{config(
materialized='table',
materialized='incremental',
partition_by = { 'field' :'feed_start_date',
'data_type' :'date',
'granularity': 'day' },
alias = 'feed_info'
alias = 'feed_info',
unique_key = 'feed_start_date',
incremental_strategy = 'insert_overwrite'
)}}


SELECT
SAFE_CAST(timestamp_captura AS STRING) AS feed_version,
SAFE_CAST(data_versao AS DATE) AS feed_start_date,
DATE_SUB(LEAD(DATE(data_versao)) OVER (ORDER BY data_versao), INTERVAL 1 DAY) AS feed_end_date,
SAFE_CAST(feed_publisher_name AS STRING) feed_publisher_name,
SAFE_CAST(JSON_VALUE(content, '$.feed_publisher_url') AS STRING) feed_publisher_url,
SAFE_CAST(JSON_VALUE(content, '$.feed_lang') AS STRING) feed_lang,
SAFE_CAST(JSON_VALUE(content, '$.default_lang') AS STRING) default_lang,
SAFE_CAST(JSON_VALUE(content, '$.feed_contact_email') AS STRING) feed_contact_email,
SAFE_CAST(JSON_VALUE(content, '$.feed_contact_url') AS STRING) feed_contact_url,
'{{ var("version") }}' AS versao_modelo
FROM
{{ source(
'br_rj_riodejaneiro_gtfs_staging',
'feed_info'
) }}
WITH feed_info AS (
SELECT
SAFE_CAST(timestamp_captura AS STRING) AS feed_version,
SAFE_CAST(data_versao AS DATE) AS feed_start_date,
NULL AS feed_end_date,
SAFE_CAST(feed_publisher_name AS STRING) feed_publisher_name,
SAFE_CAST(JSON_VALUE(content, '$.feed_publisher_url') AS STRING) feed_publisher_url,
SAFE_CAST(JSON_VALUE(content, '$.feed_lang') AS STRING) feed_lang,
SAFE_CAST(JSON_VALUE(content, '$.default_lang') AS STRING) default_lang,
SAFE_CAST(JSON_VALUE(content, '$.feed_contact_email') AS STRING) feed_contact_email,
SAFE_CAST(JSON_VALUE(content, '$.feed_contact_url') AS STRING) feed_contact_url,
CURRENT_DATETIME("America/Sao_Paulo") AS feed_update_datetime,
'{{ var("version") }}' AS versao_modelo
FROM
{{ source(
'br_rj_riodejaneiro_gtfs_staging',
'feed_info'
) }}
{% if is_incremental() %}
WHERE
data_versao = '{{ var("data_versao_gtfs") }}'
UNION ALL
SELECT
*
FROM
{{ this }}
WHERE
feed_start_date != DATE('{{ var("data_versao_gtfs") }}')
{% endif %}
)
SELECT
feed_version,
feed_start_date,
DATE_SUB(LEAD(DATE(feed_version)) OVER (ORDER BY feed_version), INTERVAL 1 DAY) AS feed_end_date,
feed_publisher_name,
feed_publisher_url,
feed_lang,
default_lang,
feed_contact_email,
feed_contact_url,
feed_update_datetime,
versao_modelo
FROM
feed_info

0 comments on commit 3bb5964

Please sign in to comment.