Skip to content

Commit

Permalink
Merge pull request #2 from matthewshaver/new-jaffle
Browse files Browse the repository at this point in the history
New jaffle
  • Loading branch information
matthewshaver authored Jun 10, 2024
2 parents 8815b97 + 3e2ba45 commit b788a0d
Show file tree
Hide file tree
Showing 50 changed files with 152,044 additions and 525 deletions.
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: requirements-txt-fixer
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
Empty file added analyses/.gitkeep
Empty file.
Empty file added data-tests/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions data-tests/positive_payment_amount.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select
order_total_cents,
sum(order_total_cents) as total_amount
from {{ ref('stg_orders')}}
group by 1
having total_amount < 0
41 changes: 22 additions & 19 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
name: 'jaffle_shop'

config-version: 2
version: '0.1'

profile: 'jaffle_shop'
name: "jaffle_shop"
version: "3.0.0"
require-dbt-version: ">=1.5.0"

dbt-cloud:
project-id: 365760 # Put your project id here

profile: default

model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["data-tests"]
seed-paths: ["seeds"]
test-paths: ["tests"]
analysis-paths: ["analysis"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"
clean-targets:
- "target"
- "dbt_modules"
- "logs"
- "target"
- "dbt_packages"

require-dbt-version: [">=1.0.0", "<2.0.0"]
vars:
truncate_timespan_to: "{{ dbt.cast(dbt.current_timestamp(), 'datetime') }}"
"dbt_date:time_zone": "America/Los_Angeles"

seeds:
+docs:
node_color: '#cd7f32'
seeds:
jaffle_shop:
+schema: raw

models:
jaffle_shop:
materialized: table
staging:
materialized: view
+docs:
node_color: 'silver'
+docs:
node_color: 'gold'
+materialized: view
marts:
+materialized: table
13 changes: 13 additions & 0 deletions macros/cents_to_dollars.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{# A basic example for a project-wide macro to cast a column uniformly #}

{% macro cents_to_dollars(column_name) -%}
{{ return(adapter.dispatch('cents_to_dollars')(column_name)) }}
{%- endmacro %}

{% macro default__cents_to_dollars(column_name) -%}
({{ column_name }} / 100)::numeric(16, 2)
{%- endmacro %}

{% macro bigquery__cents_to_dollars(column_name) %}
round(cast(({{ column_name }} / 100) as numeric), 2)
{% endmacro %}
23 changes: 23 additions & 0 deletions macros/generate_schema_name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% macro generate_schema_name(custom_schema_name, node) %}

{% set default_schema = target.schema %}

{# seeds go in a global `raw` schema #}
{% if node.resource_type == 'seed' %}
{{ custom_schema_name | trim }}

{# non-specified schemas go to the default target schema #}
{% elif custom_schema_name is none %}
{{ default_schema }}


{# specified custom schema names go to the schema name prepended with the the default schema name in prod (as this is an example project we want the schemas clearly labeled) #}
{% elif target.name == 'prod' %}
{{ default_schema }}_{{ custom_schema_name | trim }}

{# specified custom schemas go to the default target schema for non-prod targets #}
{% else %}
{{ default_schema }}
{% endif %}

{% endmacro %}
69 changes: 0 additions & 69 deletions models/customers.sql

This file was deleted.

14 changes: 0 additions & 14 deletions models/docs.md

This file was deleted.

58 changes: 58 additions & 0 deletions models/marts/customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
with

customers as (

select * from {{ ref('stg_customers') }}

),

orders as (

select * from {{ ref('orders') }}

),

customer_orders_summary as (

select
orders.customer_id,

count(distinct orders.order_id) as count_lifetime_orders,
count(distinct orders.order_id) > 1 as is_repeat_buyer,
min(orders.ordered_at) as first_ordered_at,
max(orders.ordered_at) as last_ordered_at,
sum(orders.subtotal) as lifetime_spend_pretax,
sum(orders.tax_paid) as lifetime_tax_paid,
sum(orders.order_total) as lifetime_spend

from orders

group by 1

),

joined as (

select
customers.*,

customer_orders_summary.count_lifetime_orders,
customer_orders_summary.first_ordered_at,
customer_orders_summary.last_ordered_at,
customer_orders_summary.lifetime_spend_pretax,
customer_orders_summary.lifetime_tax_paid,
customer_orders_summary.lifetime_spend,

case
when customer_orders_summary.is_repeat_buyer then 'returning'
else 'new'
end as customer_type

from customers

left join customer_orders_summary
on customers.customer_id = customer_orders_summary.customer_id

)

select * from joined
106 changes: 106 additions & 0 deletions models/marts/customers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
models:
- name: customers
description: Customer overview data mart, offering key details for each unique customer. One row per customer.
tests:
- dbt_utils.expression_is_true:
expression: "lifetime_spend_pretax + lifetime_tax_paid = lifetime_spend"
columns:
- name: customer_id
description: The unique key of the orders mart.
tests:
- not_null
- unique
- name: customer_name
description: Customers' full name.
- name: count_lifetime_orders
description: Total number of orders a customer has ever placed.
- name: first_ordered_at
description: The timestamp when a customer placed their first order.
- name: last_ordered_at
description: The timestamp of a customer's most recent order.
- name: lifetime_spend_pretax
description: The sum of all the pre-tax subtotals of every order a customer has placed.
- name: lifetime_tax_paid
description: The sum of all the tax portion of every order a customer has placed.
- name: lifetime_spend
description: The sum of all the order totals (including tax) that a customer has ever placed.
- name: customer_type
description: Options are 'new' or 'returning', indicating if a customer has ordered more than once or has only placed their first order to date.
tests:
- accepted_values:
values: ["new", "returning"]

semantic_models:
- name: customers
defaults:
agg_time_dimension: first_ordered_at
description: |
Customer grain mart.
model: ref('customers')
entities:
- name: customer
expr: customer_id
type: primary
dimensions:
- name: customer_name
type: categorical
- name: customer_type
type: categorical
- name: first_ordered_at
type: time
type_params:
time_granularity: day
- name: last_ordered_at
type: time
type_params:
time_granularity: day
measures:
- name: customers
description: Count of unique customers
agg: count_distinct
- name: count_lifetime_orders
description: Total count of orders per customer.
agg: sum
- name: lifetime_spend_pretax
description: Customer lifetime spend before taxes.
agg: sum
- name: lifetime_spend
agg: sum
description: Gross customer lifetime spend inclusive of taxes.

metrics:
- name: lifetime_spend_pretax
description: Customer's lifetime spend before tax
label: LTV Pre-tax
type: simple
type_params:
measure: lifetime_spend_pretax
- name: count_lifetime_orders
description: Count of lifetime orders
label: Count Lifetime Orders
type: simple
type_params:
measure: count_lifetime_orders
- name: average_order_value
description: LTV pre-tax / number of orders
label: Average Order Value
type: derived
type_params:
metrics:
- count_lifetime_orders
- lifetime_spend_pretax
expr: lifetime_spend_pretax / count_lifetime_orders

saved_queries:
- name: customer_order_metrics
query_params:
metrics:
- count_lifetime_orders
- lifetime_spend_pretax
- average_order_value
group_by:
- Entity('customer')
exports:
- name: customer_order_metrics
config:
export_as: table
9 changes: 9 additions & 0 deletions models/marts/locations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with

locations as (

select * from {{ ref('stg_locations') }}

)

select * from locations
Loading

0 comments on commit b788a0d

Please sign in to comment.