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 _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 _FetchAllGraphSeriesFromTheLegacyDB( label, report_type, period = None, token = None ): """Fetches graph-series from the legacy DB [see FetchAllGraphSeries()].""" if period is None: time_range = aff4.ALL_TIMES else: range_end = rdfvalue.RDFDatetime.Now() time_range = (range_end - period, range_end) series_with_timestamps = {} try: stats_for_label = aff4.FACTORY.Open( GetAFF4ClientReportsURN().Add(label), aff4_type=aff4_stats.ClientFleetStats, mode="r", age=time_range, token=token) except aff4.InstantiationError: # Nothing to return for the given label and report-type. return series_with_timestamps aff4_attr = _GetAFF4AttributeForReportType(report_type) if aff4_attr.attribute_type == rdf_stats.GraphSeries: for graphs in stats_for_label.GetValuesForAttribute(aff4_attr): graph_series = rdf_stats.ClientGraphSeries(report_type=report_type) for graph in graphs: graph_series.graphs.Append(graph) series_with_timestamps[graphs.age] = graph_series elif aff4_attr.attribute_type == rdf_stats.Graph: for graph in stats_for_label.GetValuesForAttribute(aff4_attr): graph_series = rdf_stats.ClientGraphSeries(report_type=report_type) graph_series.graphs.Append(graph) series_with_timestamps[graph.age] = graph_series else: raise AFF4AttributeTypeError(aff4_attr.attribute_type) return series_with_timestamps
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)
def _FetchMostRecentGraphSeriesFromTheLegacyDB( label, report_type, token = None ): """Fetches the latest graph-series for a client label from the legacy DB. Args: label: Client label to fetch data for. report_type: rdf_stats.ClientGraphSeries.ReportType to fetch data for. token: ACL token to use for reading from the DB. Raises: AFF4AttributeTypeError: If an unexpected report-data type is encountered. Returns: The graph series for the given label and report type that was last written to the DB, or None if no series for that label and report-type exist. """ try: stats_for_label = aff4.FACTORY.Open( GetAFF4ClientReportsURN().Add(label), aff4_type=aff4_stats.ClientFleetStats, mode="r", token=token) except aff4.InstantiationError: # Nothing to return for the given label and report-type. return None aff4_attr = _GetAFF4AttributeForReportType(report_type) graph_series = rdf_stats.ClientGraphSeries(report_type=report_type) if aff4_attr.attribute_type == rdf_stats.GraphSeries: graphs = stats_for_label.Get(aff4_attr) if graphs is None: return None for graph in graphs: graph_series.graphs.Append(graph) elif aff4_attr.attribute_type == rdf_stats.Graph: graph = stats_for_label.Get(aff4_attr) if graph is None: return None graph_series.graphs.Append(graph) else: raise AFF4AttributeTypeError(aff4_attr.attribute_type) return graph_series
def _CreateNDayActiveGraphSeries(num_graph_series): """Creates N_DAY_ACTIVE graphs for use in tests in this file. Args: num_graph_series: The number of graph series to create. Returns: A list of rdf_stats.ClientGraphSeries of type N_DAY_ACTIVE containing realistic test data. """ graph_series_list = [] for series_index in range(num_graph_series): graph_series = rdf_stats.ClientGraphSeries( report_type=rdf_stats.ClientGraphSeries.ReportType.N_DAY_ACTIVE) graph_series.graphs.Append(rdf_stats.Graph()) for i, period in enumerate([1, 2, 3, 7, 14, 30, 60]): graph_series.graphs[0].Append(x_value=period, y_value=i * series_index) graph_series.graphs[0].Append(x_value=period, y_value=i * series_index) graph_series_list.append(graph_series) return graph_series_list
def _CreateGRRVersionGraphSeries(num_graph_series): """Creates GRR_VERSION graphs for use in tests in this file. Args: num_graph_series: The number of graph series to create. Returns: A list of rdf_stats.ClientGraphSeries of type GRR_VERSION containing realistic test data. """ graph_series_list = [] for series_index in range(num_graph_series): graph_series = rdf_stats.ClientGraphSeries( report_type=rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION) for i, period in enumerate([1, 7, 14, 30]): graph = rdf_stats.Graph() graph.title = "%s day actives for %s label" % (period, _TEST_LABEL) graph.Append(label="GRR linux amd64 3000", y_value=i * series_index) graph.Append(label="GRR linux amd64 3001", y_value=i * series_index) graph_series.graphs.Append(graph) graph_series_list.append(graph_series) return graph_series_list