Skip to content

Commit

Permalink
update modelů, trackovani casu, ..
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dvorak committed Apr 5, 2024
1 parent b433423 commit e0a3ba0
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 46 deletions.
25 changes: 14 additions & 11 deletions DOSPORTAL/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@ def __init__(self,*args, user=None, **kwargs):
# #})
# )

detector = forms.ModelChoiceField(
queryset=Detector.objects.all(),
widget=forms.Select(attrs={
'class': 'form-control',
}),
required=False,
label="Detector",
help_text="Select used detector. It is not mandatory in case of detectors with auto-detect feature."
)
# detector = forms.ModelChoiceField(
# queryset=Detector.objects.all(),
# widget=forms.Select(attrs={
# 'class': 'form-control',
# }),
# required=False,
# label="Detector",
# help_text="Select used detector. It is not mandatory in case of detectors with auto-detect feature."
# )

description = MarkdownxFormField(

label="Description",
help_text="Detailed description of the record; markdown supported.",
required=False,
)

record_type = forms.ChoiceField(
Expand All @@ -98,7 +100,8 @@ def __init__(self,*args, user=None, **kwargs):

class Meta:
model = Record
exclude = ("time_end", "measurement", "log_original_filename", "metadata", "duration", "time_start", "record_duration", "author", 'data_file')
exclude = ("time_end", "measurement", "log_original_filename", "metadata", "duration", "record_duration", "author", 'data_file',
"created", "detector")


class DetectorEditForm(forms.ModelForm):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.11 on 2024-04-05 02:23

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('DOSPORTAL', '0013_detectortype_url'),
]

operations = [
migrations.AlterField(
model_name='detector',
name='data',
field=models.JSONField(blank=True, default=[{}], help_text='Detector metadata, used for advanced data processing and maintaining', verbose_name='Detector metadata'),
),
migrations.AlterField(
model_name='record',
name='metadata',
field=models.JSONField(blank=True, default='[{}]', help_text='record metadata, used for advanced data processing and maintaining', verbose_name='record_metadata'),
),
migrations.AlterField(
model_name='record',
name='time_start',
field=models.DateTimeField(default=datetime.datetime(2000, 1, 1, 0, 0), null=True, verbose_name='Measurement beginning time'),
),
]
18 changes: 18 additions & 0 deletions DOSPORTAL/migrations/0015_record_time_tracked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-04-05 02:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('DOSPORTAL', '0014_alter_detector_data_alter_record_metadata_and_more'),
]

operations = [
migrations.AddField(
model_name='record',
name='time_tracked',
field=models.BooleanField(default=False, help_text="When time is tracked, 'time_start' must be filled out", verbose_name='Is time tracked?'),
),
]
19 changes: 19 additions & 0 deletions DOSPORTAL/migrations/0016_alter_record_time_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.11 on 2024-04-05 02:27

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('DOSPORTAL', '0015_record_time_tracked'),
]

operations = [
migrations.AlterField(
model_name='record',
name='time_start',
field=models.DateTimeField(blank=True, default=datetime.datetime(2000, 1, 1, 0, 0), null=True, verbose_name='Measurement beginning time'),
),
]
16 changes: 14 additions & 2 deletions DOSPORTAL/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
from typing import Iterable
from django.db import models
import uuid
Expand Down Expand Up @@ -324,7 +325,9 @@ class Detector(UUIDMixin):

data = models.JSONField(
_("Detector metadata"),
help_text="Detector metadata, used for advanced data processing and maintaining"
help_text="Detector metadata, used for advanced data processing and maintaining",
default=[{}],
blank=True
)

owner = models.ForeignKey(
Expand Down Expand Up @@ -532,9 +535,17 @@ def user_directory_path_data(instance, filename):
blank=True
)

time_tracked = models.BooleanField(
verbose_name = _("Is time tracked?"),
default = False,
help_text=_("When time is tracked, 'time_start' must be filled out")
)

time_start = models.DateTimeField(
verbose_name = _("Measurement beginning time"),
null=True,
blank=True,
default=datetime.datetime(2000, 1, 1, 0, 0, 0)
)

created = models.DateTimeField(
Expand Down Expand Up @@ -569,7 +580,8 @@ def user_directory_path_data(instance, filename):
metadata = models.JSONField(
_("record_metadata"),
help_text=_("record metadata, used for advanced data processing and maintaining"),
default='[{}]'
default='[{}]',
blank=True
)


Expand Down
9 changes: 7 additions & 2 deletions DOSPORTAL/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
'crispy_bootstrap5',
'django_gravatar',
'markdownx',

'guardian',
'prettyjson',
'organizations',
# 'organizations',
]

MARKDOWNX_MARKDOWN_EXTENSIONS = [
Expand Down Expand Up @@ -122,6 +122,11 @@
}


AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

Expand Down
31 changes: 20 additions & 11 deletions DOSPORTAL/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile, Record, SpectrumData
from .models import Profile, Record, SpectrumData, Detector
import json
import pandas as pd
import datetime
Expand Down Expand Up @@ -61,7 +61,7 @@ def save_record(sender, instance, created = None, **kwargs):
"fw-version": parts[2],
"fw-build_info": parts[5],
"fw-commit": parts[4],
'hw-sn': parts[5]
'hw-sn': parts[6].strip()
}

elif line.startswith("$DIG"):
Expand All @@ -71,7 +71,7 @@ def save_record(sender, instance, created = None, **kwargs):
"type": parts[0],
"hw-model": parts[1],
"hw-sn": parts[2],
'-': parts[3]
'eeprom': parts[3].strip()
}

elif line.startswith("$ADC"):
Expand All @@ -81,25 +81,34 @@ def save_record(sender, instance, created = None, **kwargs):
"type": parts[0],
"hw-model": parts[1],
"hw-sn": parts[2],
'-': parts[3]
'eeprom': parts[3].strip()
}

