Author: Paolo Lulli <paolo@lulli.net>
Improve time filter ergonomics
client/main.go | 65 ++++++++++++++++++++++++++++++++-------------------
diff --git a/client/main.go b/client/main.go index d5cee29a754bf282c5b3394d58033c331e5c5ebc..15ebad76fa81a21d4d3fc7a4dac9df0a84fda5c1 100644 --- a/client/main.go +++ b/client/main.go @@ -26,7 +26,7 @@ func main() { var configPath string if len(os.Args) == 1 { - fmt.Printf("For usage: %s --help\n", os.Args[0]) + fmt.Printf("Usage: \n\n%s --help\n", os.Args[0]) os.Exit(1) } @@ -66,23 +66,19 @@ sinceYears := flag.BoolP("year", "", false, "years ago") flag.Parse() - var actualTimestamp int64 - if 0 != *timestampOption { - if *sinceSeconds { - actualTimestamp = time.Now().Unix() - *timestampOption - } - if *sinceMinutes { - actualTimestamp = time.Now().Unix() - *timestampOption*60 - } - if *sinceHours { - actualTimestamp = time.Now().Unix() - *timestampOption*60*60 - } - if *sinceDays { - actualTimestamp = time.Now().Unix() - *timestampOption*60*60*24 - } - if *sinceYears { - actualTimestamp = time.Now().Unix() - *timestampOption*60*60*24*365 - } + var fromTimestamp int64 + var toTimestamp int64 + + if *fromOption != 0 { + fromTimestamp = calculateCutDate(*fromOption, sinceSeconds, sinceMinutes, sinceHours, sinceDays, sinceYears) + } else { + fromTimestamp = 0 + } + + if *toOption != 0 { + toTimestamp = calculateCutDate(*toOption, sinceSeconds, sinceMinutes, sinceHours, sinceDays, sinceYears) + } else { + toTimestamp = 0 } if *isShowPermsOption { @@ -101,7 +97,7 @@ os.Exit(0) } if "" != *writeEvent { - saveEventResponse, err := yatsClient.EventSave(*writeEvent, actualTimestamp) + saveEventResponse, err := yatsClient.EventSave(*writeEvent, *timestampOption) if err == nil { fmt.Printf("%s", saveEventResponse) } else { @@ -111,7 +107,7 @@ os.Exit(0) } if "" != *writeMetric { - listResponse, err := yatsClient.MetricSave(*writeMetric, *metricValue, actualTimestamp) + listResponse, err := yatsClient.MetricSave(*writeMetric, *metricValue, *timestampOption) if err == nil { fmt.Printf("%s", listResponse) } else { @@ -121,7 +117,7 @@ os.Exit(0) } if "" != *writePosition { - savePositionResponse, err := yatsClient.PositionSave(*writePosition, *latitudeOption, *longitudeOption, actualTimestamp) + savePositionResponse, err := yatsClient.PositionSave(*writePosition, *latitudeOption, *longitudeOption, *timestampOption) if err == nil { fmt.Printf("%s", savePositionResponse) } else { @@ -131,13 +127,13 @@ os.Exit(0) } if *listEvents { - listEvents := yatsClient.EventList(*sourceOption, *fromOption, *toOption) + listEvents := yatsClient.EventList(*sourceOption, fromTimestamp, toTimestamp) fmt.Printf("%s", listEvents) os.Exit(0) } if "" != *listMetrics { - listResponse, err := yatsClient.MetricList(*sourceOption, *listMetrics, *fromOption, *toOption) + listResponse, err := yatsClient.MetricList(*sourceOption, *listMetrics, fromTimestamp, toTimestamp) if err == nil { fmt.Printf("%s", listResponse) } else { @@ -147,8 +143,29 @@ os.Exit(0) } if "" != *listPositions { - listResponse := yatsClient.PositionList(*sourceOption, *fromOption, *toOption) + listResponse := yatsClient.PositionList(*sourceOption, fromTimestamp, toTimestamp) fmt.Printf("%s", listResponse) os.Exit(0) } } + +func calculateCutDate(ts int64, sinceSeconds *bool, sinceMinutes *bool, sinceHours *bool, sinceDays *bool, sinceYears *bool) int64 { + if 0 != ts { + if *sinceSeconds { + return time.Now().Unix() - ts + } + if *sinceMinutes { + return time.Now().Unix() - ts*60 + } + if *sinceHours { + return time.Now().Unix() - ts*60*60 + } + if *sinceDays { + return time.Now().Unix() - ts*60*60*24 + } + if *sinceYears { + return time.Now().Unix() - ts*60*60*24*365 + } + } + return ts +}