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
7 from qcg.fields import TimeRangeField
8 from qcg.models import Task, Allocation, JobTemplate
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_]*$')
15 CHOICES_PLACEHOLDER = (None, '')
18 class FiltersForm(forms.Form):
19 ACTIVE, FINISHED, FAILED = range(3)
22 (FINISHED, u"Zakończone"),
23 (FAILED, u"Niepowodzenia"),
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],
38 Task.STATUS_CHOICES_REVERSED[TaskStatus.FINISHED],
41 Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED],
42 Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED],
45 STATUS_CHOICES_DICT = dict(STATUS_CHOICES)
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)
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)
58 def __init__(self, *args, **kwargs):
59 super(FiltersForm, self).__init__(*args, **kwargs)
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())
66 class JobDescriptionForm(forms.Form):
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'
81 class Process(object):
92 APPLICATION_CHOICES = (
100 ('plgrid', 'plgrid'),
101 ('plgrid-long', 'plgrid-long'),
102 ('plgrid-testing', 'plgrid-testing'),
105 ('plgrid/apps/python', 'plgrid/apps/python'),
106 ('plgrid/apps/matlab', 'plgrid/apps/matlab'),
110 ('mailto', u'E-mail'),
114 FORCE_FIELD_CHOICES = (
116 ('E0LL2Y', u'E0LL2Y'),
119 nstep = forms.IntegerField(label=u"NSTEP", help_text=u"Liczba kroków w trajektorii", min_value=1, initial=500000, required=False)
120 ntwe = forms.IntegerField(label=u"NTWE", help_text=u"Częstość zapisu energii w krokach", min_value=0, initial=100, required=False)
121 ntwx = forms.IntegerField(label=u"NTWX", help_text=u"Częstość zapisu współrzędnych w krokach", min_value=0, initial=1000, required=False)
122 dt = forms.DecimalField(label=u"DT", help_text=u"Krok czasowy. Wartość kroku równa jedności to 48.9 fs", max_digits=5, decimal_places=2, min_value=0.01, initial=0.1, required=False)
123 damax = forms.DecimalField(label=u"DAMAX", help_text=u"Maksymalna dopuszczalna zmiana przyspieszenia podczas jednego kroku czasowego", max_digits=5, decimal_places=2, min_value=0.01, initial=1.0, required=False)
124 force_field = forms.ChoiceField(choices=FORCE_FIELD_CHOICES, label=u"Pole siłowe", required=False)
125 nrep = forms.IntegerField(label=u"NREP", help_text=u"Liczba replik", min_value=2, initial=16, required=False)
126 nstex = forms.IntegerField(label=u"NSTEX", help_text=u"Liczba kroków po których następuje wymiana replik", min_value=2, initial=1000, required=False)
127 pdb_file = forms.CharField(label=u"Plik PDB", max_length=500, required=False)
129 application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False) # TODO choices
130 master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False)
131 executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False)
132 script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
133 arguments = forms.MultipleChoiceField(label=u"Argumenty", required=False)
134 note = forms.CharField(label=u"Opis", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
135 grant = forms.CharField(label=u"Grant", max_length=100, required=False)
137 hosts = forms.MultipleChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
138 properties = forms.CharField(label=u"Właściwości węzłów", required=False)
139 queue = forms.ChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
140 procs = forms.IntegerField(label=u"Liczba procesów", min_value=0, required=False)
141 nodes = forms.CharField(label=u"Topologia węzłów", max_length=10, validators=[nodes_validator], required=False)
142 wall_time = TimeRangeField(label=u"Wall time", required=False)
143 memory = forms.IntegerField(label=u"Pamięć (MB)", min_value=0, required=False)
144 memory_per_slot = forms.IntegerField(label=u"Pamięci per proces (MB)", min_value=0, required=False)
145 modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False) # TODO choices
146 reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
148 input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False)
149 stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False)
151 monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False)
152 notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=PROTOCOL_CHOICES, required=False, initial='',
153 widget=forms.RadioSelect)
154 notify_address = forms.EmailField(label=u"Adres", required=False)
155 watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=PROTOCOL_CHOICES, required=False,
156 initial='', widget=forms.RadioSelect)
157 watch_output_address = forms.EmailField(label=u"Adres", required=False)
158 watch_output_pattern = forms.CharField(label=u"Wzorzec", max_length=500, required=False)
160 preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=Process.CHOICES, required=False,
161 initial=Process.NONE, widget=forms.RadioSelect)
162 preprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
163 preprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
164 postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=Process.CHOICES, required=False,
165 initial=Process.NONE, widget=forms.RadioSelect)
166 postprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
167 postprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
168 native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False)
169 persistent = forms.BooleanField(label=u"Trwałe", required=False)
171 def __init__(self, data=None, initial=None, *args, **kwargs):
172 super(JobDescriptionForm, self).__init__(data, initial=initial, *args, **kwargs)
175 self._init_user_choices('queue', data, initial)
176 self._init_user_choices('arguments', data, initial)
177 self._init_user_choices('native', data, initial)
178 self._init_user_choices('stage_in', data, initial)
181 data = super(JobDescriptionForm, self).clean()
183 if bool(data['application']) + bool(data['executable']) + bool(data['script']) != 1:
184 self.add_error(None, u"Dokładnie jedno z następujących pól jest wymagane do uruchomienia zadania: "
185 u"Aplikacja, Plik wykonywalny lub Skrypt.")
187 if data['application'] and not data['master_file']:
188 self.add_error('master_file', u"W trybie uruchamiania aplikacji należy podać plik główny")
190 if data['procs'] and data['nodes']:
191 self.add_error(None, u"Zdefiniuj tylko jedno z pól: liczbę procesów lub topologię węzłów")
193 notify_type = data.get('notify_type')
194 data['notify'] = u'{}:{}'.format(notify_type, data['notify_address']) if notify_type else ''
196 wo_type = data.get('watch_output_type')
197 data['watch_output'] = u'{}:{}'.format(wo_type, data['watch_output_address']) if wo_type else ''
199 preprocess_type = data.get('preprocess_type')
200 if preprocess_type == self.Process.CMD:
201 data['preprocess'] = data['preprocess_cmd']
202 elif preprocess_type == self.Process.SCRIPT:
203 data['preprocess'] = data['preprocess_script']
205 data['preprocess'] = ''
207 postprocess_type = data.get('postprocess_type')
208 if postprocess_type == self.Process.CMD:
209 data['postprocess'] = data['postprocess_cmd']
210 elif postprocess_type == self.Process.SCRIPT:
211 data['postprocess'] = data['postprocess_script']
213 data['postprocess'] = ''
217 def clean_application(self):
218 return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else ''
220 def clean_nodes(self):
221 return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else ''
223 def clean_executable(self):
224 return self._gsiftp_suffix(self.cleaned_data['executable'])
226 def clean_master_file(self):
227 return self._gsiftp_suffix(self.cleaned_data['master_file'])
229 def clean_input(self):
230 return self._gsiftp_suffix(self.cleaned_data['input'])
232 def clean_stage_in(self):
233 return [self._gsiftp_suffix(item) for item in self.cleaned_data['stage_in']]
235 def clean_preprocess_script(self):
236 return self._gsiftp_suffix(self.cleaned_data['preprocess_script'])
238 def clean_postprocess_script(self):
239 return self._gsiftp_suffix(self.cleaned_data['postprocess_script'])
241 def clean_pdb_file(self):
242 return self._gsiftp_suffix(self.cleaned_data['pdb_file'])
245 def _gsiftp_suffix(url):
247 return url if url.startswith('gsiftp://') else 'gsiftp://' + url
249 def _init_user_choices(self, name, data, initial):
250 initial = initial.get(name) if initial is not None else None
251 choices = data.getlist(name)[:] if data is not None else []
254 choices += initial if isinstance(initial, list) else [initial]
255 self.fields[name].initial = initial
258 self.fields[name].choices += ((v, v) for v in choices)
261 class EnvForm(forms.Form):
262 name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator],
263 widget=forms.TextInput(attrs={'placeholder': u'Nazwa'}))
264 value = forms.CharField(label=u"Wartość", max_length=500,
265 widget=forms.TextInput(attrs={'placeholder': u'Wartość'}))
268 EnvFormSet = forms.formset_factory(EnvForm, can_delete=True, extra=0)
271 class ColumnsForm(forms.Form):
272 JOB_ID, DESCRIPTION, SUBMISSION, START, END, STATUS, HOST = range(7)
274 (JOB_ID, u"Identyfikator zadania"),
275 (DESCRIPTION, u"Opis"),
276 (SUBMISSION, u"Wysłane"),
283 columns = forms.MultipleChoiceField(choices=COLUMNS_CHOICES, initial=[k for k, v in COLUMNS_CHOICES[1:]],
284 label=u"Kolumny", required=False, widget=forms.CheckboxSelectMultiple)
287 class JobTemplateForm(forms.ModelForm):