Blame view

src/common/RequestBuilder.py 6.63 KB
77816f10   haribo   Workflow implemen...
1
2
from django.contrib.auth.models import User
from pyrosapp.models import *
7a79e25b   haribo   Date: 19/05/2016
3
import scheduler.tasks
77816f10   haribo   Workflow implemen...
4
5
6

from decimal import Decimal

94336356   haribo   Added starting fi...
7
8
9
from django.conf import settings


5b5566ab   haribo   added celery
10
class RequestBuilder():
77816f10   haribo   Workflow implemen...
11

5b5566ab   haribo   added celery
12
    def __init__(self):
94336356   haribo   Added starting fi...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        '''
            Might do nothing if this is not a simulation
        '''
        if settings.SIMULATION == False:
            pass
        else:
            ''' creating dependencies in DB if they don't exist '''
            if PyrosUser.objects.count() == 0:
                if Country.objects.count() == 0:
                    country = Country.objects.create(name="default")
                else:
                    country = Country.objects.get()
                if UserLevel.objects.count() == 0:
                    user_level = UserLevel.objects.create(name="default")
                else:
                    user_level = UserLevel.objects.get()
                if User.objects.count() == 0:
                    user = User.objects.create(username="default")
                else:
                    user = User.objects.get()
                PyrosUser.objects.create(
94082e77   haribo   Date: 03/06/2016
34
                    country=country, user_level=user_level, user=user, quota=1000)
94336356   haribo   Added starting fi...
35
36
37
38
39
40
41
42
43

            if ScientificProgram.objects.count() == 0:
                ScientificProgram.objects.create(name="default")

    def start_new_request(self, pyros_user, scientific_program, is_alert, name="default"):
        '''
            This function MUST be called to build a request.
            It erases the previous one, and creates the sequences', albums' and plans' dictionaries.
        '''
fd99569d   haribo   Date: 22/06/2016
44
45
        self.request = Request(name=name,
            scientific_program=scientific_program, pyros_user=pyros_user, is_alert=is_alert, complete=True, submitted=True)
94336356   haribo   Added starting fi...
46
47
48
49
50
51
52
        self.sequence_id = 1
        self.album_id = 1
        self.plan_id = 1
        self.sequences = {}
        self.albums = {}
        self.plans = {}

7a79e25b   haribo   Date: 19/05/2016
53
    def add_sequence(self, priority, jd1, jd2, t_prefered=-1, name="default", duration=0):
94336356   haribo   Added starting fi...
54
55
56
57
58
59
        '''
            Add a sequence to the current request
            start_new_request must be called one before it
            :returns : The local ID of the sequence (to add albums to it)
        '''

fafae58f   haribo   Cleaned directory
60
61
62
63
        if not hasattr(self, "request"):
            raise RuntimeError(
                "start_new_request MUST be invoked before creating any sequence")

94336356   haribo   Added starting fi...
64
        sequence = Sequence(name=name, status=Sequence.OBSERVABLE, priority=priority,
7a79e25b   haribo   Date: 19/05/2016
65
                            jd1=jd1, jd2=jd2, t_prefered=t_prefered, duration=duration)
94336356   haribo   Added starting fi...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        self.sequences[self.sequence_id] = sequence
        self.sequence_id += 1
        return self.sequence_id - 1

    def delete_sequence(self, seq_id):
        '''
            Removes locally a sequence from the current request
        '''
        if seq_id in self.sequences.keys():
            del self.sequences[seq_id]

    def add_album(self, seq_id, detector_name, name="default"):
        '''
            Creates a new album for the given sequence
            :returns : The local ID of the new album
        '''

fafae58f   haribo   Cleaned directory
83
        if not Detector.objects.filter(device__name=detector_name).exists():
7a79e25b   haribo   Date: 19/05/2016
84
            raise RuntimeError("Detector %s doesn't exist" % (detector_name,))
fafae58f   haribo   Cleaned directory
85
        if seq_id not in self.sequences.keys():
7a79e25b   haribo   Date: 19/05/2016
86
            raise RuntimeError("The sequence %d doesn't exist" % (seq_id,))
