def testMostActiveUsersReportPlugin(self): with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/14")): AddFakeAuditLog( "Fake audit description 14 Dec.", "C.123", "User123", token=self.token) with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/22")): for _ in range(10): AddFakeAuditLog( "Fake audit description 22 Dec.", "C.123", "User123", token=self.token) AddFakeAuditLog( "Fake audit description 22 Dec.", "C.456", "User456", token=self.token) report = report_plugins.GetReportByName( server_report_plugins.MostActiveUsersReportPlugin.__name__) with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/31")): now = rdfvalue.RDFDatetime().Now() month_duration = rdfvalue.Duration("30d") api_report_data = report.GetReportData( stats_api.ApiGetReportArgs( name=report.__class__.__name__, start_time=now - month_duration, duration=month_duration), token=self.token) # pyformat: disable self.assertEqual( api_report_data, rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.PIE_CHART, pie_chart=rdf_report_plugins.ApiPieChartReportData( data=[ rdf_report_plugins.ApiReportDataPoint1D( label="User123", x=11 ), rdf_report_plugins.ApiReportDataPoint1D( label="User456", x=1 ) ] )))
def GetReportData(self, get_report_args, token): """Filter the last week of user actions.""" ret = rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.PIE_CHART) try: timerange_offset = get_report_args.duration timerange_end = get_report_args.start_time + timerange_offset counts = {} try: for event in report_utils.GetAuditLogEntries( timerange_offset, timerange_end, token): counts.setdefault(event.user, 0) counts[event.user] += 1 except ValueError: # Couldn't find any logs.. pass ret.pie_chart.data = sorted( (rdf_report_plugins.ApiReportDataPoint1D(x=count, label=user) for user, count in counts.iteritems() if user not in aff4_users.GRRUser.SYSTEM_USERS), key=lambda series: series.label) except IOError: pass return ret
def GetReportData(self, get_report_args, token): """Extract only the operating system type from the active histogram.""" ret = rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.PIE_CHART) try: fd = aff4.FACTORY.Open( rdfvalue.RDFURN("aff4:/stats/ClientFleetStats").Add( get_report_args.client_label), token=token) for graph in fd.Get( aff4_stats.ClientFleetStats.SchemaCls.RELEASE_HISTOGRAM): # Find the correct graph and merge the OS categories together if "%s day" % self.__class__.ACTIVE_DAYS in graph.title: for sample in graph: ret.pie_chart.data.Append( rdf_report_plugins.ApiReportDataPoint1D( label=sample.label, x=sample.y_value)) break except (IOError, TypeError): pass ret.pie_chart.data = sorted(ret.pie_chart.data, key=lambda point: point.label) return ret
def GetReportData(self, get_report_args, token): """Filter the last week of user actions.""" ret = rdf_report_plugins.ApiReportData( representation_type=RepresentationType.PIE_CHART) counts = self._GetUserCounts(get_report_args, token) for username in aff4_users.GRRUser.SYSTEM_USERS: del counts[username] ret.pie_chart.data = [ rdf_report_plugins.ApiReportDataPoint1D(x=count, label=user) for user, count in sorted(iteritems(counts)) ] return ret
def testMostActiveUsersReportPlugin(self): with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/14")): AddFakeAuditLog(user="******") with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/22")): for _ in range(10): AddFakeAuditLog(user="******") AddFakeAuditLog(user="******") report = report_plugins.GetReportByName( server_report_plugins.MostActiveUsersReportPlugin.__name__) with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/31")): now = rdfvalue.RDFDatetime().Now() month_duration = rdfvalue.Duration.From(30, rdfvalue.DAYS) api_report_data = report.GetReportData( stats_api.ApiGetReportArgs(name=report.__class__.__name__, start_time=now - month_duration, duration=month_duration)) self.assertEqual( api_report_data, rdf_report_plugins.ApiReportData( representation_type=RepresentationType.PIE_CHART, pie_chart=rdf_report_plugins.ApiPieChartReportData(data=[ rdf_report_plugins.ApiReportDataPoint1D( label="User123", x=11), rdf_report_plugins.ApiReportDataPoint1D( label="User456", x=1) ])))
def testOSBreakdownReportPlugin(self): # Add a client to be reported. self.SetupClients(1) # Scan for clients to be reported (the one we just added). cron_system.OSBreakDownCronJob(None, None).Run() report = report_plugins.GetReportByName( client_report_plugins.OSBreakdown30ReportPlugin.__name__) api_report_data = report.GetReportData( stats_api.ApiGetReportArgs(name=report.__class__.__name__, client_label="All")) self.assertEqual( api_report_data, rdf_report_plugins.ApiReportData( pie_chart=rdf_report_plugins.ApiPieChartReportData(data=[ rdf_report_plugins.ApiReportDataPoint1D(label="Linux", x=1) ]), representation_type=RepresentationType.PIE_CHART))
def GetReportData(self, get_report_args, token): """Extract only the operating system type from the active histogram.""" report = rdf_report_plugins.ApiReportData( representation_type=rdf_report_plugins.ApiReportData. RepresentationType.PIE_CHART) graph_series = client_report_utils.FetchMostRecentGraphSeries( get_report_args.client_label, rdf_stats.ClientGraphSeries.ReportType.OS_RELEASE) if graph_series is not None: for graph in graph_series.graphs: # Find the correct graph and merge the OS categories together if "%s day" % self.__class__.ACTIVE_DAYS in graph.title: for sample in graph: report.pie_chart.data.Append( rdf_report_plugins.ApiReportDataPoint1D( label=sample.label, x=sample.y_value)) break report.pie_chart.data = sorted(report.pie_chart.data, key=lambda point: point.label) return report
def testOSReleaseBreakdownReportPlugin(self): # Add a client to be reported. self.SetupClients(1) # Scan for clients to be reported (the one we just added). flow_test_lib.TestFlowHelper( cron_system.OSBreakDown.__name__, token=self.token) report = report_plugins.GetReportByName( client_report_plugins.OSReleaseBreakdown30ReportPlugin.__name__) api_report_data = report.GetReportData( stats_api.ApiGetReportArgs( name=report.__class__.__name__, client_label="All"), token=self.token) self.assertEqual( api_report_data, rdf_report_plugins.ApiReportData( pie_chart=rdf_report_plugins.ApiPieChartReportData(data=[ rdf_report_plugins.ApiReportDataPoint1D(label="Unknown", x=1) ]), representation_type=RepresentationType.PIE_CHART))