Skip to content
Snippets Groups Projects
Commit d1006586 authored by Nico Vermaas's avatar Nico Vermaas
Browse files

finish algorithm

add validation button to Validation page
parent 8d96bb67
No related branches found
No related tags found
2 merge requests!304update branch with master,!302automatic quality validation
Pipeline #51799 passed
...@@ -69,12 +69,22 @@ def convert_summary_to_list_for_template(task): ...@@ -69,12 +69,22 @@ def convert_summary_to_list_for_template(task):
def calculate_qualities(task): def calculate_qualities(task):
"""" """"
calculate the quality for this task, but also the quality for all the combined tasks of this sas_id calculate the quality for this task, but also the quality for all the combined tasks of this sas_id
""" """
# read the quality_thresholds from the Configuration table
try:
quality_thresholds = json.loads(Configuration.objects.get(key='quality_thresholds').value)
except:
quality_thresholds = {
"moderate": 20,
"poor": 50,
"overall_poor": 50,
"overall_good": 90,
}
def calculate_quality_task(task): def calculate_quality_task(task):
"""" """"
calculate the quality of this task based on rfi_percent values calculate the quality of this task based on rfi_percent values
...@@ -87,19 +97,9 @@ def calculate_qualities(task): ...@@ -87,19 +97,9 @@ def calculate_qualities(task):
except when rfi_percent = 0 except when rfi_percent = 0
""" """
try: try:
# read the quality_thresholds from the Configuration table
try:
quality_thresholds = json.loads(Configuration.objects.get(key='quality_thresholds').value)
except:
quality_thresholds = {
"moderate": 20,
"poor": 50,
"overall_poor": 50,
"overall_good": 90,
}
summary = task.quality_json["summary"] summary = task.quality_json["summary"]
quality = task.calculated_quality quality = None
for key in summary: for key in summary:
record = summary[key] record = summary[key]
...@@ -115,7 +115,7 @@ def calculate_qualities(task): ...@@ -115,7 +115,7 @@ def calculate_qualities(task):
except Exception as error: except Exception as error:
logger.info(error) logger.info(error)
def calculate_quality_sasid(task): def calculate_quality_sasid(unsaved_task):
""" """
calculate the overall quality per sas_id, based on other tasks with the same sas_id calculate the overall quality per sas_id, based on other tasks with the same sas_id
The threshold values are written from a configuration json blob The threshold values are written from a configuration json blob
...@@ -126,27 +126,31 @@ def calculate_qualities(task): ...@@ -126,27 +126,31 @@ def calculate_qualities(task):
otherwise is moderate. otherwise is moderate.
""" """
try: try:
# read the quality_thresholds from the Configuration table
try:
quality_thresholds = json.loads(Configuration.objects.get(key='quality_thresholds').value)
except:
quality_thresholds = {
"moderate": 20,
"poor": 50,
"overall_poor": 50,
"overall_good": 90,
}
# gather the results of all the calculated_quality values for this sas_id # gather the results of all the calculated_quality values for this sas_id
d = {'poor': 0, 'moderate': 0, 'good': 0} d = {'poor': 0, 'moderate': 0, 'good': 0}
for task in Task.objects.filter(sas_id=task.sas_id):
d[task.calculated_quality] = d[task.calculated_quality] + 1
quality_sasid = "moderate" for task in Task.objects.filter(sas_id=unsaved_task.sas_id):
# because this all happens in the overridden 'Task.save', the actual saving has not yet occurred
# So use the calculated quality from the unsaved task instead.
if task.id == unsaved_task.id:
t = unsaved_task
else:
t = task
try:
key = t.calculated_qualities['per_task']
d[key] = d[key] + 1
except:
# ignore the tasks that have no calculated quality (they are probably not 'stored').
pass
total = d['poor'] + d['moderate'] + d['good'] total = d['poor'] + d['moderate'] + d['good']
if total > 0: if total > 0:
percentage_poor = (d['poor'] / total) * 100 percentage_poor = (d['poor'] / total) * 100
percentage_good = (d['good'] / total) * 100 percentage_good = (d['good'] / total) * 100
quality_sasid = "moderate"
if percentage_poor >= quality_thresholds['overall_poor']: if percentage_poor >= quality_thresholds['overall_poor']:
quality_sasid = 'poor' quality_sasid = 'poor'
...@@ -160,22 +164,24 @@ def calculate_qualities(task): ...@@ -160,22 +164,24 @@ def calculate_qualities(task):
logger.info(error) logger.info(error)
# --- main function body --- # --- main function body ---
# calculate the quality for this task # calculate the quality for this task
calculated_quality_task = calculate_quality_task(task) calculated_quality_task = calculate_quality_task(task)
# update the overall quality of all tasks for this sas_id # store the result in task.calculated_qualities (not yet saved in the database)
calculated_quality_sasid = calculate_quality_sasid(task) d = task.calculated_qualities
if not d:
d = {}
d['per_task'] = calculated_quality_task
task.calculated_qualities = d
# combine both calculated qualities in the json field (dict) # update the overall quality of all tasks for this sas_id
d = task.calculated_qualities calculated_quality_sasid = calculate_quality_sasid(task)
if not d:
d = {}
d['per_task'] = calculated_quality_task # store the result in task.calculated_qualities (not yet saved in the database)
d['per_sasid'] = calculated_quality_sasid d['per_sasid'] = calculated_quality_sasid
return d return d
class Task(models.Model): class Task(models.Model):
......
...@@ -10,3 +10,7 @@ ...@@ -10,3 +10,7 @@
{% if task.sasid_is_verified %} {% if task.sasid_is_verified %}
<a href="{% url 'task-validate-view' task.pk 'good' 'validated' my_tasks.number %}" class="btn btn-success btn-sm" role="button"><i class="fas fa-check"></i> G</a> <a href="{% url 'task-validate-view' task.pk 'good' 'validated' my_tasks.number %}" class="btn btn-success btn-sm" role="button"><i class="fas fa-check"></i> G</a>
{% endif %} {% endif %}
{% if task.sasid_is_verified %}
<a href="{% url 'task-validate-view' task.pk 'calculated' 'validated' my_tasks.number %}" class="btn btn-success btn-sm" role="button"><i class="fas fa-check"></i> Validate</a>
{% endif %}
...@@ -1069,7 +1069,13 @@ def TaskSetStatus(request, pk, new_status, page=0): ...@@ -1069,7 +1069,13 @@ def TaskSetStatus(request, pk, new_status, page=0):
@login_required @login_required
def TaskValidate(request, pk, quality, new_status, page=0): def TaskValidate(request, pk, quality, new_status, page=0):
"""
find all tasks with the same SAS_ID of the given task (pk), and set its quality to all of them
This is used by the 'P/M/G/Validate' buttons on the Validation Page
There is one special 'quality', if its value is 'calculated' then use the calculated quality of the task.
Unless there is no calculated quality, then don't change the quality and just set the status to 'validated'
"""
task = Task.objects.get(pk=pk) task = Task.objects.get(pk=pk)
# find all tasks with the same SAS_ID, and set this quality to all of them # find all tasks with the same SAS_ID, and set this quality to all of them
...@@ -1077,6 +1083,14 @@ def TaskValidate(request, pk, quality, new_status, page=0): ...@@ -1077,6 +1083,14 @@ def TaskValidate(request, pk, quality, new_status, page=0):
tasks = Task.objects.filter(sas_id=sas_id) tasks = Task.objects.filter(sas_id=sas_id)
for task in tasks: for task in tasks:
if task.status == 'stored': if task.status == 'stored':
if quality == 'calculated':
try:
quality = task.calculated_qualities['per_sasid']
except:
# no calculated quality present, just the existing quality (so no change)
quality = task.quality
task.quality = quality task.quality = quality
task.new_status = new_status task.new_status = new_status
task.save() task.save()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment