Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services plugin adjusted to services #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/ralph_scrooge/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django import forms
from django.contrib import admin
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.utils.translation import ugettext_lazy as _
from lck.django.common.admin import ModelAdmin
from simple_history.admin import SimpleHistoryAdmin

Expand Down Expand Up @@ -162,6 +163,36 @@ class ServiceAdmin(SimpleHistoryAdmin):
# =============================================================================
# PRICING SERVICE
# =============================================================================
class PricingServiceForm(forms.ModelForm):
class Meta:
model = models.PricingService

services = forms.ModelMultipleChoiceField(
help_text=_('Services used to calculate costs of Pricing Service'),
queryset=models.Service.objects.all(),
required=False,
widget=FilteredSelectMultiple('Services', False),

)

def __init__(self, *args, **kwargs):
super(PricingServiceForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['services'].initial = (
self.instance.services.exclude(pricing_service=None)
)

def save(self, commit=True):
# NOTE: Previously assigned Services and their pricing services are
# silently reset
instance = super(PricingServiceForm, self).save(commit=False)
self.fields['services'].initial.update(pricing_service=None)
instance.save()
if self.cleaned_data['services']:
self.cleaned_data['services'].update(pricing_service=instance)
return instance


class ServiceUsageTypesInline(admin.TabularInline):
model = models.ServiceUsageTypes

Expand All @@ -170,6 +201,7 @@ class ServiceUsageTypesInline(admin.TabularInline):
class PricingServiceAdmin(ModelAdmin):
list_display = ('name',)
search_fields = ('name',)
form = PricingServiceForm
inlines = [ServiceUsageTypesInline]


Expand Down

Large diffs are not rendered by default.

43 changes: 35 additions & 8 deletions src/ralph_scrooge/models/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
IntervalHistoricalRecords,
ModelDiffMixin,
)
from ralph_scrooge.models.usage import DailyUsage


class BusinessLine(Named):
Expand Down Expand Up @@ -74,6 +75,12 @@ class Service(ModelDiffMixin, EditorTrackable, TimeTrackable):
through='ServiceEnvironment',
related_name='services',
)
pricing_service = db.ForeignKey(
'PricingService',
related_name='services',
null=True,
blank=True,
)
history = IntervalHistoricalRecords()

class Meta:
Expand All @@ -94,14 +101,6 @@ class PricingService(Named):
verbose_name=_("Use universal plugin"),
default=True,
)
services = db.ManyToManyField(
'Service',
verbose_name=_("Services"),
related_name='pricing_services',
help_text=_('Services used to calculate costs of Pricing Service'),
blank=False,
null=False,
)
usage_types = db.ManyToManyField(
'UsageType',
through='ServiceUsageTypes',
Expand Down Expand Up @@ -142,11 +141,39 @@ class Meta:
verbose_name_plural = _("pricing services")
app_label = 'ralph_scrooge'

@property
def service_environments(self):
"""
Returns all services environments related with this pricing service.
"""
return ServiceEnvironment.objects.filter(
service__in=self.services.all()
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what this method do? return all environments?

ps. docstrings


def get_plugin_name(self):
"""
Returns plugin name for pricing service.
"""
if self.use_universal_plugin:
return 'pricing_service_plugin'
return self.symbol or self.name.lower().replace(' ', '_')

def get_dependent_services(self, start, end):
"""
Returns pricing services, which resources (usage types) are used by
this service (between start and end dates).
"""
return PricingService.objects.filter(
serviceusagetypes__id__in=DailyUsage.objects.filter(
type__type='SU',
service_environment__in=ServiceEnvironment.objects.filter(
service__in=self.services.all(),
),
date__gte=start,
date__lte=end
).values_list('type', flat=True).distinct()
).distinct()


class ServiceUsageTypes(db.Model):
usage_type = db.ForeignKey(
Expand Down
2 changes: 1 addition & 1 deletion src/ralph_scrooge/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import abc
import re

from lck.cache import memoize
from ralph_scrooge.utils import memoize

from ralph.util import plugin
from ralph_scrooge.models import Warehouse
Expand Down
2 changes: 1 addition & 1 deletion src/ralph_scrooge/plugins/collect/ceilometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time

from django.conf import settings
from lck.cache import memoize
from ralph_scrooge.utils import memoize
from sqlalchemy import create_engine

from ralph.util import plugin
Expand Down
Loading