Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update codebase for demo #3

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
1 change: 0 additions & 1 deletion README.md

This file was deleted.

59 changes: 0 additions & 59 deletions cube.js

This file was deleted.

85 changes: 85 additions & 0 deletions cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from cube import config, file_repository
import json
import os

# config.base_path = '/cube-api'

# config.schema_path = 'models'

# config.telemetry = False

# Access Control

# @config('query_rewrite')
# def query_rewrite(query: dict, ctx: dict) -> dict:
# if 'user_id' in ctx['securityContext']:
# query['filters'].append({
# 'member': 'orders_view.users_id',
# 'operator': 'equals',
# 'values': [ctx['securityContext']['user_id']]
# })
# return query

# Dynamic Data Model

# @config('context_to_app_id')
# def context_mapping(ctx: dict):
# return ctx['securityContext'].setdefault('team')

# @config('check_sql_auth')
# def check_sql_auth(query: dict, username: str, password: str) -> dict:
# security_context = {
# 'team': username
# }
# return {
# 'password': os.environ['CUBEJS_SQL_PASSWORD'],
# 'securityContext': security_context
# }

# @config('driver_factory')
# def driver_factory(ctx: dict) -> None:
# context = ctx['securityContext']
# data_source = ctx['dataSource']

# if data_source == 'postgres':
# return {
# 'type': 'postgres',
# 'host': 'demo-db-examples.cube.dev',
# 'user': 'cube',
# 'password': '12345',
# 'database': 'ecom'
# }


# Other

# @config('repository_factory')
# def repository_factory(ctx: dict) -> list[dict]:
# return file_repository('models')

# @config('logger')
# def logger(message: str, params: dict) -> None:
# print(f'MY CUSTOM LOGGER --> {message}: {params}')

# @config('context_to_api_scopes')
# def context_to_api_scopes(context: dict, default_scopes: list[str]) -> list[str]:
# return ['meta', 'data', 'graphql']

@config('semantic_layer_sync')
def sls(ctx: dict) -> list:
pat_name = os.getenv('TABLEAU_PAT_NAME_CUBEDEV', None)
pat_secret = os.getenv('TABLEAU_PAT_SECRET_CUBEDEV', None)
if pat_name and pat_secret:
return [{
"type": "tableau-cloud",
"name": "Tableau Cloud Sync",
"config": {
"database": "Cube Cloud: artyom_cube_demo",
"region": "us-west-2b",
"site": "cubedev",
"personalAccessToken": pat_name,
"personalAccessTokenSecret": pat_secret
}
}]
else:
return []
10 changes: 0 additions & 10 deletions model/cubes/defaults.py

This file was deleted.

32 changes: 15 additions & 17 deletions model/cubes/line_items.yml.jinja → model/cubes/line_items.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% set scheduled_refresh = get_scheduled_refresh() %}

cubes:
- name: line_items
sql_table: ECOM.LINE_ITEMS
Expand All @@ -10,16 +8,24 @@ cubes:
sql: "{CUBE}.PRODUCT_ID = {products}.ID"
relationship: many_to_one

- name: orders
sql: "{CUBE}.ORDER_ID = {orders}.ID"
relationship: many_to_one

dimensions:
- name: id
sql: ID
type: number
primary_key: true

- name: price
sql: PRICE
type: number

- name: order_id
sql: ORDER_ID
type: number

- name: product_id
sql: PRODUCT_ID
type: number

- name: created_at
sql: CREATED_AT
type: time
Expand All @@ -31,14 +37,6 @@ cubes:
- name: total_amount
sql: PRICE
type: sum

pre_aggregations:
- name: line_items_summary
scheduled_refresh: {{ scheduled_refresh }}
measures:
- count
- total_amount
time_dimension: created_at
granularity: day
dimensions:
- products.product_category
format: currency
description: "The total amount sold"

