level2producer.py 7.14 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import random
Wei Shoulin's avatar
brick  
Wei Shoulin committed
2
from ..common.delegate import Delegate
Wei Shoulin's avatar
graph    
Wei Shoulin committed
3
from csst_dfs_commons.models.errors import CSSTGenericException
Wei Shoulin's avatar
brick  
Wei Shoulin committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

class Level2ProducerApi(object):
    """
    Level 0 Data Operation API
    """
    def __init__(self):
        self.pymodule = Delegate().load(sub_module = "facility")
        self.stub = getattr(self.pymodule, "Level2ProducerApi")()

    def register(self, **kwargs):
        ''' register a Level2Producer data record into database
 
        :param kwargs: Parameter dictionary, key items support:
            name = [str]\n
            gitlink = [str]\n
            paramfiles = [str]\n
            priority = [int]\n
            pre_producers = list[int]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.register(**kwargs)

    def find(self, **kwargs):
        ''' retrieve Level2Producer records from database

        :param kwargs: Parameter dictionary, key items support:
            key: [str]
            limit: limits returns the number of records,default 0:no-limit
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.find(**kwargs)

    def get(self, **kwargs):
        '''  fetch a record from database

        :param kwargs: Parameter dictionary, key items support:
            id : [int]
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.get(**kwargs)

    def find_nexts(self, **kwargs):
        ''' retrieve Level2Producer records from database

        :param kwargs: Parameter dictionary, key items support:
            id : [int]
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.find_nexts(**kwargs)

    def find_start(self, **kwargs):
        ''' retrieve Level2Producer records from database

        :param kwargs: Parameter dictionary, key items support:
            key : [str]
        
        :returns: csst_dfs_common.models.Result
        '''
Wei Shoulin's avatar
Wei Shoulin committed
66
        return self.stub.find_start(**kwargs)
Wei Shoulin's avatar
brick  
Wei Shoulin committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

    def update(self, **kwargs):
        ''' update a Level2Producer
        
        :param kwargs: Parameter dictionary, key items support:
            id : [int]\n
            name = [str]\n
            gitlink = [str]\n
            paramfiles = [str]\n
            priority = [int]\n
            pre_producers = list[int]
        
        :returns: csst_dfs_common.models.Result            
        '''        
        return self.stub.update(**kwargs)    

    def delete(self, **kwargs):
        ''' delete a Level2Producer data
 
        :param kwargs: Parameter dictionary, key items support:
            id = [int]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.delete(**kwargs)

    def new_job(self, **kwargs):
        ''' new a Level2Producer Job
 
        :param kwargs: Parameter dictionary, key items support:
Wei Shoulin's avatar
Wei Shoulin committed
97
            name = [str]
Wei Shoulin's avatar
brick  
Wei Shoulin committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            dag = [str]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.new_job(**kwargs)

    def get_job(self, **kwargs):
        '''  fetch a record from database

        :param kwargs: Parameter dictionary, key items support:
            id : [int]
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.get_job(**kwargs)

    def update_job(self, **kwargs):
        ''' update a Level2Producer Job
 
        :param kwargs: Parameter dictionary, key items support:
            id = [int]
Wei Shoulin's avatar
Wei Shoulin committed
119
            name = [str]
Wei Shoulin's avatar
brick  
Wei Shoulin committed
120
121
122
123
124
            dag = [str]
            status = [int]
        
        :returns: csst_dfs_common.models.Result
        '''  
Wei Shoulin's avatar
Wei Shoulin committed
125
        return self.stub.update_job(**kwargs)
Wei Shoulin's avatar
brick  
Wei Shoulin committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

    def new_running(self, **kwargs):
        ''' insert a Level2ProducerRuningRecord data
 
        :param kwargs: Parameter dictionary, key items support:
            job_id = [int]\n
            producer_id = [int]\n
            brick_id = [int]\n
            start_time = [str]\n
            end_time = [str]\n
            prc_status = [int]\n
            prc_result = [str]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.new_running(**kwargs)   

    def get_running(self, **kwargs):
        '''  fetch a record from database

        :param kwargs: Parameter dictionary, key items support:
            id : [int]
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.get_running(**kwargs)

    def update_running(self, **kwargs):
        ''' udpate a Level2ProducerRuningRecord data
 
        :param kwargs: Parameter dictionary, key items support:
            id = [int]\n
            job_id = [int]\n
            producer_id = [int]\n
            brick_id = [int]\n
            start_time = [str]\n
            end_time = [str]\n
            prc_status = [int]\n
            prc_result = [str]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.update_running(**kwargs)

    def find_running(self, **kwargs):
        ''' find Level2ProducerRuningRecord data
 
        :param kwargs: Parameter dictionary, key items support:
            job_id = [int]\n
            producer_id = [int]\n
            brick_id = [int]\n
            prc_status = [int]\n
            create_time : (start, end)\n
            limit = [int]
        
        :returns: csst_dfs_common.models.Result
        '''  
        return self.stub.find_running(**kwargs)
Wei Shoulin's avatar
graph    
Wei Shoulin committed
184
185
186
187
188
189
190
191
192
193
194
195

    def make_graph(self, start_producer_id, fig_path = None):
        start_node = self.get(id = start_producer_id)
        if not start_node.success:
            raise CSSTGenericException("start node not found")

        def get_next(pre_node, node_level_x, node_level_y):
            the_nodes = self.find_nexts(id = pre_node.id)
            graph_name_edges = [(pre_node.name, n.name) for n in the_nodes.data]
            graph_id_edges = [(pre_node.id, n.id) for n in the_nodes.data]
            pos = {pre_node.name: (node_level_x,node_level_y)}
            for idx, node in enumerate(the_nodes.data):
Wei Shoulin's avatar
Wei Shoulin committed
196
197
                sub_id_edges, sub_name_edges, sub_pos = get_next(node, node_level_x+1, random.randint(-3,3))

Wei Shoulin's avatar
graph    
Wei Shoulin committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                graph_id_edges.extend(sub_id_edges)
                graph_name_edges.extend(sub_name_edges)
                pos.update(sub_pos)
            return graph_id_edges, graph_name_edges, pos

        graph_id_edges, graph_name_edges, pos = get_next(start_node.data, 0, 0)
        if fig_path:
            import networkx as nx
            from matplotlib import pyplot as plt
            g1 = nx.DiGraph()
            vertex_list = list(set([str(i) for e in graph_name_edges for i in e]))
            g1.add_nodes_from(vertex_list)
            g1.add_edges_from(graph_name_edges)
            plt.xlim(-1, 8)                     
Wei Shoulin's avatar
Wei Shoulin committed
212
            plt.ylim(-4, 4)             
Wei Shoulin's avatar
graph    
Wei Shoulin committed
213
214
215
216
217
218
            plt.tight_layout()
            nx.draw(
                    g1,
                    pos = pos,                    
                    node_color = 'orange',
                    edge_color = 'black',
Wei Shoulin's avatar
Wei Shoulin committed
219
220
                    font_size =12, 
                    node_size =360,
Wei Shoulin's avatar
graph    
Wei Shoulin committed
221
222
223
224
225
226
                    with_labels=True
                )

            plt.savefig(fig_path, format="PNG")
            plt.clf()
        return graph_id_edges