Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#65: Update helper methods with version #66

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"rules": {
"indent": [
"error",
2
2,
{"SwitchCase": 1}
],
"linebreak-style": [
"error",
Expand Down
35 changes: 20 additions & 15 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const buildAnnotationMap = (annotations = []) =>
return acc;
}, {});

/* eslint-disable */
const getSimpleDataType = (type, isCollection) => {
switch (type) {
case 'Edm.Bool':
Expand All @@ -27,6 +26,10 @@ const getSimpleDataType = (type, isCollection) => {
case 'Edm.Int16':
return 'Number';
case 'Edm.String':
// TODO: this is probably incorrect
// While it's true that any String List, Single is a string, its simple data type isn't
// If other things depend on knowing whether something is a single enumeration or not
// this will not detect it correctly
return isCollection ? 'String List, Multi' : 'String';
case 'Edm.EnumType':
return isCollection ? 'String List, Multi' : 'String List, Single';
Expand Down Expand Up @@ -81,6 +84,7 @@ const getStandardMetadata = (version = CURRENT_DD_VERSION) => {
const annotationMap = buildAnnotationMap(field?.annotations);
const url = annotationMap[DD_WIKI_URL_TERM];

// TODO: this is probably incorrect
const type = field?.type?.startsWith('Edm.') ? field.type : 'Edm.EnumType';

const payloads = (annotationMap[PAYLOADS_TERM] || '').split(',');
Expand Down Expand Up @@ -138,12 +142,12 @@ const getStandardResources = version => {
return resources;
};

const getFieldDetails = async (fieldName, resourceName) => {
return getStandardMetadata().fields.find(field => field?.fieldName === fieldName && field?.resourceName === resourceName);
const getFieldDetails = async (fieldName, resourceName, version = CURRENT_DD_VERSION) => {
return getStandardMetadata(version).fields.find(field => field?.fieldName === fieldName && field?.resourceName === resourceName);
};

const getIdxLookups = (fields, lookups) => {
const standardMetadata = getStandardMetadata();
const getIdxLookups = (fields, lookups, version = CURRENT_DD_VERSION) => {
const standardMetadata = getStandardMetadata(version);
const iDXDataTypes = ['String List, Multi', 'String List,Multi', 'String List, Single', 'String List,Single'];
const iDXLookupFields = fields.filter(field =>
standardMetadata.fields.some(
Expand All @@ -170,14 +174,14 @@ const getIdxLookups = (fields, lookups) => {

const getLastPart = (str, char) => str.substr(str.lastIndexOf(char) + 1);

const getAdvertisedCountPerResourcesByType = ({ fields, lookups }) => {
const getAdvertisedCountPerResourcesByType = ({ fields, lookups, version = CURRENT_DD_VERSION }) => {
const reducedFields = fields.reduce(function (r, a) {
r[a.resourceName] = r[a.resourceName] || [];
r[a.resourceName].push(a);
return r;
}, Object.create(null));
const advertisedCount = {};
const idxLookups = getIdxLookups(fields, lookups);
const idxLookups = getIdxLookups(fields, lookups, version);
mohit-s96 marked this conversation as resolved.
Show resolved Hide resolved
for (const [key, value] of Object.entries(reducedFields)) {
advertisedCount[key] = { fields: {}, lookups: {} };
advertisedCount[key][FIELDS] = { total: 0, reso: 0, idx: 0, local: 0 };
Expand Down Expand Up @@ -246,8 +250,8 @@ const getAdvertisedCountPerResourcesByType = ({ fields, lookups }) => {
return advertisedCount;
};

const getIdxCounts = (fields, lookups) => {
const standardMetadata = getStandardMetadata();
const getIdxCounts = (fields, lookups, version = CURRENT_DD_VERSION) => {
const standardMetadata = getStandardMetadata(version);
const idxFields = fields.filter(field =>
standardMetadata.fields.some(
x => x.resourceName === field.resourceName && x.fieldName === field.fieldName && x?.payloads?.includes('IDX')
Expand All @@ -264,13 +268,13 @@ const getIdxCounts = (fields, lookups) => {
/* iDXLookups
lookups of unique IDX fields with any of types ["String List, Single", String List, Multi]
*/
const iDXLookups = getIdxLookups(fields, lookups);
const iDXLookups = getIdxLookups(fields, lookups, version);
mohit-s96 marked this conversation as resolved.
Show resolved Hide resolved
const iDXLookupsCount = iDXLookups.length;
return { iDXFieldsCount, iDXResourcesCount, iDXLookupsCount };
};

const getFieldsCount = fields => {
const standardMetadata = getStandardMetadata();
const getFieldsCount = (fields, version = CURRENT_DD_VERSION) => {
const standardMetadata = getStandardMetadata(version);
const standardFieldsCount = fields.filter(field =>
standardMetadata.fields.some(x => x.resourceName === field.resourceName && x.fieldName === field.fieldName)
).length;
Expand All @@ -282,10 +286,10 @@ const getFieldsCount = fields => {
};
};

const getResourcesCount = fields => {
const getResourcesCount = (fields, version = CURRENT_DD_VERSION) => {
mohit-s96 marked this conversation as resolved.
Show resolved Hide resolved
const resources = [...new Set(fields.map(field => field.resourceName))];
const totalResourcesCount = resources.length;
const standardResourcesCount = resources.filter(resource => getStandardResources().some(x => x === resource)).length;
const standardResourcesCount = resources.filter(resource => getStandardResources(version).some(x => x === resource)).length;
const localResourcesCount = totalResourcesCount - standardResourcesCount;
return { standardResourcesCount, localResourcesCount, totalResourcesCount };
};
Expand Down Expand Up @@ -355,5 +359,6 @@ module.exports = {
getLookupMap,
getStandardMetadata,
reportTypes,
CATEGORIES
CATEGORIES,
CURRENT_DD_VERSION
};
26 changes: 15 additions & 11 deletions lib/process-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,52 @@ const {
getIdxCounts,
getAdvertisedCountPerResourcesByType,
getMetadata,
getStandardMetadata,
getStandardMetadata
} = require('./common');
const standardMetadata = getStandardMetadata();
const CURRENT_REFERENCE_METADATA_REPORT_JSON = getMetadata();

const ANNOTATION_TERM_STANDARD_NAME = 'RESO.OData.Metadata.StandardName';

/**
* Processes a Data Dictionary metadata report.
* TODO: add version specific logic
* @param {Object} body is the metadata report body generated during testing
* @returns processed metadata report
*/
const processMetadataReport = (
metadataReportJson
) => {
const { fields, lookups, ...reportInfo } = metadataReportJson;
const { fields, lookups, version, ...reportInfo } = metadataReportJson;

if (!version) {
throw new Error('Version is missing from metadata report!');
}

const standardMetadata = getStandardMetadata(version);

const referenceLookups = lookups?.filter(
(x) => !isReferencePlaceholderValue(x)
);

const transformedMetadataReportJson = {
...reportInfo,
fields: getTransformedFields(fields),
version,
fields: getTransformedFields(fields, standardMetadata),
lookups: getTransformedLookups(metadataReportJson),
};

return {
...transformedMetadataReportJson,
...getFieldsCount(fields),
...getResourcesCount(fields),
...getFieldsCount(fields, version),
...getResourcesCount(fields, version),
...getLookupsCount(transformedMetadataReportJson.lookups),
mohit-s96 marked this conversation as resolved.
Show resolved Hide resolved
...getIdxCounts(fields, referenceLookups),
...getIdxCounts(fields, referenceLookups, version),
advertised: getAdvertisedCountPerResourcesByType(
transformedMetadataReportJson
),
};
};

//TODO: convert to use a map instead
const getTransformedFields = (fields = []) => {
const getTransformedFields = (fields = [], standardMetadata = { fields: [] }) => {
return fields.map((field) => {
return {
...field,
Expand Down Expand Up @@ -216,7 +220,7 @@ const isReferencePlaceholderValue = ({ lookupValue = '' }) =>
const getTransformedLookups = (metadataReportJson = {}) => {
if (!metadataReportJson?.lookups) return [];

const referenceMetadataJson = CURRENT_REFERENCE_METADATA_REPORT_JSON;
const referenceMetadataJson = getMetadata(metadataReportJson.version);
const referenceFieldMap = buildFieldMap(referenceMetadataJson);

//remove placeholder values added to the reference metadata for open enumerations without values
Expand Down
2 changes: 1 addition & 1 deletion lib/references/dd-2.0/metadata-report.json

Large diffs are not rendered by default.

Loading