def calculate_costs(self, start, end, forecast=False):
     """
     Calculate costs between start and end (without forcing - when costs
     were calculated for single day, they will be not calculated again
     unless forcing it).
     """
     colletor = Collector()
     for day, status in colletor.process_period(start, end, forecast):
         pass
 def _calculate_missing_dates(self, start, end, forecast, plugins):
     """
     Calculate costs for dates on which costs were not previously calculated
     """
     collector = Collector()
     dates_to_calculate = collector._get_dates(start, end, forecast, False)
     plugins = [pl for pl in collector.get_plugins() if pl.name in plugins]
     for day in dates_to_calculate:
         collector.process(day, forecast, plugins)
 def calculate_costs(self, start, end, forecast=False):
     """
     Calculate costs between start and end (without forcing - when costs
     were calculated for single day, they will be not calculated again
     unless forcing it).
     """
     colletor = Collector()
     for day, status in colletor.process_period(start, end, forecast):
         pass
    def setUp(self):
        self.today = date(2013, 10, 11)
        self.checkpoint = date(2013, 10, 15)
        self.start = date(2013, 10, 1)
        self.end = date(2013, 10, 30)
        self.service_environments = ServiceEnvironmentFactory.create_batch(2)
        self.collector = Collector()

        self.dates1 = self._dates_between(self.start, self.checkpoint)
        self.dates2 = self._dates_between(self.checkpoint + timedelta(days=1),
                                          self.end)
        self.dates = self._dates_between(self.start, self.end)
    def _process_daily_result(self, data, date, forecast):
        """
        Process results from subtask jobs (single day) - create daily costs
        instances.

        :param data: costs per service environments
        :type data: dict of lists (key: service environment id, value: list of
            costs of service environment)
        :param date: date for which process daily results
        :type date: datetime.date
        :param forecast: True, if forecast costs
        :type forecast: bool
        """
        collector = Collector()
        return collector._create_daily_costs(date, data, forecast)
    def _save_costs(self, data, start, end, forecast):
        """
        Save costs between start and end.

        :param data: list of DailyCost instances
        :type data: list
        :param start: start date
        :type start: datetime.date
        :param end: end date
        :type end: datetime.date
        :param forecast: True, if forecast costs
        :type forecast: bool
        """
        collector = Collector()
        collector.save_period_costs(start, end, forecast, data)
    def _process_daily_result(self, data, date, forecast):
        """
        Process results from subtask jobs (single day) - create daily costs
        instances.

        :param data: costs per service environments
        :type data: dict of lists (key: service environment id, value: list of
            costs of service environment)
        :param date: date for which process daily results
        :type date: datetime.date
        :param forecast: True, if forecast costs
        :type forecast: bool
        """
        collector = Collector()
        return collector._create_daily_costs(date, data, forecast)
    def _save_costs(self, data, start, end, forecast):
        """
        Save costs between start and end.

        :param data: list of DailyCost instances
        :type data: list
        :param start: start date
        :type start: datetime.date
        :param end: end date
        :type end: datetime.date
        :param forecast: True, if forecast costs
        :type forecast: bool
        """
        collector = Collector()
        collector.save_period_costs(start, end, forecast, data)
 def run(cls, day, forecast):
     """
     Run collecting costs for one day.
     """
     collector = Collector()
     result = {}
     try:
         result = collector.process(day, forecast)
         success = True
     except Exception as e:
         logger.exception(e)
         success = False
     job = get_current_job()
     # save result to job meta to keep log clean
     job.meta['collector_result'] = result
     job.save()
     yield 100, success
Exemple #10
0
 def run(cls, day, forecast):
     """
     Run collecting costs for one day.
     """
     collector = Collector()
     result = {}
     try:
         result = collector.process(day, forecast)
         success = True
     except Exception as e:
         logger.exception(e)
         success = False
     job = get_current_job()
     # save result to job meta to keep log clean
     job.meta['collector_result'] = result
     job.save()
     yield 100, success
    def setUp(self):
        self.today = date(2013, 10, 11)
        self.checkpoint = date(2013, 10, 15)
        self.start = date(2013, 10, 1)
        self.end = date(2013, 10, 30)
        self.service_environments = ServiceEnvironmentFactory.create_batch(2)
        self.collector = Collector()

        self.dates1 = self._dates_between(self.start, self.checkpoint)
        self.dates2 = self._dates_between(
            self.checkpoint + timedelta(days=1),
            self.end
        )
        self.dates = self._dates_between(self.start, self.end)
Exemple #12
0
 def _calculate_missing_dates(self, start, end, forecast, plugins):
     """
     Calculate costs for dates on which costs were not previously calculated
     """
     collector = Collector()
     dates_to_calculate = collector._get_dates(start, end, forecast, False)
     plugins = [pl for pl in collector.get_plugins() if pl.name in plugins]
     for day in dates_to_calculate:
         collector.process(day, forecast, plugins)
Exemple #13
0
    def generate_data(self, data):
        Warehouse.objects.all().update(show_in_report=True)
        for day in days:
            call_command('scrooge_sync', today=day.strftime('%Y-%m-%d'))
        collector = Collector()
        forecast = False
        for day in days:
            costs = collector.process(day, forecast)
            processed = collector._create_daily_costs(day, costs, forecast)
            collector.save_period_costs(day, day, forecast, processed)

        CostDateStatus.objects.all().update(accepted=True)
