mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-07 05:52:57 +00:00
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import {SCBulkResponse, SCThingType, SCUuid} from '@openstapps/core';
|
|
|
|
/**
|
|
* Length of the index UID used for generation of its name
|
|
*/
|
|
export const INDEX_UID_LENGTH = 8;
|
|
|
|
/**
|
|
* A string which matches all indices
|
|
*/
|
|
export const ALL_INDICES_QUERY = 'stapps_*_*_*';
|
|
|
|
/**
|
|
* Matches index names such as stapps_<type>_<source>_<random suffix>
|
|
*/
|
|
export const VALID_INDEX_REGEX = /^stapps_([A-z0-9_]+)_([a-z0-9-_]+)_([-a-z0-9^_]+)$/;
|
|
|
|
export interface ParsedIndexName {
|
|
type: SCThingType;
|
|
source: string;
|
|
randomSuffix: string;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
export function parseIndexName(index: string): ParsedIndexName {
|
|
const match = VALID_INDEX_REGEX.exec(index);
|
|
if (!match) {
|
|
throw new SyntaxError(`Invalid index name ${index}!`);
|
|
}
|
|
|
|
return {
|
|
type: match[1] as SCThingType,
|
|
source: match[2],
|
|
randomSuffix: match[3],
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Gets the index name in elasticsearch for one SCThingType
|
|
*
|
|
* @param type SCThingType of data in the index
|
|
* @param source source of data in the index
|
|
* @param bulk bulk process which created this index
|
|
*/
|
|
export function getThingIndexName(type: SCThingType, source: string, bulk: SCBulkResponse) {
|
|
let out = type.toLowerCase();
|
|
while (out.includes(' ')) {
|
|
out = out.replace(' ', '_');
|
|
}
|
|
|
|
return `stapps_${out}_${source}_${getIndexUID(bulk.uid)}`;
|
|
}
|
|
|
|
/**
|
|
* Provides the index UID (for its name) from the bulk UID
|
|
*
|
|
* @param uid Bulk UID
|
|
*/
|
|
export function getIndexUID(uid: SCUuid) {
|
|
return uid.slice(0, Math.max(0, INDEX_UID_LENGTH));
|
|
}
|