Skip to content

Commit

Permalink
Assign business line directly to service (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurek committed Sep 25, 2018
1 parent bf6489b commit 1e0b14c
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 81 deletions.
3 changes: 2 additions & 1 deletion src/ralph_scrooge/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ class Meta:
@register(models.Service)
class ServiceAdmin(UpdateReadonlyMixin, SimpleHistoryAdmin):
form = ServiceAdminForm
list_display = ('name', 'ci_uid')
list_display = ('name', 'ci_uid', 'profit_center', 'business_line')
list_select_related = ('profit_center', 'business_line')
search_fields = ('name', 'ci_uid')
inlines = [ServiceOwnershipInline, ServiceEnvironmentsInline]
readonly_when_update = ('ci_id', 'ci_uid')
Expand Down
1 change: 0 additions & 1 deletion src/ralph_scrooge/fixtures/initial_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
name: '-'
ci_id: -1
ci_uid: -1
business_line: 1

# Base Usages
- model: ralph_scrooge.BaseUsage
Expand Down
30 changes: 30 additions & 0 deletions src/ralph_scrooge/migrations/0017_auto_20180801_1003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-08-01 10:03
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('ralph_scrooge', '0016_backofficeassetinfo_dailybackofficeassetinfo'),
]

operations = [
migrations.AddField(
model_name='historicalservice',
name='business_line',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='ralph_scrooge.BusinessLine', verbose_name='business line'),
),
migrations.AddField(
model_name='service',
name='business_line',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, related_name='services', to='ralph_scrooge.BusinessLine', verbose_name='business line'),
),
migrations.RemoveField(
model_name='profitcenter',
name='business_line',
),
]
17 changes: 9 additions & 8 deletions src/ralph_scrooge/models/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ class ProfitCenter(Named.NonUnique):
max_length=100,
verbose_name=_("uid from cmdb"),
)
business_line = db.ForeignKey(
BusinessLine,
null=False,
blank=False,
default=1,
related_name='profit_centers',
verbose_name=_('business line'),
)
description = db.TextField(null=True, default=None)

class Meta:
Expand Down Expand Up @@ -126,6 +118,15 @@ class Service(ModelDiffMixin, EditorTrackable, TimeTrackable):
related_query_name='services',
verbose_name=_('profit center'),
)
business_line = db.ForeignKey(
BusinessLine,
null=False,
blank=False,
default=1,
related_name='services',
verbose_name=_('business line'),
on_delete=db.PROTECT,
)
ownership = db.ManyToManyField(
'ScroogeUser',
through='ServiceOwnership',
Expand Down
18 changes: 4 additions & 14 deletions src/ralph_scrooge/plugins/collect/ralph3_profit_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

from django.db import transaction

# TODO(xor-xor): BusinessLine should be renamed to BusinessSegment (this also
# applies to other occurrences of this name in this plugin).
from ralph_scrooge.models import BusinessLine, ProfitCenter
from ralph_scrooge.models import ProfitCenter
from ralph_scrooge.plugins import plugin_runner
from ralph_scrooge.plugins.collect.utils import get_from_ralph

Expand All @@ -20,13 +18,7 @@


@transaction.atomic
def update_profit_center(pc, default_business_line):
if pc['business_segment'] is not None:
business_line = BusinessLine.objects.get(
ralph3_id=pc['business_segment']['id']
)
else:
business_line = default_business_line
def update_profit_center(pc):
profit_center, created = ProfitCenter.objects.get_or_create(
ralph3_id=pc['id'],
defaults=dict(
Expand All @@ -35,17 +27,15 @@ def update_profit_center(pc, default_business_line):
)
profit_center.name = pc['name']
profit_center.description = pc['description']
profit_center.business_line = business_line
profit_center.save()
return created


@plugin_runner.register(chain='scrooge', requires=['ralph3_business_segment'])
@plugin_runner.register(chain='scrooge')
def ralph3_profit_center(**kwargs):
new_pc = total = 0
default_business_line = BusinessLine.objects.get(pk=1)
for pc in get_from_ralph("profit-centers", logger):
created = update_profit_center(pc, default_business_line)
created = update_profit_center(pc)
if created:
new_pc += 1
total += 1
Expand Down
21 changes: 18 additions & 3 deletions src/ralph_scrooge/plugins/collect/ralph3_service_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ScroogeUser,
ServiceEnvironment,
ServiceOwnership,
)
BusinessLine)
from ralph_scrooge.plugins import plugin_runner
from ralph_scrooge.plugins.collect.utils import get_from_ralph

Expand All @@ -26,7 +26,8 @@

@transaction.atomic
def update_service(
service_from_ralph, default_profit_center, profit_centers, all_services
service_from_ralph, default_profit_center, profit_centers,
default_business_line, business_lines, all_services
):
created = False
try:
Expand All @@ -37,12 +38,21 @@ def update_service(
service.ralph3_id = service_from_ralph['id']
service.name = service_from_ralph['name']
service.symbol = service_from_ralph['uid']

if service_from_ralph['profit_center'] is not None:
service.profit_center = (
profit_centers[service_from_ralph['profit_center']['id']]
)
else:
service.profit_center = default_profit_center

if service_from_ralph.get('business_segment') is not None:
service.business_line = (
business_lines[service_from_ralph['business_segment']['id']]
)
else:
service.business_line = default_business_line

service.save()
_update_owners(service, service_from_ralph)
return created, service
Expand Down Expand Up @@ -126,6 +136,10 @@ def ralph3_service_environment(**kwargs):
profit_centers = {
pc.ralph3_id: pc for pc in ProfitCenter.objects.all()
}
default_business_line = BusinessLine.objects.get(pk=1) # from fixtures
business_lines = {
bl.ralph3_id: bl for bl in BusinessLine.objects.all()
}

# envs
envs = {}
Expand All @@ -150,7 +164,8 @@ def ralph3_service_environment(**kwargs):
):

