Skip to content

Commit

Permalink
Merge branch 'hotfix-fixing-census-quadrat-relation' into forestgeo-a…
Browse files Browse the repository at this point in the history
…pp-baobab
  • Loading branch information
siddheshraze committed Oct 11, 2024
2 parents fb3bf0c + 9566975 commit 1d099bc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 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 });
}
}
24 changes: 6 additions & 18 deletions frontend/config/datamapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ import { bitToBoolean, booleanToBit } from './macros';
import { Common, ResultType, Unique } from '@/config/utils';
import {
FamilyRDS,
FamilyResult,
GenusRDS,
GenusResult,
ReferenceRDS,
ReferenceResult,
SpeciesInventoryRDS,
SpeciesInventoryResult,
FamilyResult, GenusRDS, GenusResult, ReferenceRDS, ReferenceResult, SpeciesInventoryRDS, SpeciesInventoryResult,
SpeciesLimitsRDS,
SpeciesLimitsResult,
SpeciesRDS,
SpeciesResult,
SpecimensRDS,
SpecimensResult,
SpeciesResult, SpecimensRDS, SpecimensResult,
StemRDS,
StemResult
} from '@/config/sqlrdsdefinitions/taxonomies';
import { PlotRDS, PlotsResult, QuadratRDS, QuadratsResult, SitesMapper, SubquadratRDS, SubquadratResult } from '@/config/sqlrdsdefinitions/zones';
import { PlotRDS, PlotsResult, QuadratRDS, QuadratResult, SitesMapper } from '@/config/sqlrdsdefinitions/zones';
import {
AllTaxonomiesViewRDS,
AllTaxonomiesViewResult,
MeasurementsSummaryRDS,
MeasurementsSummaryResult,
MeasurementsSummaryStagingRDS,
MeasurementsSummaryStagingResult,
StemTaxonomiesViewRDS,
StemTaxonomiesViewResult,
ViewFullTableRDS,
Expand Down Expand Up @@ -207,8 +197,8 @@ class MapperFactory {
return new GenericMapper<CMVErrorRDS, CMVErrorResult>() as unknown as IDataMapper<RDS, Result>;
case 'cmattributes':
return new GenericMapper<CMAttributesRDS, CMAttributesResult>() as unknown as IDataMapper<RDS, Result>;
case 'measurementssummary_staging':
return new GenericMapper<MeasurementsSummaryStagingRDS, MeasurementsSummaryStagingResult>() as unknown as IDataMapper<RDS, Result>;
case 'measurementssummary_draft':
return new GenericMapper<MeasurementsSummaryDraftRDS, MeasurementsSummaryDraftResult>() as unknown as IDataMapper<RDS, Result>;
case 'measurementssummary':
case 'measurementssummaryview':
return new GenericMapper<MeasurementsSummaryRDS, MeasurementsSummaryResult>() as unknown as IDataMapper<RDS, Result>;
Expand All @@ -226,7 +216,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 'family':
Expand All @@ -247,8 +237,6 @@ class MapperFactory {
return new GenericMapper<StemTaxonomiesViewRDS, StemTaxonomiesViewResult>() as unknown as IDataMapper<RDS, Result>;
case 'stems':
return new GenericMapper<StemRDS, StemResult>() as unknown as IDataMapper<RDS, Result>;
case 'subquadrats':
return new GenericMapper<SubquadratRDS, SubquadratResult>() as unknown as IDataMapper<RDS, Result>;
case 'unifiedchangelog':
return new GenericMapper<UnifiedChangelogRDS, UnifiedChangelogResult>() as unknown as IDataMapper<RDS, Result>;
case 'validationchangelog':
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 @@ -138,12 +138,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 @@ -240,6 +244,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 1d099bc

Please sign in to comment.