Blame view

src/alert_manager/StrategyBuilder.py 2.27 KB
bb45cd4a   haribo   Date: 25/05/2016
1
2
3
from common.RequestBuilder import RequestBuilder
import xml.etree.ElementTree as ET
import os
ddf59dd4   haribo   Remaniement :
4
from common.models import *
bb45cd4a   haribo   Date: 25/05/2016
5
6
7
8

JD1 = 0
JD2 = 9000000

1aed430d   jeremy   Alert handled + k...
9
10
11
12
13
14
15
16
'''
    Reads a strategy file and creates a new request with it

    ENTRY POINT :
        - create_request_from_strategy
'''


bb45cd4a   haribo   Date: 25/05/2016
17
class StrategyBuilder():
1aed430d   jeremy   Alert handled + k...
18
19
20
21
22

    def validate(self):
        self.rb.validate_request()
        return self.rb.request

bb45cd4a   haribo   Date: 25/05/2016
23
    '''
1aed430d   jeremy   Alert handled + k...
24
        Reads the strategy file, and creates the request, saving everything in DB
bb45cd4a   haribo   Date: 25/05/2016
25

1aed430d   jeremy   Alert handled + k...
26
27
28
29
30
31
        :param : strategy_file, the name of the strategy file with .xml but without the path
        :param : pyros_user, the user associated with the existing alert
        :param : scientific_program, the sci prog of the existing alert
        :param : name, the name of the existing request + name of the strategy
        :returns : the created request
    '''
bb45cd4a   haribo   Date: 25/05/2016
32
    def create_request_from_strategy(self, strategy_file, pyros_user, scientific_program, name):
bb45cd4a   haribo   Date: 25/05/2016
33
34
35
36
37
38
39
40
41
42
43
44

        tree = ET.parse(os.path.join('alert_manager', 'strategies', strategy_file))
        request = tree.getroot()

        for strategy in StrategyObs.objects.all():
            if (len(strategy.name) + 1) <= len(name):
                if name[-len(strategy.name):] == strategy.name:
                    name = name[:-(len(strategy.name) + 1)]
                    break
        name = name + '_' + strategy_file[:-4]
        self.rb = RequestBuilder()
        self.rb.start_new_request(pyros_user, scientific_program, True, name)
eecfb779   haribo   Date: 26/05/2016
45
46
        for index, sequence in enumerate(request):
            self.add_sequence(sequence, scientific_program.priority, name + "_" + str(index))
bb45cd4a   haribo   Date: 25/05/2016
47
48
        return self.rb.request

eecfb779   haribo   Date: 26/05/2016
49
50
51
52
    def add_sequence(self, sequence, priority, name):
        sequence_id = self.rb.add_sequence(priority, JD1, JD2, -1, name=name)
        for index, album in enumerate(sequence):
            self.add_album(album, sequence_id, name + str(index))
bb45cd4a   haribo   Date: 25/05/2016
53

eecfb779   haribo   Date: 26/05/2016
54
55
56
57
    def add_album(self, album, sequence_id, name):
        album_id = self.rb.add_album(sequence_id, album.get('detector'), name=name)
        for index, plan in enumerate(album):
            self.add_plan(plan, album_id, name + str(index))
bb45cd4a   haribo   Date: 25/05/2016
58

eecfb779   haribo   Date: 26/05/2016
59
    def add_plan(self, plan, album_id, name):
bb45cd4a   haribo   Date: 25/05/2016
60
        self.rb.add_plan(album_id, plan.get('filter'), plan.get('duration'),
eecfb779   haribo   Date: 26/05/2016
61
                         plan.get('nb_images'), name=name)