Commit 86a3c168 authored by ale's avatar ale
Browse files

Make --to relative to --from

parent 4e0f0803
......@@ -23,13 +23,13 @@ import (
)
var (
elasticURL = flag.String("url", "http://127.0.0.1:9200", "ES URL")
fromDate = flag.String("from", "-1h", "start date")
toDate = flag.String("to", "", "end date (default now)")
elasticURL = flag.String("url", "http://127.0.0.1:9200", "Elasticsearch URL")
fromDate = flag.String("from", "-1h", "start `date`")
toDate = flag.String("to", "", "end `date` (default now, otherwise relative to --from)")
doDumpQuery = flag.Bool("dump-query", false, "don't do anything, just print the ES query as JSON")
batchSize = flag.Int("batch-size", 10000, "ES scroll batch size")
indexBase = flag.String("index", "logstash", "ES index base name")
objType = flag.String("obj-type", "events", "ES index object type")
batchSize = flag.Int("batch-size", 10000, "ES scroll batch `size`")
indexFmt = flag.String("index-format", "logstash-2006.01.02", "ES index `format` string, as supported by Go time.Format()")
objType = flag.String("obj-type", "events", "ES index object `type`")
)
var allowedTimeFormats = []string{
......@@ -40,7 +40,7 @@ var allowedTimeFormats = []string{
"2006/1/2",
}
func parseTime(now time.Time, s string) (t time.Time, err error) {
func parseTime(base time.Time, s string) (t time.Time, err error) {
var d time.Duration
switch {
case strings.HasPrefix(s, "+"):
......@@ -48,13 +48,13 @@ func parseTime(now time.Time, s string) (t time.Time, err error) {
if err != nil {
return
}
t = now.Add(d)
t = base.Add(d)
case strings.HasPrefix(s, "-"):
d, err = time.ParseDuration(s[1:])
if err != nil {
return
}
t = now.Add(-d)
t = base.Add(-d)
default:
for _, f := range allowedTimeFormats {
t, err = time.Parse(f, s)
......@@ -82,7 +82,7 @@ func parseTimeRange(fromStr, toStr string) (from time.Time, to time.Time, err er
if toStr == "" {
to = now
} else {
to, err = parseTime(now, toStr)
to, err = parseTime(from, toStr)
if err != nil {
err = fmt.Errorf("error in --to: %v", err)
return
......@@ -113,9 +113,8 @@ func (m *logMessage) format() string {
func getIndexes(from, to time.Time) []string {
var idxs []string
t := time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, time.UTC)
idxFmt := fmt.Sprintf("%s-2006.01.02", *indexBase)
for t.Before(to) {
idxs = append(idxs, t.Format(idxFmt))
idxs = append(idxs, t.Format(*indexFmt))
t = t.AddDate(0, 0, 1)
}
return idxs
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment