Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge d45d21d into 5717c07
Browse files Browse the repository at this point in the history
  • Loading branch information
szok authored Nov 18, 2016
2 parents 5717c07 + d45d21d commit dff44e6
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 9 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
'pyyaml==3.12',
'rq>=0.3.7',
'SQLAlchemy==0.7.8',
'django-filter==0.15.3'
'django-filter==0.15.3',
'ipaddress==1.0.17'
],
entry_points={
'django.pluggable_app': [
Expand Down
27 changes: 27 additions & 0 deletions src/ralph_scrooge/migrations/0004_ipinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-11-17 11:34
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

dependencies = [
('ralph_scrooge', '0003_auto_20161118_0952'),
]

operations = [
migrations.CreateModel(
name='IPInfo',
fields=[
('pricingobject_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='ralph_scrooge.PricingObject')),
('number', models.DecimalField(decimal_places=0, default=None, editable=False, help_text='Presented as int.', max_digits=39, unique=True, verbose_name='IP address')),
],
options={
'abstract': False,
},
bases=('ralph_scrooge.pricingobject',),
),
]
74 changes: 74 additions & 0 deletions src/ralph_scrooge/migrations/0005_copy_ipinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-11-17 11:41
from __future__ import unicode_literals

from datetime import datetime

from django.db import migrations
from django.db.models import Count
from ralph_scrooge.models.pricing_object import PRICING_OBJECT_TYPES


def udpdate_ip_address_number(apps, schema_editor):
PricingObject = apps.get_model('ralph_scrooge', 'PricingObject')
IPInfo = apps.get_model('ralph_scrooge', 'IPInfo')
DailyUsage = apps.get_model('ralph_scrooge', 'DailyUsage')
DailyCost = apps.get_model('ralph_scrooge', 'DailyCost')
today = datetime.now().date()

# Find duplicates
duplicates = PricingObject.objects.filter(
type=PRICING_OBJECT_TYPES.IP_ADDRESS
).values(
'name'
).annotate(Count('name')).order_by().filter(
name__count__gt=1,
)

for d in duplicates:
print('Processing IP: {}'.format(d['name']))
pricing_objects = PricingObject.objects.filter(
name=d['name'], type=PRICING_OBJECT_TYPES.IP_ADDRESS
).order_by('created')
# Default PO for the oldest entry
default_pricing_object = pricing_objects[0]
for po in pricing_objects[1:]:
DailyCost.objects.filter(pricing_object=po).update(
pricing_object=default_pricing_object
)
dpo = default_pricing_object.daily_pricing_objects.get_or_create(
date=today,
defaults=dict(
service_environment=default_pricing_object.service_environment,
pricing_object=default_pricing_object,
)
)[0]

DailyUsage.objects.filter(
daily_pricing_object__pricing_object=po
).update(daily_pricing_object=dpo)
print('Delete {} pricing object'.format(po.name))
po.delete()

for pricing_object in PricingObject.objects.filter(
type=PRICING_OBJECT_TYPES.IP_ADDRESS
):
ip_info, _ = IPInfo.objects.get_or_create(
pricingobject_ptr=pricing_object
)
ip_info.__dict__.update(pricing_object.__dict__)
ip_info.save()


class Migration(migrations.Migration):

dependencies = [
('ralph_scrooge', '0004_ipinfo'),
]

operations = [
migrations.RunPython(
udpdate_ip_address_number,
reverse_code=migrations.RunPython.noop
)
]
2 changes: 2 additions & 0 deletions src/ralph_scrooge/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
DailyVIPInfo,
DailyVirtualInfo,
DatabaseInfo,
IPInfo,
PricingObjectModel,
PricingObjectType,
PRICING_OBJECT_TYPES,
Expand Down Expand Up @@ -93,6 +94,7 @@
'Environment',
'ExtraCost',
'ExtraCostType',
'IPInfo',
'HistoricalService', # dynamic model
'OwnershipType',
'ProfitCenter',
Expand Down
21 changes: 21 additions & 0 deletions src/ralph_scrooge/models/pricing_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import print_function
from __future__ import unicode_literals

import ipaddress
from decimal import Decimal as D

from django.db import models as db
Expand Down Expand Up @@ -175,6 +176,26 @@ def save(self, *args, **kwargs):
return super(PricingObject, self).save(*args, **kwargs)


class IPInfo(PricingObject):

number = db.DecimalField(
verbose_name=_('IP address'),
help_text=_('Presented as int.'),
editable=False,
unique=True,
max_digits=39,
decimal_places=0,
default=None,
)

class Meta(PricingObject.Meta):
app_label = 'ralph_scrooge'

def save(self, *args, **kwargs):
self.number = int(ipaddress.ip_address(self.name or 0))
super(IPInfo, self).save(*args, **kwargs)


class DailyPricingObject(db.Model):
date = db.DateField(
verbose_name=_("date"),
Expand Down
4 changes: 2 additions & 2 deletions src/ralph_scrooge/plugins/collect/netflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ralph_scrooge.models import (
DailyPricingObject,
DailyUsage,
PricingObject,
IPInfo,
PRICING_OBJECT_TYPES,
ServiceEnvironment,
UsageType,
Expand Down Expand Up @@ -321,7 +321,7 @@ def update(
))
continue
total += 1
pricing_object, created = PricingObject.objects.get_or_create(
pricing_object, created = IPInfo.objects.get_or_create(
name=ip,
type_id=PRICING_OBJECT_TYPES.IP_ADDRESS,
defaults=dict(
Expand Down
4 changes: 2 additions & 2 deletions src/ralph_scrooge/plugins/subscribers/vip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import logging

from ralph_scrooge.models import (
IPInfo,
PRICING_OBJECT_TYPES,
PricingObject,
PricingObjectModel,
ServiceEnvironment,
VIPInfo,
Expand Down Expand Up @@ -121,7 +121,7 @@ def save_vip_info(event_data):
)

# get ip_info
ip_info = PricingObject.objects.get_or_create(
ip_info = IPInfo.objects.get_or_create(
name=event_data['ip'],
type_id=PRICING_OBJECT_TYPES.IP_ADDRESS,
defaults=dict(service_environment=service_env)
Expand Down
4 changes: 2 additions & 2 deletions src/ralph_scrooge/tests/plugins/collect/test_netflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ralph_scrooge.tests import ScroogeTestCase
from ralph_scrooge.tests.utils.factory import (
DailyPricingObjectFactory,
PricingObjectFactory,
IPInfoFactory,
ServiceEnvironmentFactory,
)

Expand Down Expand Up @@ -267,7 +267,7 @@ def test_get_usages_type(self):
@override_settings(UNKNOWN_SERVICES={'netflow': 1}, NFSEN_MIN_VALUE=10)
def test_update(self):
service_environment = ServiceEnvironmentFactory()
pricing_object = PricingObjectFactory.create(
pricing_object = IPInfoFactory.create(
name='8.8.8.8',
type_id=PRICING_OBJECT_TYPES.IP_ADDRESS,
service_environment=service_environment,
Expand Down
4 changes: 2 additions & 2 deletions src/ralph_scrooge/tests/plugins/subscribers/test_vip.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ralph_scrooge.tests import ScroogeTestCase
from ralph_scrooge.tests.utils.factory import (
AssetInfoFactory,
PricingObjectFactory,
IPInfoFactory,
PricingObjectModelFactory,
ServiceEnvironmentFactory,
VIPInfoFactory,
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_save_vip_info(self):
self._compare_vips(vip_info, self.event_data)

def test_save_vip_info_ip_exists(self):
ip_info = PricingObjectFactory(
ip_info = IPInfoFactory(
name=self.event_data['ip'],
type_id=PRICING_OBJECT_TYPES.IP_ADDRESS,
)
Expand Down
4 changes: 4 additions & 0 deletions src/ralph_scrooge/tests/utils/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class PricingObjectFactory(DjangoModelFactory):
model = SubFactory(PricingObjectModelFactory)


class IPInfoFactory(PricingObjectFactory):
FACTORY_FOR = models.IPInfo


class DailyPricingObjectFactory(DjangoModelFactory):
FACTORY_FOR = models.DailyPricingObject

Expand Down

0 comments on commit dff44e6

Please sign in to comment.