models.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.db import models
  5. from libs import ModelMixin, human_datetime
  6. from apps.account.models import User
  7. import json
  8. class Detection(models.Model, ModelMixin):
  9. TYPES = (
  10. ('1', '站点检测'),
  11. ('2', '端口检测'),
  12. ('3', '进程检测'),
  13. ('4', '自定义脚本'),
  14. ('5', 'Ping检测'),
  15. )
  16. STATUS = (
  17. (0, '正常'),
  18. (1, '异常'),
  19. )
  20. name = models.CharField(max_length=50)
  21. type = models.CharField(max_length=2, choices=TYPES)
  22. group = models.CharField(max_length=255, null=True)
  23. targets = models.TextField()
  24. extra = models.TextField(null=True)
  25. desc = models.CharField(max_length=255, null=True)
  26. is_active = models.BooleanField(default=True)
  27. rate = models.IntegerField(default=5)
  28. threshold = models.IntegerField(default=3)
  29. quiet = models.IntegerField(default=24 * 60)
  30. fault_times = models.SmallIntegerField(default=0)
  31. notify_mode = models.CharField(max_length=255)
  32. notify_grp = models.CharField(max_length=255)
  33. latest_run_time = models.CharField(max_length=20, null=True)
  34. created_at = models.CharField(max_length=20, default=human_datetime)
  35. created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
  36. updated_at = models.CharField(max_length=20, null=True)
  37. updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)
  38. def to_view(self):
  39. tmp = self.to_dict()
  40. tmp['type_alias'] = self.get_type_display()
  41. tmp['notify_mode'] = json.loads(self.notify_mode)
  42. tmp['notify_grp'] = json.loads(self.notify_grp)
  43. tmp['targets'] = json.loads(self.targets)
  44. return tmp
  45. def __repr__(self):
  46. return '<Detection %r>' % self.name
  47. class Meta:
  48. db_table = 'detections'
  49. ordering = ('-id',)