jobs list: filter by purged attribute
[qcg-portal.git] / qcg / forms.py
1 # coding=utf-8
2 from django import forms
3 from django.core.validators import RegexValidator
4 from django.template.defaultfilters import capfirst
5 from pyqcg.utils import TaskStatus
6
7 from qcg.fields import TimeRangeField
8 from qcg.models import Task, Allocation
9
10
11 date_range_validator = RegexValidator(r'[0-9]{2}\.[0-9]{2}\.[0-9]{4} - [0-9]{2}\.[0-9]{2}\.[0-9]{4}')
12 nodes_validator = RegexValidator(r'^[0-9]{1,3}:[0-9]{1,2}(:[0-9]{1,2})?$')
13 env_name_validator = RegexValidator(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
14
15 CHOICES_PLACEHOLDER = (None, '')
16
17
18 class FiltersForm(forms.Form):
19     ACTIVE, FINISHED, FAILED = range(3)
20     STATUS_CHOICES = (
21         (ACTIVE, u"Aktywne"),
22         (FINISHED, u"Zakończone"),
23         (FAILED, u"Niepowodzenia"),
24     )
25
26     STATUS_MAP = {
27         ACTIVE: (
28             Task.STATUS_CHOICES_REVERSED[TaskStatus.UNSUBMITTED],
29             Task.STATUS_CHOICES_REVERSED[TaskStatus.UNCOMMITTED],
30             Task.STATUS_CHOICES_REVERSED[TaskStatus.QUEUED],
31             Task.STATUS_CHOICES_REVERSED[TaskStatus.PREPROCESSING],
32             Task.STATUS_CHOICES_REVERSED[TaskStatus.PENDING],
33             Task.STATUS_CHOICES_REVERSED[TaskStatus.RUNNING],
34             Task.STATUS_CHOICES_REVERSED[TaskStatus.STOPPED],
35             Task.STATUS_CHOICES_REVERSED[TaskStatus.POSTPROCESSING],
36         ),
37         FINISHED: (
38             Task.STATUS_CHOICES_REVERSED[TaskStatus.FINISHED],
39         ),
40         FAILED: (
41             Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED],
42             Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED],
43         ),
44     }
45     STATUS_CHOICES_DICT = dict(STATUS_CHOICES)
46
47     keywords = forms.CharField(max_length=100, label=u"Wyszukaj frazę", required=False)
48     status = forms.MultipleChoiceField(choices=STATUS_CHOICES, label=u"Status", required=False,
49                                        widget=forms.CheckboxSelectMultiple)
50
51     # advanced
52     host = forms.MultipleChoiceField(label=u"Host", required=False, widget=forms.CheckboxSelectMultiple)
53     purged = forms.TypedChoiceField(label=u"Istniejący katalog roboczy?", required=False, coerce=lambda x: bool(int(x)),
54                                     choices=((0, 'Tak'), (1, 'Nie')), widget=forms.RadioSelect)
55     submission = forms.CharField(label=u"Data zlecenia", validators=[date_range_validator], required=False)
56     finish = forms.CharField(label=u"Data zakończenia", validators=[date_range_validator], required=False)
57
58     def __init__(self, *args, **kwargs):
59         super(FiltersForm, self).__init__(*args, **kwargs)
60
61         self.fields['host'].choices = tuple(
62             (host, capfirst(host.split('.', 1)[0]))
63             for host in Allocation.objects.values_list('host_name', flat=True).order_by('host_name').distinct())
64
65
66 class JobDescriptionForm(forms.Form):
67     class Host(object):
68         GALERA = 'galera.task.gda.pl'
69         HYDRA = 'hydra.icm.edu.pl'
70         INULA = 'inula.man.poznan.pl'
71         MOSS = 'moss.man.poznan.pl'
72         NOVA = 'nova.wcss.wroc.pl'
73         REEF = 'reef.man.poznan.pl'
74         ZEUS = 'zeus.cyfronet.pl'
75
76         CHOICES = (
77             CHOICES_PLACEHOLDER,
78             (GALERA, u'Galera'),
79             (HYDRA, u'Hydra'),
80             (INULA, u'Inula'),
81             (MOSS, u'Moss'),
82             (NOVA, u'Supernova'),
83             (REEF, u'Reef'),
84             (ZEUS, u'Zeus'),
85         )
86
87     class Process(object):
88         NONE = ''
89         CMD = 'c'
90         SCRIPT = 's'
91
92         CHOICES = (
93             (NONE, u'Brak'),
94             (CMD, u'Polecenie'),
95             (SCRIPT, u'Skrypt'),
96         )
97
98     APPLICATION_CHOICES = (
99         CHOICES_PLACEHOLDER,
100         ('bash', 'BASH'),
101         ('gromacs/4.6.3', 'GROMACS 4.6.3'),
102         ('matlab', 'MATLAB'),
103         ('python', 'Python'),
104     )
105     QUEUE_CHOICES = (
106         CHOICES_PLACEHOLDER,
107         ('plgid', 'plgrid'),
108         ('plgid-long', 'plgrid-long'),
109         ('plgid-testing', 'plgrid-testing'),
110     )
111     MODULES_CHOICES = (
112         ('plgrid/apps/python', 'plgrid/apps/python'),
113         ('plgrid/apps/matlab', 'plgrid/apps/matlab'),
114     )
115     PROTOCOL_CHOICES = (
116         ('', u'Brak'),
117         ('mailto', u'E-mail'),
118         ('xmpp', u'XMPP'),
119     )
120
121     application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False)  # TODO choices
122     master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False)
123     executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False)
124     script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
125     arguments = forms.MultipleChoiceField(label=u"Argumenty", required=False)
126     note = forms.CharField(label=u"Opis", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
127     grant = forms.CharField(label=u"Grant", max_length=100, required=False)
128
129     hosts = forms.MultipleChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
130     properties = forms.CharField(label=u"Właściwości węzłów", required=False)
131     queue = forms.ChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
132     procs = forms.IntegerField(label=u"Liczba procesów", min_value=0, required=False)
133     nodes = forms.CharField(label=u"Topologia węzłów", max_length=10, validators=[nodes_validator], required=False)
134     wall_time = TimeRangeField(label=u"Wall time", required=False)
135     memory = forms.IntegerField(label=u"Pamięć (MB)", min_value=0, required=False)
136     memory_per_slot = forms.IntegerField(label=u"Pamięci per proces (MB)", min_value=0, required=False)
137     modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False)  # TODO choices
138     reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
139
140     input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False)
141     stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False)
142
143     monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False)
144     notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=PROTOCOL_CHOICES, required=False, initial='',
145                                     widget=forms.RadioSelect)
146     notify_address = forms.EmailField(label=u"Adres", required=False)
147     watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=PROTOCOL_CHOICES, required=False,
148                                           initial='', widget=forms.RadioSelect)
149     watch_output_address = forms.EmailField(label=u"Adres", required=False)
150     watch_output_pattern = forms.CharField(label=u"Wzorzec", max_length=500, required=False)
151
152     preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=Process.CHOICES, required=False,
153                                         initial=Process.NONE, widget=forms.RadioSelect)
154     preprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
155     preprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
156     postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=Process.CHOICES, required=False,
157                                          initial=Process.NONE, widget=forms.RadioSelect)
158     postprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
159     postprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
160     native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False)
161     persistent = forms.BooleanField(label=u"Trwałe", required=False)
162
163     def __init__(self, data=None, *args, **kwargs):
164         super(JobDescriptionForm, self).__init__(data, *args, **kwargs)
165
166         if data is not None:
167             # accept user defined choices
168             self.fields['queue'].choices += ((data.get('queue'), data.get('queue')), )
169             self.fields['arguments'].choices += ((v, v) for v in data.getlist('arguments'))
170             self.fields['native'].choices += ((v, v) for v in data.getlist('native'))
171             self.fields['stage_in'].choices += ((v, v) for v in data.getlist('stage_in'))
172
173     def clean(self):
174         data = super(JobDescriptionForm, self).clean()
175
176         if data['application'] and not data['master_file']:
177             self.add_error('master_file', u"W trybie uruchamiania aplikacji należy podać plik główny")
178
179         notify_type = data.get('notify_type')
180         data['notify'] = u'{}:{}'.format(notify_type, data['notify_address']) if notify_type else ''
181
182         wo_type = data.get('watch_output_type')
183         data['watch_output'] = u'{}:{}'.format(wo_type, data['watch_output_address']) if wo_type else ''
184
185         preprocess_type = data.get('preprocess_type')
186         if preprocess_type == self.Process.CMD:
187             data['preprocess'] = data['preprocess_cmd']
188         elif preprocess_type == self.Process.SCRIPT:
189             data['preprocess'] = data['preprocess_script']
190         else:
191             data['preprocess'] = ''
192
193         postprocess_type = data.get('postprocess_type')
194         if postprocess_type == self.Process.CMD:
195             data['postprocess'] = data['postprocess_cmd']
196         elif postprocess_type == self.Process.SCRIPT:
197             data['postprocess'] = data['postprocess_script']
198         else:
199             data['postprocess'] = ''
200
201         return data
202
203     def clean_application(self):
204         return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else ''
205
206     def clean_executable(self):
207         return self._gsiftp_suffix(self.cleaned_data['executable'])
208
209     def clean_master_file(self):
210         return self._gsiftp_suffix(self.cleaned_data['master_file'])
211
212     def clean_nodes(self):
213         return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else ''
214
215     def clean_input(self):
216         return self._gsiftp_suffix(self.cleaned_data['input'])
217
218     def clean_stage_in(self):
219         return ['gsiftp://' + item for item in self.cleaned_data['stage_in']]
220
221     def clean_preprocess_script(self):
222         return self._gsiftp_suffix(self.cleaned_data['preprocess_script'])
223
224     def clean_postprocess_script(self):
225         return self._gsiftp_suffix(self.cleaned_data['postprocess_script'])
226
227     @staticmethod
228     def _gsiftp_suffix(url):
229         return 'gsiftp://' + url if url else ''
230
231
232 class EnvForm(forms.Form):
233     name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator],
234                            widget=forms.TextInput(attrs={'placeholder': u'Nazwa'}))
235     value = forms.CharField(label=u"Wartość", max_length=500,
236                             widget=forms.TextInput(attrs={'placeholder': u'Wartość'}))
237
238
239 EnvFormSet = forms.formset_factory(EnvForm, can_delete=True, extra=0)
240
241
242 class ColumnsForm(forms.Form):
243     JOB_ID, DESCRIPTION, SUBMISSION, START, END, STATUS, HOST = range(7)
244     COLUMNS_CHOICES = (
245         (JOB_ID, u"Identyfikator zadania"),
246         (DESCRIPTION, u"Opis"),
247         (SUBMISSION, u"Wysłane"),
248         (START, u"Start"),
249         (END, u"Koniec"),
250         (STATUS, u"Status"),
251         (HOST, u"Host"),
252     )
253
254     columns = forms.MultipleChoiceField(choices=COLUMNS_CHOICES, initial=[k for k, v in COLUMNS_CHOICES[1:]],
255                                         label=u"Kolumny", required=False, widget=forms.CheckboxSelectMultiple)