GitHub PullRequestReviewCommentEvent at 2014-01-31 10:35:00
Repository: ralph_pricing
Actor: kula1922
Payload: {"comment":{"url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments/9338639","id":9338639,"diff_hunk":"@@ -0,0 +1,526 @@\n+# -*- coding: utf-8 -*-\n+\n+from __future__ import absolute_import\n+from __future__ import division\n+from __future__ import print_function\n+from __future__ import unicode_literals\n+\n+import datetime\n+import logging\n+from collections import defaultdict\n+from decimal import Decimal as D\n+\n+from django.utils.translation import ugettext_lazy as _\n+from django.db.models import Sum, Count\n+\n+from ralph_pricing.views.reports import Report, currency\n+from ralph_pricing.models import (\n+    DailyDevice,\n+    DailyUsage,\n+    ExtraCost,\n+    ExtraCostType,\n+    UsageType,\n+    Venture,\n+)\n+from ralph_pricing.forms import DateRangeForm\n+\n+logger = logging.getLogger(__name__)\n+\n+\n+class AllVenturesBeta(Report):\n+    \"\"\"\n+        Reports for all ventures\n+    \"\"\"\n+    template_name = 'ralph_pricing/ventures_all.html'\n+    Form = DateRangeForm\n+    section = 'all-ventures'\n+    report_name = _('All Ventures Report Beta')\n+\n+    @staticmethod\n+    def _get_visible_usage_types():\n+        \"\"\"\n+        Returns usage types which should be visible on report\n+        \"\"\"\n+        return UsageType.objects.exclude(show_in_report=False).order_by('name')\n+\n+    @staticmethod\n+    def _get_assets_costs(start, end, only_active=False):\n+        \"\"\"\n+        Return mini-report for assets costs per venture.\n+\n+        :param datetime start: Start date of the report\n+        :param datetime end: End date of the report\n+        :returns list: list of dicts (example below)\n+        :rtype list:\n+\n+        Example result:\n+        [\n+            {\n+                'pricing_venture': 1,\n+                'assets_price': 1234,  # total price of assets in venture\n+                'assets_cost': 111,  # sum of daily costs (deprecation)\n+                'assets_count': 11.11,  # average count of assets\n+            },\n+            ...\n+        ]\n+        \"\"\"\n+        assets_report_query = DailyDevice.objects.filter(\n+            pricing_device__is_virtual=False,\n+            date__gte=start,\n+            date__lte=end,\n+        )\n+\n+        if only_active:\n+            assets_report_query = assets_report_query.filter(\n+                pricing_venture__is_active=True,\n+            )\n+\n+        # get sum of price, daily_cost and count of id and group by\n+        # pricing_venture\n+        assets_report = assets_report_query.values('pricing_venture')\\\n+            .annotate(assets_price=Sum('price'))\\\n+            .annotate(assets_cost=Sum('daily_cost'))\\\n+            .annotate(assets_count=Count('id'))\n+\n+        # TODO: calc blades\n+        return assets_report\n+\n+    @staticmethod\n+    def _get_usage_type_costs_in_period(\n+        start,\n+        end,\n+        usage_type,\n+        usage_price,\n+        only_active=False,\n+        warehouse=None,\n+        forecast=False,\n+    ):\n+        \"\"\"\n+        Calculate cost of usages for period in which usage_price is defined\n+\n+        Returns list of dicts with usage cost per venture.\n+\n+        :param datetime start: Start date of the report\n+        :param datetime end: End date of the report\n+        :param UsageType usage_type: usage type to calculate cost of usages\n+        :param UsagePrice usage_price: usage price to calculate cost of usages\n+        :param bool only_active: calculate only for active ventures\n+        :param Warehouse warehouse: calculate for specified warehouse\n+        :param bool forecast: use forecast costs/prices\n+        :returns list: mini-report for specified usage price costs\n+        :rtype list:\n+\n+        Example result:\n+        [\n+            {\n+                'pricing_venture': 1,\n+                'total_usage': 1234,  # total usage in usage_price period\n+                'total_cost': 1111,  # total cost of usages in this period\n+            },\n+            ...\n+        ]\n+        \"\"\"\n+        up_start = max(start, usage_price.start)\n+        up_end = min(end, usage_price.end)\n+        days = (up_end - up_start).days + 1\n+\n+        price = usage_price.forecast_price if forecast else \\\n+            usage_price.price\n+\n+        # if usage type has cost - price has to be calculated from cost\n+        # and total usage\n+        if usage_type.by_cost:\n+            cost = usage_price.forecast_cost if forecast else \\\n+                usage_price.cost\n+\n+            # get total usage in usage_price period\n+            total_usage = DailyUsage.objects.filter(\n+                date__gte=usage_price.start,\n+                date__lte=usage_price.end,\n+                type=usage_type.id,\n+            ).aggregate(total=Sum('value')).get('total_usage', 0)\n+\n+            # calculate price for single unit usage\n+            if total_usage == 0:\n+                price = 0\n+            else:\n+                price = D(cost / total_usage / days)\n+\n+        # query for usages in given period grouped by venture\n+        up_report_query = DailyUsage.objects.filter(\n+            date__gte=up_start,\n+            date__lte=up_end,\n+            type=usage_type,\n+        )\n+        if usage_type.by_warehouse:\n+            up_report_query = up_report_query.filter(\n+                warehouse_id=warehouse.id,\n+            )\n+        if only_active:\n+            up_report_query = up_report_query.filter(\n+                pricing_venture__is_active=True,\n+            )\n+        up_report = up_report_query.values('pricing_venture').annotate(\n+            total_usage=Sum('value'),\n+        )\n+\n+        for up in up_report:\n+            up['total_cost'] = D(up['total_usage']) * price\n+\n+        return up_report\n+\n+    @staticmethod\n+    def _get_usage_type_costs(\n+        start,\n+        end,\n+        usage_type,\n+        only_active=False,\n+        warehouse=None,\n+        forecast=False,\n+    ):\n+        \"\"\"\n+        Calculate cost of usages in report date range\n+\n+        Returns list of dicts with usage cost per venture.\n+\n+        :param datetime start: Start date of the report\n+        :param datetime end: End date of the report\n+        :param UsageType usage_type: usage type to calculate cost of usages\n+        :param bool only_active: calculate only for active ventures\n+        :param Warehouse warehouse: calculate for specified warehouse\n+        :param bool forecast: use forecast costs/prices\n+        :returns dict: mini-report for specified usage type costs\n+        :rtype dict:\n+\n+        Example result:\n+        {\n+            # venture with id 123\n+            123: {\n+                'usage_1_count': 1234,  # total usage of usage type with id=1\n+                'usage_1_cost': 1111,  # total cost of usages\n+            },\n+            ...\n+        }\n+        \"\"\"\n+        report_days = (end - start).days + 1\n+        usage_type_count = 'usage_{0}_count'.format(usage_type.id)","path":"src/ralph_pricing/views/ventures_beta.py","position":206,"original_position":206,"commit_id":"5d326152033aaeb7f5ddcdcd52d67288a1af43c4","original_commit_id":"5d326152033aaeb7f5ddcdcd52d67288a1af43c4","user":{"login":"kula1922","id":2130852,"avatar_url":"https://gravatar.com/avatar/4e9db77cc232a4b6fcd7b73308e00a38?d=https%3A%2F%2Fidenticons.github.com%2F0fd21bd03659b93f45796c70dc7988e4.png&r=x","gravatar_id":"4e9db77cc232a4b6fcd7b73308e00a38","url":"https://api.github.com/users/kula1922","html_url":"https://github.com/kula1922","followers_url":"https://api.github.com/users/kula1922/followers","following_url":"https://api.github.com/users/kula1922/following{/other_user}","gists_url":"https://api.github.com/users/kula1922/gists{/gist_id}","starred_url":"https://api.github.com/users/kula1922/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kula1922/subscriptions","organizations_url":"https://api.github.com/users/kula1922/orgs","repos_url":"https://api.github.com/users/kula1922/repos","events_url":"https://api.github.com/users/kula1922/events{/privacy}","received_events_url":"https://api.github.com/users/kula1922/received_events","type":"User","site_admin":false},"body":"usage_{0}_count is displayed name?","created_at":"2014-01-31T10:35:00Z","updated_at":"2014-01-31T10:35:00Z","html_url":"https://github.com/allegro/ralph_pricing/pull/95#discussion_r9338639","pull_request_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/95","_links":{"self":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments/9338639"},"html":{"href":"https://github.com/allegro/ralph_pricing/pull/95#discussion_r9338639"},"pull_request":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/95"}}}}