def testFetchAllGraphSeries_MissingType(self): graph_series = _CreateGRRVersionGraphSeries(1)[0] client_report_utils.WriteGraphSeries(graph_series, _TEST_LABEL) self.assertNotEmpty( client_report_utils.FetchAllGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION)) self.assertEmpty( client_report_utils.FetchAllGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.OS_TYPE))
def GetReportData(self, get_report_args, token): """Show how the last active breakdown evolved over time.""" report = rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.LINE_CHART) series_with_timestamps = client_report_utils.FetchAllGraphSeries( get_report_args.client_label, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION, period=rdfvalue.DurationSeconds("180d")) categories = {} for timestamp, graph_series in sorted( iteritems(series_with_timestamps)): self._ProcessGraphSeries(graph_series, timestamp, categories) graphs = [] for k, v in iteritems(categories): graph = dict(label=k, data=v) graphs.append(graph) report.line_chart.data = sorted( (rdf_report_plugins.ApiReportDataSeries2D( label=label, points=(rdf_report_plugins.ApiReportDataPoint2D(x=x, y=y) for x, y in points)) for label, points in iteritems(categories)), key=lambda series: series.label) return report
def GetReportData(self, get_report_args, token): """Show how the last active breakdown evolved over time.""" report = rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.LINE_CHART) series_with_timestamps = client_report_utils.FetchAllGraphSeries( get_report_args.client_label, rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE, period=rdfvalue.Duration.From(180, rdfvalue.DAYS)) categories = {} for timestamp, graph_series in sorted( iteritems(series_with_timestamps)): self._ProcessGraphSeries(graph_series, timestamp, categories) series = [] for label, points in iteritems(categories): series.append( rdf_report_plugins.ApiReportDataSeries2D( label=label, points=(rdf_report_plugins.ApiReportDataPoint2D(x=x, y=y) for x, y in points))) report.line_chart.data = sorted(series, key=lambda s: int(s.label.split()[0]), reverse=True) return report
def testFetchAllGraphSeries_InPeriod(self): graph_series_list = _CreateGRRVersionGraphSeries(10) for i, graph_series in enumerate(graph_series_list): with test_lib.FakeTime( rdfvalue.RDFDatetime.FromSecondsSinceEpoch(i)): client_report_utils.WriteGraphSeries(graph_series, _TEST_LABEL, token=self.token) with test_lib.FakeTime(rdfvalue.RDFDatetime.FromSecondsSinceEpoch(10)): # It is now 1 second after the last graph-series was written. Fetch all # series written starting from 4 seconds ago. fetched_data = client_report_utils.FetchAllGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION, rdfvalue.DurationSeconds("4s")) expected_data = { rdfvalue.RDFDatetime.FromSecondsSinceEpoch(6): graph_series_list[6], rdfvalue.RDFDatetime.FromSecondsSinceEpoch(7): graph_series_list[7], rdfvalue.RDFDatetime.FromSecondsSinceEpoch(8): graph_series_list[8], rdfvalue.RDFDatetime.FromSecondsSinceEpoch(9): graph_series_list[9], } self.assertSequenceEqual(fetched_data, expected_data)
def testWriteGraphSeries_SingleGraph(self): # Simulate two runs of the cronjob that computes n-day-active stats. graph_series_list = _CreateNDayActiveGraphSeries(2) with test_lib.FakeTime(rdfvalue.RDFDatetime(1000)): client_report_utils.WriteGraphSeries(graph_series_list[0], _TEST_LABEL) with test_lib.FakeTime(rdfvalue.RDFDatetime(2000)): client_report_utils.WriteGraphSeries(graph_series_list[1], _TEST_LABEL) fetched_data = client_report_utils.FetchAllGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE) expected_data = { rdfvalue.RDFDatetime(1000): graph_series_list[0], rdfvalue.RDFDatetime(2000): graph_series_list[1], } self.assertDictEqual(fetched_data, expected_data)
def testWriteGraphSeries_MultipleGraphs(self): # Simulate two runs of the cronjob that computes GRR-version stats. graph_series_list = _CreateGRRVersionGraphSeries(2) with test_lib.FakeTime(rdfvalue.RDFDatetime(1000)): client_report_utils.WriteGraphSeries(graph_series_list[0], _TEST_LABEL) with test_lib.FakeTime(rdfvalue.RDFDatetime(2000)): client_report_utils.WriteGraphSeries(graph_series_list[1], _TEST_LABEL) fetched_data = client_report_utils.FetchAllGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION) expected_data = { rdfvalue.RDFDatetime(1000): graph_series_list[0], rdfvalue.RDFDatetime(2000): graph_series_list[1], } self.assertDictEqual(fetched_data, expected_data)
def testFetchAllGraphSeries_NonExistentLabel(self): self.assertEmpty( client_report_utils.FetchAllGraphSeries( "nonexistent", rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION))