def testFetchMostRecentGraphSeries_MultipleGraphs(self): 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) self.assertEqual( client_report_utils.FetchMostRecentGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION), graph_series_list[1])
def testFetchMostRecentGraphSeries_SingleGraph(self): 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) self.assertEqual( client_report_utils.FetchMostRecentGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE), graph_series_list[1])
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 _WriteFleetBreakdownStatsToDB(fleet_stats, report_type): """Saves a snapshot of client activity stats to the DB. Args: fleet_stats: Client activity stats returned by the DB. report_type: rdf_stats.ClientGraphSeries.ReportType for the client stats. """ graph_series_by_label = collections.defaultdict( lambda: rdf_stats.ClientGraphSeries(report_type=report_type)) for day_bucket in fleet_stats.GetDayBuckets(): for client_label in fleet_stats.GetAllLabels(): graph = rdf_stats.Graph(title="%d day actives for %s label" % (day_bucket, client_label)) values = fleet_stats.GetValuesForDayAndLabel( day_bucket, client_label) for category_value, num_actives in sorted(values.items()): graph.Append(label=category_value, y_value=num_actives) graph_series_by_label[client_label].graphs.Append(graph) # Generate aggregate graphs for all clients in the snapshot (total for # every category_value regardless of label). for day_bucket in fleet_stats.GetDayBuckets(): graph = rdf_stats.Graph(title="%d day actives for %s label" % (day_bucket, _ALL_CLIENT_FLEET_STATS_LABEL)) totals = fleet_stats.GetTotalsForDay(day_bucket) for category_value, num_actives in sorted(totals.items()): graph.Append(label=category_value, y_value=num_actives) graph_series_by_label[_ALL_CLIENT_FLEET_STATS_LABEL].graphs.Append( graph) for client_label, graph_series in graph_series_by_label.items(): client_report_utils.WriteGraphSeries(graph_series, client_label)
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 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 testFetchMostRecentGraphSeries_MissingType(self): graph_series = _CreateNDayActiveGraphSeries(1)[0] client_report_utils.WriteGraphSeries(graph_series, _TEST_LABEL) self.assertIsNotNone( client_report_utils.FetchMostRecentGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE)) self.assertIsNone( client_report_utils.FetchMostRecentGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.OS_TYPE))
def _WriteFleetAggregateStatsToDB(client_label, bucket_dict): graph = rdf_stats.Graph() for day_bucket, num_actives in sorted(bucket_dict.items()): graph.Append(x_value=rdfvalue.Duration.From( day_bucket, rdfvalue.DAYS).microseconds, y_value=num_actives) graph_series = rdf_stats.ClientGraphSeries( report_type=rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE) graph_series.graphs.Append(graph) client_report_utils.WriteGraphSeries(graph_series, client_label)
def FinishProcessing(self): # Build and store the graph now. Day actives are cumulative. for label in self.values: cumulative_count = 0 graph_series = rdf_stats.ClientGraphSeries( report_type=rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE) graph_series.graphs.Append(rdf_stats.Graph()) for x, y in zip(self._bins, self.values[label]): cumulative_count += y graph_series.graphs[0].Append(x_value=x, y_value=cumulative_count) client_report_utils.WriteGraphSeries( graph_series, label, token=self.token)
def Save(self): """Generate a histogram object and store in the specified attribute.""" graph_series_by_label = {} for active_time in self.active_days: for label in self.categories[active_time]: graphs_for_label = graph_series_by_label.setdefault( label, rdf_stats.ClientGraphSeries(report_type=self._report_type)) graph = rdf_stats.Graph(title="%s day actives for %s label" % (active_time, label)) for k, v in sorted(iteritems(self.categories[active_time][label])): graph.Append(label=k, y_value=v) graphs_for_label.graphs.Append(graph) for label, graph_series in iteritems(graph_series_by_label): client_report_utils.WriteGraphSeries(graph_series, label)