def GetReportData(self, get_report_args, token): """Filter the last week of user actions.""" ret = report_plugins.ApiReportData( representation_type=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( (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 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 xrange(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) with test_lib.FakeTime( rdfvalue.RDFDatetime.FromHumanReadable("2012/12/31")): report = server_report_plugins.MostActiveUsersReportPlugin() 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, report_plugins.ApiReportData( representation_type=report_plugins.ApiReportData. RepresentationType.PIE_CHART, pie_chart=report_plugins.ApiPieChartReportData(data=[ report_plugins.ApiReportDataPoint1D(label="User123", x=11), report_plugins.ApiReportDataPoint1D(label="User456", x=1) ])))