Skip to content

Commit

Permalink
Merge pull request #346 from aayushgauba/main
Browse files Browse the repository at this point in the history
Organization and Species total counter
  • Loading branch information
dadiorchen committed Jul 19, 2023
2 parents 4db26f2 + 5043279 commit 01e6d7c
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 21 deletions.
29 changes: 29 additions & 0 deletions server/infra/database/SpeciesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ export default class SpeciesRepository extends BaseRepository<Species> {
return object.rows;
}

async countByOrganization(organization_id: number) {
const totalSql = `
SELECT
species_id as id, total, ts.name, ts.desc
FROM
(
SELECT
ss.species_id, count(ss.species_id) as total
from webmap.species_stat ss
WHERE
ss.planter_id IN (
SELECT
id
FROM planter p
WHERE
p.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${organization_id}))
)
OR
ss.planting_organization_id = ${organization_id}
GROUP BY ss.species_id
) s_count
JOIN tree_species ts
ON ts.id = s_count.species_id
ORDER BY total DESC
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
}

async getByPlanter(planter_id: number, options: FilterOptions) {
const { limit, offset } = options;
const sql = `
Expand Down
33 changes: 31 additions & 2 deletions server/infra/database/SpeciesRepositoryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,36 @@ export default class SpeciesRepositoryV2 extends BaseRepository<Species> {
return object.rows;
}

async getByPlanter(planter_id: number, options: FilterOptions) {
async countByOrganization(organization_id: number) {
const totalSql = `
SELECT
species_id as id, total, ts.name, ts.desc
FROM
(
SELECT
ss.species_id, count(ss.species_id) as total
from webmap.species_stat ss
WHERE
ss.planter_id IN (
SELECT
id
FROM planter p
WHERE
p.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${organization_id}))
)
OR
ss.planting_organization_id = ${organization_id}
GROUP BY ss.species_id
) s_count
JOIN tree_species ts
ON ts.id = s_count.species_id
ORDER BY total DESC
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
}

async getByPlanter(planter_id: number, options: FilterOptions) {
const { limit, offset } = options;
const sql = `
SELECT
Expand Down Expand Up @@ -119,4 +148,4 @@ async getByPlanter(planter_id: number, options: FilterOptions) {
const object = await this.session.getDB().raw(sql);
return object.rows;
}
}
}
10 changes: 10 additions & 0 deletions server/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@ function getOrganizationLinks(organization) {
return links;
}

function countByFilter(
organizationRepository: OrganizationRepository,
): (filter: Filter) => Promise<number> {
return async function (filter: Filter) {
const total = await organizationRepository.countByFilter(filter);
return total;
};
}

export default {
getById: delegateRepository<OrganizationRepository, Organization>('getById'),
getByMapName: delegateRepository<OrganizationRepository, Organization>(
'getByMapName',
),
getByFilter,
getOrganizationLinks,
countByFilter,
getFeaturedOrganizations: delegateRepository<
OrganizationRepository,
Organization
Expand Down
10 changes: 9 additions & 1 deletion server/models/OrganizationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ function getOrganizationLinks(organization) {
};
return links;
}

function countByFilter(
organizationRepository: OrganizationRepositoryV2,
): (filter: Filter) => Promise<number> {
return async function (filter: Filter) {
const total = await organizationRepository.countByFilter(filter);
return total;
};
}
export default {
getById: delegateRepository<OrganizationRepositoryV2, Organization>('getById'),
getByMapName: delegateRepository<OrganizationRepositoryV2, Organization>(
'getByMapName',
),
getByFilter,
countByFilter,
getOrganizationLinks,
getFeaturedOrganizations: delegateRepository<
OrganizationRepositoryV2,
Expand Down
17 changes: 17 additions & 0 deletions server/models/Species.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,24 @@ function getByFilter(
};
}

function countByFilter(
speciesRepository: SpeciesRepository,
): (filter: Filter) => Promise<number> {
return async function (filter: Filter) {
if (filter.organization_id) {
log.warn('using org filter...');
const total = await speciesRepository.countByOrganization(
filter.organization_id,
);
return total;
}
const total = await speciesRepository.countByFilter(filter);
return total;
};
}

export default {
getById: delegateRepository<SpeciesRepository, Species>('getById'),
getByFilter,
countByFilter,
};
35 changes: 25 additions & 10 deletions server/models/SpeciesV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Filter = Partial<{
planter_id: number;
organization_id: number;
wallet_id: string;
grower_id:string;
grower_id: string;
}>;

function getByFilter(
Expand Down Expand Up @@ -41,21 +41,36 @@ function getByFilter(
return trees;
}
if (filter.grower_id) {
log.warn('using grower filter...');
const trees = await speciesRepository.getByGrower(
filter.grower_id,
options,
);
return trees;
}
log.warn('using grower filter...');
const trees = await speciesRepository.getByGrower(
filter.grower_id,
options,
);
return trees;
}

const trees = await speciesRepository.getByFilter(filter, options);
return trees;
};
}

function countByFilter(
speciesRepository: SpeciesRepositoryV2,
): (filter: Filter) => Promise<number> {
return async function (filter: Filter) {
if (filter.organization_id) {
log.warn('using org filter...');
const total = await speciesRepository.countByOrganization(
filter.organization_id,
);
return total;
}
const total = await speciesRepository.countByFilter(filter);
return total;
};
}
export default {
getById: delegateRepository<SpeciesRepositoryV2, Species>('getById'),
getByGrower:delegateRepository<SpeciesRepositoryV2, Species>('getByGrower'),
getByGrower: delegateRepository<SpeciesRepositoryV2, Species>('getByGrower'),
getByFilter,
countByFilter,
};
2 changes: 1 addition & 1 deletion server/routers/organizationsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ router.get(
offset,
});
res.send({
total: null,
total: await OrganizationModel.countByFilter(repo)(filter),
offset,
limit,
organizations: result.map((organization) => ({
Expand Down
2 changes: 1 addition & 1 deletion server/routers/organizationsRouterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ router.get(
offset,
});
res.send({
total: null,
total: await OrganizationModel.countByFilter(repo)(filter),
offset,
limit,
organizations: result.map((organization) => ({
Expand Down
2 changes: 1 addition & 1 deletion server/routers/speciesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ router.get(
});
log.warn('species filter:', filter, 'took time:', Date.now() - begin, 'ms');
res.send({
total: null,
total: await SpeciesModel.countByFilter(repo)(filter),
offset,
limit,
species: result,
Expand Down
10 changes: 5 additions & 5 deletions server/routers/speciesRouterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ router.get(
Joi.object().keys({
organization_id: Joi.number().integer().min(0),
planter_id: Joi.number().integer().min(0),
grower_id:Joi.string().guid(),
grower_id: Joi.string().guid(),
wallet_id: Joi.string(),
limit: Joi.number().integer().min(1).max(1000),
offset: Joi.number().integer().min(0),
Expand All @@ -56,17 +56,17 @@ router.get(
filter.planter_id = planter_id;
} else if (wallet_id) {
filter.wallet_id = wallet_id;
} else if(grower_id){
filter.grower_id = grower_id;
}
} else if (grower_id) {
filter.grower_id = grower_id;
}
const begin = Date.now();
const result = await SpeciesModel.getByFilter(repo)(filter, {
limit,
offset,
});
log.warn('species filter:', filter, 'took time:', Date.now() - begin, 'ms');
res.send({
total: null,
total: await SpeciesModel.countByFilter(repo)(filter),
offset,
limit,
species: result,
Expand Down

0 comments on commit 01e6d7c

Please sign in to comment.