class TestCollector(TestCase):
    def setUp(self):
        self.today = date(2013, 10, 11)
        self.checkpoint = date(2013, 10, 15)
        self.start = date(2013, 10, 1)
        self.end = date(2013, 10, 30)
        self.service_environments = ServiceEnvironmentFactory.create_batch(2)
        self.collector = Collector()

        self.dates1 = self._dates_between(self.start, self.checkpoint)
        self.dates2 = self._dates_between(
            self.checkpoint + timedelta(days=1),
            self.end
        )
        self.dates = self._dates_between(self.start, self.end)

    def _dates_between(self, start, end):
        return [d.date() for d in rrule.rrule(
            rrule.DAILY,
            dtstart=start,
            until=end
        )]

    def test_get_dates_force_recalculation(self):
        dates = self.collector._get_dates(self.start, self.end, True, True)
        self.assertEquals(dates, self.dates)

    def test_get_dates_forecast(self):
        for days, calculated in [(self.dates1, True), (self.dates2, False)]:
            for day in days:
                CostDateStatusFactory(date=day, forecast_calculated=calculated)
        dates = self.collector._get_dates(self.start, self.end, True, False)
        self.assertEquals(dates, self.dates2)

    def test_get_dates(self):
        for days, calculated in [(self.dates1, True), (self.dates2, False)]:
            for day in days:
                CostDateStatusFactory(date=day, calculated=calculated)
        dates = self.collector._get_dates(self.start, self.end, False, False)
        self.assertEquals(dates, self.dates2)

    def test_get_services_environments(self):
        se = self.collector._get_services_environments()
        self.assertEquals(list(se), [
            self.service_environments[0],
            self.service_environments[1],
        ])

    @mock.patch('ralph_scrooge.plugins.cost.collector.Collector.process')
    @mock.patch('ralph_scrooge.plugins.cost.collector.Collector._get_dates')
    @mock.patch('ralph_scrooge.plugins.cost.collector.Collector._get_services_environments')  # noqa
    def test_process_period(self, get_se_mock, get_dates_mock, process_mock):
        get_dates_mock.return_value = self.dates1
        process_mock.return_value = None
        get_se_mock.return_value = self.service_environments
        for day, success in self.collector.process_period(
            self.start,
            self.end,
            True,
            False,
            a=1,
            service_environments=self.service_environments,
        ):
            pass
        calls = []
        for day in self.dates1:
            calls.append(mock.call(
                day,
                service_environments=self.service_environments,
                forecast=True,
                a=1,
            ))
        process_mock.assert_has_calls(calls)
class TestCollector(TestCase):
    def setUp(self):
        self.today = date(2013, 10, 11)
        self.checkpoint = date(2013, 10, 15)
        self.start = date(2013, 10, 1)
        self.end = date(2013, 10, 30)
        self.service_environments = ServiceEnvironmentFactory.create_batch(2)
        self.collector = Collector()

        self.dates1 = self._dates_between(self.start, self.checkpoint)
        self.dates2 = self._dates_between(self.checkpoint + timedelta(days=1),
                                          self.end)
        self.dates = self._dates_between(self.start, self.end)

    def _dates_between(self, start, end):
        return [
            d.date()
            for d in rrule.rrule(rrule.DAILY, dtstart=start, until=end)
        ]

    def test_get_dates_force_recalculation(self):
        dates = self.collector._get_dates(self.start, self.end, True, True)
        self.assertEquals(dates, self.dates)

    def test_get_dates_forecast(self):
        for days, calculated in [(self.dates1, True), (self.dates2, False)]:
            for day in days:
                CostDateStatusFactory(date=day, forecast_calculated=calculated)
        dates = self.collector._get_dates(self.start, self.end, True, False)
        self.assertEquals(dates, self.dates2)

    def test_get_dates(self):
        for days, calculated in [(self.dates1, True), (self.dates2, False)]:
            for day in days:
                CostDateStatusFactory(date=day, calculated=calculated)
        dates = self.collector._get_dates(self.start, self.end, False, False)
        self.assertEquals(dates, self.dates2)

    def test_get_services_environments(self):
        se = self.collector._get_services_environments()
        self.assertEquals(list(se), [
            self.service_environments[0],
            self.service_environments[1],
        ])

    @mock.patch('ralph_scrooge.plugins.cost.collector.Collector.process')
    @mock.patch('ralph_scrooge.plugins.cost.collector.Collector._get_dates')
    @mock.patch(
        'ralph_scrooge.plugins.cost.collector.Collector._get_services_environments'
    )  # noqa
    def test_process_period(self, get_se_mock, get_dates_mock, process_mock):
        get_dates_mock.return_value = self.dates1
        process_mock.return_value = None
        get_se_mock.return_value = self.service_environments
        for day, success in self.collector.process_period(
                self.start,
                self.end,
                True,
                False,
                a=1,
                service_environments=self.service_environments,
        ):
            pass
        calls = []
        for day in self.dates1:
            calls.append(
                mock.call(
                    day,
                    service_environments=self.service_environments,
                    forecast=True,
                    a=1,
                ))
        process_mock.assert_has_calls(calls)