@@ -28,74 +28,87 @@ import (
28
28
"github.com/sirupsen/logrus"
29
29
)
30
30
31
- func StartExporter (
32
- ctx context.Context ,
33
- logger * logrus.Entry ,
34
- key , secret , orgid string ,
35
- tagsF * string ,
36
- resolution , timeWindowMinutes int ,
37
- destinationS3Bucket string ,
38
- aggregationStat string ,
39
- compress , enableAlignTimeWindow bool ) error {
31
+ type samplesExporter struct {
32
+ iotClient * iot.Client
33
+ logger * logrus.Entry
34
+ tagsF * string
35
+ compress bool
36
+ enableAlignTimeWindow bool
37
+ }
40
38
41
- // Init client
39
+ func New ( key , secret , orgid string , tagsF * string , compress , enableAlignTimeWindow bool , logger * logrus. Entry ) ( * samplesExporter , error ) {
42
40
iotcl , err := iot .NewClient (key , secret , orgid )
43
41
if err != nil {
44
- return err
42
+ return nil , err
45
43
}
46
44
47
- if tagsF != nil {
48
- logger .Infoln ("Filtering things linked to configured account using tags: " , * tagsF )
45
+ return & samplesExporter {
46
+ iotClient : iotcl ,
47
+ logger : logger ,
48
+ tagsF : tagsF ,
49
+ compress : compress ,
50
+ enableAlignTimeWindow : enableAlignTimeWindow ,
51
+ }, nil
52
+ }
53
+
54
+ func (s * samplesExporter ) StartExporter (
55
+ ctx context.Context ,
56
+ resolution , timeWindowMinutes int ,
57
+ destinationS3Bucket string ,
58
+ aggregationStat string ) error {
59
+
60
+ if s .tagsF != nil {
61
+ s .logger .Infoln ("Filtering things linked to configured account using tags: " , * s .tagsF )
49
62
} else {
50
- logger .Infoln ("Importing all things linked to configured account" )
63
+ s . logger .Infoln ("Importing all things linked to configured account" )
51
64
}
52
65
53
- things , err := iotcl . ThingList (ctx , nil , nil , true , utils .ParseTags (tagsF ))
66
+ things , err := s . iotClient . ThingList (ctx , nil , nil , true , utils .ParseTags (s . tagsF ))
54
67
if err != nil {
55
68
return err
56
69
}
57
70
thingsMap := make (map [string ]iotclient.ArduinoThing , len (things ))
58
71
for _ , thing := range things {
59
- logger .Infoln (" Thing: " , thing .Id , thing .Name )
72
+ s . logger .Infoln (" Thing: " , thing .Id , thing .Name )
60
73
thingsMap [thing .Id ] = thing
61
74
}
62
75
63
76
// Extract data points from thing and push to S3
64
- tsextractorClient := tsextractor .New (iotcl , logger )
77
+ tsextractorClient := tsextractor .New (s . iotClient , s . logger )
65
78
66
79
// Open s3 output writer
67
80
s3cl , err := s3 .NewS3Client (destinationS3Bucket )
68
81
if err != nil {
69
82
return err
70
83
}
71
84
72
- if writer , from , err := tsextractorClient .ExportTSToFile (ctx , timeWindowMinutes , thingsMap , resolution , aggregationStat , enableAlignTimeWindow ); err != nil {
85
+ if writer , from , err := tsextractorClient .ExportTSToFile (ctx , timeWindowMinutes , thingsMap , resolution , aggregationStat , s . enableAlignTimeWindow ); err != nil {
73
86
if writer != nil {
74
87
writer .Close ()
75
88
defer writer .Delete ()
76
89
}
77
- logger .Error ("Error aligning time series samples: " , err )
90
+ s . logger .Error ("Error aligning time series samples: " , err )
78
91
return err
79
92
} else {
80
93
writer .Close ()
81
94
defer writer .Delete ()
82
95
83
96
fileToUpload := writer .GetFilePath ()
84
97
destinationKeyFormat := "%s/%s.csv"
85
- if compress {
86
- logger .Infof ("Compressing file: %s\n " , fileToUpload )
98
+ if s . compress {
99
+ s . logger .Infof ("Compressing file: %s\n " , fileToUpload )
87
100
compressedFile , err := utils .GzipFileCompression (fileToUpload )
88
101
if err != nil {
89
102
return err
90
103
}
91
104
fileToUpload = compressedFile
92
- logger .Infof ("Generated compressed file: %s\n " , fileToUpload )
105
+ s . logger .Infof ("Generated compressed file: %s\n " , fileToUpload )
93
106
destinationKeyFormat = "%s/%s.csv.gz"
94
107
defer func (f string ) { os .Remove (f ) }(fileToUpload )
95
108
}
96
109
97
110
destinationKey := fmt .Sprintf (destinationKeyFormat , from .Format ("2006-01-02" ), from .Format ("2006-01-02-15-04" ))
98
- logger .Infof ("Uploading file %s to bucket %s/%s\n " , fileToUpload , s3cl .DestinationBucket (), destinationKey )
111
+ s . logger .Infof ("Uploading file %s to bucket %s/%s\n " , fileToUpload , s3cl .DestinationBucket (), destinationKey )
99
112
if err := s3cl .WriteFile (ctx , destinationKey , fileToUpload ); err != nil {
100
113
return err
101
114
}
0 commit comments