Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Added NETJSONGRAPH_LINK_EXPIRATION days setting
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jan 3, 2016
1 parent f90c639 commit d6fff61
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
23 changes: 13 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,19 @@ For a good default ``LOGGING`` configuration refer to the `test settings
Settings
--------

+---------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| Setting | Default value | Description |
+=================================+=====================================+===================================================================================================+
| ``NETJSONGRAPH_PARSERS`` | ``[]`` | List with additional custom `netdiff parsers <https://github.com/ninuxorg/netdiff#parsers>`_ |
+---------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| ``NETJSONGRAPH_SIGNALS`` | ``None`` | String representing python module to import on initialization. |
| | | Useful for loading django signals or to define custom behaviour. |
+---------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| ``NETJSONGRAPH_TIMEOUT`` | ``8`` | Timeout when requesting topology URLs |
+---------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
+----------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| Setting | Default value | Description |
+==================================+=====================================+===================================================================================================+
| ``NETJSONGRAPH_PARSERS`` | ``[]`` | List with additional custom `netdiff parsers <https://github.com/ninuxorg/netdiff#parsers>`_ |
+----------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| ``NETJSONGRAPH_SIGNALS`` | ``None`` | String representing python module to import on initialization. |
| | | Useful for loading django signals or to define custom behaviour. |
+----------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| ``NETJSONGRAPH_TIMEOUT`` | ``8`` | Timeout when requesting topology URLs |
+----------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+
| ``NETJSONGRAPH_LINK_EXPIRATION`` | ``60`` | If a link is down for more days than this number, it will be deleted by the ``update_topology`` |
| | | management command. Setting this to ``False`` will disable this feature. |
+----------------------------------+-------------------------------------+---------------------------------------------------------------------------------------------------+

Installing for development
--------------------------
Expand Down
1 change: 1 addition & 0 deletions django_netjsongraph/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
PARSERS = DEFAULT_PARSERS + getattr(settings, 'NETJSONGRAPH_PARSERS', [])
SIGNALS = getattr(settings, 'NETJSONGRAPH_SIGNALS', None)
TIMEOUT = getattr(settings, 'NETJSONGRAPH_TIMEOUT', 8)
LINK_EXPIRATION = getattr(settings, 'NETJSONGRAPH_LINK_EXPIRATION', 60)
41 changes: 41 additions & 0 deletions django_netjsongraph/tests/test_topology.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import six
import json
import responses
from datetime import timedelta

from django.test import TestCase
from django.core.management import call_command
from django.utils.timezone import now
from netdiff import OlsrParser

from ..models import Topology, Link, Node
from ..utils import update_topology
from ..settings import LINK_EXPIRATION
from .utils import StringIO, LoadMixin, redirect_stdout


Expand Down Expand Up @@ -293,3 +297,40 @@ def test_update_topology_func_unpublished(self):
update_topology()
self.assertEqual(Node.objects.count(), 0)
self.assertEqual(Link.objects.count(), 0)

@responses.activate
def test_delete_expired_links(self):
t = Topology.objects.first()
t.parser = 'netdiff.NetJsonParser'
t.save()
# should not delete
almost_expired_date = now() - timedelta(days=LINK_EXPIRATION-10)
l = Link.objects.create(source_id='d083b494-8e16-4054-9537-fb9eba914861',
target_id='d083b494-8e16-4054-9537-fb9eba914862',
cost=1,
status='down',
topology=t)
Link.objects.filter(pk=l.pk).update(created=almost_expired_date,
modified=almost_expired_date)
empty_topology=json.dumps({
"type": "NetworkGraph",
"protocol": "OLSR",
"version": "0.8",
"metric": "ETX",
"nodes": [],
"links": []
})
responses.add(responses.GET,
'http://127.0.0.1:9090',
body=empty_topology,
content_type='application/json')
update_topology('testnetwork')
self.assertEqual(Node.objects.count(), 2)
self.assertEqual(Link.objects.count(), 1)
# should delete
expired_date = now() - timedelta(days=LINK_EXPIRATION+10)
Link.objects.filter(pk=l.pk).update(created=expired_date,
modified=expired_date)
update_topology('testnetwork')
self.assertEqual(Node.objects.count(), 2)
self.assertEqual(Link.objects.count(), 0)
40 changes: 34 additions & 6 deletions django_netjsongraph/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
import sys

from datetime import timedelta
from django.utils.timezone import now

from .contextmanagers import log_on_fail
from .models import Topology
from .models import Topology, Link
from .settings import LINK_EXPIRATION


def print_info(message): # pragma no cover
"""
print info message if calling from management command ``update_topology``
"""
if 'update_topology' in sys.argv:
print('{0}\n'.format(message))


def delete_expired_links():
"""
deletes links that have been down for more than
``NETJSONGRAPH_LINK_EXPIRATION`` days
"""
if LINK_EXPIRATION not in [False, None]:
expiration_date = now() - timedelta(days=int(LINK_EXPIRATION))
expired_links = Link.objects.filter(status='down',
modified__lt=expiration_date)
expired_links_length = len(expired_links)
if expired_links_length:
print_info('Deleting {0} expired links'.format(expired_links_length))
for link in expired_links:
link.delete()


def update_topology(label=None):
"""
updates all the topology
sends logs to the "nodeshot.networking" logger
- updates topologies
- logs failures
- calls delete_expired_links()
"""
queryset = Topology.objects.filter(published=True)
if label:
queryset = queryset.filter(label__icontains=label)
for topology in queryset:
# print info message if calling from management command
if 'update_topology' in sys.argv: # pragma no cover
print('Updating topology {0}\n'.format(topology))
print_info('Updating topology {0}'.format(topology))
with log_on_fail('update', topology):
topology.update()
delete_expired_links()

0 comments on commit d6fff61

Please sign in to comment.