Skip to content

Commit 3f0dbd0

Browse files
committed
Code refactoring and testing
1 parent 672531e commit 3f0dbd0

File tree

6 files changed

+107
-34
lines changed

6 files changed

+107
-34
lines changed

business/tsextractor/tsextractor.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ func New(iotcl *iot.Client, logger *logrus.Entry) *TsExtractor {
4545
return &TsExtractor{iotcl: iotcl, logger: logger}
4646
}
4747

48+
func computeTimeAlignment(resolutionSeconds, timeWindowInMinutes int) (time.Time, time.Time) {
49+
// Compute time alignment
50+
if resolutionSeconds <= 60 {
51+
resolutionSeconds = 300 // Align to 5 minutes
52+
}
53+
to := time.Now().Truncate(time.Duration(resolutionSeconds) * time.Second).UTC()
54+
from := to.Add(-time.Duration(timeWindowInMinutes) * time.Minute)
55+
return from, to
56+
}
57+
4858
func (a *TsExtractor) ExportTSToS3(
4959
ctx context.Context,
5060
timeWindowInMinutes int,
@@ -53,8 +63,7 @@ func (a *TsExtractor) ExportTSToS3(
5363
destinationS3Bucket string) error {
5464

5565
// Truncate time to given resolution
56-
to := time.Now().Truncate(time.Duration(resolution) * time.Second).UTC()
57-
from := to.Add(-time.Duration(timeWindowInMinutes) * time.Minute)
66+
from, to := computeTimeAlignment(resolution, timeWindowInMinutes)
5867

5968
// Open s3 output writer
6069
s3cl, err := s3.NewS3Client(destinationS3Bucket)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tsextractor
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestTimeAlignment_HourlyTimeWindows(t *testing.T) {
10+
// Test the time alignment with hourly time windows
11+
from, to := computeTimeAlignment(3600, 60)
12+
assert.Equal(t, int64(3600), to.Unix()-from.Unix())
13+
}
14+
15+
func TestTimeAlignment_15minTimeWindows(t *testing.T) {
16+
// Test the time alignment with hourly time windows
17+
from, to := computeTimeAlignment(3600, 15)
18+
assert.Equal(t, int64(900), to.Unix()-from.Unix())
19+
}
20+
21+
func TestTimeAlignment_15min_HourlyTimeWindows(t *testing.T) {
22+
// Test the time alignment with hourly time windows and 15min resolution
23+
from, to := computeTimeAlignment(900, 60)
24+
assert.Equal(t, int64(3600), to.Unix()-from.Unix())
25+
}
26+
27+
func TestTimeAlignment_5min_HourlyTimeWindows(t *testing.T) {
28+
// Test the time alignment with hourly time windows and 5min resolution
29+
from, to := computeTimeAlignment(300, 60)
30+
assert.Equal(t, int64(3600), to.Unix()-from.Unix())
31+
}
32+
33+
func TestTimeAlignment_raw_HourlyTimeWindows(t *testing.T) {
34+
// Test the time alignment with hourly time windows and 5min resolution
35+
from, to := computeTimeAlignment(-1, 60)
36+
assert.Equal(t, int64(3600), to.Unix()-from.Unix())
37+
}

deployment/cloud-formation-template/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Parameters:
4242

4343
Resolution:
4444
Type: String
45-
Description: "Samples resolution data extraction resolution."
45+
Description: "Samples resolution data extraction resolution. 'raw' and '1 minute' are not supported for '1 day' scheduling"
4646
AllowedValues:
4747
- raw
4848
- 1 minute

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/aws/aws-sdk-go-v2/service/s3 v1.61.1
1111
github.com/aws/aws-sdk-go-v2/service/ssm v1.50.1
1212
github.com/sirupsen/logrus v1.9.3
13+
github.com/stretchr/testify v1.9.0
1314
golang.org/x/oauth2 v0.21.0
1415
)
1516

@@ -29,6 +30,9 @@ require (
2930
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
3031
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
3132
github.com/aws/smithy-go v1.20.4 // indirect
33+
github.com/davecgh/go-spew v1.1.1 // indirect
3234
github.com/jmespath/go-jmespath v0.4.0 // indirect
35+
github.com/pmezard/go-difflib v1.0.0 // indirect
3336
golang.org/x/sys v0.20.0 // indirect
37+
gopkg.in/yaml.v3 v3.0.1 // indirect
3438
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbht
6262
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6363
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
6464
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
65+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6566
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6667
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
6768
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

lambda.go

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,55 @@ func HandleRequest(ctx context.Context, event *AWSS3ImportTrigger) (*string, err
8181
}
8282

8383
// Resolve resolution
84+
resolution, err := configureExtractionResolution(logger, paramReader)
85+
if err != nil {
86+
return nil, err
87+
}
88+
89+
// Resolve scheduling
90+
extractionWindowMinutes, err := configureDataExtractionTimeWindow(logger, paramReader)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
if *extractionWindowMinutes > 60 && *resolution <= 60 {
96+
logger.Warn("Resolution must be greater than 60 seconds for time windows greater than 60 minutes. Setting resolution to 5 minutes.")
97+
defReso := SamplesResolutionSeconds
98+
resolution = &defReso
99+
}
100+
101+
logger.Infoln("------ Running import...")
102+
if event.Dev || os.Getenv("DEV") == "true" {
103+
logger.Infoln("Running in dev mode")
104+
os.Setenv("IOT_API_URL", "https://api2.oniudra.cc")
105+
}
106+
logger.Infoln("key:", *apikey)
107+
logger.Infoln("secret:", "*********")
108+
if organizationId != "" {
109+
logger.Infoln("organizationId:", organizationId)
110+
} else {
111+
logger.Infoln("organizationId: not set")
112+
}
113+
if tags != nil {
114+
logger.Infoln("tags:", *tags)
115+
}
116+
if *resolution <= 0 {
117+
logger.Infoln("resolution: raw")
118+
} else {
119+
logger.Infoln("resolution:", *resolution, "seconds")
120+
}
121+
logger.Infoln("data extraction time windows:", extractionWindowMinutes, "minutes")
122+
123+
err = importer.StartImport(ctx, logger, *apikey, *apiSecret, organizationId, tags, *resolution, *extractionWindowMinutes, *destinationS3Bucket)
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
message := "Data exported successfully"
129+
return &message, nil
130+
}
131+
132+
func configureExtractionResolution(logger *logrus.Entry, paramReader *parameters.ParametersClient) (*int, error) {
84133
resolution, err := paramReader.ReadIntConfig(SamplesResoSec)
85134
if err != nil {
86135
// Possibly this parameter is not set. Try SamplesReso
@@ -108,8 +157,10 @@ func HandleRequest(ctx context.Context, event *AWSS3ImportTrigger) (*string, err
108157
logger.Errorf("Resolution %d is invalid", *resolution)
109158
return nil, errors.New("resolution must be between -1 and 3600")
110159
}
160+
return resolution, nil
161+
}
111162

112-
// Resolve scheduling
163+
func configureDataExtractionTimeWindow(logger *logrus.Entry, paramReader *parameters.ParametersClient) (*int, error) {
113164
schedule, err := paramReader.ReadConfig(Scheduling)
114165
if err != nil {
115166
logger.Error("Error reading parameter "+Scheduling, err)
@@ -126,36 +177,7 @@ func HandleRequest(ctx context.Context, event *AWSS3ImportTrigger) (*string, err
126177
case "1 day":
127178
extractionWindowMinutes = 24 * 60
128179
}
129-
130-
logger.Infoln("------ Running import...")
131-
if event.Dev || os.Getenv("DEV") == "true" {
132-
logger.Infoln("Running in dev mode")
133-
os.Setenv("IOT_API_URL", "https://api2.oniudra.cc")
134-
}
135-
logger.Infoln("key:", *apikey)
136-
logger.Infoln("secret:", "*********")
137-
if organizationId != "" {
138-
logger.Infoln("organizationId:", organizationId)
139-
} else {
140-
logger.Infoln("organizationId: not set")
141-
}
142-
if tags != nil {
143-
logger.Infoln("tags:", *tags)
144-
}
145-
if *resolution <= 0 {
146-
logger.Infoln("resolution: raw")
147-
} else {
148-
logger.Infoln("resolution:", *resolution, "seconds")
149-
}
150-
logger.Infoln("data extraction time windows:", extractionWindowMinutes, "minutes")
151-
152-
err = importer.StartImport(ctx, logger, *apikey, *apiSecret, organizationId, tags, *resolution, extractionWindowMinutes, *destinationS3Bucket)
153-
if err != nil {
154-
return nil, err
155-
}
156-
157-
message := "Data exported successfully"
158-
return &message, nil
180+
return &extractionWindowMinutes, nil
159181
}
160182

161183
func main() {

0 commit comments

Comments
 (0)