df = pd.read_csv(instance.log_file.path, sep = ',', header = None, names=range(max_size))
df_spectrum = pd.read_csv(instance.log_file.path, sep = ',', header = None, names=range(max_size))

df = df [df[0] == '$HIST']
df = df.drop(columns=[0, 1, 3, 4, 5, 6, 7, 8])
df_spectrum = df_spectrum [df_spectrum[0] == '$HIST']
df_spectrum = df_spectrum.drop(columns=[0, 1, 3, 4, 5, 6, 7, 8])

new_columns = ['time'] + list(range(df.shape[1] - 1))
df.columns = new_columns
new_columns = ['time'] + list(range(df_spectrum.shape[1] - 1))
df_spectrum.columns = new_columns

duration = df['time'].max()
duration = df_spectrum['time'].max()
instance.record_duration = datetime.timedelta(seconds=float(duration))

new_name = instance.user_directory_path_data('pk')
df.to_pickle('data/media/'+new_name)
df_spectrum.to_pickle('data/media/'+new_name)

instance.data_file.name = new_name

try:
sn = metadata['log_device_info']['DOS']['hw-sn']
print("Traying to find detector with SN", sn)
det = Detector.objects.get(sn=sn)
print(det)
instance.detector = det
except Exception as e:
print(e)

print(instance.data_file)


Expand Down
29 changes: 17 additions & 12 deletions DOSPORTAL/templates/records/record_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
</tr>
<tr>
<td class="w-auto">Record start time:</td>
<td class="">{{ record.time_start }}</td>
{% if record.time_tracked %}
<td class="">{{ record.time_start }}</td>
{% else %}
<td class="text-muted text-small">Time is not tracked for this record</td>
{% endif %}
</tr>
<tr>
<td class="w-auto">Record duration:</td>
Expand All @@ -44,21 +48,21 @@
<td class="">{{ record.get_record_type_display }}</td>
</tr>
<tr>
<td class="w-auto">Author:</td>
<td class="">{{record.author}} on behalf of {{ record.belongs }}, Log is {{record.get_data_policy_display}}, {{record.author}}</td>
<td class="w-auto">Author/Organization:</td>
<td class=""> <a class="btn btn-sm btn-outline-info" href="/user/{{record.author.username}}"> {{record.author}} </a> <a class="btn btn-sm btn-outline-success" href="{{record.belongs.get_absolute_url}}">{{ record.belongs }} </a> </td>
</tr>
<tr>
<td class="w-auto">Log file:</td>
<td class="">{{record.log_original_filename}} ({{ record.log_file | filesize_mb }})</td>
</tr>
{% if record.metadata|length > 4 %} <tr>
{% if record.description|length > 1 %}<tr>
<td class="w-auto">Description:</td>
<td class=""><div class="callout m-0 p-2">{{record.formatted_markdown | safe }}</div></td>
</tr> {% endif %}
</tr>
{% if record.metadata|length > 4 %} <tr>
<td class="w-auto">Advanced metadata:</td>
<td class="callout"><code>{{record.metadata}})</code></td>
</tr> {% endif %}
{% if record.description|length > 1 %}<tr>
<td class="w-auto">Description:</td>
<td class="callout">{{record.formatted_markdown | safe }}</td>
</tr> {% endif %}
<td class=""><div class="callout m-0 p-2" style="max-height: 200pt;overflow-y: auto;"><code>{{record.metadata}})</code></div></td>
</tr> {% endif %}
</tbody>
</table>

Expand Down Expand Up @@ -164,7 +168,8 @@ <h3>Energetic spectrogram:</h3>
}),
],
xAxis: [{
type: 'time',
{% if record.time_tracked %} type: 'time', {% else %} type: 'value', {% endif %}

// min: 'dataMin',
//max: 'dataMax'
}, {
Expand Down
17 changes: 10 additions & 7 deletions DOSPORTAL/views_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def render_row_number(self, record):
return format_html('<a href="{}">{}</a>', reverse('record-view', args=[record.pk]), next(self.row_number))



def RecordsListView(request):
table = RecordTable(Record.objects.all(),
table = RecordTable(Record.objects.all(), order_by = 'created',
template_name="django_tables2/bootstrap5-responsive.html")

table.paginate(page=request.GET.get("page", 1), per_page=25)
Expand Down Expand Up @@ -221,22 +220,26 @@ def GetEvolution(request, pk):

df['time'] = df['time'].astype(float)

start_time = record[0].time_start.timestamp()*1000
if record[0].time_tracked:
start_time = record[0].time_start.timestamp()*1000
else:
start_time = 0

print(start_time)

if not (minTime != 'nan' and maxTime != 'nan'):
minTime = (float(minTime)-start_time)*1000
maxTime = (float(maxTime)-start_time)*1000
minTime = (float(minTime)/1000-start_time)
maxTime = (float(maxTime)/1000-start_time)
df = df[(df['time'] >= minTime) & (df['time'] <= maxTime)]

time = df['time'].astype(float).add(start_time)*1000
time = df['time'].astype(float).mul(1000).add(start_time)
sums = df.drop('time', axis=1).sum(axis=1)

data = pd.DataFrame({'time': time, 'value': sums})
data_list = data[['time', 'value']].apply(tuple, axis=1).tolist()


return JsonResponse({'evolution_values': data_list})
return JsonResponse({'evolution_values': data_list, 'time_tracked': record[0].time_tracked})

def GetHistogram(request, pk):

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ gpxpy

django-crispy-forms
crispy-bootstrap5
django-organizations
#django-organizations
django-guardian
django-gravatar2
django-prettyjson

Expand Down

0 comments on commit e0a3ba0

Please sign in to comment.