Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Updates Scenes templates #1538

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
import React from 'react';
import { SceneApp, useSceneApp } from '@grafana/scenes';
import { AppRootProps } from '@grafana/data';
import { config } from '@grafana/runtime';
import { Alert } from '@grafana/ui';
import { DATASOURCE_REF } from '../../constants';
import { PluginPropsContext } from '../../utils/utils.plugin';
import { Routes } from '../Routes/Routes';
import { helloWorldPage } from '../../pages/HelloWorld/helloWorldPage';
import { homePage } from '../../pages/Home/homePage';
import { withDrilldownPage } from '../../pages/WithDrilldown/withDrilldownPage';
import { withTabsPage } from '../../pages/WithTabs/withTabsPage';

function getSceneApp() {
return new SceneApp({
pages: [helloWorldPage, homePage, withDrilldownPage, withTabsPage],
urlSyncOptions: {
updateUrlOnInit: true,
createBrowserHistorySteps: true,
},
});
}

function AppWithScenes() {
const scene = useSceneApp(getSceneApp);

return (
<>
{!config.datasources[DATASOURCE_REF.uid] && (
<Alert title={`Missing ${DATASOURCE_REF.uid} datasource`}>
These demos depend on <b>testdata</b> datasource: <code>{JSON.stringify(DATASOURCE_REF)}</code>. See{' '}
<a href="https://github.com/grafana/grafana/tree/main/devenv#set-up-your-development-environment">
https://github.com/grafana/grafana/tree/main/devenv#set-up-your-development-environment
</a>{' '}
for more details.
</Alert>
)}

<scene.Component model={scene} />
</>
);
}


function App(props: AppRootProps) {
return (
<PluginPropsContext.Provider value={props}>
<Routes />
<AppWithScenes />
</PluginPropsContext.Provider>
);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SceneAppPage } from '@grafana/scenes';
import { helloWorldScene } from './helloWorldScene';
import { prefixRoute } from '../../utils/utils.routing';
import { ROUTES } from '../../constants';

export const helloWorldPage = new SceneAppPage({
title: 'Hello World',
url: prefixRoute(ROUTES.HelloWorld),
getScene: helloWorldScene,
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmbeddedScene, SceneFlexLayout, SceneFlexItem, PanelBuilders } from '@grafana/scenes';

export function getScene() {
export function helloWorldScene() {
return new EmbeddedScene({
body: new SceneFlexLayout({
children: [
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SceneAppPage } from '@grafana/scenes';
import { homeScene } from './homeScene';
import { prefixRoute } from '../../utils/utils.routing';
import { ROUTES } from '../../constants';

export const homePage = new SceneAppPage({
title: 'Home page',
url: prefixRoute(ROUTES.Home),
subTitle:
'This scene showcases a basic scene functionality, including query runner, variable and a custom scene object.',
getScene: () => homeScene(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { DATASOURCE_REF } from '../../constants';
import { CustomSceneObject } from './CustomSceneObject';

export function getBasicScene(templatised = true, seriesToShow = '__server_names') {
export function homeScene(templatised = true, seriesToShow = '__server_names') {
const timeRange = new SceneTimeRange({
from: 'now-6h',
to: 'now',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ReducerID } from '@grafana/data';
import { PanelBuilders, SceneDataTransformer } from '@grafana/scenes';

export function RoomTemperatureStat(reducers: ReducerID[]) {
const data = new SceneDataTransformer({
transformations: [
{
id: 'reduce',
options: {
reducers,
},
},
],
});
return PanelBuilders.stat().setData(data).setUnit('celsius').build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { PanelBuilders, SceneByFrameRepeater, SceneDataNode, SceneFlexItem, SceneFlexLayout } from '@grafana/scenes';

export function RoomsTemperatureStat() {
const stat = PanelBuilders.stat()
.setUnit('celsius')
.setLinks([
{
title: 'Go to room temperature overview',
url: '${__url.path}/room/${__field.name}/temperature${__url.params}',
},
{
title: 'Go to room humidity overview',
url: '${__url.path}/room/${__field.name}/humidity${__url.params}',
},
]);

return new SceneByFrameRepeater({
body: new SceneFlexLayout({
direction: 'row',
wrap: 'wrap',
children: [],
}),
getLayoutChild: (data, frame) => {
return new SceneFlexItem({
height: '50%',
minWidth: '20%',
body: stat
.setTitle(frame.name || '')
.setData(
new SceneDataNode({
data: {
...data,
series: [frame],
},
})
)
.build(),
});
},
});
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { ReducerID } from '@grafana/data';
import {
PanelBuilders,
SceneByFrameRepeater,
SceneDataNode,
SceneDataTransformer,
SceneFlexItem,
SceneFlexLayout,
} from '@grafana/scenes';
import { PanelBuilders, SceneDataTransformer } from '@grafana/scenes';
import { TableCellBackgroundDisplayMode, TableCellDisplayMode, ThresholdsMode } from '@grafana/schema';

export function getRoomsTemperatureTable() {
export function RoomsTemperatureTable() {
const data = new SceneDataTransformer({
transformations: [
{
Expand Down Expand Up @@ -75,57 +67,3 @@ export function getRoomsTemperatureTable() {
)
.build();
}

export function getRoomsTemperatureStats() {
const stat = PanelBuilders.stat()
.setUnit('celsius')
.setLinks([
{
title: 'Go to room temperature overview',
url: '${__url.path}/room/${__field.name}/temperature${__url.params}',
},
{
title: 'Go to room humidity overview',
url: '${__url.path}/room/${__field.name}/humidity${__url.params}',
},
]);

return new SceneByFrameRepeater({
body: new SceneFlexLayout({
direction: 'row',
wrap: 'wrap',
children: [],
}),
getLayoutChild: (data, frame) => {
return new SceneFlexItem({
height: '50%',
minWidth: '20%',
body: stat
.setTitle(frame.name || '')
.setData(
new SceneDataNode({
data: {
...data,
series: [frame],
},
})
)
.build(),
});
},
});
}

export function getRoomTemperatureStatPanel(reducers: ReducerID[]) {
const data = new SceneDataTransformer({
transformations: [
{
id: 'reduce',
options: {
reducers,
},
},
],
});
return PanelBuilders.stat().setData(data).setUnit('celsius').build();
}
Loading
Loading