refactor: change function of put es templates

This commit is contained in:
Wieland Schöbl
2021-08-05 14:47:13 +02:00
parent bf862cfb78
commit ccb4cedc5f
3 changed files with 35 additions and 22 deletions

View File

@@ -20,6 +20,7 @@ import {resolve} from 'path';
import {exit} from 'process';
import {generateTemplate} from './mapping';
import {getProjectReflection} from './project-reflection';
import {ElasticsearchTemplateCollection} from './types/mapping';
// handle unhandled promise rejections
process.on('unhandledRejection', async (reason: unknown) => {
@@ -81,38 +82,31 @@ commander
// tslint:disable-next-line:no-magic-numbers
writeFileSync(errPath, JSON.stringify(result.errors, null, 2));
Logger.ok(`Mapping errors written to ${errPath}.`);
} else {
for (const error of result.errors) {
await Logger.error(error);
}
throw new Error('Mapping generation failed');
}
});
commander
.command('put-es-templates <srcPath> <esAddress> [ignoredTags]')
.action(async (relativeSrcPath, esAddress, ignoredTags) => {
.action(async (relativeSrcPath, esAddress) => {
// get absolute paths
const srcPath = resolve(relativeSrcPath);
let ignoredTagsList: string[] = [];
if (typeof ignoredTags === 'string') {
ignoredTagsList = ignoredTags.split(',');
}
// get project reflection
const projectReflection = getProjectReflection(srcPath);
const templates = JSON.parse(srcPath) as ElasticsearchTemplateCollection;
const result = generateTemplate(projectReflection, ignoredTagsList, true);
if (result.errors.length !== 0) {
await Logger.error(`Mapping generated with errors:\n${JSON.stringify(result.errors)}`);
exit(-1);
} else {
Logger.ok('Mapping generated without errors!');
}
for (const template in result.mappings) {
if (!result.mappings.hasOwnProperty(template)) {
for (const template in templates) {
if (!templates.hasOwnProperty(template)) {
continue;
}
const response = await got.put(`${esAddress}_template/${template}`, {
json: result.mappings[template],
json: templates[template],
});
const HTTP_STATUS_OK = 200;

View File

@@ -38,7 +38,7 @@ import {
ElasticsearchObject,
ElasticsearchTemplateCollection,
ElasticsearchType,
ElasticsearchValue,
ElasticsearchValue, MappingGenTemplate,
} from './types/mapping';
let dynamicTemplates: ElasticsearchDynamicTemplate[] = [];
@@ -665,9 +665,7 @@ function reset(resetInheritTags = true) {
export function generateTemplate(projectReflection: ProjectReflection,
ignoredTags: string[],
showErrorOutput = true,
interfaceFilter: string[] = []):
// tslint:disable-next-line:completed-docs
{ aggregations: AggregationSchema; errors: string[]; mappings: ElasticsearchTemplateCollection; } {
interfaceFilter: string[] = []): MappingGenTemplate {
reset();
showErrors = showErrorOutput;

View File

@@ -13,9 +13,30 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {ElasticsearchDataType} from '../config/typemap';
import {AggregationSchema} from './aggregation';
// tslint:disable:no-any
/**
* Template output of the mapping generation
*/
export interface MappingGenTemplate {
/**
* All generated aggregations
*/
aggregations: AggregationSchema;
/**
* All errors that occurred
*/
errors: string[];
/**
* All mappings that were generated
*/
mappings: ElasticsearchTemplateCollection;
}
/**
* ElasticsearchValue can be either a type or an object.
*