Skip to content

Commit

Permalink
Revert changes to datetime displays
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Jan 24, 2024
1 parent 52af3a3 commit 8c0bad6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 14 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI
on: [push, pull_request]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
env:
NETBOX_CONFIGURATION: netbox.configuration_testing
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
services:
redis:
image: redis
ports:
- 6379:6379
postgres:
image: postgres
env:
POSTGRES_USER: netbox
POSTGRES_PASSWORD: netbox
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Check out NetBox
uses: actions/checkout@v4
with:
repository: netbox-community/netbox
ref: master
path: netbox

- name: Check out repo
uses: actions/checkout@v4
with:
path: netbox-config-backup

- name: Install dependencies & set up configuration
run: |
python -m pip install --upgrade pip
pip install -r netbox\requirements.txt
pip install pycodestyle coverage tblib
pip install -e netbox-config-backup
- name: Run tests
run: coverage run --source="netbox-config-backup/" netbox/manage.py test netbox-config-backup/ --parallel
19 changes: 17 additions & 2 deletions netbox_config_backup/models/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ class Meta:
ordering = ['name']

@property
def backup_count(self):
return self.changes.count()
def last_backup(self):
return self.jobs.filter(status=JobResultStatusChoices.STATUS_COMPLETED).last().completed

@property
def next_attempt(self):
return self.jobs.filter(
status__in=[
JobResultStatusChoices.STATUS_SCHEDULED,
JobResultStatusChoices.STATUS_PENDING,
JobResultStatusChoices.STATUS_RUNNING
]
).last().scheduled

@property
def last_change(self):

return self.changes.last().commit.time

def get_absolute_url(self):
return reverse('plugins:netbox_config_backup:backup', args=[self.pk])
Expand Down
24 changes: 13 additions & 11 deletions netbox_config_backup/querysets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models import Count

from extras.choices import JobResultStatusChoices
from utilities.querysets import RestrictedQuerySet
Expand All @@ -9,14 +10,15 @@ class BackupQuerySet(RestrictedQuerySet):
def default_annotate(self):
from netbox_config_backup.models import BackupJob, BackupCommitTreeChange

return self.prefetch_related('jobs').annotate(
last_backup=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status=JobResultStatusChoices.STATUS_COMPLETED).order_by('completed').values('completed')[:1]
),
next_attempt=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status__in=['pending', 'running']).order_by('scheduled').values('scheduled')[:1]
),
last_change=models.Subquery(
BackupCommitTreeChange.objects.filter(backup=models.OuterRef('id')).values('commit__time')[:1]
)
)
return self.prefetch_related('jobs', 'changes', 'changes__commit')
#.annotate(
#last_backup=models.Subquery(
# BackupJob.objects.filter(backup=models.OuterRef('id'), status=JobResultStatusChoices.STATUS_COMPLETED).order_by('-completed').values('completed')[:1]
#),
#next_attempt=models.Subquery(
# BackupJob.objects.filter(backup=models.OuterRef('id'), status__in=['pending', 'running']).order_by('-scheduled').values('scheduled')[:1]
#),
#last_change=models.Subquery(
# BackupCommitTreeChange.objects.filter(backup=models.OuterRef('id')).order_by('-id').values('commit__time')[:1]
#)
#)
9 changes: 8 additions & 1 deletion netbox_config_backup/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ class BackupTable(BaseTable):
)
last_backup = tables.DateTimeColumn()
next_attempt = tables.DateTimeColumn()
last_change = tables.DateTimeColumn()
backup_count = tables.Column(
accessor='changes'
)

class Meta(BaseTable.Meta):
model = Backup
fields = (
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'backup_count'
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'last_change', 'backup_count'
)
default_columns = (
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'backup_count'
)

def render_backup_count(self, value):
return f'{value.count()}'


class BackupsTable(NetBoxTable):
date = tables.Column(
Expand Down
Empty file.
30 changes: 30 additions & 0 deletions netbox_config_backup/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from dcim.models import Site, Manufacturer, DeviceType, DeviceRole, Device, Interface
from ipam.models import IPAddress
from netbox_config_backup.models import *

class TestBackup(TestCase):

@classmethod
def setUpTestData(cls):

site = Site.objects.create(name='Site 1')
manufacturer = Manufacturer.objects.create(name='Manufacturer 1')
device_type = DeviceType.objects.create(model='Device Type 1', manufacturer=manufacturer)
role = DeviceRole.objects.create(name='Switch')
device = Device.objects.create(
name='Device 1',
site=site,
device_type=device_type,
role=role,
status='active'
)
interface = Interface.objects.create(name='Interface 1', device=device, type='1000baset')
address = IPAddress.objects.create(assigned_object=interface, address='10.0.0.1/32')
device.primary_ip4 = address
device.save()

def test_create_backup(self):
pass

0 comments on commit 8c0bad6

Please sign in to comment.