Skip to content

Commit

Permalink
Refactor: use restInfo handler directly in output-geoservice (#967)
Browse files Browse the repository at this point in the history
* refactor: use restInfo handler directly
  • Loading branch information
rgwozdz committed Apr 29, 2024
1 parent 611a0f7 commit 4adb735
Show file tree
Hide file tree
Showing 17 changed files with 1,167 additions and 123 deletions.
873 changes: 872 additions & 1 deletion .coverage_json/coverage-summary.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
branches:
- master
- beta
- next
paths:
- "./.github/**.yml"
- "**/packages/**.js"
- "**/packages/**/package.json"
- "test/**/*.js"
- "ci/**/*.js"

jobs:
pr-tests:
Expand Down
3 changes: 3 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh
echo "\nRunning code linting...\n"
npm run lint
45 changes: 31 additions & 14 deletions ci/format-branch-coverage-changes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { writeFileSync, existsSync } = require('fs');
const json2md = require('json2md');
const coverageSummary = require('../.coverage_json/coverage-summary.json');

const markdownFilePath = '.branch-coverage-changes.md';

if (!existsSync('.coverage_changes_json/coverage-summary.json')) {
Expand All @@ -16,29 +15,43 @@ if (!existsSync('.coverage_changes_json/coverage-summary.json')) {
}

const coverageChangesSummary = require('../.coverage_changes_json/coverage-summary.json');

const rows = Object.entries(coverageChangesSummary)
.filter(([filePath]) => {
return filePath !== 'total';
})
.map(([filePath, changesCoverage]) => {
const packageFilePath = `packages${filePath.split('packages')[1]}`;
const masterCoverage = coverageSummary[packageFilePath];

return [
packageFilePath,
formatCovComparison(changesCoverage.statements.pct, masterCoverage?.statements?.pct || 0),
formatCovComparison(changesCoverage.branches.pct, masterCoverage?.branches?.pct || 0),
formatCovComparison(changesCoverage.functions.pct, masterCoverage?.functions?.pct || 0),
formatCovComparison(changesCoverage.lines.pct, masterCoverage?.lines?.pct || 0),
formatCovComparison(
changesCoverage.statements.pct,
masterCoverage?.statements?.pct || null,
),
formatCovComparison(
changesCoverage.branches.pct,
masterCoverage?.branches?.pct || null,
),
formatCovComparison(
changesCoverage.functions.pct,
masterCoverage?.functions?.pct || null,
),
formatCovComparison(
changesCoverage.lines.pct,
masterCoverage?.lines?.pct || null,
),
];
});

const headers = ['File Path', 'Statements', 'Branches', 'Functions', 'Lines'];
const headers = ['File Path', 'Statements', 'Branches', 'Functions', ' Lines '];

const table = json2md([{ h2: 'Coverage Report (change vs master)' }, { table: { headers, rows } }]);

const alignedTable = table.replace(
'| --------- | ---------- | -------- | --------- | ----- |',
'| :--------- | ----------: | --------: | ---------: | -----: |',
'| --------- | --------- | -------- | --------- | --------- |',
'| :--------- | ---------: | --------: | ---------: | ---------: |',
);

const markdown = `[g-img]: https://github.com/koopjs/koop/assets/4369192/fd82d4b7-7f6e-448c-a56c-82ac6781a629
Expand All @@ -52,25 +65,29 @@ ${alignedTable}`;
writeFileSync(markdownFilePath, markdown, 'utf8');

function formatCovComparison(changePct, mainPct) {
return `${formatCovPct(changePct)} vs ${formatCovPct(mainPct)}`;
return `${formatCovPct(changePct)}<br>vs<br>${formatCovPct(mainPct)}`;
}

function formatCovPct(pct) {
if (!pct) {
return '(NA)';
}

if (pct === 100) {
return `${pct} ![green][g-img]`;
return `${pct.toFixed(1)} ![green][g-img]`;
}

if (pct > 90) {
return `${pct} ![yellowGreen][yg-img]`;
return `${pct.toFixed(1)} ![yellowGreen][yg-img]`;
}

if (pct > 80) {
return `${pct} ![yellow][y-img]`;
return `${pct.toFixed(1)} ![yellow][y-img]`;
}

if (pct > 70) {
return `${pct} ![orange][o-img]`;
return `${pct.toFixed(1)} ![orange][o-img]`;
}

return `${pct} ![red][r-img]`;
return `${pct.toFixed(1)} ![red][r-img]`;
}
8 changes: 4 additions & 4 deletions packages/featureserver/coverage-unit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions packages/featureserver/src/helpers/combine-body-query-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const _ = require('lodash');

function combineBodyQueryParameters(body, query) {
const definedQueryParams = _.pickBy(query, isNotEmptyString);
const definedBodyParams = _.pickBy(body, isNotEmptyString);

return {
...definedBodyParams,
...definedQueryParams,
};
}

function isNotEmptyString(str) {
return !_.isString(str) || !_.isEmpty(str);
}

module.exports = { combineBodyQueryParameters };
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const should = require('should'); // eslint-disable-line
const { combineBodyQueryParameters } = require('./combine-body-query-params');

describe('combineBodyQueryParameters', () => {
it('should merge objects with deference to query', () => {
combineBodyQueryParameters(
{ foo: 'bar', hello: 'body-param' },
{ foo: 'baz', test: 'query-param' },
).should.deepEqual({
foo: 'baz',
test: 'query-param',
hello: 'body-param',
});
});

it('should merge objects with deference to query, strip empty strings', () => {
combineBodyQueryParameters(
{ foo: 'bar', hello: '' },
{ foo: 'baz', test: 'query-param' },
).should.deepEqual({
foo: 'baz',
test: 'query-param',
});
});
});
1 change: 1 addition & 0 deletions packages/featureserver/src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = {
...require('./renderers'),
...require('./validate-inputs'),
...require('./normalize-request-params'),
...require('./combine-body-query-params'),
};
26 changes: 11 additions & 15 deletions packages/featureserver/src/helpers/normalize-request-params.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
const _ = require('lodash');
const defaults = require('../metadata-defaults');
const { combineBodyQueryParameters } = require('./combine-body-query-params');

function normalizeRequestParameters(query, body, maxRecordCount = defaults.maxRecordCount()) {
const definedQueryParams = _.chain(query)
.pickBy(isNotEmptyString)
.mapValues(coerceStrings)
.value();
function normalizeRequestParameters(
query,
body,
maxRecordCount = defaults.maxRecordCount(),
) {
const requestParams = combineBodyQueryParameters(body, query);

const definedBodyParams = _.chain(body).pickBy(isNotEmptyString).mapValues(coerceStrings).value();

const { resultRecordCount, ...params } = {
...definedQueryParams,
...definedBodyParams,
};
const { resultRecordCount, ...params } = _.mapValues(
requestParams,
coerceStrings,
);

return {
...params,
resultRecordCount: resultRecordCount || maxRecordCount,
};
}

function isNotEmptyString(str) {
return !_.isString(str) || !_.isEmpty(str);
}

function coerceStrings(val) {
if (val === 'false') {
return false;
Expand Down
63 changes: 48 additions & 15 deletions packages/featureserver/src/rest-info-route-handler.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
const _ = require('lodash');
const defaults = require('./metadata-defaults');
const joi = require('joi');
const metadataDefaults = require('./metadata-defaults');
const { generalResponseHandler } = require('./response-handlers');
const { combineBodyQueryParameters } = require('./helpers');

function restInfo(data = {}, req) {
const versionDefaults = defaults.restInfoDefaults();
const parameterSchema = joi
.object({
f: joi.string().valid('json', 'pjson').default('json'),
})
.unknown();

function restInfo(req, res, data = {}) {
const { currentVersion, fullVersion } = getVersions(req.app.locals);

const requestParams = combineBodyQueryParameters(req.body, req.query);

validate(requestParams);

return generalResponseHandler(
res,
{
currentVersion,
fullVersion,
owningSystemUrl: data.owningSystemUrl,
authInfo: {
...data.authInfo,
},
},
requestParams,
);
}

function getVersions(locals) {
const versionDefaults = metadataDefaults.restInfoDefaults();
const currentVersion = _.get(
req,
'app.locals.config.featureServer.currentVersion',
locals,
'config.featureServer.currentVersion',
versionDefaults.currentVersion,
);

const fullVersion = _.get(
req,
'app.locals.config.featureServer.fullVersion',
locals,
'config.featureServer.fullVersion',
versionDefaults.fullVersion,
);
return { currentVersion, fullVersion };
}

return {
currentVersion,
fullVersion,
owningSystemUrl: data.owningSystemUrl,
authInfo: {
...data.authInfo,
},
};
function validate(parameters) {
const { error } = parameterSchema.validate(parameters);

if (error) {
const err = new Error('Invalid format');
err.code = 400;
throw err;
}
}

module.exports = restInfo;
Loading

0 comments on commit 4adb735

Please sign in to comment.