Merge "update the huawei's lab"
[pharos.git] / tools / pharos-dashboard / 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     def get_utilization(self, timedelta):
33         """
34         Return a dictionary containing the count of idle, online and offline measurements in the time from
35         now-timedelta to now
36         """
37         utilization = {'idle': 0, 'online': 0, 'offline': 0}
38         statistics = self.jenkinsstatistic_set.filter(timestamp__gte=timezone.now() - timedelta)
39         utilization['idle'] = statistics.filter(idle=True).count()
40         utilization['online'] = statistics.filter(online=True).count()
41         utilization['offline'] = statistics.filter(offline=True).count()
42         return utilization
43
44     class Meta:
45         db_table = 'jenkins_slave'
46
47     def __str__(self):
48         return self.name
49
50
51 class JenkinsStatistic(models.Model):
52     id = models.AutoField(primary_key=True)
53     slave = models.ForeignKey(JenkinsSlave, on_delete=models.CASCADE)
54     offline = models.BooleanField(default=False)
55     idle = models.BooleanField(default=False)
56     online = models.BooleanField(default=False)
57     timestamp = models.DateTimeField(auto_now_add=True)
58
59     class Meta:
60         db_table = 'jenkins_statistic'