Skip to content

Commit

Permalink
fixed units and cols endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
amyfromandi committed Sep 17, 2024
1 parent 0541deb commit 152eb83
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 53 deletions.
5 changes: 2 additions & 3 deletions api-tests/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ const testFiles = [

const testDir = path.join(__dirname, 'v2Tests');


testFiles.forEach((file) => {
describe(file, function () {
console.log(`Loading test file: ${file}`);
require(path.join(testDir, file));
console.log(`Loading test file: ${file}`);
require(path.join(testDir, file));
});
});

Expand Down
2 changes: 2 additions & 0 deletions api-tests/v2Tests/carto_small.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ request(settings.host)
});

/*
//These tests fail production and dev. They are commented out for now.
it("should return geometry when asked", function (done) {
request(settings.host)
.get("/carto/small?lat=43&lng=-89.3&format=geojson")
Expand Down
49 changes: 27 additions & 22 deletions api-tests/v2Tests/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ request(settings.host)
done();
});
});
/*

//Fixed this test to work on dev and local
it("should return a sample", function (done) {
request(settings.host)
.get("/columns?sample")
Expand All @@ -26,6 +27,7 @@ request(settings.host)
});
});

//Fixed this test to work on dev and local
it("should accept an interval name", function (done) {
request(settings.host)
.get("/columns?interval_name=Permian")
Expand All @@ -37,7 +39,7 @@ request(settings.host)
done();
});
});
*/


it("should accept an age", function (done) {
request(settings.host)
Expand All @@ -51,6 +53,7 @@ request(settings.host)
});
});


it("should accept an age_top and age_bottom", function (done) {
request(settings.host)
.get("/columns?age_top=200&age_bottom=250")
Expand All @@ -63,7 +66,7 @@ request(settings.host)
});
});

/*
//fixed test in dev and it is now working.
it("should accept a strat_name parameter", function (done) {
request(settings.host)
.get("/columns?strat_name=mancos")
Expand All @@ -75,7 +78,7 @@ request(settings.host)
done();
});
});
*/

it("should accept a strat_name_id parameter", function (done) {
request(settings.host)
.get("/columns?strat_name_id=1205")
Expand All @@ -88,46 +91,49 @@ request(settings.host)
});
});

