|
| 1 | +--- |
| 2 | +id: getting-started |
| 3 | +title: Getting started |
| 4 | +--- |
| 5 | + |
| 6 | +`@grafana/scenes-ml` is a separate npm package which lets you add Machine Learning powered functionality to your scenes. |
| 7 | + |
| 8 | +This topic explains hows to install Scenes ML and use it within a Grafana App plugin. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +If you're adding Scenes ML to an existing Scenes app plugin, first make sure your plugin config is up-to-date by running: |
| 13 | + |
| 14 | +```bash |
| 15 | +npx @grafana/create-plugin@latest --update |
| 16 | +``` |
| 17 | + |
| 18 | +Then add `@grafana/scenes-ml` to your plugin by running the following commands in your project: |
| 19 | + |
| 20 | +```bash |
| 21 | +yarn add @grafana/scenes-ml |
| 22 | +``` |
| 23 | + |
| 24 | +## Add ML features to a scene |
| 25 | + |
| 26 | +### 1. Create a scene |
| 27 | + |
| 28 | +Create a scene using the snippet below. This will add a time series panel to the scene with built-in controls to add trend, lower and upper bounds to all series in the panel. |
| 29 | + |
| 30 | +```ts |
| 31 | +// helloMLScene.ts |
| 32 | + |
| 33 | +import { EmbeddedScene, SceneFlexLayout, SceneFlexItem, SceneQueryRunner, PanelBuilders, sceneUtils } from '@grafana/scenes'; |
| 34 | +import { SceneBaseliner, MLDemoDS } from '@grafana/scenes-ml'; |
| 35 | + |
| 36 | +// Register the demo datasource from `scenes-ml`. |
| 37 | +// This isn't required for normal usage, it just gives us some sensible demo data. |
| 38 | +sceneUtils.registerRuntimeDataSource({ dataSource: new MLDemoDS('ml-test', 'ml-test') }) |
| 39 | + |
| 40 | +function getForecastQueryRunner() { |
| 41 | + return new SceneQueryRunner({ |
| 42 | + queries: [ |
| 43 | + { refId: 'A', datasource: { uid: 'ml-test', type: 'ml-test', }, type: 'forecasts' }, |
| 44 | + ], |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +export function getScene() { |
| 49 | + return new EmbeddedScene({ |
| 50 | + body: new SceneFlexLayout({ |
| 51 | + children: [ |
| 52 | + new SceneFlexItem({ |
| 53 | + width: '50%', |
| 54 | + height: 300, |
| 55 | + body: PanelBuilders.timeseries() |
| 56 | + .setTitle('Forecast demo') |
| 57 | + .setData(getForecastQueryRunner()) |
| 58 | + // Add the `SceneBaseliner` to the panel. |
| 59 | + .setHeaderActions([new SceneBaseliner({ interval: 0.95 })]) |
| 60 | + .build() |
| 61 | + }), |
| 62 | + ], |
| 63 | + }), |
| 64 | + }); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### 2. Render the scene |
| 69 | + |
| 70 | +Use the following code in your Grafana app plugin page to render the "Hello ML" scene: |
| 71 | + |
| 72 | +```tsx |
| 73 | +import React from 'react'; |
| 74 | +import { getScene } from './helloMLScene'; |
| 75 | + |
| 76 | +export const HelloMLPluginPage = () => { |
| 77 | + const scene = getScene(); |
| 78 | + |
| 79 | + return <scene.Component model={scene} />; |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +## Source code |
| 84 | + |
| 85 | +[View the example source code](https://github.com/grafana/scenes/tree/main/docusaurus/docs/scenes-ml/getting-started.tsx) |
0 commit comments