created, service_obj = update_service(
service, default_profit_center, profit_centers, all_services
service, default_profit_center, profit_centers,
default_business_line, business_lines, all_services
)
if created:
new_services += 1
Expand Down
10 changes: 8 additions & 2 deletions src/ralph_scrooge/plugins/report/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ def costs(self, start, end, service_environments=None, *args, **kwargs):
'service',
flat=True
).distinct(),
).select_related('profit_center').order_by('history_id')
).select_related(
'profit_center', 'business_line'
).order_by('history_id')
services = {}
profit_centers = defaultdict(list)
business_lines = defaultdict(list)
for service_history in services_history:
services[service_history.id] = service_history.history_object
profit_centers[service_history.id].append(
service_history.profit_center
)
business_lines[service_history.id].append(
service_history.business_line
)
for service_environment in service_environments:
info[service_environment.id] = {
'id': service_environment.id,
Expand All @@ -86,7 +92,7 @@ def costs(self, start, end, service_environments=None, *args, **kwargs):
])
]),
'business_line': ' / '.join(set([
pc.business_line.name for pc in profit_centers[
bl.name for bl in business_lines[
service_environment.service.id
]
])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@
'id': 1,
'name': 'Profit Center #1',
'description': 'Profit Center #1 description',
'business_segment': {
'id': 11,
'name': "some business segment",
},
},
{
'id': 2,
'name': 'Profit Center #2',
'description': 'Profit Center #2 description',
'business_segment': {
'id': 22,
'name': "some other business segment",
},
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'uid': 's-1',
'name': 'Service #1',
'profit_center': {'id': 2},
'business_segment': {'id': 2},
'business_owners': [
{'id': 1, 'username': 'james_smith'},
{'id': 2, 'username': 'michael_wilson'},
Expand All @@ -19,6 +20,7 @@
'uid': 's-2',
'name': 'Service #2',
'profit_center': {'id': 3},
'business_segment': {'id': 3},
'business_owners': [
{'id': 1, 'username': 'james_smith'},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import mock

from ralph_scrooge.models import BusinessLine, ProfitCenter
from ralph_scrooge.models import ProfitCenter
from ralph_scrooge.plugins.collect.ralph3_profit_center import (
ralph3_profit_center as profit_center_plugin,
update_profit_center
Expand All @@ -16,55 +16,32 @@
from ralph_scrooge.tests.plugins.collect.samples.ralph3_profit_center import (
SAMPLE_PROFIT_CENTERS,
)
from ralph_scrooge.tests.utils.factory import BusinessLineFactory


class TestProfitCenterPlugin(ScroogeTestCase):
def setUp(self):
self.default_business_line = BusinessLine.objects.get(pk=1)
self.business_line1 = BusinessLineFactory(
ralph3_id=SAMPLE_PROFIT_CENTERS[0]['business_segment']['id']
)
self.business_line2 = BusinessLineFactory(
ralph3_id=SAMPLE_PROFIT_CENTERS[1]['business_segment']['id']
)

def _compare_profit_center(self, profit_center, sample_data):
self.assertEquals(profit_center.name, sample_data['name'])
self.assertEquals(profit_center.ralph3_id, sample_data['id'])
self.assertEquals(
profit_center.description,
sample_data['description']
)
self.assertEquals(
profit_center.business_line.ralph3_id,
sample_data['business_segment']['id']
)

def test_add_profit_center(self):
sample_data = SAMPLE_PROFIT_CENTERS[0]
self.assertTrue(update_profit_center(
sample_data,
self.default_business_line
))
self.assertTrue(update_profit_center(sample_data))
profit_center = ProfitCenter.objects.get(ralph3_id=sample_data['id'])
self._compare_profit_center(profit_center, sample_data)

def test_update_profit_center(self):
sample_data = SAMPLE_PROFIT_CENTERS[0]
self.assertTrue(update_profit_center(
sample_data,
self.default_business_line
))
self.assertTrue(update_profit_center(sample_data))
profit_center = ProfitCenter.objects.get(ralph3_id=sample_data['id'])
self._compare_profit_center(profit_center, sample_data)

sample_data2 = SAMPLE_PROFIT_CENTERS[1]
sample_data2['id'] = sample_data['id']
self.assertFalse(update_profit_center(
sample_data2,
self.default_business_line
))
self.assertFalse(update_profit_center(sample_data2))
profit_center = ProfitCenter.objects.get(ralph3_id=sample_data2['id'])
self._compare_profit_center(profit_center, sample_data2)

Expand All @@ -75,7 +52,7 @@ def test_batch_update(
get_from_ralph_mock,
update_profit_center_mock
):
def sample_update_profit_center(data, default_business_line):
def sample_update_profit_center(data):
return data['id'] % 2 == 0

def sample_get_from_ralph(endpoint, logger):
Expand All @@ -89,11 +66,5 @@ def sample_get_from_ralph(endpoint, logger):
(True, '1 new profit center(s), 1 updated, 2 total')
)
update_profit_center_mock.call_count = 2
update_profit_center_mock.assert_any_call(
SAMPLE_PROFIT_CENTERS[0],
self.default_business_line,
)
update_profit_center_mock.assert_any_call(
SAMPLE_PROFIT_CENTERS[1],
self.default_business_line,
)
update_profit_center_mock.assert_any_call(SAMPLE_PROFIT_CENTERS[0])
update_profit_center_mock.assert_any_call(SAMPLE_PROFIT_CENTERS[1])
Loading

0 comments on commit 1e0b14c

Please sign in to comment.