45 changes: 41 additions & 4 deletions model/cubes/orders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,84 @@ cubes:
sql: "{CUBE}.USER_ID = {users}.ID"
relationship: many_to_one

- name: line_items
sql: "{CUBE}.ID = {line_items}.ORDER_ID"
relationship: one_to_many

dimensions:
- name: id
sql: ID
type: number
primary_key: true

- name: user_id
sql: USER_ID
type: number
public: false

- name: status
description: "Status can be processing, shipped or completed"
sql: STATUS
type: string

- name: created_at
description: "Order created timestamp"
sql: CREATED_AT
type: time

- name: completed_at
description: "Order completed timestamp. An order is completed when it has arrived at its destination"
sql: COMPLETED_AT
type: time

- name: days_to_complete
description: "The number of days between order creation and completion (arriving at its destination)"
sql: "{completed_at}::DATE - {created_at}::DATE"
type: number

- name: amount
description: "The total dollar amount of the order"
sql: '{line_items.total_amount}'
type: number
sub_query: true

measures:
- name: count
description: "Count of orders"
type: count

- name: completed_count
description: "Count of orders with status 'completed'"
type: count
filters:
- sql: "{CUBE}.STATUS = 'completed'"
- sql: "{CUBE}.STATUS = 'completed'"

- name: completed_percentage
description: "Percent of orders with status 'completed'"
type: number
sql: "({completed_count} / NULLIF({count}, 0)) * 100.0"
format: percent

- name: avg_days_to_complete
description: "Average number of days between order creation and order completion (arriving at its destination)"
sql: "{days_to_complete}"
type: avg

- name: total_amount
description: "The total dollar amount of orders sold, used to calculate revenue"
sql: '{CUBE.amount}'
type: sum
format: currency

- name: average_order_value
sql: '{CUBE.amount}'
type: avg
pre_aggregations:
- name: orders_rollup
dimensions:
- status
measures:
- count
- completed_count
- total_amount
time_dimension: created_at
granularity: day
refresh_key:
every: 12 hours
7 changes: 6 additions & 1 deletion model/cubes/products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ cubes:
type: number
primary_key: true

- name: product_category
- name: category
description: "Product category"
sql: PRODUCT_CATEGORY
type: string

- name: name
description: "Product name"
sql: NAME
type: string

- name: created_at
description: "Timestamp of product creation"
sql: CREATED_AT
type: time

measures:
- name: count
description: "Product count"
type: count

40 changes: 23 additions & 17 deletions model/cubes/users.yml.jinja → model/cubes/users.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
{# Is the current team trusted? #}
{% set trusted_teams = ['cx', 'exec' ] %}
{% set is_trusted_team = COMPILE_CONTEXT.securityContext.team in trusted_teams %}

{# Convenient function to mask values if the current team is not trusted #}
{% macro masked(sql, is_visible) -%}
{{ sql if is_visible else "\"'--- masked ---'\"" }}
{%- endmacro %}

cubes:
- name: users
sql_table: ECOM.USERS
Expand All @@ -18,30 +9,45 @@ cubes:
type: number
primary_key: true

- name: state
sql: STATE
- name: age
description: "The age of the user"
sql: AGE
type: number

- name: gender
description: "Options are male and female"
sql: GENDER
type: string

- name: city
description: "The user's city"
sql: CITY
type: string

- name: gender
sql: GENDER
type: string

- name: last_name
sql: {{ masked('LAST_NAME', is_trusted_team) }}
sql: LAST_NAME
type: string

- name: first_name
sql: {{ masked('FIRST_NAME', is_trusted_team) }}
sql: FIRST_NAME
type: string

- name: name
description: "The user's full name"
sql: "{first_name} || {last_name}"
type: string

- name: state
description: "The user's state"
sql: STATE
type: string

- name: created_at
description: "Timestamp of user account creation"
sql: CREATED_AT
type: time

measures:
- name: count
description: "User count"
type: count
Loading
Loading