it("should return topojson", function (done) {

//Checking to see if the rest of the tests below work in prod.
//count for max number of columns ~50?
it("should accept a latitude and longitude", function (done) {
request(settings.host)
.get("/columns?age=2&format=topojson")
.get("/columns?lat=43.3&lng=-89.3")
.expect(validators.aSuccessfulRequest)
.expect(validators.topoJSON)
.expect(validators.json)
.expect(validators.atLeastOneResult)
.expect(function (res: { body: { success: { data: { col_id: number; }[]; }; }; }) {
if (res.body.success.data[0].col_id != 187) {
throw new Error("Columns returning the wrong column for the lat/lng");
}
})
.end(function (error: any, res: any) {
if (error) return done(error);
done();
});
});

it("should return csv", function (done) {
it("should return topojson", function (done) {
request(settings.host)
.get("/columns?age=2&format=csv")
.get("/columns?age=2&format=topojson")
.expect(validators.aSuccessfulRequest)
.expect(validators.csv)
.expect(validators.topoJSON)
.end(function (error: any, res: any) {
if (error) return done(error);
done();
});
});

/*
it("should accept a latitude and longitude", function (done) {
it("should return csv", function (done) {
request(settings.host)
.get("/columns?lat=43.3&lng=-89.3")
.get("/columns?age=2&format=csv")
.expect(validators.aSuccessfulRequest)
.expect(validators.json)
.expect(validators.atLeastOneResult)
.expect(function (res: { body: { success: { data: { col_id: number; }[]; }; }; }) {
if (res.body.success.data[0].col_id != 187) {
throw new Error("Columns returning the wrong column for the lat/lng");
}
})
.expect(validators.csv)
.end(function (error: any, res: any) {
if (error) return done(error);
done();
});
});


it("should accept a project_id", function (done) {
request(settings.host)
.get("/columns?project_id=4")
Expand All @@ -141,7 +147,7 @@ request(settings.host)
});

it("should accept a lat/lng and return all adjacent columns", function (done) {
request(settings.host)
request(settings.host)
.get("/columns?lat=43.3&lng=-89.3&adjacents=true")
.expect(validators.aSuccessfulRequest)
.expect(validators.json)
Expand All @@ -156,5 +162,4 @@ request(settings.host)
done();
});
});
*/

34 changes: 18 additions & 16 deletions v2/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ module.exports = function (req, res, next, callback) {
return callback(null, data);
}

//call units to group units by col_id
require("./units")(req, null, null, function (error, result) {
if (error) {
callback(error);
}

if (!result || !result.length) {
return callback(null, null);
}
console.log(result[0])
var cols = _.groupBy(result, function (d) {
return d.col_id;
});

var new_cols = {};
Object.keys(cols).forEach(function (col_id) {
new_cols[col_id] = {
Expand Down Expand Up @@ -144,29 +148,28 @@ module.exports = function (req, res, next, callback) {
}

var geo = "";
var params = { "col_id":
var params = [
Object.keys(new_cols).map(function (d) {
return parseInt(d);
}),
};
];


var limit = "sample" in req.query ? " LIMIT 5" : "";
var groupBy = "";
var orderby = "";

console.log("COLUMN PARAMS", params)
if (req.query.status_code) {
params["status_code"]= decodeURI(req.query.status_code);
params.push(decodeURI(req.query.status_code));
} else {
params["status_code"] = "active";
params.push("active");
}

if (req.query.format && api.acceptedFormats.geo[req.query.format]) {
if (req.query.shape) {
geo =
", ST_AsGeoJSON(ST_Intersection(col_areas.col_area, ST_MakeValid(:shape))) geojson";
params["shape"] = req.query.shape;
", ST_AsGeoJSON(ST_Intersection(col_areas.col_area, ST_MakeValid($3))) geojson";
params.push(req.query.shape);
} else {
geo = ", ST_AsGeoJSON(col_areas.col_area) geojson";
}
Expand All @@ -175,19 +178,18 @@ module.exports = function (req, res, next, callback) {

if (req.query.lat && req.query.lng && req.query.adjacents) {
orderby =
"ORDER BY ST_Distance(ST_SetSRID(col_areas.col_area, 4326), ST_GeometryFromText(:point, 4326))";
params["point"] =
"ORDER BY ST_Distance(ST_SetSRID(col_areas.col_area, 4326), ST_GeometryFromText($3, 4326))";
params.push(
"POINT(" +
larkin.normalizeLng(req.query.lng) +
" " +
req.query.lat +
")"

")");
groupBy = ", col_areas.col_area";
} else if (req.query.col_id && req.query.adjacents) {
orderby =
"ORDER BY ST_Distance(ST_Centroid(col_areas.col_area), (SELECT ST_Centroid(col_area) FROM macrostrat.col_areas WHERE col_id = :col_id))";
params["col_id"] = req.query.col_id;
"ORDER BY ST_Distance(ST_Centroid(col_areas.col_area), (SELECT ST_Centroid(col_area) FROM macrostrat.col_areas WHERE col_id = $3))";
params.push(req.query.col_id);
groupBy = ", col_areas.col_area";
}

Expand All @@ -206,15 +208,15 @@ module.exports = function (req, res, next, callback) {
cols.lng,
round(cols.col_area::numeric, 1) AS col_area,
cols.project_id,
cols.col_type,
col_type,
string_agg(col_refs.ref_id::varchar, '|') AS refs
${geo}
FROM macrostrat.cols
LEFT JOIN macrostrat.col_areas on col_areas.col_id = cols.id
LEFT JOIN macrostrat.col_groups ON col_groups.id = cols.col_group_id
LEFT JOIN macrostrat.col_refs ON cols.id = col_refs.col_id
WHERE cols.status_code = :status_code
AND cols.id = ANY(:col_id)
WHERE cols.status_code = $2
AND cols.id = ANY($1)
GROUP BY col_areas.col_id, cols.id, col_groups.col_group, col_groups.id ${groupBy}
${orderby}
${limit}
Expand Down
31 changes: 19 additions & 12 deletions v2/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ module.exports = function (req, res, next, cb) {
[
function (callback) {
if (req.query.interval_name) {
params["interval_name"] = [req.query.interval_name];
params["interval_name"] = req.query.interval_name;
let sql = `SELECT age_bottom, age_top, interval_name FROM macrostrat.intervals WHERE interval_name = :interval_name LIMIT 1`
larkin.queryPg("burwell",
sql,
params,
function (error, result) {
console.log(result)
if (error) {
callback(error);
} else {
Expand Down Expand Up @@ -101,16 +102,19 @@ module.exports = function (req, res, next, cb) {
},
);
} else if (req.query.lat && req.query.lng) {

//TODO: review and see why all cols are returning with lat and lng.
var sql =
req.query.adjacents === "true"
? "WITH containing_geom AS (SELECT poly_geom FROM macrostrat.cols WHERE ST_Contains(poly_geom, ST_GeomFromText(:point, 4326))) SELECT id FROM macrostrat.cols WHERE ST_Intersects((SELECT * FROM containing_geom), poly_geom) ORDER BY ST_Distance(ST_Centroid(poly_geom), ST_GeomFromText($1, 4326))"
: "SELECT id FROM macrostrat.cols WHERE ST_Contains(poly_geom, st_setsrid(ST_GeomFromText(:point), 4326)) ORDER BY ST_Distance(ST_Centroid(poly_geom), ST_GeomFromText($1, 4326))";
: "SELECT id FROM macrostrat.cols WHERE ST_Contains(poly_geom, st_setsrid(ST_GeomFromText(:point), 4326)) ORDER BY ST_Distance(ST_Centroid(poly_geom), ST_GeomFromText(:point, 4326))";
params['point'] =
"POINT(" +
larkin.normalizeLng(req.query.lng) +
" " +
req.query.lat +
")"

larkin.queryPg(
"burwell",
sql,
Expand All @@ -119,19 +123,19 @@ module.exports = function (req, res, next, cb) {
if (error) {
callback(error);
} else {
callback(null, {
return callback(null, {
interval_name: "none",
age_bottom: 99999,
age_top: 0,
col_ids: response.rows.map(function (d) {
return d.id;
return d.id;
}),
});
}
},
);
} else if (req.query.col_id && req.query.adjacents) {
var col_ids = larkin.parseMultipleIds(req.query.col_id),
var col_ids = larkin.parseMultipleIds(req.query.col_id),
placeholders = col_ids.map(function (d, i) {
return "$" + (i + 1);
});
Expand Down Expand Up @@ -178,7 +182,7 @@ module.exports = function (req, res, next, cb) {
);
} else if (req.query.strat_name) {
larkin.queryPg("burwell",
"SELECT strat_name_id FROM macrostrat.lookup_strat_names WHERE strat_name LIKE :strat_name ",
"SELECT strat_name_id FROM macrostrat.lookup_strat_names WHERE strat_name ILIKE :strat_name ",
{'strat_name': "%" + req.query.strat_name + "%"},
function (error, result) {
if (error) {
Expand Down Expand Up @@ -284,6 +288,8 @@ module.exports = function (req, res, next, cb) {
orderby = [],
params = {};

console.log(data)

if (req.query.status_code) {
where += "cols.status_code = ANY(:status_code)";
params["status_code"] = larkin.parseMultipleStrings(
Expand Down Expand Up @@ -389,17 +395,18 @@ module.exports = function (req, res, next, cb) {
where += " AND units_sections.section_id = ANY(:section_id)";
params["section_id"] = larkin.parseMultipleIds(req.query.section_id);
}

if ("col_id" in data) {
console.log(data)
if ("col_ids" in data) {
where += " AND units_sections.col_id = ANY(:col_id)";
if (!data.col_id.length) {
data.col_id = [""];
if (!data.col_ids.length) {
data.col_ids = [""];
}
params["col_id"] = data.col_id;
params["col_id"] = data.col_ids;
} else if (req.query.col_id) {
where += " AND units_sections.col_id = ANY(:col_id)";
params["col_id"] = larkin.parseMultipleIds(req.query.col_id);
}
console.log(where)

if (data.strat_ids) {
where +=
Expand Down Expand Up @@ -495,7 +502,7 @@ module.exports = function (req, res, next, cb) {
if ("sample" in req.query) {
// Speeds things up...
where +=
" AND units_sections.col_id = ANY(92, 488, 463, 289, 430, 481, 261, 534, 369, 798, 771, 1675) ";
" AND units_sections.col_id = ANY(ARRAY[92, 488, 463, 289, 430, 481, 261, 534, 369, 798, 771, 1675]) ";
}

const measureField =
Expand Down

0 comments on commit 152eb83

Please sign in to comment.