/
ingest.go
716 lines (626 loc) · 22.9 KB
/
ingest.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//
// Copyright © 2021 Uncharted Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package task
import (
"fmt"
"path"
"time"
"github.com/pkg/errors"
"github.com/uncharted-distil/distil-compute/metadata"
"github.com/uncharted-distil/distil-compute/middleware"
"github.com/uncharted-distil/distil-compute/model"
"github.com/uncharted-distil/distil-compute/primitive/compute"
log "github.com/unchartedsoftware/plog"
"github.com/uncharted-distil/distil/api/env"
api "github.com/uncharted-distil/distil/api/model"
"github.com/uncharted-distil/distil/api/postgres"
"github.com/uncharted-distil/distil/api/serialization"
)
const (
baseTableSuffix = "_base"
explainTableSuffix = "_explain"
)
// IngestTaskConfig captures the necessary configuration for an data ingest.
type IngestTaskConfig struct {
DatasetBatchSize int
HasHeader bool
FeaturizationEnabled bool
GeocodingEnabled bool
ClassificationOutputPathRelative string
ClassificationEnabled bool
RankingOutputPathRelative string
DatabasePassword string
DatabaseUser string
Database string
DatabaseHost string
DatabasePort int
DatabaseBatchSize int
DatabaseLogLevel string
ImputeEnabled bool
SummaryOutputPathRelative string
SummaryMachineOutputPathRelative string
SummaryEnabled bool
ESEndpoint string
HardFail bool
IngestOverwrite bool
SampleRowLimit int
}
// IngestSteps is a collection of parameters that specify ingest behaviour.
type IngestSteps struct {
ClassificationOverwrite bool
VerifyMetadata bool
FallbackMerged bool
CreateMetadataTables bool
CheckMatch bool
SkipFeaturization bool
}
// NewDefaultClient creates a new client to use when submitting pipelines.
func NewDefaultClient(config env.Config, userAgent string, discoveryLogger middleware.MethodLogger) (*compute.Client, error) {
return compute.NewClient(
config.SolutionComputeEndpoint,
config.SolutionComputeTrace,
userAgent,
"TA2",
time.Duration(config.SolutionComputePullTimeout)*time.Second,
config.SolutionComputePullMax,
config.SkipPreprocessing,
discoveryLogger)
}
// NewConfig creates an ingest config based on a distil config.
func NewConfig(config env.Config) *IngestTaskConfig {
return &IngestTaskConfig{
DatasetBatchSize: config.DatasetBatchSize,
HasHeader: true,
FeaturizationEnabled: config.FeaturizationEnabled,
GeocodingEnabled: config.GeocodingEnabled,
ClassificationOutputPathRelative: config.ClassificationOutputPath,
ClassificationEnabled: config.ClassificationEnabled,
RankingOutputPathRelative: config.RankingOutputPath,
DatabasePassword: config.PostgresPassword,
DatabaseUser: config.PostgresUser,
Database: config.PostgresDatabase,
DatabaseHost: config.PostgresHost,
DatabasePort: config.PostgresPort,
DatabaseBatchSize: config.PostgresBatchSize,
DatabaseLogLevel: config.PostgresLogLevel,
SummaryOutputPathRelative: config.SummaryPath,
SummaryMachineOutputPathRelative: config.SummaryMachinePath,
SummaryEnabled: config.SummaryEnabled,
ESEndpoint: config.ElasticEndpoint,
HardFail: config.IngestHardFail,
IngestOverwrite: config.IngestOverwrite,
SampleRowLimit: config.IngestSampleRowLimit,
ImputeEnabled: config.ImputeEnabled,
}
}
// IngestResult captures the result of a dataset ingest process.
type IngestResult struct {
DatasetID string
Sampled bool
RowCount int
}
// IngestParams contains the parameters needed to ingest a dataset
type IngestParams struct {
Source metadata.DatasetSource
DataCtor api.DataStorageCtor
MetaCtor api.MetadataStorageCtor
ID string
Origins []*model.DatasetOrigin
Type api.DatasetType
Path string
RawGroupings []map[string]interface{}
IndexFields []string
DefinitiveTypes map[string]*model.Variable
}
// GetSchemaDocPath returns the schema path to use when ingesting.
func (i *IngestParams) GetSchemaDocPath() string {
if i.Path != "" {
return path.Join(i.Path, compute.D3MDataSchema)
}
return path.Join(env.ResolvePath(i.Source, i.ID), compute.D3MDataSchema)
}
// IngestDataset executes the complete ingest process for the specified dataset.
func IngestDataset(params *IngestParams, config *IngestTaskConfig, steps *IngestSteps) (*IngestResult, error) {
metaStorage, err := params.MetaCtor()
if err != nil {
return nil, errors.Wrap(err, "unable to initialize metadata storage")
}
dataStorage, err := params.DataCtor()
if err != nil {
return nil, errors.Wrap(err, "unable to initialize data storage")
}
originalSchemaFile := params.GetSchemaDocPath()
latestSchemaOutput := originalSchemaFile
output, err := Merge(latestSchemaOutput, params.ID, config)
if err != nil {
return nil, errors.Wrap(err, "unable to merge all data into a single file")
}
latestSchemaOutput = output
log.Infof("finished merging the dataset")
output, err = Clean(latestSchemaOutput, params.ID, params, config)
if err != nil {
return nil, errors.Wrap(err, "unable to clean all data")
}
latestSchemaOutput = output
log.Infof("finished cleaning the dataset")
if config.ClassificationEnabled {
if steps.ClassificationOverwrite || !classificationExists(latestSchemaOutput, config) {
_, err = Classify(latestSchemaOutput, params.ID, config)
if err != nil {
if config.HardFail {
return nil, errors.Wrap(err, "unable to classify fields")
}
log.Errorf("unable to classify fields: %+v", err)
}
log.Infof("finished classifying the dataset")
} else {
log.Infof("skipping classification because it already exists")
}
} else {
log.Infof("classification disabled")
}
_, err = Rank(latestSchemaOutput, params.ID, config)
if err != nil {
log.Errorf("unable to rank field importance: %v", err)
}
log.Infof("finished ranking the dataset")
if config.SummaryEnabled {
_, err = Summarize(latestSchemaOutput, params.ID, config)
log.Infof("finished summarizing the dataset")
if err != nil {
if config.HardFail {
return nil, errors.Wrap(err, "unable to summarize the dataset")
}
log.Errorf("unable to summarize the dataset: %v", err)
}
} else {
log.Infof("summarization disabled")
}
if config.GeocodingEnabled {
output, err = GeocodeForwardDataset(latestSchemaOutput, params.ID, config)
if err != nil {
return nil, errors.Wrap(err, "unable to geocode all data")
}
latestSchemaOutput = output
log.Infof("finished geocoding the dataset")
}
// not sure if better to call canSample here, or as the first part of the sample step
sampled := false
rowCount := 0
if canSample(latestSchemaOutput, config) {
log.Infof("sampling dataset")
latestSchemaOutput, sampled, rowCount, err = Sample(originalSchemaFile, latestSchemaOutput, params.ID, config)
if err != nil {
return nil, errors.Wrap(err, "unable to sample dataset")
}
log.Infof("finished sampling dataset")
}
datasetID, err := Ingest(originalSchemaFile, latestSchemaOutput, dataStorage, metaStorage, params, config, steps)
if err != nil {
return nil, errors.Wrap(err, "unable to ingest ranked data")
}
log.Infof("finished ingesting the dataset")
// set the known grouping information
if params.RawGroupings != nil {
log.Infof("creating groupings in metadata")
err = SetGroups(datasetID, params.RawGroupings, dataStorage, metaStorage, config)
if err != nil {
return nil, errors.Wrap(err, "unable to set grouping")
}
log.Infof("done creating groupings in metadata")
}
// featurize dataset for downstream efficiencies
if config.FeaturizationEnabled && !steps.SkipFeaturization && canFeaturize(datasetID, metaStorage) {
ingestedDataset, err := metaStorage.FetchDataset(datasetID, true, true, false)
if err != nil {
return nil, errors.Wrap(err, "unable to load metadata")
}
_, featurizedDatasetPath, err := FeaturizeDataset(originalSchemaFile, latestSchemaOutput, datasetID, metaStorage, config)
if err != nil {
// if the featurize step fails hard delete the dataset
_ = DeleteDataset(ingestedDataset, metaStorage, dataStorage, false)
return nil, errors.Wrap(err, "unable to featurize dataset")
}
log.Infof("finished featurizing the dataset")
ingestedDataset.LearningDataset = featurizedDatasetPath
err = metaStorage.UpdateDataset(ingestedDataset)
if err != nil {
return nil, errors.Wrap(err, "unable to store updated metadata")
}
}
// updating extremas is optional
err = UpdateExtremas(datasetID, metaStorage, dataStorage)
if err != nil {
log.Errorf("unable to update extremas ranked data: %v", err)
}
log.Infof("finished updating extremas")
return &IngestResult{
DatasetID: datasetID,
Sampled: sampled,
RowCount: rowCount,
}, nil
}
// Featurize provides a separate step for featurzing data so that it can be called independently of the ingest step.
func Featurize(originalSchemaFile string, schemaFile string, data api.DataStorage, storage api.MetadataStorage, dataset string, config *IngestTaskConfig) error {
// featurize dataset for downstream efficiencies
if config.FeaturizationEnabled && canFeaturize(dataset, storage) {
_, featurizedDatasetPath, err := FeaturizeDataset(originalSchemaFile, schemaFile, dataset, storage, config)
if err != nil {
return errors.Wrap(err, "unable to featurize dataset")
}
log.Infof("finished featurizing the dataset")
ingestedDataset, err := storage.FetchDataset(dataset, true, true, false)
if err != nil {
return errors.Wrap(err, "unable to load metadata")
}
ingestedDataset.LearningDataset = featurizedDatasetPath
err = storage.UpdateDataset(ingestedDataset)
if err != nil {
return errors.Wrap(err, "unable to store updated metadata")
}
}
return nil
}
// Ingest the metadata to ES and the data to Postgres.
func Ingest(originalSchemaFile string, schemaFile string, data api.DataStorage,
storage api.MetadataStorage, params *IngestParams, config *IngestTaskConfig, steps *IngestSteps) (string, error) {
// TODO: A LOT OF THIS CODE SHOULD BE IN THE STORAGE PACKAGES!!!
_, meta, err := loadMetadataForIngest(originalSchemaFile, schemaFile, params, config, steps)
if err != nil {
return "", err
}
// original datasets should NOT be changed
meta.Immutable = true
if !config.IngestOverwrite {
// get the unique name, and if it is different then write out the updated metadata
uniqueID, err := getUniqueDatasetID(meta, storage)
if err != nil {
return "", err
}
if uniqueID != meta.ID {
extendedOutput := params.Source == metadata.Augmented
log.Infof("storing (extended: %v) metadata with new id to %s (new: '%s', old: '%s')", extendedOutput, originalSchemaFile, uniqueID, meta.ID)
meta.ID = uniqueID
datasetStorage := serialization.GetStorage(originalSchemaFile)
err = datasetStorage.WriteMetadata(originalSchemaFile, meta, extendedOutput, false)
if err != nil {
return "", errors.Wrap(err, "unable to store updated metadata")
}
log.Infof("updated metadata with new name written to %s", originalSchemaFile)
}
}
// Connect to the database.
postgresConfig := &postgres.Config{
Password: config.DatabasePassword,
User: config.DatabaseUser,
Database: config.Database,
Host: config.DatabaseHost,
Port: config.DatabasePort,
BatchSize: config.DatabaseBatchSize,
PostgresLogLevel: "error",
}
pg, err := postgres.NewDatabase(postgresConfig, true)
if err != nil {
return "", errors.Wrap(err, "unable to initialize a new database")
}
// Check for existing dataset
if steps.CheckMatch && config.IngestOverwrite {
match, err := matchDataset(storage, meta)
// Ignore the error for now as if this fails we still want ingest to succeed.
if err != nil {
log.Error(err)
}
if match != "" {
log.Infof("Matched %s to dataset %s", meta.Name, match)
err = deleteDataset(match, pg, storage)
if err != nil {
log.Errorf("error deleting dataset: %v", err)
}
log.Infof("Deleted dataset %s", match)
}
}
// ingest the metadata
updatedDatasetID, err := IngestMetadata(originalSchemaFile, schemaFile, data, storage, params, config, steps)
if err != nil {
return "", err
}
// ingest the data
err = IngestPostgres(originalSchemaFile, schemaFile, params, config, steps)
if err != nil {
return "", err
}
// expand the suggested types to be the exhaustive list of types it can be
err = VerifySuggestedTypes(updatedDatasetID, data, storage)
if err != nil {
return "", err
}
return updatedDatasetID, nil
}
// VerifySuggestedTypes checks expands the suggested types to include all valid
// types the database storage can support.
func VerifySuggestedTypes(dataset string, dataStorage api.DataStorage, metaStorage api.MetadataStorage) error {
meta, err := metaStorage.FetchDataset(dataset, false, false, false)
if err != nil {
return err
}
err = dataStorage.VerifyData(meta.ID, meta.StorageName)
if err != nil {
return err
}
return nil
}
// IngestMetadata ingests the data to ES.
func IngestMetadata(originalSchemaFile string, schemaFile string, data api.DataStorage,
storage api.MetadataStorage, params *IngestParams, config *IngestTaskConfig, steps *IngestSteps) (string, error) {
_, meta, err := loadMetadataForIngest(originalSchemaFile, schemaFile, params, config, steps)
if err != nil {
return "", err
}
meta.Type = string(params.Type)
if data != nil {
storageName, err := data.GetStorageName(meta.ID)
if err != nil {
return "", err
}
if meta.StorageName != storageName {
log.Infof("updating storage name in metadata from %s to %s", meta.StorageName, storageName)
meta.StorageName = storageName
datasetStorage := serialization.GetStorage(schemaFile)
err = datasetStorage.WriteMetadata(schemaFile, meta, true, false)
if err != nil {
return "", err
}
}
}
// ingested datasets are immutable
meta.Immutable = true
for _, v := range meta.GetMainDataResource().Variables {
v.Immutable = true
}
// Ingest the dataset info into the metadata storage
err = storage.IngestDataset(params.Source, meta)
if err != nil {
return "", errors.Wrap(err, "unable to ingest metadata")
}
log.Infof("ingested metadata for dataset")
return meta.ID, nil
}
// IngestPostgres ingests a dataset to PG storage.
func IngestPostgres(originalSchemaFile string, schemaFile string, params *IngestParams, config *IngestTaskConfig, steps *IngestSteps) error {
_, meta, err := loadMetadataForIngest(originalSchemaFile, schemaFile, params, config, steps)
if err != nil {
return err
}
mainDR := meta.GetMainDataResource()
dataDir := model.GetResourcePath(schemaFile, mainDR)
// Connect to the database.
postgresConfig := &postgres.Config{
Password: config.DatabasePassword,
User: config.DatabaseUser,
Database: config.Database,
Host: config.DatabaseHost,
Port: config.DatabasePort,
BatchSize: config.DatabaseBatchSize,
PostgresLogLevel: "error",
}
pg, err := postgres.NewDatabase(postgresConfig, true)
if err != nil {
return errors.Wrap(err, "unable to initialize a new database")
}
dbTable := meta.StorageName
if steps.CreateMetadataTables {
err = pg.CreateSolutionMetadataTables()
if err != nil {
return err
}
}
// Drop the current table if requested.
dbTableBase := fmt.Sprintf("%s%s", dbTable, baseTableSuffix)
_ = pg.DropView(dbTable)
_ = pg.DropTable(dbTableBase)
_ = pg.DropTable(fmt.Sprintf("%s%s", dbTable, explainTableSuffix))
// Create the database table.
ds, err := pg.InitializeDataset(meta)
if err != nil {
return errors.Wrap(err, "unable to initialize a new dataset")
}
err = pg.InitializeTable(dbTable, ds)
if err != nil {
return errors.Wrap(err, "unable to initialize a table")
}
err = pg.StoreMetadata(dbTable)
if err != nil {
return errors.Wrap(err, "unable to store the metadata")
}
err = pg.CreateResultTable(dbTable)
if err != nil {
return errors.Wrap(err, "unable to create the result table")
}
// Load the data.
log.Infof("inserting rows into database based on data found in %s", dataDir)
datasetStorage := serialization.GetStorage(dataDir)
data, err := datasetStorage.ReadData(dataDir)
if err != nil {
return errors.Wrap(err, "unable to read input data")
}
// skip header
data = data[1:]
count := 0
for _, line := range data {
err = pg.AddWordStems(line)
if err != nil {
log.Warn(fmt.Sprintf("%v", err))
}
err = pg.IngestRow(dbTable, line)
if err != nil {
return errors.Wrap(err, "unable to ingest row")
}
count = count + 1
if count%10000 == 0 {
log.Infof("inserted %d rows so far", count)
}
}
log.Infof("ingesting final rows")
err = pg.InsertRemainingRows()
if err != nil {
return errors.Wrap(err, "unable to ingest last rows")
}
log.Infof("checking if indices are necessary")
err = createIndices(pg, meta.ID, params.IndexFields, meta, config)
if err != nil {
return err
}
log.Infof("all data ingested")
return nil
}
func loadMetadataForIngest(originalSchemaFile string, schemaFile string, params *IngestParams, config *IngestTaskConfig, steps *IngestSteps) (string, *model.Metadata, error) {
datasetDir := path.Dir(schemaFile)
meta, err := metadata.LoadMetadataFromClassification(schemaFile, path.Join(datasetDir, config.ClassificationOutputPathRelative), true, steps.FallbackMerged)
if err != nil {
return "", nil, errors.Wrap(err, "unable to load original schema file")
}
if params.Source == metadata.Seed {
meta.DatasetFolder = path.Base(path.Dir(path.Dir(originalSchemaFile)))
} else {
meta.DatasetFolder = path.Base(path.Dir(originalSchemaFile))
}
mainDR := meta.GetMainDataResource()
dataDir := model.GetResourcePath(schemaFile, mainDR)
log.Infof("using %s as data directory (built from %s and %s)", dataDir, datasetDir, mainDR.ResPath)
// check and fix metadata issues
if steps.VerifyMetadata {
updated, err := metadata.VerifyAndUpdate(meta, dataDir, params.Source)
if err != nil {
return "", nil, errors.Wrap(err, "unable to fix metadata")
}
// store the updated metadata
if updated {
extendedOutput := params.Source == metadata.Augmented
log.Infof("storing updated (extended: %v) metadata to %s", extendedOutput, originalSchemaFile)
datasetStorage := serialization.GetStorage(originalSchemaFile)
err = datasetStorage.WriteMetadata(originalSchemaFile, meta, extendedOutput, false)
if err != nil {
return "", nil, errors.Wrap(err, "unable to store updated metadata")
}
log.Infof("updated metadata written to %s", originalSchemaFile)
}
}
err = metadata.LoadImportance(meta, path.Join(datasetDir, config.RankingOutputPathRelative))
if err != nil {
log.Warnf("unable to load importance from file: %v", err)
}
// load stats and adjust for header row (may want to do in the LoadDatasetStats function)
err = metadata.LoadDatasetStats(meta, dataDir)
if err != nil {
log.Warnf("unable to load stats: %v", err)
}
meta.NumRows = meta.NumRows - 1
// load summary
metadata.LoadSummaryFromDescription(meta, path.Join(datasetDir, config.SummaryOutputPathRelative))
// load machine summary
err = metadata.LoadSummaryMachine(meta, path.Join(datasetDir, config.SummaryMachineOutputPathRelative))
// NOTE: For now ignore summary errors!
if err != nil {
log.Warnf("unable to load machine summary: %v", err)
}
// set the origin
if params.Origins != nil {
meta.DatasetOrigins = params.Origins
}
// set the definitive types
for _, v := range meta.GetMainDataResource().Variables {
if params.DefinitiveTypes != nil && params.DefinitiveTypes[v.Key] != nil {
v.Type = params.DefinitiveTypes[v.Key].Type
}
}
return datasetDir, meta, nil
}
func matchDataset(storage api.MetadataStorage, meta *model.Metadata) (string, error) {
// load the datasets from ES.
datasets, err := storage.FetchDatasets(true, true, false)
if err != nil {
return "", errors.Wrap(err, "unable to fetch datasets for matching")
}
// See if any of the loaded datasets match.
for _, dataset := range datasets {
if dataset.ID == meta.ID {
return dataset.Name, nil
}
variables := make([]string, 0)
for _, v := range dataset.Variables {
variables = append(variables, v.Key)
}
if metadata.DatasetMatches(meta, variables) {
return dataset.Name, nil
}
}
// No matching set.
return "", nil
}
func deleteDataset(name string, pg *postgres.Database, meta api.MetadataStorage) error {
success := false
for i := 0; i < 10 && !success; i++ {
err := meta.DeleteDataset(name, false)
if err != nil {
log.Error(err)
} else {
success = true
}
}
if success {
pg.DeleteDataset(name)
}
return nil
}
func getUniqueDatasetID(meta *model.Metadata, storage api.MetadataStorage) (string, error) {
// create a unique name if the current name is already in use
datasets, err := storage.FetchDatasets(false, false, false)
if err != nil {
return "", err
}
datasetIDs := make([]string, 0)
for _, ds := range datasets {
datasetIDs = append(datasetIDs, ds.ID)
}
// get a unique id based on the existence of the file on disk
// this approach will result in the concatenation of "_1" until a unique dataset is found
datasetExists := true
uniqueDatasetID := meta.ID
for datasetExists {
uniqueDatasetID = getUniqueString(uniqueDatasetID, datasetIDs)
// make sure the dataset doesnt exist
datasetExists, err = storage.DatasetExists(uniqueDatasetID)
if err != nil {
return "", err
}
// set the existing datasets to the unique id since that will be the base
datasetIDs = []string{uniqueDatasetID}
}
return uniqueDatasetID, nil
}
func createIndices(pg *postgres.Database, datasetID string, fields []string, meta *model.Metadata, config *IngestTaskConfig) error {
// build variable lookup
mappedVariables := api.MapVariables(meta.GetMainDataResource().Variables, func(variable *model.Variable) string { return variable.Key })
// create indices for flagged fields
for _, fieldName := range fields {
field := mappedVariables[fieldName]
log.Infof("creating index on %s", field.Key)
err := pg.CreateIndex(fmt.Sprintf("%s%s", meta.StorageName, baseTableSuffix), field.Key, field.Type)
if err != nil {
return err
}
}
return nil
}