Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:allegro/ralph_pricing into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurek committed Jul 11, 2014
2 parents fa4fe54 + 669e54f commit 51f1384
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
15 changes: 2 additions & 13 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
Overview
--------
Ralph Scrooge
-------------

What is Scrooge? The simple answer for this question is: it is a "billing system for corporations". How does it work? It distributes costs of equipment, electricity, network, etc. to clients. For example, if you are COO or the chief accountant at a big company with numerous assets, projects and people, you probably need to know how many resources is used by each team or employee. Maybe you need to charge some people with these costs or just need financial statements to make an important decision. No matter why you need this information, you can obtain it from Scrooge. However, the first thing you need to know is that Scrooge is neither an Excel file nor an advanced calculator – it works on data collected from many devices or supplied by many people. It is a system to calculate and display results with many additional features that make it easier to use.


Scrooge
-------

.. toctree::
:maxdepth: 3

Expand All @@ -20,11 +17,3 @@ Scrooge
costs
api
statements


Indices and tables
------------------

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
1 change: 1 addition & 0 deletions src/ralph_pricing/plugins/collects/ceilometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def set_usage(usage_symbol, venture, value, date):
symbol=usage_symbol,
defaults=dict(
name=usage_symbol,
show_in_ventures_report=False,
),
)
usage, created = DailyUsage.objects.get_or_create(
Expand Down
47 changes: 27 additions & 20 deletions src/ralph_pricing/plugins/reports/ceilometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,42 @@

import logging

from collections import OrderedDict
from collections import defaultdict, OrderedDict
from decimal import Decimal as D

from django.db.models import Sum

from ralph_pricing.plugins.base import register
from ralph_pricing.plugins.reports.service import ServiceBasePlugin
from ralph_pricing.models import DailyUsage, UsagePrice

logger = logging.getLogger(__name__)


class CeilometerBasePlugin(ServiceBasePlugin):
def costs(self, service, start, end, ventures, forecast=False, **kwargs):
logger.debug("Calculating report for service {0}".format(service))
coin_ventures = defaultdict(D)
usage_types = service.usage_types.all()
usages = DailyUsage.objects.filter(
type__in=usage_types,
date__gte=start,
date__lte=end,
)
coin_ventures = {}
for usage in usages:
try:
price = usage.type.usageprice_set.get(
start__lte=usage.date,
end__gte=usage.date,
).price
except UsagePrice.DoesNotExist:
price = 0
venture = usage.pricing_venture.id
val = float(usage.value) * float(price)
coin_ventures[venture] = coin_ventures.get(venture, 0) + val

for usage_type in usage_types:
for usage_price in usage_type.usageprice_set.filter(
start__lte=end,
end__gte=start
):
price = D(usage_price.price)
up_start = max(start, usage_price.start)
up_end = min(end, usage_price.end)

ventures_usages = usage_type.dailyusage_set.filter(
date__gte=up_start,
date__lte=up_end,
).values('pricing_venture').annotate(value=Sum('value'))

for usage in ventures_usages:
coin_ventures[usage['pricing_venture']] += (
D(usage['value']) * price
)

res = dict([(v[0], {
'ceilometer_cost': v[1],
}) for v in coin_ventures.iteritems()])
Expand All @@ -44,7 +51,7 @@ def costs(self, service, start, end, ventures, forecast=False, **kwargs):
def schema(self, service, *args, **kwargs):
schema = OrderedDict([
('ceilometer_cost', {
'name': "Cloud 2.0 cost",
'name': "{} cost".format(service.name),
'currency': True,
'total_cost': True,
}),
Expand Down

0 comments on commit 51f1384

Please sign in to comment.