kibana/x-pack/plugins/ml/scripts/apidoc_scripts
Dima Arnautov 4c3afc5f42
[ML] Update code editors for Transform, Data Frame and Anomaly Detection wizards (#184518)
## Summary

Closes https://github.com/elastic/kibana/issues/66716

Improves code editors in Anomaly detection, Data frame analytics and
Transform wizards with autocomplete, data types validation and inline
documentation from elasticsearch specification.

![Jun-19-2024
15-33-00](c230deae-962e-4295-8146-0bf3579a66bd)


Adds a package with JSON schemas extracted from the [openapi output of
elasticsearch-specification](https://github.com/elastic/elasticsearch-specification/tree/main/output/openapi).

Schema file is generated per editor/endpoint, keeping only relevant
components.

To test the script locally, execute 
```
yarn run jsonSchema
```
from `/x-pack/packages/ml/json_schemas`.

By default it assumes that your `elasticsearch-specification` folder is
located next to the `kibana` repo, but you can also provide a path to
`openapi` file as a parameter, e.g. `yarn run jsonSchema
/Users/my_user/dev/elasticsearch-specification/output/openapi/elasticsearch-serverless-openapi.json`

#### How JSON files are served 

JSON files are asynchronously imported at the React component level and
passed to the `CodeEditor` instances on mount.

Initially I tried different approaches to take advantage of CDN, but
unfortunately it didn't work out:

#####  Using static assets
 
I made an attempt to retrieve a URL to the JSON schema file as a static
asset using Kibana service
```typescript
const schemaJsonAsset = http?.staticAssets.getPluginAssetHref('my_schema.json') ?? '';
```
and passing it as part of the schema definition, but the browser was
blocking a request 🤔


![image](accf47b1-2a89-4408-9c5a-36bb269e8889)

![image](aa64ae66-8749-4d4d-b645-6cd11b221c68)

##### Using raw loader

Approach with a raw loader also didn't succeed.

```typescript
import mySchema from '!!raw-loader!./my_schema.json';
```

<details>
  <summary>See error </summary>
 
  ```
ERROR in
./public/app/sections/create_transform/components/advanced_pivot_editor/my_schema.json
(/Users/dimaarnautov/Repos/kibana/node_modules/raw-loader/dist/cjs.js!./public/app/sections/create_transform/components/advanced_pivot_editor/my_schema.json)
│ Module parse failed: Unexpected token 'e', "export def"... is not
valid JSON while parsing 'export default "{\n \"type\": \"object\'
       │          File was processed with these loaders:
       │           * ../../../node_modules/raw-loader/dist/cjs.js
│ You may need an additional loader to handle the result of these
loaders.
│ SyntaxError: Unexpected token 'e', "export def"... is not valid JSON
while parsing 'export default "{\n \"type\": \"object\'
       │              at JSON.parse (<anonymous>)
│ at parseJson
(/Users/dimaarnautov/Repos/kibana/node_modules/json-parse-better-errors/index.js:7:17)
│ at JsonParser.parse
(/Users/dimaarnautov/Repos/kibana/node_modules/webpack/lib/JsonParser.js:16:16)
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/webpack/lib/NormalModule.js:482:32
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/webpack/lib/NormalModule.js:358:12
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:373:3
│ at iterateNormalLoaders
(/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
│ at iterateNormalLoaders
(/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:236:3
│ at runSyncOrAsync
(/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
│ at iterateNormalLoaders
(/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/loader-runner/lib/LoaderRunner.js:205:4
│ at
/Users/dimaarnautov/Repos/kibana/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:85:15
│ at processTicksAndRejections (node:internal/process/task_queues:77:11)
  ```
</details>



### Checklist

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
2024-06-21 16:43:20 +02:00
..
apidoc_config [ML] Update code editors for Transform, Data Frame and Anomaly Detection wizards (#184518) 2024-06-21 16:43:20 +02:00
content_page [ML] Fix indentation in the API content page template (#155498) 2023-04-21 14:46:47 +02:00
header_generator
schema_extractor
schema_parser
schema_worker [ML] Enable consistent-type-imports eslint rule (#176921) 2024-03-07 03:29:20 -07:00
version_filter [ML] Enable consistent-type-imports eslint rule (#176921) 2024-03-07 03:29:20 -07:00
template.md
types.ts