Skip to content

Commit

Permalink
viewfulltable bug -- no data being returned. reworked backend SQL con…
Browse files Browse the repository at this point in the history
…dition and verified function on local dev server. Added missing handling for quadrat form file processing -- forgot to add censusquadrat handling to that API
  • Loading branch information
siddheshraze committed Oct 11, 2024
1 parent 5439c4f commit 9566975
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
4 changes: 2 additions & 2 deletions frontend/components/processors/processcensus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { runQuery, SpecialProcessingProps } from '@/components/processors/proces
import moment from 'moment';
import { createError, fetchPrimaryKey, handleUpsert } from '@/config/utils';
import { SpeciesResult, StemResult, TreeResult } from '@/config/sqlrdsdefinitions/taxonomies';
import { QuadratsResult } from '@/config/sqlrdsdefinitions/zones';
import { QuadratResult } from '@/config/sqlrdsdefinitions/zones';
import { CMAttributesResult, CoreMeasurementsResult } from '@/config/sqlrdsdefinitions/core';

export async function processCensus(props: Readonly<SpecialProcessingProps>): Promise<number | undefined> {
Expand All @@ -21,7 +21,7 @@ export async function processCensus(props: Readonly<SpecialProcessingProps>): Pr
const speciesID = await fetchPrimaryKey<SpeciesResult>(schema, 'species', { SpeciesCode: spcode }, connection, 'SpeciesID');

// Fetch quadrat
const quadratID = await fetchPrimaryKey<QuadratsResult>(schema, 'quadrats', { QuadratName: quadrat, PlotID: plotID }, connection, 'QuadratID');
const quadratID = await fetchPrimaryKey<QuadratResult>(schema, 'quadrats', { QuadratName: quadrat, PlotID: plotID }, connection, 'QuadratID');

if (tag) {
// Handle Tree Upsert
Expand Down
4 changes: 3 additions & 1 deletion frontend/components/processors/processormacros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { processSpecies } from '@/components/processors/processspecies';
import { processCensus } from '@/components/processors/processcensus';
import { PoolMonitor } from '@/config/poolmonitor';
import { processPersonnel } from '@/components/processors/processpersonnel';
import { processQuadrats } from '@/components/processors/processquadrats';

export interface SpecialProcessingProps {
connection: PoolConnection;
Expand Down Expand Up @@ -71,7 +72,8 @@ export const fileMappings: Record<string, FileMapping> = {
dimy: 'DimensionY',
dimensionunit: 'DimensionUnits',
quadratshape: 'QuadratShape'
}
},
specialProcessing: processQuadrats
},
// "subquadrats": "subquadrat, quadrat, dimx, dimy, xindex, yindex, unit, orderindex",
subquadrats: {
Expand Down
45 changes: 45 additions & 0 deletions frontend/components/processors/processquadrats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { SpecialProcessingProps } from '@/components/processors/processormacros';
import { createError, handleUpsert } from '@/config/utils';
import { CensusQuadratResult, QuadratResult } from '@/config/sqlrdsdefinitions/zones';

export async function processQuadrats(props: Readonly<SpecialProcessingProps>) {
const { connection, rowData, schema, censusID } = props;
if (!censusID) throw createError('CensusID missing', { censusID });

const { quadrat, startx, starty, coordinateunit, dimx, dimy, dimensionunit, area, areaunit, quadratshape } = rowData;

try {
await connection.beginTransaction();
const quadratsData = {
QuadratName: quadrat,
StartX: startx,
StartY: starty,
CoordinateUnit: coordinateunit,
DimensionX: dimx,
DimensionY: dimy,
DimensionUnit: dimensionunit,
Area: area,
AreaUnit: areaunit,
QuadratShape: quadratshape
};

const quadratID = await handleUpsert<QuadratResult>(connection, schema, 'quadrats', quadratsData, 'QuadratID');
if (!quadratID) throw createError('upsert failure for row: ', { quadratsData });

// need to update censusquadrat

const cqData = {
CensusID: censusID,
QuadratID: quadratID
};
const cqID = await handleUpsert<CensusQuadratResult>(connection, schema, 'censusquadrat', cqData, 'CQID');
if (!cqID) throw createError('upsert failure on censusquadrat for row: ', { cqData });

await connection.commit();
return quadratID;
} catch (error: any) {
await connection.rollback();
console.error('Upsert failed:', error.message);
throw createError('Upsert failed', { error });
}
}
4 changes: 2 additions & 2 deletions frontend/config/datamapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import moment from 'moment';
import { bitToBoolean, booleanToBit } from './macros';
import { Common, ResultType, Unique } from '@/config/utils';
import { SpeciesLimitsRDS, SpeciesLimitsResult, SpeciesRDS, SpeciesResult, StemRDS, StemResult } from '@/config/sqlrdsdefinitions/taxonomies';
import { PlotRDS, PlotsResult, QuadratRDS, QuadratsResult, SitesMapper } from '@/config/sqlrdsdefinitions/zones';
import { PlotRDS, PlotsResult, QuadratRDS, QuadratResult, SitesMapper } from '@/config/sqlrdsdefinitions/zones';
import {
AllTaxonomiesViewRDS,
AllTaxonomiesViewResult,
Expand Down Expand Up @@ -208,7 +208,7 @@ class MapperFactory {
case 'quadratpersonnel':
return new GenericMapper<QuadratPersonnelRDS, QuadratPersonnelResult>() as unknown as IDataMapper<RDS, Result>;
case 'quadrats':
return new GenericMapper<QuadratRDS, QuadratsResult>() as unknown as IDataMapper<RDS, Result>;
return new GenericMapper<QuadratRDS, QuadratResult>() as unknown as IDataMapper<RDS, Result>;
case 'sites':
return new SitesMapper() as any;
case 'species':
Expand Down
6 changes: 0 additions & 6 deletions frontend/config/sqlrdsdefinitions/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ export function getStemTaxonomiesViewHCs(): ColumnStates {
};
}

/**
* materialized view --> do this before batch updates to make sure that refresh function isn't called for each row
* INSERT INTO batchprocessingflag (flag_status) VALUES ('STARTED')
ON DUPLICATE KEY UPDATE flag_status = 'STARTED';
*/

export type ViewFullTableRDS = {
// datagrid
id?: number;
Expand Down
10 changes: 9 additions & 1 deletion frontend/config/sqlrdsdefinitions/zones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export type QuadratRDS = {
areaUnits?: string;
quadratShape?: string;
};
export type QuadratsResult = ResultType<QuadratRDS>;
export type QuadratResult = ResultType<QuadratRDS>;
export type Quadrat = QuadratRDS | undefined;
export const validateQuadratsRow: ValidationFunction = row => {
const errors: RowValidationErrors = {};
Expand All @@ -112,6 +112,14 @@ export function getQuadratHCs(): ColumnStates {
};
}

export type CensusQuadratRDS = {
id?: number;
cqID?: number;
quadratID?: number;
censusID?: number;
};
export type CensusQuadratResult = ResultType<CensusQuadratRDS>;

export type SubquadratRDS = {
id?: number;
subquadratID?: number;
Expand Down
30 changes: 18 additions & 12 deletions frontend/config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ export const toggleSidebar = () => {
export type CapitalizeFirstLetter<T extends string> = T extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : T;

// Utility type to transform 'dbh' and 'hom' to uppercase
export type TransformSpecialCases<T extends string> = T extends `${infer Prefix}dbh${infer Suffix}`
? `${Prefix}DBH${Suffix}`
: T extends `${infer Prefix}Dbh${infer Suffix}`
export type TransformSpecialCases<T extends string> = T extends `${infer Prefix}CqID${infer Suffix}`
? `${Prefix}CQID${Suffix}`
: T extends `${infer Prefix}dbh${infer Suffix}`
? `${Prefix}DBH${Suffix}`
: T extends `${infer Prefix}hom${infer Suffix}`
? `${Prefix}HOM${Suffix}`
: T extends `${infer Prefix}Hom${infer Suffix}`
: T extends `${infer Prefix}Dbh${infer Suffix}`
? `${Prefix}DBH${Suffix}`
: T extends `${infer Prefix}hom${infer Suffix}`
? `${Prefix}HOM${Suffix}`
: T extends `${infer Prefix}Id${infer Suffix}`
? `${Prefix}ID${Suffix}`
: T extends `${infer Prefix}cma${infer Suffix}`
? `${Prefix}CMA${Suffix}`
: T extends `${infer Prefix}Cma${infer Suffix}`
: T extends `${infer Prefix}Hom${infer Suffix}`
? `${Prefix}HOM${Suffix}`
: T extends `${infer Prefix}Id${infer Suffix}`
? `${Prefix}ID${Suffix}`
: T extends `${infer Prefix}cma${infer Suffix}`
? `${Prefix}CMA${Suffix}`
: T;
: T extends `${infer Prefix}Cma${infer Suffix}`
? `${Prefix}CMA${Suffix}`
: T extends `${infer Prefix}cqID${infer Suffix}`
? `${Prefix}CQID${Suffix}`
: T;

// Utility type to omit specific keys
export type OmitKey<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
Expand Down Expand Up @@ -224,6 +228,8 @@ export function transformSpecialCases(field: string): string {
return field.replace(/hom/gi, 'HOM');
} else if (/cma/i.test(field)) {
return field.replace(/cma/gi, 'CMA');
} else if (/cq/i.test(field)) {
return field.replace(/cq/gi, 'CQ');
}

// General transformation for ID
Expand Down

0 comments on commit 9566975

Please sign in to comment.