Class: Skadi::Schema

Inherits:
Object
  • Object
show all
Defined in:
app/models/skadi/schema.rb

Class Method Summary collapse

Class Method Details

.database_schemaObject

A hash where the key represents a dataset name and the value is its configuration



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/skadi/schema.rb', line 9

def database_schema
  return @_database_schema if defined?(@_database_schema)

  @_database_schema ||= {
    # A hash with the following keys:
    # model: The model for this dataset
    # visit_key: used for chart-level visit filters
    # count_sql: The SQL expression used to count this field. Defaults to COUNT().
    # fields: A hash where the key represents a field and the value is its configuration
    visits: {
      model: Skadi::Visit,
      visit_key: "id",
      # If the table has a date field, the fields Hash expects a value for :date
      fields: {
        # A hash with the following keys:
        # label: The label to show in the front end
        # type: The datatype, one of :one_of, :date, :string, :number, :boolean. Defaults to :string.
        # filter: True if this field can be filtered
        # split: True if this field can be split
        # sql: The SQL expression used to get this value for derived fields
        # options: A list of possible values
        # description: used in the front end as help text for this field
        date: {
          type: :date,
          filter: true,
          sql: "skadi_visits.created_at",
        },
        landing_page: FILTER_AND_SPLIT,
        referrer_domain: {
          **FILTER_AND_SPLIT,
          sql: Helpers::Sql.string_before_separator(Skadi::Visit, "referrer", "/"),
        },
        utm_source: FILTER_AND_SPLIT,
        utm_medium: FILTER_AND_SPLIT,
        utm_term: FILTER_AND_SPLIT,
        utm_content: FILTER_AND_SPLIT,
        utm_campaign: FILTER_AND_SPLIT,
      },
    },
    views: {
      model: Skadi::View,
      visit_key: "visit_id",
      fields: {
        date: {
          type: :date,
          filter: true,
          sql: "skadi_views.created_at",
        },
        verified: {
          type: :boolean,
          filter: true,
        },
        controller: FILTER_AND_SPLIT,
        action: {
          filter: true,
        },
        controller_and_action: {
          split: true,
          sql: "CONCAT(skadi_views.controller, '::', skadi_views.action)",
        },
        path: FILTER_AND_SPLIT,
        verb: {
          type: :one_of,
          filter: true,
          split: true,
          description: "Typically, GET requests are page views, and POST, PUT, PATCH and DELETE are form submissions.",
          options: %w[GET POST PUT PATCH DELETE],
        },
        version: FILTER_AND_SPLIT,
        exit_page: FILTER_AND_SPLIT,
      },
    },
    events: {
      model: Skadi::Event,
      visit_key: "visit_id",
      fields: {
        date: {
          type: :date,
          filter: true,
          sql: "skadi_events.created_at",
        },
        name: FILTER_AND_SPLIT,
        **(Skadi.configuration.dashboard_custom_event_fields || {}),
      },
    },
    demographics: {
      model: Demographic,
      count_sql: "COALESCE(SUM(skadi_demographics.count), 0)",
      fields: {
        date: {
          type: :date,
          filter: true,
          sql: "skadi_demographics.recorded_on",
        },
        uri: FILTER_AND_SPLIT,
        name: FILTER_AND_SPLIT,
        value: FILTER_AND_SPLIT,
        count: {
          **FILTER_AND_SPLIT,
          type: :number,
        },
      },
    },
    **(Skadi.configuration.dashboard_custom_schema || {}),
  }
end

.frontend_schemaObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/skadi/schema.rb', line 116

def frontend_schema
  return @_frontend_schema if defined?(@_frontend_schema)

  # Redact the SQL from the dashboard schema
  schema = database_schema.deep_dup
  schema.each_value do |dataset|
    dataset.keys.each do |key|
      dataset.delete(key) unless key == :fields
    end

    dataset[:fields].each_value do |field|
      field.delete(:sql)
    end
  end

  @_frontend_schema = schema
end