All files / global/search search-config.mjs

95.45% Statements 42/44
100% Branches 39/39
80% Functions 8/10
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                    17x 17x                         169x 7x   162x 162x 5x   157x 157x 2x   155x                     113x 113x 113x 26x   87x 152x 152x 3x 3x   149x   87x                   60x                 55x 55x 5x   50x 34x 2x 2x   32x   16x 16x               49x 49x 35x 55x 45x   14x 14x 20x 20x      
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import { getLoggerWithTag } from '@global/logger/v2/logger.mjs';
 
const LOG_TAG = 'search-config';
const logger = getLoggerWithTag(LOG_TAG);
 
/**
 * @typedef {Object} SearchEntityConfig
 * @property {string} entityId
 * @property {string} [datasetId]
 */
 
/**
 * @param {unknown} entry
 * @returns {SearchEntityConfig|null}
 */
export function parseSearchEntityEntry(entry) {
    if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
        return null;
    }
    const entityId = entry.id;
    if (!entityId || typeof entityId !== 'string') {
        return null;
    }
    const datasetId = entry.dataset;
    if (datasetId != null && typeof datasetId !== 'string') {
        return null;
    }
    return {
        entityId,
        datasetId: datasetId || undefined
    };
}
 
/**
 * @param {object} manifest
 * @returns {Map<string, SearchEntityConfig>}
 */
export function parseSearchEntitiesConfig(manifest) {
    const result = new Map();
    const entries = manifest?.search?.entities;
    if (!Array.isArray(entries)) {
        return result;
    }
    for (const entry of entries) {
        const parsed = parseSearchEntityEntry(entry);
        if (!parsed) {
            logger.warn(() => `Invalid search.entities entry: ${JSON.stringify(entry)}`);
            continue;
        }
        result.set(parsed.entityId, parsed);
    }
    return result;
}
 
/**
 * @param {object} manifest
 * @param {string} entityId
 * @returns {SearchEntityConfig|null}
 */
// TODO: ERA-2782 - some functions in this file run very often, their output could be calculated once during manifest creation.
export function getSearchEntityConfig(manifest, entityId) {
    return parseSearchEntitiesConfig(manifest).get(entityId) ?? null;
}
 
/**
 * @param {object} manifest
 * @param {SearchEntityConfig} config
 * @returns {boolean}
 */
function isSearchEntityAvailable(manifest, config) {
    const ent = manifest?.entities?.[config.entityId];
    if (!ent?.schema || !ent?.title) {
        return false;
    }
    if (config.datasetId) {
        if (!manifest?.datasets?.[config.datasetId]) {
            logger.warn(() => `Search entity ${config.entityId}: dataset ${config.datasetId} not found in manifest`);
            return false;
        }
        return true;
    }
    const lakeData = manifest?.[config.entityId];
    return lakeData && typeof lakeData === 'object';
}
 
/**
 * @param {object} manifest
 * @returns {string[]}
 */
export function getSearchableEntityIds(manifest) {
    const configMap = parseSearchEntitiesConfig(manifest);
    if (configMap.size > 0) {
        return [...configMap.values()]
            .filter(config => isSearchEntityAvailable(manifest, config))
            .map(config => config.entityId);
    }
    const entities = manifest?.entities || {};
    return Object.keys(entities).filter(key => {
        const ent = entities[key];
        return ent?.schema && ent?.title && manifest[key] && typeof manifest[key] === 'object';
    });
}