mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
refactor: change function of put es templates
This commit is contained in:
30
src/cli.ts
30
src/cli.ts
@@ -20,6 +20,7 @@ import {resolve} from 'path';
|
|||||||
import {exit} from 'process';
|
import {exit} from 'process';
|
||||||
import {generateTemplate} from './mapping';
|
import {generateTemplate} from './mapping';
|
||||||
import {getProjectReflection} from './project-reflection';
|
import {getProjectReflection} from './project-reflection';
|
||||||
|
import {ElasticsearchTemplateCollection} from './types/mapping';
|
||||||
|
|
||||||
// handle unhandled promise rejections
|
// handle unhandled promise rejections
|
||||||
process.on('unhandledRejection', async (reason: unknown) => {
|
process.on('unhandledRejection', async (reason: unknown) => {
|
||||||
@@ -81,38 +82,31 @@ commander
|
|||||||
// tslint:disable-next-line:no-magic-numbers
|
// tslint:disable-next-line:no-magic-numbers
|
||||||
writeFileSync(errPath, JSON.stringify(result.errors, null, 2));
|
writeFileSync(errPath, JSON.stringify(result.errors, null, 2));
|
||||||
Logger.ok(`Mapping errors written to ${errPath}.`);
|
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
|
commander
|
||||||
.command('put-es-templates <srcPath> <esAddress> [ignoredTags]')
|
.command('put-es-templates <srcPath> <esAddress> [ignoredTags]')
|
||||||
.action(async (relativeSrcPath, esAddress, ignoredTags) => {
|
.action(async (relativeSrcPath, esAddress) => {
|
||||||
// get absolute paths
|
// get absolute paths
|
||||||
const srcPath = resolve(relativeSrcPath);
|
const srcPath = resolve(relativeSrcPath);
|
||||||
|
|
||||||
let ignoredTagsList: string[] = [];
|
|
||||||
if (typeof ignoredTags === 'string') {
|
|
||||||
ignoredTagsList = ignoredTags.split(',');
|
|
||||||
}
|
|
||||||
|
|
||||||
// get project reflection
|
// get project reflection
|
||||||
const projectReflection = getProjectReflection(srcPath);
|
const templates = JSON.parse(srcPath) as ElasticsearchTemplateCollection;
|
||||||
|
|
||||||
const result = generateTemplate(projectReflection, ignoredTagsList, true);
|
for (const template in templates) {
|
||||||
if (result.errors.length !== 0) {
|
if (!templates.hasOwnProperty(template)) {
|
||||||
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)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await got.put(`${esAddress}_template/${template}`, {
|
const response = await got.put(`${esAddress}_template/${template}`, {
|
||||||
json: result.mappings[template],
|
json: templates[template],
|
||||||
});
|
});
|
||||||
|
|
||||||
const HTTP_STATUS_OK = 200;
|
const HTTP_STATUS_OK = 200;
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import {
|
|||||||
ElasticsearchObject,
|
ElasticsearchObject,
|
||||||
ElasticsearchTemplateCollection,
|
ElasticsearchTemplateCollection,
|
||||||
ElasticsearchType,
|
ElasticsearchType,
|
||||||
ElasticsearchValue,
|
ElasticsearchValue, MappingGenTemplate,
|
||||||
} from './types/mapping';
|
} from './types/mapping';
|
||||||
|
|
||||||
let dynamicTemplates: ElasticsearchDynamicTemplate[] = [];
|
let dynamicTemplates: ElasticsearchDynamicTemplate[] = [];
|
||||||
@@ -665,9 +665,7 @@ function reset(resetInheritTags = true) {
|
|||||||
export function generateTemplate(projectReflection: ProjectReflection,
|
export function generateTemplate(projectReflection: ProjectReflection,
|
||||||
ignoredTags: string[],
|
ignoredTags: string[],
|
||||||
showErrorOutput = true,
|
showErrorOutput = true,
|
||||||
interfaceFilter: string[] = []):
|
interfaceFilter: string[] = []): MappingGenTemplate {
|
||||||
// tslint:disable-next-line:completed-docs
|
|
||||||
{ aggregations: AggregationSchema; errors: string[]; mappings: ElasticsearchTemplateCollection; } {
|
|
||||||
reset();
|
reset();
|
||||||
|
|
||||||
showErrors = showErrorOutput;
|
showErrors = showErrorOutput;
|
||||||
|
|||||||
21
src/types/mapping.d.ts
vendored
21
src/types/mapping.d.ts
vendored
@@ -13,9 +13,30 @@
|
|||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {ElasticsearchDataType} from '../config/typemap';
|
import {ElasticsearchDataType} from '../config/typemap';
|
||||||
|
import {AggregationSchema} from './aggregation';
|
||||||
|
|
||||||
// tslint:disable:no-any
|
// 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.
|
* ElasticsearchValue can be either a type or an object.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user