/
ehb.go
247 lines (202 loc) · 5.29 KB
/
ehb.go
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
This package will request details from the BRP API and create
an ehb_link table with the results of the request.
*/
package main
import (
"crypto/tls"
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"
"sync"
)
type BRPConfig struct {
Url string
Protocols []int
Token string
}
type DataSource struct {
Id int64
Name string
Url string
DescriptionHelp string `json:"desc_help"`
Description string
EhbServiceEsId int64 `json:"ehb_service_es_id"`
}
type ProtocolDataSource struct {
Id int64
Protocol string
DataSource DataSource
Path string
Driver int64
DriverConfiguration interface{} `json:"driver_configuration"`
DisplayLabel string `json:"display_label"`
MaxRecordsPerSubject int64 `json:"max_records_per_subject"`
Authorized bool
}
type ApiSubjects struct {
Count int64
Subjects []Subject
}
type Subject struct {
Id int64
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
OrganizationId int64 `json:"organization_id"`
OrganizationSubjectId string `json:"organization_subject_id"`
Dob string
Modified string
Created string
ExternalRecords []ExternalRecord `json:"external_records"`
}
type ExternalRecord struct {
RecordId string `json:"record_id"`
SubjectId int64 `json:"subject_id"`
ExternalSystemId int64 `json:"external_system_id"`
Modified string
Created string
Path string
Id int64
LabelId int64 `json:"label_id"`
}
var Bc = BRPConfig{}
var w sync.WaitGroup
var count = 0
// Create an insecure transport because eHB not currently providing proper certs
var tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var client = &http.Client{Transport: tr}
var ehbStagingCmd = &cobra.Command{
Use: "ehb",
Short: "eHB Staging ETL",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Error: You must provide a target.")
}
staging.Add(1)
generateLinkTable(args)
},
}
var brpAPIRequest = func(url string) []byte {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", Bc.Token))
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
return body
}
var getSubjects = func(d ProtocolDataSource, c chan []Subject) {
var as ApiSubjects
url := fmt.Sprintf("%sprotocoldatasources/%d/subjects/", Bc.Url, d.Id)
r := brpAPIRequest(url)
err := json.Unmarshal(r, &as)
if err != nil {
log.Fatal(err)
}
c <- as.Subjects
}
var generateLinkTable = func(args []string) {
defer staging.Done()
target_config := targets[args[0]]
err := viper.UnmarshalKey("BRP", &Bc)
if err != nil {
log.Fatal(err)
}
db, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable",
target_config.Host,
target_config.User,
target_config.Db))
if err != nil {
log.Fatal(err)
}
defer db.Close()
stmt, _ := ioutil.ReadFile("ddl/ehb_link.sql")
_, err = db.Query(string(stmt))
if err != nil {
log.Fatal(err)
}
var datasources []ProtocolDataSource
// Get all DataSources associated with Protocols
for _, protocol := range Bc.Protocols {
var ds []ProtocolDataSource
url := fmt.Sprintf("%sprotocols/%d/data_sources/", Bc.Url, protocol)
r := brpAPIRequest(url)
err := json.Unmarshal(r, &ds)
if err != nil {
log.Fatal(err)
}
datasources = append(datasources, ds...)
}
subjects_queue := make(chan []Subject)
for _, ds := range datasources {
go getSubjects(ds, subjects_queue)
}
// Start DB Transaction
txn, err := db.Begin()
if err != nil {
log.Fatal(err)
}
var c = len(datasources)
for subjects := range subjects_queue {
for _, subject := range subjects {
for _, record := range subject.ExternalRecords {
stmt, err := txn.Prepare(fmt.Sprintf("insert into ehb_link (ehb_id, external_system_id, external_id, organization_id, organization_subject_id, created, dob) values (%d, %d, '%s', %d, '%s', '%s', '%s')",
subject.Id,
record.ExternalSystemId,
record.RecordId,
subject.OrganizationId,
subject.OrganizationSubjectId,
record.Created,
subject.Dob))
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec()
if err != nil {
log.Fatal(err)
}
err = stmt.Close()
}
}
c -= 1
if c == 0 {
close(subjects_queue)
}
}
err = txn.Commit()
if err != nil {
log.Fatal(err)
}
log.Println("eHB Link Table Complete.")
}
var transformLinkTable = func(args []string) {
log.Println("Running transforms on eHB link table.")
target_config := targets[args[0]]
db, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable",
target_config.Host,
target_config.User,
target_config.Db))
if err != nil {
log.Fatal(err)
}
defer db.Close()
stmt, _ := ioutil.ReadFile("transform/eHBTransform.sql")
_, err = db.Query(string(stmt))
if err != nil {
log.Fatal(err)
}
log.Println("Link table transforms complete.")
}