Skip to content

Commit

Permalink
Add new tables
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea authored and alexeh committed Nov 2, 2024
1 parent 7af35a6 commit 111aebe
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 48 deletions.
6 changes: 6 additions & 0 deletions admin/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { MRV } from "@shared/entities/cost-inputs/mrv.entity.js";
import { ProjectSize } from "@shared/entities/cost-inputs/project-size.entity.js";
import { ValidationCost } from "@shared/entities/cost-inputs/validation.entity.js";
import { ImplementationLaborCost } from "@shared/entities/cost-inputs/implementation-labor-cost.entity.js";
import { BaseSize } from "@shared/entities/base-size.entity.js";
import { BaseIncrease } from "@shared/entities/base-increase.entity.js";
import { ModelAssumptions } from "@shared/entities/model-assumptions.entity.js";

// TODO: If we import the COMMON_DATABASE_ENTITIES from shared, we get an error where DataSouce is not set for a given entity
export const ADMINJS_ENTITIES = [
Expand Down Expand Up @@ -56,6 +59,9 @@ export const ADMINJS_ENTITIES = [
EcosystemExtent,
Project,
ImplementationLaborCost,
BaseSize,
BaseIncrease,
ModelAssumptions,
];

export const dataSource = new DataSource({
Expand Down
6 changes: 6 additions & 0 deletions admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import { SequestrationRateResource } from "./resources/sequestration-rate/seques
import { ProjectsResource } from "./resources/projects/projects.resource.js";
import { ProjectSizeResource } from "./resources/project-size/project-size.resource.js";
import { ImplementationLaborCostResource } from "./resources/implementation-labor-cost/implementation-labor-cost.resource.js";
import { BaseSizeResource } from "./resources/base-size/base-size.resource.js";
import { BaseIncreaseResource } from "./resources/base-increase/base-increase.resource.js";
import { ModelAssumptionResource } from "./resources/model-assumptions/model-assumptions.resource.js";

AdminJS.registerAdapter({
Database: AdminJSTypeorm.Database,
Expand Down Expand Up @@ -79,6 +82,9 @@ const start = async () => {
SequestrationRateResource,
ProjectsResource,
ImplementationLaborCostResource,
BaseSizeResource,
BaseIncreaseResource,
ModelAssumptionResource,
{
resource: Country,
name: "Country",
Expand Down
16 changes: 16 additions & 0 deletions admin/resources/base-increase/base-increase.resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ResourceWithOptions } from "adminjs";
import { BaseIncrease } from "@shared/entities/base-increase.entity.js";

export const BaseIncreaseResource: ResourceWithOptions = {
resource: BaseIncrease,
options: {
sort: {
sortBy: "ecosystem",
direction: "desc",
},
navigation: {
name: "Data Management",
icon: "Database",
},
},
};
16 changes: 16 additions & 0 deletions admin/resources/base-size/base-size.resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ResourceWithOptions } from "adminjs";
import { BaseSize } from "@shared/entities/base-size.entity.js";

export const BaseSizeResource: ResourceWithOptions = {
resource: BaseSize,
options: {
sort: {
sortBy: "ecosystem",
direction: "desc",
},
navigation: {
name: "Data Management",
icon: "Database",
},
},
};
16 changes: 16 additions & 0 deletions admin/resources/model-assumptions/model-assumptions.resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ResourceWithOptions } from "adminjs";
import { ModelAssumptions } from "@shared/entities/model-assumptions.entity.js";

export const ModelAssumptionResource: ResourceWithOptions = {
resource: ModelAssumptions,
options: {
sort: {
sortBy: "name",
direction: "desc",
},
navigation: {
name: "Data Management",
icon: "Database",
},
},
};
42 changes: 0 additions & 42 deletions api/src/modules/import/dtos/excel-base-data.dto.ts

This file was deleted.

15 changes: 15 additions & 0 deletions api/src/modules/import/dtos/excel-base-increase.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type ExcelBaseIncrease = {
ecosystem: string;
feasibility_analysis: number;
conservation_planning_and_admin: number;
data_collection_and_field_cost: number;
community_representation: number;
blue_carbon_project_planning: number;
establishing_carbon_rights: number;
financing_cost: number;
validation: number;
monitoring: number;
baseline_reassessment: number;
MRV: number;
long_term_project_operating_cost: number;
};
19 changes: 19 additions & 0 deletions api/src/modules/import/dtos/excel-base-size.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type ExcelBaseSize = {
activity: string;
ecosystem: string;
feasibility_analysis: number;
conservation_planning_and_admin: number;
data_collection_and_field_cost: number;
community_representation: number;
blue_carbon_project_planning: number;
establishing_carbon_rights: number;
financing_cost: number;
validation: number;
implementation_labor_planting: number;
implementation_labor_hybrid: number;
implementation_labor_hydrology: number;
monitoring: number;
baseline_reassessment: number;
MRV: number;
long_term_project_operating_cost: number;
};
5 changes: 5 additions & 0 deletions api/src/modules/import/dtos/excel-model-assumptions.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ExcelModelAssumptions = {
Assumptions: string;
Units: string;
Value: string;
};
11 changes: 11 additions & 0 deletions api/src/modules/import/import.repostiory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { MonitoringCost } from '@shared/entities/cost-inputs/monitoring.entity';
import { MRV } from '@shared/entities/cost-inputs/mrv.entity';
import { ValidationCost } from '@shared/entities/cost-inputs/validation.entity';
import { ImplementationLaborCost } from '@shared/entities/cost-inputs/implementation-labor-cost.entity';
import { BaseIncrease } from '@shared/entities/base-increase.entity';
import { BaseSize } from '@shared/entities/base-size.entity';
import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';

@Injectable()
export class ImportRepository {
Expand Down Expand Up @@ -54,6 +57,9 @@ export class ImportRepository {
sequestrationRate: SequestrationRate[];
emissionFactors: EmissionFactors[];
implementationLaborCost: ImplementationLaborCost[];
baseSize: BaseSize[];
baseIncrease: BaseIncrease[];
modelAssumptions: ModelAssumptions[];
}) {
return this.dataSource.transaction(async (manager) => {
await manager.save(importData.projects);
Expand Down Expand Up @@ -84,6 +90,11 @@ export class ImportRepository {
await manager.save(importData.restorableLand);
await manager.save(importData.sequestrationRate);
await manager.save(importData.emissionFactors);

// Other tables ingestion
await manager.save(importData.baseSize);
await manager.save(importData.baseIncrease);
await manager.save(importData.modelAssumptions);
});
}
}
122 changes: 120 additions & 2 deletions api/src/modules/import/services/entity.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ExcelEstablishingCarbonRights } from './../dtos/excel-establishing-carb
import { Injectable } from '@nestjs/common';

import { Country } from '@shared/entities/country.entity';
import { ExcelMasterTable } from '@api/modules/import/dtos/excel-base-data.dto';
import { Project } from '@shared/entities/projects.entity';
import { ExcelProjects } from '@api/modules/import/dtos/excel-projects.dto';
import { ExcelProjectSize } from '@api/modules/import/dtos/excel-project-size.dto';
Expand Down Expand Up @@ -64,6 +63,12 @@ import { ProjectSize } from '@shared/entities/cost-inputs/project-size.entity';
import { ValidationCost } from '@shared/entities/cost-inputs/validation.entity';
import { ExcelImplementationLaborCost } from '../dtos/excel-implementation-labor.dto';
import { ImplementationLaborCost } from '@shared/entities/cost-inputs/implementation-labor-cost.entity';
import { ExcelBaseSize } from '../dtos/excel-base-size.dto';
import { ExcelBaseIncrease } from '../dtos/excel-base-increase.dto';
import { ExcelModelAssumptions } from '../dtos/excel-model-assumptions.dto';
import { BaseSize } from '@shared/entities/base-size.entity';
import { BaseIncrease } from '@shared/entities/base-increase.entity';
import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';

export type ParsedDBEntities = {
projects: Project[];
Expand All @@ -90,12 +95,14 @@ export type ParsedDBEntities = {
sequestrationRate: SequestrationRate[];
emissionFactors: EmissionFactors[];
implementationLaborCost: ImplementationLaborCost[];
baseSize: BaseSize[];
baseIncrease: BaseIncrease[];
modelAssumptions: ModelAssumptions[];
};

@Injectable()
export class EntityPreprocessor {
toDbEntities(raw: {
master_table: ExcelMasterTable[];
Projects: ExcelProjects[];
'Project size': ExcelProjectSize[];
'Feasibility analysis': ExcelFeasibilityAnalysis[];
Expand All @@ -120,6 +127,9 @@ export class EntityPreprocessor {
'Sequestration rate': ExcelSequestrationRate[];
'Emission factors': ExcelEmissionFactors[];
'Implementation labor': ExcelImplementationLaborCost[];
base_size_table: ExcelBaseSize[];
base_increase: ExcelBaseIncrease[];
'Model assumptions': ExcelModelAssumptions[];
}): ParsedDBEntities {
const processedProjects = this.processProjects(raw.Projects);

Expand Down Expand Up @@ -181,6 +191,13 @@ export class EntityPreprocessor {
raw['Emission factors'],
);

// process other data
const baseSize = this.processBaseSize(raw.base_size_table);
const baseIncrease = this.processBaseIncrease(raw.base_increase);
const modelAssumptions = this.processModelAssumptions(
raw['Model assumptions'],
);

return {
projects: processedProjects,
projectSize: projectSize,
Expand All @@ -206,9 +223,110 @@ export class EntityPreprocessor {
sequestrationRate: sequestrationRate,
emissionFactors: emissionFactors,
implementationLaborCost: implementationLaborCost,
baseSize: baseSize,
baseIncrease: baseIncrease,
modelAssumptions: modelAssumptions,
};
}

private processModelAssumptions(raw: ExcelModelAssumptions[]) {
const parsedArray: ModelAssumptions[] = [];
raw.forEach((row: ExcelModelAssumptions) => {
const modelAssumption = new ModelAssumptions();
modelAssumption.name = row['Assumptions'];
modelAssumption.unit = row['Units'];
modelAssumption.value = row['Value'];
parsedArray.push(modelAssumption);
});
return parsedArray;
}

private processBaseIncrease(raw: ExcelBaseIncrease[]) {
const parsedArray: BaseIncrease[] = [];
raw.forEach((row: ExcelBaseIncrease) => {
const baseIncrease = new BaseIncrease();
baseIncrease.ecosystem = row.ecosystem as ECOSYSTEM;
baseIncrease.feasibilityAnalysis = this.stringToNumeric(
row['feasibility_analysis'],
);
baseIncrease.conservationPlanningAndAdmin = this.stringToNumeric(
row['conservation_planning_and_admin'],
);
baseIncrease.dataCollectionAndFieldCost = this.stringToNumeric(
row['data_collection_and_field_cost'],
);
baseIncrease.communityRepresentation = this.stringToNumeric(
row['community_representation'],
);
baseIncrease.blueCarbonProjectPlanning = this.stringToNumeric(
row['blue_carbon_project_planning'],
);
baseIncrease.establishingCarbonRights = this.stringToNumeric(
row['establishing_carbon_rights'],
);
baseIncrease.financingCost = this.stringToNumeric(row['financing_cost']);
baseIncrease.validation = this.stringToNumeric(row['validation']);
baseIncrease.monitoring = this.stringToNumeric(row['monitoring']);
baseIncrease.baselineReassessment = this.stringToNumeric(
row['baseline_reassessment'],
);
baseIncrease.mrv = this.stringToNumeric(row['MRV']);
baseIncrease.longTermProjectOperatingCost = this.stringToNumeric(
row['long_term_project_operating_cost'],
);
parsedArray.push(baseIncrease);
});
return parsedArray;
}

private processBaseSize(raw: ExcelBaseSize[]) {
const parsedArray: BaseSize[] = [];
raw.forEach((row: ExcelBaseSize) => {
const baseSize = new BaseSize();
baseSize.ecosystem = row.ecosystem as ECOSYSTEM;
baseSize.activity = row.activity as ACTIVITY;
baseSize.feasibilityAnalysis = this.stringToNumeric(
row.feasibility_analysis,
);
baseSize.conservationPlanningAndAdmin = this.stringToNumeric(
row['conservation_planning_and_admin'],
);
baseSize.dataCollectionAndFieldCost = this.stringToNumeric(
row['data_collection_and_field_cost'],
);
baseSize.communityRepresentation = this.stringToNumeric(
row['community_representation'],
);
baseSize.blueCarbonProjectPlanning = this.stringToNumeric(
row['blue_carbon_project_planning'],
);
baseSize.establishingCarbonRights = this.stringToNumeric(
row['establishing_carbon_rights'],
);
baseSize.financingCost = this.stringToNumeric(row['financing_cost']);
baseSize.validation = this.stringToNumeric(row['validation']);
baseSize.implementationLaborPlanting = this.stringToNumeric(
row['implementation_labor_planting'],
);
baseSize.implementationLaborHybrid = this.stringToNumeric(
row['implementation_labor_hybrid'],
);
baseSize.implementationLaborHydrology = this.stringToNumeric(
row['implementation_labor_hydrology'],
);
baseSize.monitoring = this.stringToNumeric(row['monitoring']);
baseSize.baselineReassessment = this.stringToNumeric(
row['baseline_reassessment'],
);
baseSize.mrv = this.stringToNumeric(row['MRV']);
baseSize.longTermProjectOperatingCost = this.stringToNumeric(
row['long_term_project_operating_cost'],
);
parsedArray.push(baseSize);
});
return parsedArray;
}

private processImplementationLaborCost(raw: ExcelImplementationLaborCost[]) {
const parsedArray: ImplementationLaborCost[] = [];
raw.forEach((row: ExcelImplementationLaborCost) => {
Expand Down
3 changes: 3 additions & 0 deletions api/src/modules/import/services/excel-parser.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const SHEETS_TO_PARSE = [
'Sequestration rate',
'Emission factors',
'Implementation labor',
'base_size_table',
'base_increase',
'Model assumptions',
] as const;

export interface ExcelParserInterface {
Expand Down
Loading

0 comments on commit 111aebe

Please sign in to comment.