94336356   haribo   Added starting fi...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

        detector = Detector.objects.get(device__name=detector_name)

        album = Album(name=name, detector=detector)
        self.albums[self.album_id] = (album, seq_id)
        self.album_id += 1
        return self.album_id - 1

    def del_album(self, album_id):
        '''
            Removes locally an album from the current request
        '''
        if album_id in self.albums.keys():
            del self.albums[album_id]

    def add_plan(self, album_id, filter_name, duration, nb_images, name="default"):
        '''
            Creates a new plan for the given album
            :returns : The local ID of the new plan
        '''

fafae58f   haribo   Cleaned directory
108
        if not Filter.objects.filter(device__name=filter_name).exists():
7a79e25b   haribo   Date: 19/05/2016
109
            raise RuntimeError("Filer %s doesn't exist" % (filter_name,))
fafae58f   haribo   Cleaned directory
110
        if album_id not in self.albums.keys():
7a79e25b   haribo   Date: 19/05/2016
111
            raise RuntimeError("The album %d doesn't exist" % (album_id,))
94336356   haribo   Added starting fi...
112
113
114

        filter_ = Filter.objects.get(device__name=filter_name)

7a79e25b   haribo   Date: 19/05/2016
115
        duration = Decimal(float(duration) / 86400.0)
94336356   haribo   Added starting fi...
116
117
118
119
120
121
122
123
124
125
126
127
128
        plan = Plan(name=name, filter=filter_,
                    duration=duration, nb_images=nb_images)
        self.plans[self.plan_id] = (plan, album_id)
        self.plan_id += 1
        return self.plan_id - 1

    def del_plan(self, plan_id):
        '''
            Removes locally a plan from the current request
        '''
        if plan_id in self.plans.keys():
            del self.plans[plan_id]

5b5566ab   haribo   added celery
129
    def validate_request(self):
94336356   haribo   Added starting fi...
130
131
132
133
134
135
136
        '''
            Saves the different objects : request, sequences, albums, plans
            Computes sequences' duration
            Creates a scheduling task
            :returns : The request
        '''

fafae58f   haribo   Cleaned directory
137
138
139
140
        if len(self.sequences) == 0:
            raise RuntimeError(
                "There must be at least one sequence in a request")

7a79e25b   haribo   Date: 19/05/2016
141
142
143
144
145
146
147
148
149
        if settings.SIMULATION == False:
            sequences_having_album = list(set([
                seq_id for album, seq_id in self.albums.values()]))
            if len(sequences_having_album) != len(self.sequences):
                raise RuntimeError("All sequences must have at least one album")
            albums_having_plan = list(set([
                album_id for plan, album_id in self.plans.values()]))
            if len(albums_having_plan) != len(self.albums):
                raise RuntimeError("All albums must have at least one plan")
94336356   haribo   Added starting fi...
150

77816f10   haribo   Workflow implemen...
151
        self.request.save()
94336356   haribo   Added starting fi...
152
        for sequence in self.sequences.values():
77816f10   haribo   Workflow implemen...
153
154
            sequence.request = self.request
            sequence.save()
94336356   haribo   Added starting fi...
155
156
157
158
159
160
161
        for album, seq_id in self.albums.values():
            album.sequence = self.sequences[seq_id]
            album.save()
        for plan, album_id in self.plans.values():
            plan.album = self.albums[album_id][0]
            plan.save()

7a79e25b   haribo   Date: 19/05/2016
162
163
164
165
166
167
168
169
170
        if settings.SIMULATION == False:
            ''' computes the duration of all sequences '''
            for sequence in self.sequences.values():
                max_duration = 0
                for album in sequence.albums.all():
                    duration = sum([plan.duration for plan in album.plans.all()])
                    max_duration = duration if duration > max_duration else max_duration
                sequence.duration = max_duration
                sequence.save()
bbf6e698   haribo   Date: 14/06/2016
171
        scheduler.tasks.scheduling.delay(first_schedule=True, alert=self.request.is_alert)  # TODO : changer le first_schedule ...
94336356   haribo   Added starting fi...
172
        return self.request