# DAR: the metrics, declared on the business model. Daily revenue on daana.dev's bookstore example.
spec: dar/v1.0
anchor:
  entity: order_line
source:
  dab: order_line                     # reads model.yaml: attributes, types, definitions

outputs:
  dim:                                # compiles to dim__order_line (+ __latest)
    kind: dimension
    description: The order line, with its fulfillment status and amounts over time
    latest: view
    attributes: all                   # descriptions from model.yaml

  line_fact:                          # compiles to fct__order_line
    kind: fact.state
    description: >
      One row per order line, at the time its order was placed. Lines of
      today's orders appear once the day is over.
    axis:
      expr: eff_tmstp                 # the mapping sets it to the order's order_date
      description: When the order the line is on was placed (event time, not load time)
    measures:
      - name: order_line_amount_amount
        additivity: additive
        description: Amount charged for the line, tax included
    columns: [order_line_amount_currency]

  daily_revenue:                      # compiles to fct__order_line_daily_revenue
    kind: fact.consolidated
    description: >
      Daily revenue: order lines' amounts summed by the day the order was
      placed. One row per day and currency; every line is USD in this data.
    grain: [order_date, order_line_amount_currency]
    spine: {driving: lines}
    legs:
      lines:
        fact: order_line.line_fact
        date: "CAST(fact_ts AS DATE)"   # the day the order was placed
        order_line_amount_currency: order_line_amount_currency
        aggregates:
          daily_revenue_amount:
            expr: order_line_amount_amount
            agg: sum
            description: >
              Daily revenue: the amounts charged on the order lines of orders
              placed that day, tax included, in the lines' currency.
            ai_context:
              synonyms: [daily revenue, revenue per day, daily sales, sales by day]
