1

I'm currently learning about data warehousing for university and my work and want to build a small data warehouse for a project myself. Now I found the active_reporting gem and trying to understand this gem's usage.

I made myself familiar with star schema, snowflake schema, etc. but I don't really understand how I link the FactTable with the actual table in the database and how to then use the metrics to generate a report.

I know it should be a table with some belongs_to associations to the dimensions, but how would I specify a migration for this? At least I didn't find an example in the docs.

So consider my following example where I'm modeling a simple cube for reporting details to hospital stays and patients. I have 3 models - patient, case and diagnoses:

create_table "cases", force: :cascade do |t|
    t.string "casenumber"
    t.date "admission_date"
    t.date "discharge_date"
    t.bigint "patient_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["patient_id"], name: "index_cases_on_patient_id"
  end

  create_table "diagnoses", force: :cascade do |t|
    t.string "icd_code"
    t.text "icd_description"
    t.bigint "case_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["case_id"], name: "index_diagnoses_on_case_id"
  end

  create_table "patients", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.date "birthdate"
    t.string "gender", limit: 1
    t.string "domicile"
    t.string "street"
    t.integer "zip_code"
    t.integer "house_number"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

Now I have a CaseFactModel with the dimensions patient, case, and diagnosis

class CaseFactModel < ActiveReporting::FactModel
    self.model = CaseFact
    
    dimension :patient
    dimension :case
    dimension :diagnosis
end

class CaseFact < ApplicationRecord
  belongs_to :case
  belongs_to :patient
  belongs_to :diagnosis
end

Now, from what I've read this FactTable has to be a database table as well, right? In the example test files in the repository, I couldn't find any migration related to the fact tables. So I created a fact table how I would do it normally with the following migration:

create_table :case_facts do |t|
      t.references :case, null: false, foreign_key: true
      t.references :patient, null: false, foreign_key: true
      t.references :diagnosis, null: false, foreign_key: true
      t.integer :value
      t.timestamps
    end

Now when I try to generate a report and I try to use the patients as dimension, I get an error that the column "name" can't be found, but I can't switch it.

def report
        metric = ActiveReporting::Metric.new(
            :total,
            fact_model: CaseFactModel,
            dimensions: [:patient],
            aggregate: :sum

        )
        report = ActiveReporting::Report.new(metric)
        report.run
    end

This produces the following error:

 SQL (9.2ms)  SELECT SUM(value) AS total, "patients"."name" AS "patient", "patients"."id" AS "patient_identifier" FROM "case_facts" INNER JOIN "patients" ON "patients"."id" = "case_facts"."patient_id" GROUP BY "patients"."name", "patients"."id"
  ↳ (byebug):1:in `report'
*** ActiveRecord::StatementInvalid Exception: PG::UndefinedColumn: ERROR:  column doesn't exist

Does anyone have some basic knowledge about this gem and can help? That'd be awesome! Thanks.

Max Kirsch
  • 441
  • 5
  • 24

0 Answers0