Skip to content

Commit

Permalink
Fix NaN in query results
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Apr 24, 2018
1 parent 13756fa commit 9b879e9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions daiquiri/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ipaddress
import importlib
import math
import re

from django import forms
Expand Down Expand Up @@ -169,6 +170,16 @@ def make_query_dict_upper_case(input_dict):
return output_dict


def replace_nan(elements):
for i, element in enumerate(elements):
if isinstance(element, (list, tuple)):
replace_nan(element)
else:
if math.isnan(element):
print('-->', elements[i])
elements[i] = None


def send_mail(request, template_prefix, context, to_emails, cc_emails=[], bcc_emails=[]):
'''
This is heavily inspired by allauth.account.adapter.
Expand Down
6 changes: 3 additions & 3 deletions daiquiri/query/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
TokenAuthentication
)

from django_filters.rest_framework import DjangoFilterBackend

from daiquiri.core.viewsets import ChoicesViewSet, RowViewSetMixin
from daiquiri.core.permissions import HasModelPermission
from daiquiri.core.paginations import ListPagination
from daiquiri.core.utils import get_client_ip
from daiquiri.core.utils import get_client_ip, replace_nan
from daiquiri.jobs.viewsets import SyncJobViewSet, AsyncJobViewSet

from .models import QueryJob, DownloadJob, QueryArchiveJob, Example
Expand Down Expand Up @@ -171,6 +169,8 @@ def rows(self, request, pk=None):
# get the count and the rows from the job
count, results = job.rows(column_names, ordering, page, page_size, search, filters)

replace_nan(results)

# return ordered dict to be send as json
return Response(OrderedDict((
('count', count),
Expand Down
2 changes: 1 addition & 1 deletion testing/sql/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ GRANT SELECT ON ALL TABLES IN SCHEMA daiquiri_data_obs TO daiquiri_data;
INSERT INTO daiquiri_data_test.test VALUES
(1, TRUE, '{1, 2, 3, 4, 5}', '{{1, 10}, {0, 0.1}}'),
(2, TRUE, '{2, 4, 6, 8, 10}', '{{2, 20}, {0, 0.2}}'),
(3, FALSE, '{3, 6, 9, 12, 15}', '{{3, 30}, {0, 0.3}}');
(3, FALSE, '{3, 6, Nan, 12, 15}', '{{3, 30}, {0, 0.3}}');

INSERT INTO daiquiri_archive.files VALUES
('074fec76-6143-4d58-a1f6-a7e8c23af15e', '2017-10-01 00:00:00', 'image_01.jpg', 'c01', 'images/image_01.jpg', 5.810915172365783, -27.665464907831577),
Expand Down

0 comments on commit 9b879e9

Please sign in to comment.