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.
25 changes: 25 additions & 0 deletions greenery/analyses/avg_order_delivery_time.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
with delivered_orders as (

select
created_at_utc,
delivered_at_utc
from
{{ ref('stg_postgres__orders') }}
where
order_status = 'delivered'
),

order_length as (

select
datediff(hour, created_at_utc, delivered_at_utc) as order_length_hours
from
delivered_orders

)

select
round(avg(order_length_hours), 2) as avg_order_delivered_hours,
round(avg(order_length_hours / 24), 2) as avg_order_delivered_days
from
order_length
24 changes: 24 additions & 0 deletions greenery/analyses/avg_orders_per_hour.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
with orders as (

select
created_at_utc,
order_id
from
{{ ref('stg_postgres__orders') }}

),

order_hours as (

select
date_part(hour, created_at_utc) as order_hour,
count(order_id) as order_count
from
orders
group by 1
)

select
round(avg(order_count), 0) as avg_orders_per_hour
from
order_hours
26 changes: 26 additions & 0 deletions greenery/analyses/unique_sessions_per_hour.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
with events as (

select
session_id,
created_at_utc,
date_part(hour, created_at_utc) as event_hour
from
{{ ref('stg_postgres__events') }}

),

sessions_hours as (

select
event_hour,
count(distinct session_id) as unique_sessions
from
events
group by 1
order by 1


)

select avg(unique_sessions) from sessions_hours

13 changes: 13 additions & 0 deletions greenery/analyses/user_count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
with users as (

select
user_id
from
{{ ref('stg_postgres__users') }}

)

select
count(user_id)
from
users
35 changes: 35 additions & 0 deletions greenery/analyses/users_per_order_quantity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
with orders as (

select
user_id,
order_id
from
{{ ref('stg_postgres__orders') }}

),

orders_per_user as (

select
user_id,
count(order_id) as n_orders,
case
when
n_orders = 1 then 'one_order'
when
n_orders = 2 then 'two_orders'
else
'3+_orders'
end as n_orders_bucket
from
orders
group by 1

)

select
n_orders_bucket,
count(user_id) as n_users
from
orders_per_user
group by 1
40 changes: 40 additions & 0 deletions greenery/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# 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"]

target-path: "target" # directory which will store compiled SQL files
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 tables. These settings can be overridden in the individual model files
# using the `{{ config(...) }}` macro.
models:
greenery:
# Config indicated by + and applies to all files under models/example/
example:
+materialized: view
staging:
+materialized: view
Empty file added greenery/macros/.gitkeep
Empty file.
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/positive_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% test positive_values(model, column_name) %}

select
*
from
{{ model }}
where
{{ column_name }} < 0

{% endtest %}
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='table') }}

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
77 changes: 77 additions & 0 deletions greenery/models/staging/postgres/_postgres_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
version: 2

models:
- name: stg_postgres__addresses
description: >
Staging model for address data.
columns:
- name: address_id
description: >
Primary key for stg_postgres__addresses table
- name: stg_postgres__events
description: >
Staging model for event stream data.
columns:
- name: event_id
description: >
Primary key for stg_postgres__events table
- name: user_id
description: >
Foreign key to stg_postgres__users table
- name: order_id
description: >
Foreign key to stg_postgres__orders table
- name: product_id
description: >
Foreign key to stg_postgres__products table
- name: stg_postgres__order_items
description: >
Staging model for order items data
columns:
- name: order_id
description: >
Foreign key to stg_postgres__orders table
- name: product_id
description: >
Foreign key to stg_postgres__products table
- name: stg_postgres__orders
description: >
Staging model for orders data
columns:
- name: order_id
description: >
Primary key for stg_postgres__orders table
- name: user_id
description: >
Foreign key to stg_postgres__users table
- name: address_id
description: >
Foreign key to stg_postgres__addresses table
- name: promo_type
description: >
Foreign key to stg_postgres__promos table
- name: stg_postgres__products
description: >
Staging model for products data
columns:
- name: product_id
description: >
Primary key for stg_postgres__products table
- name: stg_postgres__promos
description: >
Staging model for promos data
columns:
- name: promo_type
description: >
Primary key to stg_postgres__promos table.
Not an ID - more of a promo descriptor that is nonetheless unique.
- name: stg_postgres__users
description: >
Staging model for users data
columns:
- name: user_id
description: >
Primary key to stg_postgres__users table.
- name: address_id
description: >
Foreign key to stg_postgres__addresses table.
43 changes: 43 additions & 0 deletions greenery/models/staging/postgres/_postgres_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: 2

sources:

- name: postgres # name of the source (this can be named whatever you want, but should be descriptive. this source (superheroes) is used for our week 1 tutorial so thats why i named it this way)
schema: public # this is the schema our raw data lives in
database: raw # this is the name of our database

quoting:
database: false
schema: false
identifier: false

freshness:
warn_after: {count: 24, period: hour}
error_after: {count: 48, period: hour}

tables:
- name: addresses
description: >
Contains address details for user or order addresses
- name: events
loaded_at_field: created_at
description: >
Event stream data for our e-commerce website
- name: orders
loaded_at_field: created_at
description: >
Information about each order created on greenery's website
- name: order_items
description: >
Information about the items purchased within each order
- name: products
description: >
Information about the products available for purchase on greenery's website
- name: promos
description: >
Information about promos available for greenery purchases
- name: users
loaded_at_field: created_at
description: >
Information about each greenery userß

Loading