Class: Nquery::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/nquery/seeder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!Object



5
6
7
# File 'lib/nquery/seeder.rb', line 5

def self.run!
  new.run!
end

Instance Method Details

#run!Object



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
# File 'lib/nquery/seeder.rb', line 9

def run!
  seed_sample_data!

  admin_group = ensure_group!("Administrators", "administrators")
  all_users_group = ensure_group!("All Users", "all_users")
  engineering = ensure_group!("Engineering", "custom", "Engineering team")

  admin = ensure_user!("admin@nquery.dev", "Admin", "User", "password123")
  analyst = ensure_user!("analyst@nquery.dev", "Data", "Analyst", "password123")

  [admin, analyst].each(&:ensure_all_users_membership!)
  admin_group.group_memberships.find_or_create_by!(user: admin)
  engineering.group_memberships.find_or_create_by!(user: analyst)

  data_source = DataSource.find_or_create_by!(name: "Main Database") do |ds|
    ds.adapter = "rails"
    ds.connection_config = {}.to_json
  end

  root_collection = Collection.find_or_create_by!(name: "Our analytics", kind: "root")
  admin.ensure_personal_collection!

  seed_permissions!(admin_group, all_users_group, engineering, root_collection, data_source)

  sample_tables = SampleData::Ecommerce::TABLES
  query = Query.find_or_initialize_by(name: "Monthly revenue")
  query.assign_attributes(
    statement: <<~SQL.squish,
      SELECT strftime('%Y-%m', #{sample_tables[:orders]}.ordered_at) AS month,
             ROUND(SUM(#{sample_tables[:order_items]}.quantity * #{sample_tables[:order_items]}.unit_price), 2) AS revenue
      FROM #{sample_tables[:orders]}
      INNER JOIN #{sample_tables[:order_items]} ON #{sample_tables[:order_items]}.order_id = #{sample_tables[:orders]}.id
      GROUP BY strftime('%Y-%m', #{sample_tables[:orders]}.ordered_at)
      ORDER BY month
    SQL
    data_source: data_source,
    creator: admin,
    collection: root_collection
  )
  query.save!

  chart = Chart.find_or_create_by!(name: "Revenue by month") do |c|
    c.query = query
    c.collection = root_collection
    c.creator = admin
    c.visualization = { "type" => "bar", "x" => "month", "y" => "revenue" }
  end

  dashboard = Dashboard.find_or_create_by!(name: "Executive overview") do |d|
    d.description = "Key metrics at a glance"
    d.collection = root_collection
    d.creator = admin
  end

  DashboardCard.find_or_create_by!(dashboard: dashboard, chart: chart) do |card|
    card.pos_x = 0
    card.pos_y = 0
    card.width = 6
    card.height = 4
  end

  EmbedTokenService.sign(
    resource_type: "Nquery::Chart",
    resource_id: chart.id,
    creator: admin,
    expires_at: 1.year.from_now
  ) unless EmbedToken.exists?(resource_type: "Nquery::Chart", resource_id: chart.id)

  ApplicationPermission::FEATURES.each do |feature|
    ApplicationPermission.find_or_create_by!(group: admin_group, feature: feature) do |p|
      p.access_level = "yes"
    end
  end
end