forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sample data] handle index aliases when installing/uninstalling datas…
…ets (elastic#122689) * refactor install/uninstall routes * only skip failing tests * check for aliases when uninstalling sample datasets * fix return value * add unit tests * factorize installer creation * add tests for the alias scenario Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
b0f4e88
commit 683ab10
Showing
10 changed files
with
740 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export class SampleDataInstallError extends Error { | ||
constructor(message: string, public readonly httpCode: number) { | ||
super(message); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/plugins/home/server/services/sample_data/lib/insert_data_into_index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { IScopedClusterClient, Logger } from 'kibana/server'; | ||
import type { DataIndexSchema } from './sample_dataset_registry_types'; | ||
import { | ||
translateTimeRelativeToDifference, | ||
translateTimeRelativeToWeek, | ||
} from './translate_timestamp'; | ||
import { loadData } from './load_data'; | ||
|
||
export const insertDataIntoIndex = ({ | ||
dataIndexConfig, | ||
logger, | ||
esClient, | ||
index, | ||
nowReference, | ||
}: { | ||
dataIndexConfig: DataIndexSchema; | ||
index: string; | ||
nowReference: string; | ||
esClient: IScopedClusterClient; | ||
logger: Logger; | ||
}) => { | ||
const updateTimestamps = (doc: any) => { | ||
dataIndexConfig.timeFields | ||
.filter((timeFieldName: string) => doc[timeFieldName]) | ||
.forEach((timeFieldName: string) => { | ||
doc[timeFieldName] = dataIndexConfig.preserveDayOfWeekTimeOfDay | ||
? translateTimeRelativeToWeek( | ||
doc[timeFieldName], | ||
dataIndexConfig.currentTimeMarker, | ||
nowReference | ||
) | ||
: translateTimeRelativeToDifference( | ||
doc[timeFieldName], | ||
dataIndexConfig.currentTimeMarker, | ||
nowReference | ||
); | ||
}); | ||
return doc; | ||
}; | ||
|
||
const bulkInsert = async (docs: unknown[]) => { | ||
const insertCmd = { index: { _index: index } }; | ||
const bulk: unknown[] = []; | ||
docs.forEach((doc: unknown) => { | ||
bulk.push(insertCmd); | ||
bulk.push(updateTimestamps(doc)); | ||
}); | ||
|
||
const { body: resp } = await esClient.asCurrentUser.bulk({ | ||
body: bulk, | ||
}); | ||
|
||
if (resp.errors) { | ||
const errMsg = `sample_data install errors while bulk inserting. Elasticsearch response: ${JSON.stringify( | ||
resp, | ||
null, | ||
'' | ||
)}`; | ||
logger.warn(errMsg); | ||
return Promise.reject( | ||
new Error(`Unable to load sample data into index "${index}", see kibana logs for details`) | ||
); | ||
} | ||
}; | ||
return loadData(dataIndexConfig.dataPath, bulkInsert); // this returns a Promise | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.