Class: Cellect::Server::Adapters::Postgres
- Inherits:
-
Default
- Object
- Default
- Cellect::Server::Adapters::Postgres
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
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_options ⇒ Object
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 = " SELECT sms.id as id, sms.priority as priority, sms.subject_set_id as group_id\n FROM workflows w\n JOIN subject_sets ss ON (ss.workflow_id = w.id)\n JOIN set_member_subjects sms ON (sms.subject_set_id = ss.id)\n WHERE w.id = \#{ workflow_name }\n SQL\n pg.exec(statement).collect do |row|\n {\n 'id' => row['id'].to_i,\n 'priority' => row['priority'].to_f,\n 'group_id' => row['group_id'].to_i\n }\n end\n end\nend\n"
|
#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 = " SELECT set_member_subject_ids FROM user_seen_subjects\n WHERE user_id = \#{ id } AND workflow_id = \#{ workflow_name }\n SQL\n pg.exec(statement).collect do |row|\n row['subject_ids'].map(&:to_i)\n end\n end\nend\n"
|
#status ⇒ Object
68
69
70
71
72
|
# File 'lib/cellect/server/adapters/postgres.rb', line 68
def status
{
connected: pg.status == PG::CONNECTION_OK
}
end
|
#with_pg ⇒ Object
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
|