Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add water quality data to site model and use it to power site filter #1076

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/api/src/sites/sites.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export class Site {

maskedSpotterApiToken?: string;

waterQuality?: string[];

@Expose()
get applied(): boolean {
return !!this.siteApplication?.permitRequirements;
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/sites/sites.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
filterMetricDataByDate,
getConflictingExclusionDates,
hasHoboDataSubQuery,
hasWaterQualityDataSubQuery,
getLatestData,
getSite,
createSite,
Expand Down Expand Up @@ -198,11 +199,16 @@ export class SitesService {

const hasHoboDataSet = await hasHoboDataSubQuery(this.sourceRepository);

const waterQualityDataSet = await hasWaterQualityDataSubQuery(
this.latestDataRepository,
);

return res.map((site) => ({
...site,
applied: site.applied,
collectionData: mappedSiteData[site.id],
hasHobo: hasHoboDataSet.has(site.id),
waterQuality: waterQualityDataSet.get(site.id),
}));
}

Expand Down
48 changes: 47 additions & 1 deletion packages/api/src/utils/site.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
NotFoundException,
} from '@nestjs/common';
import { ObjectLiteral, Repository } from 'typeorm';
import { mapValues, some } from 'lodash';
import { groupBy, mapValues, some, camelCase } from 'lodash';
import geoTz from 'geo-tz';
import { Region } from '../regions/regions.entity';
import { ExclusionDates } from '../sites/exclusion-dates.entity';
Expand Down Expand Up @@ -278,6 +278,52 @@ export const hasHoboDataSubQuery = async (
return hasHoboDataSet;
};

export const hasWaterQualityDataSubQuery = async (
latestDataRepository: Repository<LatestData>,
): Promise<Map<number, string[]>> => {
const latestData: LatestData[] = await latestDataRepository
.createQueryBuilder('water_quality_data')
.select('site_id', 'siteId')
.addSelect('metric')
.addSelect('source')
.where(`source != '${SourceType.HOBO}'`)
.getRawMany();

const sondeMetrics = [
'odoConcentration',
'cholorophyllConcentration',
'ph',
'salinity',
'turbidity',
];

const waterQualityDataSet = new Map<number, string[]>();

Object.entries(groupBy(latestData, (o) => o.siteId)).forEach(
([siteId, data]) => {
let sondeMetricsCount = 0;
const id = Number(siteId);
waterQualityDataSet.set(id, []);
data.forEach((siteData) => {
if (siteData.source === 'hui') {
// eslint-disable-next-line fp/no-mutating-methods
waterQualityDataSet.get(id)!.push('hui');
}
if (sondeMetrics.includes(camelCase(siteData.metric))) {
// eslint-disable-next-line fp/no-mutation
sondeMetricsCount += 1;
if (sondeMetricsCount >= 3) {
// eslint-disable-next-line fp/no-mutating-methods
waterQualityDataSet.get(id)!.push('sonde');
}
}
});
},
);

return waterQualityDataSet;
};

export const getLatestData = async (
site: Site,
latestDataRepository: Repository<LatestData>,
Expand Down
2 changes: 2 additions & 0 deletions packages/website/src/helpers/siteUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export const sitesFilterFn = (
return hasDeployedSpotter(s);
case 'HOBO loggers':
return s?.hasHobo;
case 'Water quality':
return s?.waterQuality?.length;
default:
console.error(`Unhandled Option: ${filter}`);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ exports[`SiteTable should render with given state from Redux store 1`] = `
HOBO loggers
</mock-typography>
</mock-menuitem>
<mock-menuitem
data-value="Water quality"
role="option"
selected="false"
>
<mock-typography
style="color: black;"
>
Water quality
</mock-typography>
</mock-menuitem>
</mock-menu>
</div>
</mock-box>
Expand Down
2 changes: 2 additions & 0 deletions packages/website/src/store/Sites/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export interface Site {
contactInformation?: string;
maskedSpotterApiToken?: string;
iframe?: string | null;
waterQuality?: string[];
}

export interface SiteSketchFab {
Expand Down Expand Up @@ -456,4 +457,5 @@ export const siteOptions = [
'Live streams',
'3D Models',
'HOBO loggers',
'Water quality',
] as const;
Loading