Add unit tests
[pharos.git] / tools / pharos-dashboard / src / jenkins / models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from django.db import models
12 from django.utils import timezone
13
14
15 class JenkinsSlave(models.Model):
16     id = models.AutoField(primary_key=True)
17     name = models.CharField(max_length=100, unique=True)
18     status = models.CharField(max_length=30, default='offline')
19     url = models.CharField(max_length=1024)
20     ci_slave = models.BooleanField(default=False)
21     dev_pod = models.BooleanField(default=False)
22
23     building = models.BooleanField(default=False)
24
25     last_job_name = models.CharField(max_length=1024, default='')
26     last_job_url = models.CharField(max_length=1024, default='')
27     last_job_scenario = models.CharField(max_length=50, default='')
28     last_job_branch = models.CharField(max_length=50, default='')
29     last_job_installer = models.CharField(max_length=50, default='')
30     last_job_result = models.CharField(max_length=30, default='')
31
32     active = models.BooleanField(default=False)
33
34     def get_utilization(self, timedelta):
35         """
36         Return a dictionary containing the count of idle, online and offline measurements in the time from
37         now-timedelta to now
38         """
39         utilization = {'idle': 0, 'online': 0, 'offline': 0}
40         statistics = self.jenkinsstatistic_set.filter(timestamp__gte=timezone.now() - timedelta)
41         utilization['idle'] = statistics.filter(idle=True).count()
42         utilization['online'] = statistics.filter(online=True).count()
43         utilization['offline'] = statistics.filter(offline=True).count()
44         return utilization
45
46     class Meta:
47         db_table = 'jenkins_slave'
48
49     def __str__(self):
50         return self.name
51
52
53 class JenkinsStatistic(models.Model):
54     id = models.AutoField(primary_key=True)
55     slave = models.ForeignKey(JenkinsSlave, on_delete=models.CASCADE)
56     offline = models.BooleanField(default=False)
57     idle = models.BooleanField(default=False)
58     online = models.BooleanField(default=False)
59     timestamp = models.DateTimeField(auto_now_add=True)
60
61     class Meta:
62         db_table = 'jenkins_statistic'