Skip to content
Open
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
4 changes: 4 additions & 0 deletions greenery/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target/
dbt_packages/
logs/
15 changes: 15 additions & 0 deletions greenery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Welcome to your new dbt project!

### Using the starter project

Try running the following commands:
- dbt run
- dbt test


### Resources:
- Learn more about dbt [in the docs](https://docs.getdbt.com/docs/introduction)
- Check out [Discourse](https://discourse.getdbt.com/) for commonly asked questions and answers
- Join the [chat](https://community.getdbt.com/) on Slack for live discussions and support
- Find [dbt events](https://events.getdbt.com) near you
- Check out [the blog](https://blog.getdbt.com/) for the latest news on dbt's development and best practices
Empty file added greenery/analyses/.gitkeep
Empty file.
41 changes: 41 additions & 0 deletions greenery/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: "greenery"
version: "1.0.0"
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: "greenery"

# These configurations specify where dbt should look for different types of files.
# The `model-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"

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

# In this example config, we tell dbt to build all models in the example/
# directory as views. These settings can be overridden in the individual model
# files using the `{{ config(...) }}` macro.
models:
greenery:
+post-hook:
- "{{ grant('reporting') }}"
# Config indicated by + and applies to all files under models/example/
example:
+materialized: view
staging:
+materialized: view
marts:
+materialized: table
Empty file added greenery/macros/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions greenery/macros/grant.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% macro grant(role) %}

{% set sql %}
GRANT USAGE ON SCHEMA {{ schema }} TO ROLE {{ role }};
GRANT SELECT ON {{ this }} TO ROLE {{ role }};
{% endset %}

{% set table = run_query(sql) %}

{% endmacro %}
6 changes: 6 additions & 0 deletions greenery/macros/lbs_to_kgs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% macro lbs_to_kgs(column_name, precision=2) %}
ROUND(
(CASE WHEN {{ column_name }} = -99 THEN NULL ELSE {{ column_name }} END / 2.205)::NUMERIC,
{{ precision }}
)
{% endmacro %}
10 changes: 10 additions & 0 deletions greenery/macros/macros_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2

macros:
- name: sum_of
description: Aggregation function that sums a column with a specific value
arguments:
- name: col_name
description: Target column name
- name: col_value
description: Target value of the column
5 changes: 5 additions & 0 deletions greenery/macros/sum_of.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{%- macro sum_of(col_name, col_value) -%}

sum(case when {{ col_name }} = '{{ col_value }}' then 1 else 0 end)

{%- endmacro -%}
27 changes: 27 additions & 0 deletions greenery/models/example/my_first_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/*
Welcome to your first dbt model!
Did you know that you can also configure models directly within SQL files?
This will override configurations stated in dbt_project.yml

Try changing "table" to "view" below
*/

{{ config(materialized='view') }}

with source_data as (

select 1 as id
union all
select null as id

)

select *
from source_data

/*
Uncomment the line below to remove records with null `id` values
*/

-- where id is not null
6 changes: 6 additions & 0 deletions greenery/models/example/my_second_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- Use the `ref` function to select from other models

select *
from {{ ref('my_first_dbt_model') }}
where id = 1
21 changes: 21 additions & 0 deletions greenery/models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

version: 2

models:
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
17 changes: 17 additions & 0 deletions greenery/models/exposures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2

exposures:
- name: product_funnel
label: Product funnel of Greenery data
type: dashboard
maturity: high
url: https://app.sigmacomputing.com/corise-dbt/workbook/My-Workbook-1gQ02tYzd7dWwrNWtJof0t/edit?:nodeId=3oxgp-Qknb
description: >
This dashboard shows product funnel for Greenery data

depends_on:
- ref('fact_page_views')
- ref('stg_postgres__users')

owner:
name: Yulia
12 changes: 12 additions & 0 deletions greenery/models/intermediate/int_session_timing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{
config(
materialized='view'
)
}}

select
session_id,
min(created_at) as session_started_at,
max(created_at) as session_ended_at
from {{ ref('stg_postgres__events') }}
group by 1
30 changes: 30 additions & 0 deletions greenery/models/marts/product/fact_page_views.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{
config(
materialized='table'
)
}}

{% set event_types = dbt_utils.get_column_values(
table=ref('stg_postgres__events'),
column='event_type')
%}


with session_timing as (
select * from {{ ref('int_session_timing') }}
)

select
e.session_id,
e.user_id,
coalesce(e.product_id, oi.product_id) as product_id,
session_started_at,
session_ended_at,
{% for event_type in event_types %}
{{ sum_of('e.event_type', event_type )}} as {{ event_type }}s,
{% endfor %}
datediff('minute', session_started_at, session_ended_at) as session_length
from {{ ref('stg_postgres__events') }} e
left join {{ ref('stg_postgres__order_items') }} oi using (order_id)
left join session_timing using (session_id)
group by 1,2,3,4,5
14 changes: 14 additions & 0 deletions greenery/models/staging/_postgres_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2

sources:
- name: postgres
database: RAW
schema: PUBLIC
tables:
- name: orders
- name: users
- name: addresses
- name: events
- name: products
- name: order_items
- name: promos
150 changes: 150 additions & 0 deletions greenery/models/staging/postgres/_postgres_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
version: 2

models:
- name: stg_postgres__addresses
description: Table with addresses of clients
columns:
- name: address_id
description: Address unique identifier
tests:
- not_null
- unique
- name: address
description: Address
- name: zipcode
description: Zipcode
- name: state
description: State
- name: country
description: Country

- name: stg_postgres__events
description: Table with events
columns:
- name: event_id
description: Event unique identifier
tests:
- not_null
- unique
- name: session_id
description: Session unique identifier
tests:
- not_null
- name: user_id
description: User unique identifier
tests:
- not_null
- name: event_type
description: Type of event
- name: page_url
description: Webpage of event
- name: created_at
description: Timestamp of creation
- name: order_id
description: Order unique identifier
- name: product_id
description: Product unique identifier

- name: stg_postgres__order_items
description: Table with ordered items
columns:
- name: order_id
description: Order unique identifier
tests:
- not_null
- name: product_id
description: Product unique identifier
tests:
- not_null
- name: quantity
description: Quantity of ordered items

- name: stg_postgres__orders
description: Table with description of orders
columns:
- name: order_id
description: Order unique identifier
tests:
- not_null
- name: promo_id
description: Promo unique identifier
- name: user_id
description: User unique identifier
tests:
- not_null
- name: address_id
description: Address unique identifier
tests:
- not_null
- name: created_at
description: Creation timestamp
- name: order_cost
description: Cost of the order
- name: shipping_cost
description: Cost of the shipping
- name: order_total
description: Total cost of the order (including shipping)
tests:
- dbt_utils.accepted_range:
min_value: 0
max_value: 1000
- name: tracking_id
description: Tracking unique identifier
- name: shipping_service
description: Shipping service provider
- name: estimated_delivery_at
description: Date of estimated delivery
- name: delivered_at
description: Date of executed delivery
- name: status
description: Status of delivery

- name: stg_postgres__products
description: Table with description of products
columns:
- name: product_id
description: Product unique identifier
tests:
- not_null
- name: name
description: Product name
- name: price
description: Product price
- name: inventory
description: Amount of available products

- name: stg_postgres__promos
description: Table with description of promos
columns:
- name: promo_id
description: Promo unique identifier
tests:
- not_null
- name: discount
description: Discount percentage
- name: status
description: Status of promo (active/inactive)

- name: stg_postgres__users
description: Table with users information
columns:
- name: user_id
description: User unique identifier
tests:
- not_null
- name: first_name
description: User first name
- name: last_name
description: User last name
- name: email
description: User email address
- name: phone_number
description: User phone number
- name: created_at
description: Timestamp of creation
- name: updated_at
description: Timestamp of update
- name: address_id
description: Address unique identifier
tests:
- not_null
Loading