Skip to content

Commit

Permalink
feat: updated cross ref, and record compliance tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 21, 2023
1 parent 6d0025b commit 0f65f2a
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 76 deletions.
52 changes: 33 additions & 19 deletions app/backend/wells/serializers_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,35 +495,49 @@ class Meta:


class CrossReferencingSerializer(serializers.ModelSerializer):

work_start_date = serializers.DateField(read_only=True)
work_end_date = serializers.DateField(read_only=True)

class Meta:
model = Well
fields = [
'well_tag_number',
'work_type',
'well_status',
'work_start_date',
'created_by',
'created_date',
'updated_by',
'updated_date',
'natural_resource_region',
'notes',
'work_end_date',
'create_user',
'create_date',
'update_date',
'update_user',
# 'natural_resource_region',
'internal_comments',
]


class RecordComplianceSerializer(serializers.ModelSerializer):

work_start_date = serializers.DateField(read_only=True)
work_end_date = serializers.DateField(read_only=True)

class Meta:
model = Well
fields = [
'well_tag_number',
'work_type',
'work_start_date',
'lat',
'lon',
'widp',
'finished_well_depth',
'class_of_well',
'person_responsible_for_work',
'company_that_did_the_work',
'well_tag_number',
'identification_plate_number',
'well_class',
'latitude',
'longitude',
'finished_well_depth',
'diameter',
'surface_seal_depth',
'surface_seal_thickness',
'aquifer_lithology',
'well_status',
'work_start_date',
'work_end_date',
'person_responsible',
'company_of_person_responsible',
'create_date',
'create_user',
# 'natural_resource_region',
'internal_comments'
]
123 changes: 103 additions & 20 deletions app/backend/wells/views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.contrib.gis.gdal import GDALException
from django.db.models import FloatField, Q
from django.db.models.functions import Cast
from django.db.models import FloatField, Q, Case, When, F, Value, DateField

