def FetchAllGraphSeries(label, report_type, period=None, token=None): """Fetches graph series for the given label and report-type from the DB. Args: label: Client label to fetch data for. report_type: rdf_stats.ClientGraphSeries.ReportType to fetch data for. period: rdfvalue.Duration specifying how far back in time to fetch data. If not provided, all data for the given label and report-type will be returned. token: ACL token to use for reading from the legacy (non-relational) datastore. Raises: AFF4AttributeTypeError: If, when reading to the legacy DB, an unexpected report-data type is encountered. Returns: A dict mapping timestamps to graph-series. The timestamps represent when the graph-series were written to the datastore. """ if _ShouldUseLegacyDatastore(): return _FetchAllGraphSeriesFromTheLegacyDB(label, report_type, period=period, token=token) if period is None: time_range = None else: range_end = rdfvalue.RDFDatetime.Now() time_range = time_utils.TimeRange(range_end - period, range_end) return data_store.REL_DB.ReadAllClientGraphSeries(label, report_type, time_range=time_range)
def FetchAllGraphSeries( label: Text, report_type: rdf_structs.EnumNamedValue, period: Optional[rdfvalue.Duration] = None, ) -> Dict[rdfvalue.RDFDatetime, rdf_stats.ClientGraphSeries]: """Fetches graph series for the given label and report-type from the DB. Args: label: Client label to fetch data for. report_type: rdf_stats.ClientGraphSeries.ReportType to fetch data for. period: rdfvalue.Duration specifying how far back in time to fetch data. If not provided, all data for the given label and report-type will be returned. Returns: A dict mapping timestamps to graph-series. The timestamps represent when the graph-series were written to the datastore. """ if period is None: time_range = None else: range_end = rdfvalue.RDFDatetime.Now() time_range = time_utils.TimeRange(range_end - period, range_end) return data_store.REL_DB.ReadAllClientGraphSeries(label, report_type, time_range=time_range)
def testReadAllClientGraphSeries_InTimeRange(self): graph_series_list = _CreateGRRVersionGraphSeries(10) for i, graph_series in enumerate(graph_series_list): with test_lib.FakeTime( rdfvalue.RDFDatetime.FromSecondsSinceEpoch(i)): self.db.WriteClientGraphSeries(graph_series, _TEST_LABEL) time_range = time_utils.TimeRange( rdfvalue.RDFDatetime.FromSecondsSinceEpoch(6), rdfvalue.RDFDatetime.FromSecondsSinceEpoch(10)) fetched_data = self.db.ReadAllClientGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION, time_range=time_range) 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.assertDictEqual(fetched_data, expected_data)
def testReadAllClientGraphSeries_InTimeRange(self): date = rdfvalue.RDFDatetime.FromHumanReadable("2017-10-02") graph_series_list = _CreateGRRVersionGraphSeries(10) for i, graph_series in enumerate(graph_series_list): with test_lib.FakeTime(date + rdfvalue.Duration.From(i, rdfvalue.DAYS)): self.db.WriteClientGraphSeries(graph_series, _TEST_LABEL) time_range = time_utils.TimeRange( date + rdfvalue.Duration.From(6, rdfvalue.DAYS), date + rdfvalue.Duration.From(10, rdfvalue.DAYS)) fetched_data = self.db.ReadAllClientGraphSeries( _TEST_LABEL, rdf_stats.ClientGraphSeries.ReportType.GRR_VERSION, time_range=time_range) expected_data = { date + rdfvalue.Duration.From(6, rdfvalue.DAYS): graph_series_list[6], date + rdfvalue.Duration.From(7, rdfvalue.DAYS): graph_series_list[7], date + rdfvalue.Duration.From(8, rdfvalue.DAYS): graph_series_list[8], date + rdfvalue.Duration.From(9, rdfvalue.DAYS): graph_series_list[9], } self.assertDictEqual(fetched_data, expected_data)
def testIncludesTimeRange(self): time_range = time_utils.TimeRange(rdfvalue.RDFDatetime(1000), rdfvalue.RDFDatetime(2000)) self.assertFalse(time_range.Includes(rdfvalue.RDFDatetime(500))) self.assertTrue(time_range.Includes(rdfvalue.RDFDatetime(1000))) self.assertTrue(time_range.Includes(rdfvalue.RDFDatetime(1500))) self.assertTrue(time_range.Includes(rdfvalue.RDFDatetime(2000))) self.assertFalse(time_range.Includes(rdfvalue.RDFDatetime(2500)))
def testInvalidTimeRange(self): with self.assertRaisesWithLiteralMatch( ValueError, "Invalid time-range: 2000 > 1000."): time_utils.TimeRange(rdfvalue.RDFDatetime(2000), rdfvalue.RDFDatetime(1000))