Exemplo n.º 1
0
    def test_get_db_prep_value_unprepared_other(self):
        field = DateTimeInfinityField()
        test_val = dt(2018, 3, 5)
        result = field.get_db_prep_value(test_val, connection, prepared=False)

        # The actual result here will vary with Django versions and which
        # database engine has been selected in the Django settings!
        expected = super(DateTimeInfinityField, field).get_db_prep_value(
            test_val, connection, prepared=False)

        assert result == expected
Exemplo n.º 2
0
class MaintenanceTask(models.Model):
    """From NAV Wiki: The maintenance task created in the maintenance task
    tool."""

    STATE_SCHEDULED = 'scheduled'
    STATE_ACTIVE = 'active'
    STATE_PASSED = 'passed'
    STATE_CANCELED = 'canceled'
    STATES = (
        (STATE_SCHEDULED, 'Scheduled'),
        (STATE_ACTIVE, 'Active'),
        (STATE_PASSED, 'Passed'),
        (STATE_CANCELED, 'Canceled'),
    )

    id = models.AutoField(db_column='maint_taskid', primary_key=True)
    start_time = models.DateTimeField(db_column='maint_start')
    end_time = DateTimeInfinityField(db_column='maint_end', blank=True)
    description = models.TextField()
    author = VarcharField()
    state = VarcharField(choices=STATES)

    class Meta(object):
        db_table = 'maint_task'

    def __unicode__(self):
        return u'"%s" by %s' % (self.description, self.author)
Exemplo n.º 3
0
 def test_get_db_prep_value_unprepared_other(self):
     field = DateTimeInfinityField()
     test_val = dt(2018, 3, 5)
     result = field.get_db_prep_value(test_val, connection, prepared=False)
     self.assertEqual(result, u'2018-03-05 00:00:00')
Exemplo n.º 4
0
 def test_get_db_prep_value_infinity(self):
     field = DateTimeInfinityField()
     result_min = field.get_db_prep_value(dt.min, connection)
     self.assertEqual(result_min, u'-infinity')
     result_max = field.get_db_prep_value(dt.max, connection)
     self.assertEqual(result_max, u'infinity')
Exemplo n.º 5
0
class MaintenanceTask(models.Model):
    """From NAV Wiki: The maintenance task created in the maintenance task
    tool."""
    objects = MaintenanceTaskManager()

    STATE_SCHEDULED = 'scheduled'
    STATE_ACTIVE = 'active'
    STATE_PASSED = 'passed'
    STATE_CANCELED = 'canceled'
    STATES = (
        (STATE_SCHEDULED, 'Scheduled'),
        (STATE_ACTIVE, 'Active'),
        (STATE_PASSED, 'Passed'),
        (STATE_CANCELED, 'Canceled'),
    )

    id = models.AutoField(db_column='maint_taskid', primary_key=True)
    start_time = models.DateTimeField(db_column='maint_start')
    end_time = DateTimeInfinityField(db_column='maint_end', blank=True)
    description = models.TextField()
    author = VarcharField()
    state = VarcharField(choices=STATES)

    class Meta(object):
        db_table = 'maint_task'

    def __str__(self):
        return u'"%s" by %s' % (self.description, self.author)

    def full_representation(self):
        """
        Help function to represent a task with desc, start and end.
        """
        return u'%s (%s - %s)' % (
            self.description,
            self.start_time,
            ('No end time' if self.is_endless() else self.end_time))

    def get_components(self):
        """
        Returns the list of model objects involved in this task
        """
        return [c.component for c in self.maintenancecomponent_set.all()]

    def get_event_subjects(self):
        """
        Returns a list of the model objects, represented by this task,
        that can be the subjects of actual maintenanceState events.
        """
        subjects = []
        for component in self.get_components():
            if isinstance(component, (manage.Room, manage.NetboxGroup)):
                subjects.extend(component.netbox_set.all())
            elif isinstance(component, manage.Location):
                for location in component.get_descendants(include_self=True):
                    subjects.extend(manage.Netbox.objects.filter(
                        room__location=location))
            elif component is None:
                continue  # no use in including deleted components
            else:
                subjects.append(component)

        return list(set(subjects))

    def is_endless(self):
        """Returns true if the task is endless"""
        return self.end_time >= INFINITY
Exemplo n.º 6
0
class AlertHistory(models.Model, EventMixIn):
    """From NAV Wiki: The alert history. Simular to the alert queue with one
    important distinction; alert history stores stateful events as one row,
    with the start and end time of the event."""
    objects = AlertHistoryManager()

    id = models.AutoField(db_column='alerthistid', primary_key=True)
    source = models.ForeignKey('Subsystem', db_column='source')
    device = models.ForeignKey('models.Device',
                               db_column='deviceid',
                               null=True)
    netbox = models.ForeignKey('models.Netbox',
                               db_column='netboxid',
                               null=True)
    subid = VarcharField(default='')
    start_time = models.DateTimeField()
    end_time = DateTimeInfinityField(null=True)
    event_type = models.ForeignKey('EventType', db_column='eventtypeid')
    alert_type = models.ForeignKey('AlertType',
                                   db_column='alerttypeid',
                                   null=True)
    value = models.IntegerField()
    severity = models.IntegerField()

    varmap = StateVariableMap()

    class Meta(object):
        db_table = 'alerthist'

    def __unicode__(self):
        return u'Source %s, severity %d' % (self.source, self.severity)

    def is_stateful(self):
        """Returns true if the alert is stateful."""

        return self.end_time is not None

    def is_open(self):
        """Returns true if stateful and open."""

        return self.is_stateful() and self.end_time == dt.datetime.max

    def get_downtime(self):
        """Returns the difference between start_time and end_time, the current
        downtime if the alert is still open, and None if the alert is
        stateless."""

        if self.is_stateful():
            if self.is_open():
                # Open alert
                return (dt.datetime.now() - self.start_time)
            else:
                # Closed alert
                return (self.end_time - self.start_time)
        else:
            # Stateless alert
            return None

    def is_acknowledged(self):
        """
        Returns an Acknowledgement instance if this alert has been
        acknowledged, otherwise None.
        """
        try:
            return self.acknowledgement
        except Acknowledgement.DoesNotExist:
            return

    def acknowledge(self, account, comment):
        """
        Acknowledges this alert using a given account and comment.

        Any pre-existing acknowledgement will be overwritten.
        """
        try:
            ack = self.acknowledgement
        except Acknowledgement.DoesNotExist:
            ack = Acknowledgement(alert=self, account=account, comment=comment)
        else:
            ack.account = account
            ack.comment = comment
            ack.date = dt.datetime.now()

        ack.save()

    def save(self, *args, **kwargs):
        new_object = self.pk is None
        super(AlertHistory, self).save(*args, **kwargs)
        if new_object:
            assert self.pk
            self.varmap = self.varmap