from rest_framework import status, filters
from rest_framework.exceptions import PermissionDenied, NotFound, ValidationError
Expand All @@ -38,7 +38,8 @@
GeometryFilterBackend,
RadiusFilterBackend
)
from wells.models import Well, WellAttachment
from wells.models import Well, WellAttachment, \
WELL_STATUS_CODE_ALTERATION, WELL_STATUS_CODE_CONSTRUCTION, WELL_STATUS_CODE_DECOMMISSION
from wells.serializers_v2 import (
WellLocationSerializerV2,
WellVerticalAquiferExtentSerializerV2,
Expand Down Expand Up @@ -602,40 +603,122 @@ def get(self, request, *args, **kwargs):
return Response(serializer.data)


class CrossReferencingListView(ListAPIView):
serializer_class = CrossReferencingSerializer
class RecordComplianceListView(ListAPIView):
serializer_class = RecordComplianceSerializer

swagger_schema = None
permission_classes = (WellsEditOrReadOnly,)
model = Well
pagination_class = APILimitOffsetPagination

# Allow searching on name fields, names of related companies, etc.
filter_backends = (WellListFilterBackend, BoundingBoxFilterBackend,
filters.SearchFilter, WellListOrderingFilter, GeometryFilterBackend)
ordering = ('well_tag_number',)

def get_queryset(self):
"""
Optionally restricts the returned wells to a given date range,
by filtering against `start_date` and `end_date` query parameters in the URL.
Retrieves wells that are missing information in any of the specified fields.
"""
queryset = Well.objects.all()
start_date = self.request.query_params.get('start_date')
end_date = self.request.query_params.get('end_date')

if start_date and end_date:
queryset = queryset.filter(created_date__range=[start_date, end_date])
queryset = Well.objects.select_related('well_status').annotate(
work_start_date=Case(
When(well_status__well_status_code=WELL_STATUS_CODE_CONSTRUCTION, then=F('construction_start_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_ALTERATION, then=F('alteration_start_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_DECOMMISSION, then=F('decommission_start_date')),
default=Value(None),
output_field=DateField()
),
work_end_date=Case(
When(well_status__well_status_code=WELL_STATUS_CODE_CONSTRUCTION, then=F('construction_end_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_ALTERATION, then=F('alteration_end_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_DECOMMISSION, then=F('decommission_end_date')),
default=Value(None),
output_field=DateField()
)
)

# Filtering for records missing any of the specified fields
missing_info_filter = (
Q(well_tag_number__isnull=True) |
Q(identification_plate_number__isnull=True) |
Q(well_class__isnull=True) |
Q(geom__isnull=True) | # for latitude and longitude
Q(finished_well_depth__isnull=True) |
Q(surface_seal_depth__isnull=True) |
Q(surface_seal_thickness__isnull=True) |
Q(aquifer_lithology__isnull=True) |
Q(well_status__isnull=True) |
Q(work_start_date__isnull=True) |
Q(work_end_date__isnull=True) |
Q(person_responsible__isnull=True) |
Q(company_of_person_responsible__isnull=True) |
Q(create_date__isnull=True) |
Q(create_user__isnull=True)
# Q(natural_resource_region__isnull=True) |
# Q(internal_comments__isnull=True)
)

queryset = queryset.filter(missing_info_filter)

# Additional filtering based on query parameters
work_start_date = self.request.query_params.get('work_start_date')
work_end_date = self.request.query_params.get('work_end_date')

if work_start_date:
queryset = queryset.filter(work_start_date__gte=work_start_date)
if work_end_date:
queryset = queryset.filter(work_end_date__lte=work_end_date)

return queryset


class RecordComplianceListView(ListAPIView):
serializer_class = RecordComplianceSerializer
class CrossReferencingListView(ListAPIView):
serializer_class = CrossReferencingSerializer

swagger_schema = None
permission_classes = (WellsEditOrReadOnly,)
model = Well
pagination_class = APILimitOffsetPagination

# Allow searching on name fields, names of related companies, etc.
filter_backends = (WellListFilterBackend, BoundingBoxFilterBackend,
filters.SearchFilter, WellListOrderingFilter, GeometryFilterBackend)
ordering = ('well_tag_number',)

def get_queryset(self):
"""
Retrieves wells and allows filtering for compliance checks.
Filters can include work type, date ranges, lat/lon presence, etc.
Optionally restricts the returned wells to those that have certain keywords like 'x-ref'd' or 'cross-ref'
in their internal_comments.
"""
queryset = Well.objects.all()
work_start_date = self.request.query_params.get('work_start_date')
has_issues = self.request.query_params.get('has_issues', None)

if work_start_date:
queryset = queryset.filter(work_start_date__gte=work_start_date)
queryset = Well.objects.select_related('well_status').annotate(
work_start_date=Case(
When(well_status__well_status_code=WELL_STATUS_CODE_CONSTRUCTION, then=F('construction_start_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_ALTERATION, then=F('alteration_start_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_DECOMMISSION, then=F('decommission_start_date')),
default=Value(None),
output_field=DateField()
),
work_end_date=Case(
When(well_status__well_status_code=WELL_STATUS_CODE_CONSTRUCTION, then=F('construction_end_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_ALTERATION, then=F('alteration_end_date')),
When(well_status__well_status_code=WELL_STATUS_CODE_DECOMMISSION, then=F('decommission_end_date')),
default=Value(None),
output_field=DateField()
)
)

search_terms = ["x-ref'd", "x-ref", "cross-ref"]

# Build a Q object for the search terms
comments_query = Q()
for term in search_terms:
comments_query |= Q(internal_comments__icontains=term)

if has_issues is not None:
queryset = queryset.filter(lat__isnull=True, lon__isnull=True) if has_issues.lower() == 'true' else queryset.exclude(lat__isnull=True, lon__isnull=True)
# Filter the queryset based on the search terms
queryset = queryset.filter(comments_query)

return queryset
54 changes: 51 additions & 3 deletions app/frontend/src/qaqc/components/QaQcTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@
<div id="qaqcResults">
<b-col sm="4"></b-col>
<b-row align-h="between" class="mb-2">
<b-col sm="4"></b-col>
<b-col sm="4">
<!-- Date Range Filter specifically for createDate -->
<div><b>Created Date Range</b></div>
<div :class="`qaqc-filters-${dateColumn.type}`">
<qaqc-filters
v-if="dateColumn"
:type="dateColumn.type"
:id="`${dateColumn.id}ResultFilter`"
:errors="resultErrors[dateColumn.param]"
:param-names="dateColumn.params"
:options="dateColumn.options || filterSelectOptions[dateColumn.id]"
:value="filterParams[dateColumn.id]"
:text-field="dateColumn.textField"
@input="applyFilter(dateColumn, $event)" />
</div>
</b-col>
<b-col sm="4" class="form-inline">
Show <b-form-select class="mx-1" :value="limit" @input="setLimit($event)" :options="limitOptions" /> results
</b-col>
Expand All @@ -29,7 +44,7 @@
:key="column.id"
class="text-nowrap"
scope="col">
{{ column.resultLabel ? column.resultLabel : column.label }}
{{ columnLabels(column.id) }}
<b-button
class="sort-button px-0"
:class="{active: column.sortParam === orderingParam}"
Expand All @@ -42,6 +57,7 @@
<tr class="filters">
<th v-for="column in columns" :key="column.id" :class="`qaqc-filters-${column.type}`">
<qaqc-filters
v-if="!excludedFilterColumns.includes(column.id)"
:type="column.type"
:id="`${column.id}ResultFilter`"
:errors="resultErrors[column.param]"
Expand Down Expand Up @@ -211,6 +227,38 @@ export default {
},
isReset () {
return (!this.isBusy && this.results === null)
},
dateColumn () {
return this.columns.find(column => column.param === 'create_date')
},
excludedFilterColumns () {
return ['latitude', 'longitude']
},
columnLabels () {
// Define a mapping for updated labels
const labelMapping = {
'wellTagNumber': 'WTN',
'identificationPlateNumber': 'WIDP',
'wellClass': 'Class of well',
'latitude': 'Lat',
'longitude': 'Lon',
'finishedWellDepth': 'Finished well depth (feet)',
'diameter': 'Casing Diameter (inches)',
'surfaceSealDepth': 'Seal Depth (feet)',
'surfaceSealThickness': 'Seal Thickness (inches)',
'aquiferLithology': 'Lithology',
'wellStatus': 'Work Type',
'dateOfWork': 'Work Start Date',
'personResponsible': 'Person Responsible',
'orgResponsible': 'Company that did the work',
'createDate': 'Created Date',
'createUser': 'Created By',
'updateDate': 'Updated Date',
'updateUser': 'Updated By',
'internalOfficeComments': 'Internal Office Comments',
'internalComments': 'Notes'
}
return (columnId) => labelMapping[columnId] || columnId
}
},
methods: {
Expand All @@ -236,7 +284,7 @@ export default {
},
applyFilter ({ id }, values) {
this.filterParams[id] = values
const filterGroup = { ...this.qaqcQueryParams }
const filterGroup = { ...this.searchQueryParams }
this.$store.commit(SET_QAQC_RESULT_FILTERS, filterGroup)
this.$emit('filter-changed', filterGroup)
Expand Down
Loading

0 comments on commit 0f65f2a

Please sign in to comment.