forked from inferno-framework/client-fhir-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfhir-transaction-db.rb
85 lines (81 loc) · 2.5 KB
/
fhir-transaction-db.rb
1
2
3
4
5
6
7
8
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
require 'sqlite3'
class FHIRTransactionDB
def initialize(file_name = 'data.db')
@db = SQLite3::Database.new(file_name)
sql = %{
CREATE TABLE IF NOT EXISTS requests (
request_id INTEGER PRIMARY KEY,
request_method TEXT NOT NULL,
fhir_action TEXT NOT NULL,
request_uri TEXT NOT NULL,
remote_addr TEXT NOT NULL,
user_agent TEXT NOT NULL,
headers TEXT NOT NULL,
dt DATETIME default (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),
data TEXT
);
}
@db.execute(sql)
sql = %{
CREATE TABLE IF NOT EXISTS responses (
response_id INTEGER PRIMARY KEY,
request_id INTEGER NOT NULL,
status TEXT NOT NULL,
headers TEXT NOT NULL,
dt DATETIME default (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),
data TEXT
);
}
@db.execute(sql)
sql = %{
CREATE TABLE IF NOT EXISTS sessions (
session_id INTEGER PRIMARY KEY,
first_request_id INTEGER NOT NULL,
last_request_id INTEGER NOT NULL,
dt DATETIME default (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))
);
}
ret = @db.execute(sql)
result = ret
end
def insert_request(headers, data, backend)
request_method = headers['REQUEST_METHOD']
request_uri = headers['REQUEST_URI']
remote_addr = headers['REMOTE_ADDR']
user_agent = headers['HTTP_USER_AGENT']
# https://regexr.com/
# match after the first slash + word after the domain name
# need to pull out the backend first
# like 'Patient'
str_to_rm = URI(backend).path.chomp('/')
removed_backend = headers['REQUEST_URI'].sub(/#{Regexp.escape(str_to_rm)}/, '')
m = removed_backend.match(%r{^/([^/\?]+)/*.*$})
if m.nil? || m[1].nil?
# no regex match for action
fhir_action = "unknown"
else
fhir_action = m[1]
end
sql = %{
INSERT INTO requests
(request_method, fhir_action, request_uri, remote_addr, user_agent, headers, data)
VALUES
(?, ?, ?, ?, ?, ?, ?);
}
ins = @db.prepare(sql)
ins.execute(request_method, fhir_action, request_uri, remote_addr,
user_agent, headers.to_json, data.to_s)
return @db.last_insert_row_id
end
def insert_response(request_id, status, headers, data)
sql = %{
INSERT INTO responses
(request_id, status, headers, data)
VALUES
(?, ?, ?, ?);
}
ins = @db.prepare(sql)
ins.execute(request_id, status, headers.to_json, data.to_s)
return @db.last_insert_row_id
end
end