Class: Cellect::Server::Adapters::Postgres

Inherits:
Default
  • Object
show all
Defined in:
lib/cellect/server/adapters/postgres.rb

Instance Method Summary collapse

Methods inherited from Default

#load_workflow, #load_workflows, #workflow_for

Constructor Details

#initializePostgres

Returns a new instance of Postgres.



8
9
10
11
12
# File 'lib/cellect/server/adapters/postgres.rb', line 8

def initialize
  @pg ||= ConnectionPool.new(size: ENV.fetch('PG_POOL_SIZE', 25).to_i) do
    PG.connect connection_options
  end
end

Instance Method Details

#connection_optionsObject



78
79
80
81
82
83
84
85
86
# File 'lib/cellect/server/adapters/postgres.rb', line 78

def connection_options
  {
    host: ENV.fetch('PG_HOST', '127.0.0.1'),
    port: ENV.fetch('PG_PORT', '5432'),
    dbname: ENV.fetch('PG_DB', 'cellect'),
    user: ENV.fetch('PG_USER', 'cellect'),
    password: ENV.fetch('PG_PASS', '')
  }
end

#load_data_for(workflow_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cellect/server/adapters/postgres.rb', line 37

def load_data_for(workflow_name)
  with_pg do |pg|
    statement = <<-SQL
      SELECT sms.id as id, sms.priority as priority, sms.subject_set_id as group_id
      FROM workflows w
      JOIN subject_sets ss ON (ss.workflow_id = w.id)
      JOIN set_member_subjects sms ON (sms.subject_set_id = ss.id)
      WHERE w.id = #{ workflow_name }
    SQL
    pg.exec(statement).collect do |row|
      {
        'id' => row['id'].to_i,
        'priority' => row['priority'].to_f,
        'group_id' => row['group_id'].to_i
      }
    end
  end
end

#load_user(workflow_name, id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cellect/server/adapters/postgres.rb', line 56

def load_user(workflow_name, id)
  with_pg do |pg|
    statement = <<-SQL
      SELECT set_member_subject_ids FROM user_seen_subjects
      WHERE user_id = #{ id } AND workflow_id = #{ workflow_name }
    SQL
    pg.exec(statement).collect do |row|
      row['subject_ids'].map(&:to_i)
    end
  end
end

#statusObject



68
69
70
71
72
# File 'lib/cellect/server/adapters/postgres.rb', line 68

def status
  {
    connected: pg.status == PG::CONNECTION_OK
  }
end

#with_pgObject



74
75
76
# File 'lib/cellect/server/adapters/postgres.rb', line 74

def with_pg
  @pg.with{ |pg| yield pg }
end

#workflow_list(*names) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cellect/server/adapters/postgres.rb', line 14

def workflow_list(*names)
  with_pg do |pg|
    statement = 'SELECT * FROM workflows'
    statement += case names.length
                 when 0
                   ""
                 when 1
                   "WHERE \"workflows\".\"id\" = #{ names.first }"
                 else
                   "WHERE \"workflows\".\"id\" IN (#{ names.join(',') })"
                 end
    pg.exec(statement).collect do |row|
      {
        'id' => row['id'].to_i,
        'name' => row['id'],
        'prioritized' => row['prioritized'] == 't',
        'pairwise' => row['pairwise'] == 't',
        'grouped' => row['grouped'] == 't'
      }
    end
  end
end