Skip to content

Commit 4896824

Browse files
authored
Merge pull request #15 from bchaoss/dev
update
2 parents a552d23 + c976697 commit 4896824

22 files changed

Lines changed: 744 additions & 166 deletions

.devcontainer/devcontainer.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
]
1919
}
2020
},
21-
"initializeCommand": {
22-
"bi local env setup": "echo \"EVIDENCE_SOURCE__us_tariff_dataset__token=${MOTHERDUCK_TOKEN}\" > reports/.env"
21+
"containerEnv": {
22+
"MOTHERDUCK_TOKEN": "${localEnv:MOTHERDUCK_TOKEN}"
2323
},
24-
"postCreateCommand": {
25-
"dbt dependencies": "pip install -r requirements.txt",
26-
"bi dependencies": "cd reports/ && npm install && npm run sources"
27-
}
24+
"forwardPorts": [3000],
25+
"postCreateCommand": "bash ./.devcontainer/setup.sh"
2826
}

.devcontainer/setup.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -e # stop when error
3+
set -x
4+
5+
# dbt install and deps
6+
pip install -r requirements.txt
7+
8+
# bi install and setup
9+
cd reports
10+
npm install
11+
if [ -z "${MOTHERDUCK_TOKEN}" ]; then
12+
echo "MOTHERDUCK_TOKEN is not setup."
13+
else
14+
echo "EVIDENCE_SOURCE__us_tariff_dataset__token=${MOTHERDUCK_TOKEN}" >> .env
15+
npm run sources
16+
fi
17+
18+
echo "post setup finished."

.github/workflows/dbt-CICD.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,32 @@ on:
99
branches: [ "main" ]
1010
paths:
1111
- 'pipeline/**'
12+
- 'reports/**'
1213

1314
workflow_dispatch:
1415

1516
permissions:
1617
contents: read
1718

1819
jobs:
20+
detect:
21+
runs-on: ubuntu-latest
22+
outputs:
23+
pipeline_changed: ${{ steps.filter.outputs.pipeline }}
24+
reports_changed: ${{ steps.filter.outputs.reports }}
25+
steps:
26+
- id: filter
27+
uses: dorny/paths-filter@v3
28+
with:
29+
filters: |
30+
pipeline:
31+
- 'pipeline/**'
32+
reports:
33+
- 'reports/**'
34+
1935
run-dbt-pipeline:
36+
needs: detect
37+
if: needs.detect.outputs.pipeline_changed == 'true'
2038
runs-on: ubuntu-latest
2139
steps:
2240
- name: Checkout Code
@@ -64,3 +82,16 @@ jobs:
6482
with:
6583
name: previous-manifest
6684
path: target/manifest.json
85+
86+
trigger-reports-workflow:
87+
needs: [detect, run-dbt-pipeline]
88+
if: |
89+
needs.detect.outputs.pipeline_changed == 'true' ||
90+
needs.detect.outputs.reports_changed == 'true'
91+
runs-on: ubuntu-latest
92+
steps:
93+
- name: Dispatch reports workflow
94+
uses: peter-evans/repository-dispatch@v3
95+
with:
96+
token: ${{ secrets.GITHUB_TOKEN }}
97+
event-type: run-reports-workflow

.github/workflows/reports-CICD.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ env:
66
PAGE_DEPLOY_DIR: ${{ github.workspace }}/reports/build/US-Tariff-What-Didnt-and-What-Did
77

88
on:
9-
workflow_run:
10-
workflows: ["dbt_pipeline_cicd"]
11-
types:
12-
- completed
13-
14-
push:
15-
branches: [ "main" ]
16-
paths:
17-
- 'reports/**'
9+
repository_dispatch:
10+
types: [run-reports-workflow]
1811

1912
workflow_dispatch:
2013

ingest/trade.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import pyarrow as pa
66

77
# Configuration
8-
target_monthly_start = "2023-01"
8+
target_monthly_start = "2022-01"
99
DATABASE_NAME = "us_tariff"
1010
SCHEMA = "raw"
1111

12-
target_trade_type_list = ["exports"]
1312
# target_trade_type_list = ["imports", "exports"]
13+
# BY_COUNTRY = False
14+
# BY_ENDUSE = True
15+
16+
target_trade_type_list = ["exports"]
1417
BY_COUNTRY = True
15-
BY_ENDUSE = True
18+
BY_ENDUSE = False
1619

1720

1821
def calc_timeout(monthly_start, by_country=False):

pipeline/dbt_project.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ models:
2222
+schema: marts
2323
+materialized: table
2424

25-
seeds:
26-
seeds_file_name:
27-
+quote_columns: false
25+
# seeds:
26+
# seeds_file_name:
27+
# +quote_columns: false
2828

2929
vars:
3030
raw_data_path: "/workspaces/US-Tariff-What-Didnt-and-What-Did/data"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from {{ source('raw', 'us_trade_exports_country_monthly') }}
2+
3+
select
4+
SUBSTR(cty_code,1,1) AS region_code,
5+
cty_name AS region_name,
6+
7+
where cty_code like '%XXX'
8+
group by all
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- depends_on: {{ ref('dim_region_lookup') }}
2+
3+
from {{ source('raw', 'us_trade_exports_country_monthly') }} a
4+
left join {{ ref('dim_region_lookup') }} c
5+
on SUBSTR(a.cty_code,1,1) = c.region_code
6+
7+
select
8+
DF AS export_code,
9+
case when DF = '1' then 'Domestic Export'
10+
when DF = '2' then 'Foreign Export'
11+
end AS export_type,
12+
13+
a.CTY_CODE,
14+
a.CTY_NAME,
15+
c.REGION_NAME,
16+
17+
YEAR(STRPTIME(time, '%Y-%m')) as year,
18+
SUBSTR(time, 6, 2) as month,
19+
STRPTIME(time || '-01', '%Y-%m-%d')::DATE as month_begin_date,
20+
21+
{% for column in ['ALL_VAL_MO'] %}
22+
{{ column }}::DECIMAL(18,2) AS {{ column }},
23+
{% endfor %}
24+
25+
where not (
26+
a.cty_code='-' -- Total
27+
OR
28+
a.cty_code like '00%' -- Orgs
29+
OR
30+
a.cty_code like '%XXX' -- Regions
31+
)
32+
and DF <> '-'

pipeline/models/marts/fact_us_trade_exports_enduse_country_monthly.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ select
1919

2020
YEAR(STRPTIME(time, '%Y-%m')) as year,
2121
substr(time, 6, 2) as month,
22-
STRPTIME(time || '-01', '%Y-%m-%d') as month_begin_date,
22+
STRPTIME(time || '-01', '%Y-%m-%d')::DATE as month_begin_date,
2323

2424
{% for column in ['ALL_VAL_MO'] %}
2525
a.{{ column }}::DECIMAL(18,2) AS {{ column }},

pipeline/models/marts/fact_us_trade_exports_enduse_monthly.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ select
1616

1717
YEAR(STRPTIME(time, '%Y-%m')) as year,
1818
substr(time, 6, 2) as month,
19-
STRPTIME(time || '-01', '%Y-%m-%d') as month_begin_date,
19+
STRPTIME(time || '-01', '%Y-%m-%d')::DATE as month_begin_date,
2020

2121
{% for column in ['ALL_VAL_MO'] %}
2222
a.{{ column }}::DECIMAL(18,2) AS {{ column }},

0 commit comments

Comments
 (0)