Skip to content

Commit b5770f1

Browse files
author
Evan Damon
committed
[feat] Add support for time ranges longer than one hour
1 parent fa61084 commit b5770f1

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

cmd/logshare-cli/main.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,40 @@ func run(conf *config) func(c *cli.Context) error {
113113
if err != nil {
114114
return errors.Wrap(err, "failed to fetch field names")
115115
}
116+
} else if conf.iterate {
117+
// Iterate over the time range in 1h (3600s) blocks, making sure to
118+
// stay within the request rate limit (1 request/5 seconds)
119+
hourStart := conf.startTime
120+
hourEnd := hourStart + 3600
121+
var previousRequest time.Time
122+
for hourStart < conf.endTime {
123+
if hourEnd > conf.endTime {
124+
hourEnd = conf.endTime
125+
}
126+
time.Sleep(5*time.Second - time.Since(previousRequest))
127+
previousRequest = time.Now()
128+
meta, err = client.GetFromTimestamp(
129+
conf.zoneID, hourStart, hourEnd, conf.count)
130+
if err != nil {
131+
return errors.Wrap(err, "failed to fetch via timestamp")
132+
}
133+
log.Printf("HTTP status %d | %dms | %s",
134+
meta.StatusCode, meta.Duration, meta.URL)
135+
log.Printf("Retrieved %d logs", meta.Count)
136+
hourStart = hourEnd
137+
hourEnd += 3600
138+
}
116139
} else {
117140
meta, err = client.GetFromTimestamp(
118141
conf.zoneID, conf.startTime, conf.endTime, conf.count)
119142
if err != nil {
120143
return errors.Wrap(err, "failed to fetch via timestamp")
121144
}
145+
log.Printf("HTTP status %d | %dms | %s",
146+
meta.StatusCode, meta.Duration, meta.URL)
147+
log.Printf("Retrieved %d logs", meta.Count)
122148
}
123149

124-
log.Printf("HTTP status %d | %dms | %s",
125-
meta.StatusCode, meta.Duration, meta.URL)
126-
log.Printf("Retrieved %d logs", meta.Count)
127-
128150
return nil
129151
}
130152
}
@@ -143,6 +165,7 @@ func parseFlags(conf *config, c *cli.Context) error {
143165
conf.listFields = c.Bool("list-fields")
144166
conf.googleStorageBucket = c.String("google-storage-bucket")
145167
conf.googleProjectID = c.String("google-project-id")
168+
conf.iterate = c.Bool("iterate")
146169

147170
return conf.Validate()
148171
}
@@ -161,6 +184,7 @@ type config struct {
161184
listFields bool
162185
googleStorageBucket string
163186
googleProjectID string
187+
iterate bool
164188
}
165189

166190
func (conf *config) Validate() error {
@@ -181,6 +205,10 @@ func (conf *config) Validate() error {
181205
return errors.New("Both google-storage-bucket and google-project-id must be provided to upload to Google Storage")
182206
}
183207

208+
if conf.endTime > (conf.startTime+3600) && conf.iterate == false {
209+
return errors.New("Time range too long; ranges longer than 1h0m0s require the --iterate option")
210+
}
211+
184212
return nil
185213
}
186214

@@ -246,4 +274,8 @@ var flags = []cli.Flag{
246274
Name: "google-project-id",
247275
Usage: "Project ID of the Google Cloud Storage Bucket to upload logs to",
248276
},
277+
cli.BoolFlag{
278+
Name: "iterate",
279+
Usage: "Iterate over time ranges longer than normally allowed by the Logpull Rest API",
280+
},
249281
}

0 commit comments

Comments
 (0)