Skip to content

Commit

Permalink
Improve performance, caching
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Jan 23, 2024
1 parent 5f29daf commit 186e58c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 52 deletions.
1 change: 1 addition & 0 deletions server/routes/detailedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ router.get("/", async (req, res) => {
} else {
clientData = null
}
res.set("Cache-Control", "public, max-age=86400")
res.json(clientData)
} catch (err) {
const msg = `An error occurred executing search search query. Message: ${err}`
Expand Down
1 change: 1 addition & 0 deletions server/routes/generalDBSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ router.get("/", async (req, res) => {
clientData = null
break
}
res.set("Cache-Control", "public, max-age=86400")
res.json(clientData)
} catch (err) {
const msg = `An error occurred executing search search query. Message: ${err}`
Expand Down
10 changes: 7 additions & 3 deletions server/routes/geojsonSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ router.get("/", async (req, res) => {

// check to see if there is a distance of 0
// which represents a target match
const { features } = pgData.data[0].jsonb_build_object

const features = pgData.data.map((feature) => ({
type: "Feature",
geometry: feature.geometry,
properties: feature,
}))
// returns arrays
const { targetAddress, nearbyAddresses } = findTargetAddress(features)

Expand Down Expand Up @@ -137,7 +140,7 @@ router.get("/", async (req, res) => {
year,
})

geoJSON = pgData.data[0].jsonb_build_object
geoJSON = { type: "FeatureCollection", features: pgData.data }
clientData = checkEmptyGeoJSON(geoJSON)
praxisDataType = "zipcode-intersect"
break
Expand All @@ -147,6 +150,7 @@ router.get("/", async (req, res) => {
break
}
clientData.praxisDataType = praxisDataType
res.set("Cache-Control", "public, max-age=86400")
res.json(clientData)
} catch (err) {
const msg = `An error occurred executing parcels geoJSON query. Message: ${err}`
Expand Down
1 change: 1 addition & 0 deletions server/routes/primarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ router.get("/", async (req, res) => {
clientData = null
}

res.set("Cache-Control", "public, max-age=86400")
res.json(clientData)
} catch (err) {
const msg = `An error occurred executing primary search query. Message: ${err}`
Expand Down
105 changes: 56 additions & 49 deletions server/utils/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ async function queryPGDB({
break

case GEOJSON_ZIPCODES_PARCELS:
query = `SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
query = `
SELECT jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geometry, 6)::json,
Expand All @@ -109,8 +105,7 @@ async function queryPGDB({
ON p.year = ${year}
AND ST_Intersects(z.geometry, p.geom)
${createCodeSQLPredicate({ code, ownid, coordinates })}
) inputs
) features;`
) inputs;`
break

case GEOJSON_PARCELS_CODE:
Expand Down Expand Up @@ -197,59 +192,71 @@ async function queryPGDB({

/*currenlty dead query - not used*/
case GEOJSON_PARCELS_DISTANCE:
query = `SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'id', feature_id,
'geometry', ST_AsGeoJSON(geom, 6)::json,
'properties', to_jsonb(inputs) - 'geom',
'centroid', ST_AsText(centroid)
) AS feature
FROM (
SELECT *, ST_Distance(
ST_SetSRID(
ST_MakePoint(${longitude}, ${latitude}),
4326)::geography,
geom::geography) AS distance
query = `
SELECT
feature_id,
saledate,
saleprice,
totsqft,
totacres,
cityrbuilt,
resyrbuilt,
prop_id,
year,
propaddr,
own_id,
taxpayer,
count,
own_group,
propno,
propdir,
propzip,
ST_AsText(centroid) AS centroid,
ST_AsGeoJSON(geom, 6)::json AS geometry,
ST_Distance(
ST_SetSRID(
ST_MakePoint(${longitude}, ${latitude}),
4326)::geography,
geom::geography) AS distance
FROM parcels
WHERE year = ${year}
AND ST_Distance(
ST_SetSRID(
ST_MakePoint(${longitude}, ${latitude}),
4326)::geography,
geom::geography) < ${searchRadius}
) inputs
) features;`
geom::geography) < ${searchRadius};`
break

case GEOJSON_PARCELS_CODE_DISTANCE:
query = `SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'id', feature_id,
'geometry', ST_AsGeoJSON(geom, 6)::json,
'properties', to_jsonb(inputs) - 'geom',
'centroid', ST_AsText(centroid)
) AS feature
FROM (
SELECT *, ST_Distance(
ST_SetSRID(
ST_MakePoint(${longitude}, ${latitude}),
4326)::geography,
geom::geography) AS distance
query = `
SELECT
feature_id,
saledate,
saleprice,
totsqft,
totacres,
cityrbuilt,
resyrbuilt,
prop_id,
year,
propaddr,
own_id,
taxpayer,
count,
own_group,
propno,
propdir,
propzip,
ST_AsText(centroid) AS centroid,
ST_AsGeoJSON(geom, 6)::json AS geometry,
ST_Distance(
ST_SetSRID(
ST_MakePoint(${longitude}, ${latitude}),
4326)::geography,
geom::geography) AS distance
FROM parcels
WHERE year = ${year}
AND propzip LIKE'${code}%'
) inputs
) features;`
AND propzip LIKE '${code}%';`
break

case POINT_CODE:
Expand Down
1 change: 1 addition & 0 deletions tf/dev/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ module "cloudfront" {
# viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
cached_methods = ["GET", "HEAD"]
compress = true
query_string = true
}
]
Expand Down

0 comments on commit 186e58c

Please sign in to comment.