Cubes – modelling patterns

Cubes modeling patterns
r2, December 2012, Cubes v0.10.1 Schema 1
store id code address sales product_id store_id amount product id code name

Model or configuration
"cubes": [ { "name": "sales", "dimensions": ["product", "store"], "joins": [ {"master":"product_id", "detail":"product.id"}, {"master":"store_id", "detail":"store.id"} ] } ], "dimensions": [ { "name": "product", "attributes": ["code", "name"] }, { "name": "store", "attributes": ["code", "address"] } ]

fact table has same name as cube, dimension tables have same names as dimensions

2
store ...

sales ... year amount product ...

dimension represented by only one attribute in fact table

"cubes": [ { ... "dimensions": ["product", "store", "year"], ... } ], "dimensions": [ ... { "name": "year" } ]

3

Python:
dim_store id code address ft_sales product_id store_id amount dim_product id code name

cubes.create_workspace("sql", url=DATABASE_URL, dimension_prefix="dim_", fact_prefix="fact_") slicer.ini:

all dimension tables have prefix “dim_” and all fact tables have prefix “ft_”

[workspacee] dimension_prefix="dim_" fact_prefix="fact_"

4

schema: sales_datamart
store id code address sales product_id store_id amount product id code name

Python: cubes.create_workspace("sql", url=DATABASE_URL, schema="sales_datamart") slicer.ini: [workspacee] schema="sales_datamart"

all tables are stored in other than default database schema

5

dimensions
store id code address

facts
sales product_id store_id amount product id code name

Python: cubes.create_workspace("sql", url=DATABASE_URL, schema="facts", dimension_schema="dimensions", ) slicer.ini: [workspacee] schema="facts" dimensions_schema="dimensions"

all fact tables are stored in one schema, all dimension tables in another

Cubes – modelling patterns

Schema 6
store ... sales ... sales_year total_amount product ...

Model or configuration
"cubes": [ { "dimensions": [..., "year"], "measures": ["amount"], "mappings": { "year":"sales_year", "amount":"total_amount"] } } ], "dimensions": [ ... { "name": "year" } ]

flat dimension is called “year”, but column is “sales_year”; measure is reported as “amount”, column is named “total_amount”

7

dim_suppliers ft_sales supplier_id client_id amount id name address dim_clients id name address dim_organisation id name address

clients and suppliers share one table with all organisations and companies

"cubes": [ { "name": "sales" "dimensions": ["supplier", "client"], "measures": ["amount"], "joins": [ { "master":"supplier_id", "detail":"dim_organisation.id", "alias":"dim_supplier" }, { "master":"client_id", "detail":"dim_organisation.id", "alias":"dim_client" } ] } ], "dimensions": [ { "name": "supplier", "attributes": ["id", "name", "address"] } { "name": "client", "attributes": ["id", "name", "address"] } ]

8

sales product_id ... amount

product id code name category_code category product category

product dimension has two levels: product category and product

"cubes": [ { "dimensions": ["product", ...], "measures": ["amount"], "joins": [ {"master":"product_id", "detail":"product.id"} ] } ], "dimensions": [ { "name": "product", "levels": [ { "name":"category", "attributes": ["category_code", "category"] }, { "name":"product", "attributes": ["code", "name"] } ] } ]

Cubes – modelling patterns

Schema 9
key label
product id code name price

Model or configuration

"dimensions": [ { "name": "product", "levels": [ { "name": "product", "attributes": ["code", "name", "price"] "key": "code", "label_attribute": "name" } ] } ] Use: result = browser.aggregate(drilldown=["product"]) for row in result.table_rows("product"): print "%s: %s" % (row.label, row.record["amount_sum"])

aggregate or filter

report
Product coffee tea milk Total Amount 200 250 50 500

attribute code to be used for aggregation, filtering or links and attribute name used as labels in user interface tables

10

product id code name price

sales product_id ... amount

Product ...

Unit Price ...

Amount ...

user interface labels for dimensions, dimension attributes and measures

"cubes": [ { "name": "sales", "label": "Product Sales", "dimensions": ["product", ...] } ], "dimensions": [ { "name": "product", "label": "Product", "attributes": [ {"name": "code", "label": "Code"}, {"name": "name", "label": "Product"}, {"name": "price", "label": "Unit Price"}, ] } ]

11

dim_date id year quarter month month_name week day weekday

year quarter month day

year month day

(in “dimensions”) { "name":"date", "levels": [ { "name": "year", "attributes": ["year"] }, { "name": "quarter", "attributes": ["quarter"] }, { "name": "month", "attributes": ["month", "month_name"] }, { "name": "week", "attributes": ["week"] }, { "name": "weekday", "attributes": ["weekday"] }, { "name": "day", "attributes": ["day"] } ], "hierarchies": [ {"name": "ymd", "levels":["year", "month", "day"]}, {"name": "ym", "levels":["year", "month"]}, {"name": "yqmd", "levels":["year", "quarter", "month", "day"]}, {"name": "ywd", "levels":["year", "week", "weekday"]} ], "default_hierarchy_name": "ymd" }

year year month week weekday

dimension, such as date or geography, has multiple ways of organizing attributes into a hierarchy

Cubes – modelling patterns

Schema 12
product id code name_en name_fr name_es

Model or configuration
"dimensions": [ { "name": "product", "label": "Product", "attributes": [ {"name": "code", "label": "Code"}, { "name": "name", "label": "Product", “locales”: ["en", "fr", "es"] } ] } ]

dimension attributes have languagespecific content (requirement: one column per language, use locale suffix)

Use:
! browser = workspace.browser(cube, locale=”fr”)

Then browse as usual. Localization is transparent. Notes: 1.only one locale per browser 2.refer to dimension attributes as there was no localisation: “product.name” 3.if non-existing locale is requested, then default (first in the list) locale is used

Cubes – modelling patterns

Schema
{
sales product_id ... amount

Model or configuration
"locale": "en", "cubes": [ { "name": "sales", "label": "Product Sales", "dimensions": ["product"], "measures": [ {"name": "amount", "label": "Amount"} ] } ], "dimensions": [ { "name": "product", "label": "Product", "attributes": [ { "name": "code", "label": "Code" }, { "name": "name", "label": "Product", "locales": ["en", "sk"] }, { "name": "price", "label": "Unit Price" } ] } ] }

product id code name_en name_sk unit_price

Product ...

Unit Price ...

Amount ...

Produkt ...

Jednotková cena ...

Suma ...

dimension attributes have languagespecific content; labels in report (including measures) should be displayed according to locale

Translation dictionary for non-default locale:
{ "locale": "sk", "dimensions": { "product”: { "levels": { "product" : { "label" : "Produkt", "attributes" : { "code":{"label": "Kód produktu"}, "name":{"label": "Produkt"}, "price":{"label": "Jednotková cena"} } } } } } “cubes”: { "sales": { "measures": { "amount" : {"label": "Suma"} } } } }

Use:
! model_sk = model.localize(translation)

Warning: interface for model localization is not final, might be changed in the future.

Cubes – modelling patterns

Cubes - lightweight Python OLAP Source Documentation https://github.com/Stiivi/cubes http://packages.python.org/cubes/