add Reporter.HasReports() for cheap report availability checking

This requires no report reading / merging.

We plan to expose this in the HTTP API, so the UI gets a cheap way of
checking whether the app is currently receiving data from probes.
This commit is contained in:
Matthias Radestock
2017-12-14 00:13:45 +00:00
parent 54fe1e37da
commit 72b9e9c6b9
2 changed files with 25 additions and 0 deletions
+20
View File
@@ -29,6 +29,7 @@ const reportQuantisationInterval = 3 * time.Second
// interface for parts of the app, and several experimental components.
type Reporter interface {
Report(context.Context, time.Time) (report.Report, error)
HasReports(context.Context, time.Time) (bool, error)
HasHistoricReports() bool
WaitOn(context.Context, chan struct{})
UnWait(context.Context, chan struct{})
@@ -161,6 +162,19 @@ func (c *collector) Report(_ context.Context, timestamp time.Time) (report.Repor
return rpt, nil
}
// HasReports indicates whether the collector contains reports between
// timestamp-app.window and timestamp.
func (c *collector) HasReports(ctx context.Context, timestamp time.Time) (bool, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
if len(c.timestamps) < 1 {
return false, nil
}
return !c.timestamps[0].After(timestamp) && !c.timestamps[len(c.reports)-1].Before(timestamp.Add(-c.window)), nil
}
// HasHistoricReports indicates whether the collector contains reports
// older than now-app.window.
func (c *collector) HasHistoricReports() bool {
@@ -223,6 +237,12 @@ func (c StaticCollector) Report(context.Context, time.Time) (report.Report, erro
return report.Report(c), nil
}
// HasReports indicates whether the collector contains reports between
// timestamp-app.window and timestamp.
func (c StaticCollector) HasReports(context.Context, time.Time) (bool, error) {
return true, nil
}
// HasHistoricReports indicates whether the collector contains reports
// older than now-app.window.
func (c StaticCollector) HasHistoricReports() bool {
+5
View File
@@ -349,6 +349,11 @@ func (c *awsCollector) Report(ctx context.Context, timestamp time.Time) (report.
return c.merger.Merge(reports), nil
}
func (c *awsCollector) HasReports(ctx context.Context, timestamp time.Time) (bool, error) {
reportKeys, err := c.getReportKeys(ctx, timestamp)
return len(reportKeys) > 0, err
}
func (c *awsCollector) HasHistoricReports() bool {
return true
}