/
exporter_api.go
383 lines (322 loc) · 10.7 KB
/
exporter_api.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package api
import (
"context"
"encoding/base64"
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/SafetyCulture/safetyculture-exporter/pkg/httpapi"
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/exporter"
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/feed"
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/inspections"
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/templates"
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/util"
"github.com/pkg/errors"
)
var ctx context.Context
var cancelFunc context.CancelFunc
// NewSafetyCultureExporter builds a SafetyCultureExporter with clients inferred from own configuration
func NewSafetyCultureExporter(cfg *ExporterConfiguration, version *AppVersion) (*SafetyCultureExporter, error) {
apiClient, err := getAPIClient(cfg.ToApiConfig(), version)
if err != nil {
return nil, err
}
sheqsyApiClient, err := getSheqsyAPIClient(cfg.ToApiConfig(), version)
if err != nil {
return nil, err
}
return &SafetyCultureExporter{
apiClient: apiClient,
sheqsyApiClient: sheqsyApiClient,
cfg: cfg,
exportStatus: feed.GetExporterStatus(),
}, nil
}
func getAPIClient(cfg *HttpApiCfg, version *AppVersion) (*httpapi.Client, error) {
var apiOpts []httpapi.Opt
if cfg.tlsSkipVerify {
apiOpts = append(apiOpts, httpapi.OptSetInsecureTLS(true))
}
if cfg.tlsCert != "" {
apiOpts = append(apiOpts, httpapi.OptAddTLSCert(cfg.tlsCert))
}
if cfg.proxyUrl != "" {
proxyURL, err := url.Parse(cfg.proxyUrl)
if err != nil {
return nil, fmt.Errorf("unable to parse proxy URL")
}
apiOpts = append(apiOpts, httpapi.OptSetProxy(proxyURL))
}
config := httpapi.ClientCfg{
Addr: cfg.apiUrl,
AuthorizationHeader: fmt.Sprintf("Bearer %s", cfg.accessToken),
IntegrationID: version.IntegrationID,
IntegrationVersion: version.IntegrationVersion,
}
return httpapi.NewClient(&config, apiOpts...), nil
}
func getSheqsyAPIClient(cfg *HttpApiCfg, version *AppVersion) (*httpapi.Client, error) {
var apiOpts []httpapi.Opt
if cfg.tlsSkipVerify {
apiOpts = append(apiOpts, httpapi.OptSetInsecureTLS(true))
}
if cfg.tlsCert != "" {
apiOpts = append(apiOpts, httpapi.OptAddTLSCert(cfg.tlsCert))
}
if cfg.proxyUrl != "" {
proxyURL, err := url.Parse(cfg.proxyUrl)
if err != nil {
return nil, fmt.Errorf("unable to parse proxy URL")
}
apiOpts = append(apiOpts, httpapi.OptSetProxy(proxyURL))
}
token := base64.StdEncoding.EncodeToString(
[]byte(
fmt.Sprintf(
"%s:%s",
cfg.sheqsyUsername,
cfg.sheqsyPassword,
),
),
)
config := httpapi.ClientCfg{
Addr: cfg.sheqsyApiUrl,
AuthorizationHeader: fmt.Sprintf("Basic %s", token),
IntegrationID: version.IntegrationID,
IntegrationVersion: version.IntegrationVersion,
}
return httpapi.NewClient(&config, apiOpts...), nil
}
// NewReportExporter returns a new instance of ReportExporter
func NewReportExporter(exportPath string, reportCfg *ReportExporterCfg) (*feed.ReportExporter, error) {
sqlExporter, err := feed.NewSQLExporter("sqlite", filepath.Join(exportPath, "reports.db"), true, "")
if err != nil {
return nil, err
}
return &feed.ReportExporter{
SQLExporter: sqlExporter,
Logger: sqlExporter.Logger,
ExportPath: exportPath,
Format: reportCfg.Format,
PreferenceID: reportCfg.PreferenceID,
Filename: reportCfg.Filename,
RetryTimeout: reportCfg.RetryTimeout,
}, nil
}
type ReportExporterCfg struct {
Format []string
PreferenceID string
Filename string
RetryTimeout int
}
type HttpApiCfg struct {
tlsSkipVerify bool
tlsCert string
proxyUrl string
apiUrl string
accessToken string
sheqsyApiUrl string
sheqsyUsername string
sheqsyPassword string
}
type SafetyCultureExporter struct {
apiClient *httpapi.Client
sheqsyApiClient *httpapi.Client
cfg *ExporterConfiguration
exportStatus *feed.ExportStatus
}
func (s *SafetyCultureExporter) SetApiClient(apiClient *httpapi.Client) {
s.apiClient = apiClient
}
func (s *SafetyCultureExporter) SetSheqsyApiClient(apiClient *httpapi.Client) {
s.sheqsyApiClient = apiClient
}
func (s *SafetyCultureExporter) RunInspectionJSON() error {
exportPath := fmt.Sprintf("%s/json/", s.cfg.Export.Path)
err := os.MkdirAll(exportPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", exportPath)
}
e := exporter.NewJSONExporter(exportPath)
cfg := inspections.InspectionClientCfg{
SkipIDs: s.cfg.Export.Inspection.SkipIds,
ModifiedAfter: s.cfg.Export.ModifiedAfter.Time,
TemplateIDs: s.cfg.Export.TemplateIds,
Archived: s.cfg.Export.Inspection.Archived,
Completed: s.cfg.Export.Inspection.Completed,
Incremental: s.cfg.Export.Incremental,
}
inspectionsClient := inspections.NewInspectionClient(&cfg, s.apiClient, e)
err = inspectionsClient.Export(context.Background())
if err != nil {
return errors.Wrap(err, "error while exporting JSON")
}
return nil
}
func (s *SafetyCultureExporter) CheckDBConnection() error {
_, err := feed.GetDatabase(s.cfg.Db.Dialect, s.cfg.Db.ConnectionString)
if err != nil {
return errors.Wrap(err, "create sql exporter")
}
return nil
}
func (s *SafetyCultureExporter) RunSQL() error {
ctx, cancelFunc = context.WithCancel(context.Background())
if s.cfg.Export.Media {
err := os.MkdirAll(s.cfg.Export.MediaPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", s.cfg.Export.MediaPath)
}
}
e, err := feed.NewSQLExporter(s.cfg.Db.Dialect, s.cfg.Db.ConnectionString, !s.cfg.Db.AutoMigrateDisabled, s.cfg.Export.MediaPath)
if err != nil {
return errors.Wrap(err, "create sql exporter")
}
exporterApp := feed.NewExporterApp(s.apiClient, s.sheqsyApiClient, s.cfg.ToExporterConfig())
if s.cfg.Export.SchemaOnly {
return exporterApp.ExportSchemas(e)
}
if len(s.cfg.AccessToken) != 0 || len(s.cfg.SheqsyUsername) != 0 {
err = exporterApp.ExportFeeds(e, ctx)
if err != nil {
return errors.Wrap(err, "exporting feeds")
}
}
return nil
}
// RunSQLite - runs the export and will save into a local sqlite db file
func (s *SafetyCultureExporter) RunSQLite() error {
ctx, cancelFunc = context.WithCancel(context.Background())
exportPath := s.cfg.Export.Path
err := os.MkdirAll(exportPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", exportPath)
}
if s.cfg.Export.Media {
err := os.MkdirAll(s.cfg.Export.MediaPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", s.cfg.Export.MediaPath)
}
}
sqlExporter, err := feed.NewSQLiteExporter(exportPath, s.cfg.Export.MediaPath)
if err != nil {
return errors.Wrap(err, "unable to create sqlite exporter")
}
exporterApp := feed.NewExporterApp(s.apiClient, s.sheqsyApiClient, s.cfg.ToExporterConfig())
if s.cfg.Export.SchemaOnly {
return exporterApp.ExportSchemas(sqlExporter)
}
if len(s.cfg.AccessToken) != 0 || len(s.cfg.SheqsyUsername) != 0 {
err = exporterApp.ExportFeeds(sqlExporter, ctx)
if err != nil {
return errors.Wrap(err, "exporting feeds")
}
}
return nil
}
func (s *SafetyCultureExporter) RunCSV() error {
ctx, cancelFunc = context.WithCancel(context.Background())
exportPath := s.cfg.Export.Path
err := os.MkdirAll(exportPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", exportPath)
}
if s.cfg.Export.Media {
err := os.MkdirAll(s.cfg.Export.MediaPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", s.cfg.Export.MediaPath)
}
}
e, err := feed.NewCSVExporter(exportPath, s.cfg.Export.MediaPath, s.cfg.Csv.MaxRowsPerFile)
if err != nil {
return errors.Wrap(err, "unable to create csv exporter")
}
exporterApp := feed.NewExporterApp(s.apiClient, s.sheqsyApiClient, s.cfg.ToExporterConfig())
if s.cfg.Export.SchemaOnly {
return exporterApp.ExportSchemas(e)
}
if len(s.cfg.AccessToken) != 0 || len(s.cfg.SheqsyUsername) != 0 {
err = exporterApp.ExportFeeds(e, ctx)
if err != nil {
return errors.Wrap(err, "exporting feeds")
}
}
return nil
}
func (s *SafetyCultureExporter) RunInspectionReports() error {
ctx, cancelFunc = context.WithCancel(context.Background())
err := os.MkdirAll(s.cfg.Export.Path, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to create directory %s", s.cfg.Export.Path)
}
e, err := NewReportExporter(s.cfg.Export.Path, s.cfg.ToReporterConfig())
if err != nil {
return errors.Wrap(err, "unable to create report exporter")
}
exporterApp := feed.NewExporterApp(s.apiClient, s.sheqsyApiClient, s.cfg.ToExporterConfig())
err = exporterApp.ExportInspectionReports(e, ctx)
if err != nil {
return errors.Wrap(err, "generate reports")
}
return nil
}
func (s *SafetyCultureExporter) RunPrintSchema() error {
e, err := feed.NewSchemaExporter(os.Stdout)
if err != nil {
return errors.Wrap(err, "unable to create exporter")
}
exporterApp := feed.NewExporterApp(s.apiClient, s.sheqsyApiClient, s.cfg.ToExporterConfig())
err = exporterApp.PrintSchemas(e)
if err != nil {
return errors.Wrap(err, "error while printing schema")
}
return nil
}
func (s *SafetyCultureExporter) GetTemplateList() []TemplateResponseItem {
client := templates.NewTemplatesClient(s.apiClient)
res := client.GetTemplateList(context.Background(), 1000)
transformer := func(data templates.TemplateResponseItem) TemplateResponseItem {
return TemplateResponseItem{
ID: data.ID,
Name: data.Name,
ModifiedAt: data.ModifiedAt,
}
}
return util.GenericCollectionMapper(res, transformer)
}
// GetExportStatus called by UI
func (s *SafetyCultureExporter) GetExportStatus() *ExportStatusResponse {
data := s.exportStatus.ReadStatus()
var res []ExportStatusResponseItem
for _, v := range data {
res = append(res, ExportStatusResponseItem{
FeedName: v.Name,
Started: v.Started,
Finished: v.Finished,
HasError: v.HasError,
DurationMs: v.DurationMs,
Counter: v.Counter,
CounterDecremental: v.CounterDecremental,
StatusMessage: v.StatusMessage,
Stage: string(v.Stage),
})
}
s.exportStatus.PurgeFinished()
return &ExportStatusResponse{
ExportStarted: s.exportStatus.GetExportStarted(),
ExportCompleted: s.exportStatus.GetExportCompleted(),
Feeds: res,
}
}
// SetConfiguration will replace the configuration. Used by the UI to pass in the newly saved configuration
func (s *SafetyCultureExporter) SetConfiguration(cfg *ExporterConfiguration) {
s.cfg = cfg
}
// CleanExportStatus will clean the status items. Used by the UI
func (s *SafetyCultureExporter) CleanExportStatus() {
s.exportStatus.Reset()
}
func (s *SafetyCultureExporter) CancelExport() {
cancelFunc()
}