This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
138 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters