Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
test-graph-view | code cleanup in backend and frontend + dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosantiagomuro committed Oct 8, 2023
1 parent 3992b69 commit 88dd5b5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 3,812 deletions.
8 changes: 2 additions & 6 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AppService } from './app.service';
import { RCController } from './conditions/rc.controller';
import { RCService } from './conditions/rc.service';

import { DB_LIRAMAP_CONFIG, POSTGIS_DB_CONFIG } from './database';
import { DB_LIRAMAP_CONFIG } from './database';

const database = (config: any, name: string) => {
return KnexModule.forRootAsync(
Expand All @@ -20,11 +20,7 @@ const database = (config: any, name: string) => {
};

@Module({
imports: [
ConfigModule.forRoot(),
database(POSTGIS_DB_CONFIG, 'postgis'),
database(DB_LIRAMAP_CONFIG, 'lira-map'),
],
imports: [ConfigModule.forRoot(), database(DB_LIRAMAP_CONFIG, 'lira-map')],
controllers: [AppController, RCController],
providers: [AppService, ConfigService, RCService],
})
Expand Down
10 changes: 1 addition & 9 deletions backend/src/conditions/rc.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { Controller, Get, Query } from '@nestjs/common';

import { Condition, WaysConditions } from 'src/models';
import { Condition } from 'src/models';
import { RCService } from './rc.service';

@Controller('conditions')
export class RCController {
constructor(private readonly service: RCService) {}

@Get('ways')
getWaysConditions(
@Query() query: { type: string; zoom: string },
): Promise<WaysConditions> {
const { type, zoom } = query;
return this.service.getWaysConditions(type, zoom);
}

@Get('way')
getWayConditions(@Query() query: { dbId: string }): Promise<Condition[]> {
const { dbId } = query;
Expand Down
84 changes: 6 additions & 78 deletions backend/src/conditions/rc.service.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
import { Injectable } from '@nestjs/common';

import { InjectConnection, Knex } from 'nestjs-knex';
import { Condition, LatLngDist, WayId, WaysConditions } from 'src/models';
import groupBy from '../util';
import { Ways, ZoomConditions, Conditions2, Conditions } from '../tables';
import { Condition } from 'src/models';
import { Conditions2, Conditions } from '../tables';

import knexPostgis = require('knex-postgis');

@Injectable()
export class RCService {
constructor(
@InjectConnection('postgis') private readonly knex: Knex,
@InjectConnection('lira-map') private readonly knex_liramap: Knex,
) {}

async getWays(
wayIds: string[],
): Promise<[{ [key: WayId]: LatLngDist[] }, { [key: WayId]: number }]> {
const ways = await Ways(this.knex)
.select(
'id as way_id',
this.knex.raw(
"ST_AsGeoJSON((ST_DumpPoints(geom)).geom)::json->'coordinates' as pos",
),
this.knex.raw(
'ST_LineLocatePoint(geom, (ST_DumpPoints(geom)).geom) as way_dist',
),
this.knex.raw('ST_Length(geom::geography) as length'),
)
.whereIn('id', wayIds)
.orderBy(this.knex.raw('id::integer') as any);

return [
groupBy<any, LatLngDist>(ways, 'way_id', (cur: any) => ({
lat: cur.pos[1],
lng: cur.pos[0],
way_dist: cur.way_dist,
})),
ways.reduce((acc, cur) => {
acc[cur.way_id] = cur.length;
return acc;
}, {}),
];
}
constructor(@InjectConnection('lira-map') private readonly liramap: Knex) {}

async getWayRoadConditions(dbId: string): Promise<Condition[]> {
return (
Conditions(this.knex_liramap)
Conditions(this.liramap)
.select(
'cond1.value as KPI',
'cond2.value as DI',
Expand All @@ -60,51 +26,13 @@ export class RCService {
)
.where('cond1.type', 'KPI')
.where('cond2.type', 'DI')
.where(this.knex_liramap.raw('cond1.fk_way_id = cond2.fk_way_id'))
.where(this.liramap.raw('cond1.fk_way_id = cond2.fk_way_id'))
// .where('cond1.distance01', '<>', 0)
.where('cond1.fk_way_id', dbId)
.orderBy('cond1.distance01')
);
}

async getZoomConditions(
type: string,
zoom: string,
): Promise<{ [key: WayId]: Condition[] }> {
const res = await ZoomConditions(this.knex)
.select('way_id', 'way_dist', 'value')
.where({ type: type, zoom: parseInt(zoom, 10) })
.orderBy('way_dist');
return groupBy(res, 'way_id', ({ way_dist, value }) => ({
way_dist,
value,
}));
}

async getWaysConditions(type: string, zoom: string): Promise<WaysConditions> {
const zoomConditions = await this.getZoomConditions(type, zoom);
const wayIds = Object.keys(zoomConditions);
const [ways, way_lengths] = await this.getWays(wayIds);

return wayIds.reduce(
(acc, way_id) => {
{
acc.way_ids.push(way_id);
acc.way_lengths.push(way_lengths[way_id]);
acc.geometry.push(ways[way_id]);
acc.conditions.push(zoomConditions[way_id]);
}
return acc;
},
{
way_ids: [],
way_lengths: [],
geometry: [],
conditions: [],
} as WaysConditions,
);
}

async getConditions(
minLat: string,
maxLat: string,
Expand All @@ -115,7 +43,7 @@ export class RCService {
valid_after: string,
computed_after: string,
) {
const db = this.knex_liramap;
const db = this.liramap;
const st = knexPostgis(db);

let res;
Expand Down
14 changes: 0 additions & 14 deletions backend/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as dotenv from 'dotenv';
dotenv.config();

const {
DB_USER_POSTGIS,
DB_PWD_POSTGIS,
DB_LIRAMAP_HOST,
DB_LIRAMAP_PORT,
DB_LIRAMAP_NAME,
Expand Down Expand Up @@ -41,18 +39,6 @@ const BASE_CONFIG = {
},
};

export const POSTGIS_DB_CONFIG = {
...BASE_CONFIG,
connection: {
host: 'liradb.postgres.database.azure.com',
port: 5432,
user: DB_USER_POSTGIS,
password: DB_PWD_POSTGIS,
database: 'postgis',
ssl: true,
},
};

export const DB_LIRAMAP_CONFIG = {
...BASE_CONFIG,
connection: {
Expand Down
Loading

0 comments on commit 88dd5b5

Please sign in to comment.