Author: Paolo Lulli <paolo@lulli.net>
Client side pagination for events
client/main.go | 48 ++++++++++++++++++++++++++++++------------------
diff --git a/client/main.go b/client/main.go index 2bb9c987373ad8949fe8a93e8fce0d0799928cd0..e48659d360b0e653e0a6001337e73b23042f33ec 100644 --- a/client/main.go +++ b/client/main.go @@ -162,23 +162,20 @@ } if *listEvents { listEvents := yatsClient.EventList(*sourceOption, fromTimestamp, toTimestamp) - if *reformatOutput { - dataJson, errDataPortion := getDataJson(listEvents) - if errDataPortion { - fmt.Printf("Could not parse data attribute: %v", errDataPortion) - return - } + eventListModel, maxpage, _ := responseToEventArray(listEvents) + var composedList []EventModel + composedList = append(composedList, eventListModel...) - var eventListModel []EventModel - err := json.Unmarshal(dataJson, &eventListModel) + for (len(eventListModel) == 100) && !stopSearching(maxpage, toTimestamp) { + listEvents = yatsClient.EventList(*sourceOption, maxpage/1000, toTimestamp) + eventListModel, maxpage, _ = responseToEventArray(listEvents) + composedList = append(composedList, eventListModel...) + } - if err == nil { - for _, event := range eventListModel { - eventTime := formattedUtcTimeInSec(event.Etime) - fmt.Printf("%s %s\n", eventTime, event.Name) - } - } else { - fmt.Println(err) + if *reformatOutput { + for _, event := range composedList { + eventTime := formattedUtcTimeInSec(event.Etime) + fmt.Printf("%s %s\n", eventTime, event.Name) } } else { fmt.Printf("%s", listEvents) @@ -188,13 +185,13 @@ } if "" != *listMetrics { listResponse, err := yatsClient.MetricList(*sourceOption, *listMetrics, fromTimestamp, toTimestamp) - metricListModel, maxpage, _ := responseToArray(listResponse) + metricListModel, maxpage, _ := responseToMetricArray(listResponse) var composedList []MetricModel composedList = append(composedList, metricListModel...) for (len(metricListModel) == 100) && !stopSearching(maxpage, toTimestamp) { listResponse, _ = yatsClient.MetricList(*sourceOption, *listMetrics, maxpage/1000, toTimestamp) - metricListModel, maxpage, _ = responseToArray(listResponse) + metricListModel, maxpage, _ = responseToMetricArray(listResponse) composedList = append(composedList, metricListModel...) } @@ -281,7 +278,7 @@ os.Exit(0) } } -func responseToArray(listResponse string) ([]MetricModel, int64, bool) { +func responseToMetricArray(listResponse string) ([]MetricModel, int64, bool) { dataJson, errDataPortion := getDataJson(listResponse) maxpage := getMaxpage(listResponse) if errDataPortion { @@ -297,6 +294,21 @@ } return metricListModel, maxpage, false } +func responseToEventArray(listResponse string) ([]EventModel, int64, bool) { + dataJson, errDataPortion := getDataJson(listResponse) + maxpage := getMaxpage(listResponse) + if errDataPortion { + fmt.Printf("Could not parse data attribute: %v", errDataPortion) + return nil, 0, true + } + + var eventListModel []EventModel + err := json.Unmarshal(dataJson, &eventListModel) + if err == nil { + return eventListModel, maxpage, true + } + return eventListModel, maxpage, false +} func stopSearching(maxpage int64, toTimestamp int64) bool { if toTimestamp == 0 { return false