|
| 1 | +import { css } from '@emotion/css'; |
| 2 | +import { GrafanaTheme2 } from '@grafana/data'; |
| 3 | +import { SceneComponentProps, sceneGraph, SceneObjectBase, SceneObjectState } from '@grafana/scenes'; |
| 4 | +import { Alert, Button, IconButton, Spinner, useStyles2 } from '@grafana/ui'; |
| 5 | +import { getProfileMetric, ProfileMetricId } from '@shared/infrastructure/profile-metrics/getProfileMetric'; |
| 6 | +import { DomainHookReturnValue } from '@shared/types/DomainHookReturnValue'; |
| 7 | +import { InlineBanner } from '@shared/ui/InlineBanner'; |
| 8 | +import { Panel } from '@shared/ui/Panel/Panel'; |
| 9 | +import React from 'react'; |
| 10 | + |
| 11 | +import { ProfilesDataSourceVariable } from '../../domain/variables/ProfilesDataSourceVariable'; |
| 12 | +import { getSceneVariableValue } from '../../helpers/getSceneVariableValue'; |
| 13 | +import { AiReply } from './components/AiReply'; |
| 14 | +import { FollowUpForm } from './components/FollowUpForm'; |
| 15 | +import { useOpenAiChatCompletions } from './domain/useOpenAiChatCompletions'; |
| 16 | +import { FetchParams, useFetchDotProfiles } from './infrastructure/useFetchDotProfiles'; |
| 17 | + |
| 18 | +interface SceneAiPanelState extends SceneObjectState {} |
| 19 | + |
| 20 | +export class SceneAiPanel extends SceneObjectBase<SceneAiPanelState> { |
| 21 | + constructor() { |
| 22 | + super({ key: 'ai-panel' }); |
| 23 | + } |
| 24 | + |
| 25 | + validateFetchParams(isDiff: boolean, fetchParams: FetchParams) { |
| 26 | + let params = fetchParams; |
| 27 | + let error; |
| 28 | + |
| 29 | + if (isDiff && fetchParams.length !== 2) { |
| 30 | + error = new Error( |
| 31 | + `Invalid number of fetch parameters for analyzing the diff flame graph (${fetchParams.length})!` |
| 32 | + ); |
| 33 | + params = []; |
| 34 | + } else if (!isDiff && fetchParams.length !== 1) { |
| 35 | + error = new Error(`Invalid number of fetch parameters for analyzing the flame graph (${fetchParams.length})!`); |
| 36 | + params = []; |
| 37 | + } |
| 38 | + |
| 39 | + return { params, error }; |
| 40 | + } |
| 41 | + |
| 42 | + useSceneAiPanel = (isDiff: boolean, fetchParams: FetchParams): DomainHookReturnValue => { |
| 43 | + const dataSourceUid = sceneGraph.findByKeyAndType(this, 'dataSource', ProfilesDataSourceVariable).useState() |
| 44 | + .value as string; |
| 45 | + |
| 46 | + const { params, error: validationError } = this.validateFetchParams(isDiff, fetchParams); |
| 47 | + |
| 48 | + const { error: fetchError, isFetching, profiles } = useFetchDotProfiles(dataSourceUid, params); |
| 49 | + |
| 50 | + const profileMetricId = getSceneVariableValue(this, 'profileMetricId'); |
| 51 | + const profileType = getProfileMetric(profileMetricId as ProfileMetricId).type; |
| 52 | + |
| 53 | + const { reply, error: llmError, retry } = useOpenAiChatCompletions(profileType, profiles); |
| 54 | + |
| 55 | + return { |
| 56 | + data: { |
| 57 | + validationError, |
| 58 | + isLoading: isFetching || (!isFetching && !fetchError && !llmError && !reply.text.trim()), |
| 59 | + fetchError, |
| 60 | + llmError, |
| 61 | + reply, |
| 62 | + shouldDisplayReply: Boolean(reply?.hasStarted || reply?.hasFinished), |
| 63 | + shouldDisplayFollowUpForm: !fetchError && !llmError && Boolean(reply?.hasFinished), |
| 64 | + }, |
| 65 | + actions: { |
| 66 | + retry, |
| 67 | + submitFollowupQuestion(question: string) { |
| 68 | + reply.askFollowupQuestion(question); |
| 69 | + }, |
| 70 | + }, |
| 71 | + }; |
| 72 | + }; |
| 73 | + |
| 74 | + static Component = ({ |
| 75 | + model, |
| 76 | + isDiff, |
| 77 | + fetchParams, |
| 78 | + onClose, |
| 79 | + }: SceneComponentProps<SceneAiPanel> & { |
| 80 | + isDiff: boolean; |
| 81 | + fetchParams: FetchParams; |
| 82 | + onClose: () => void; |
| 83 | + }) => { |
| 84 | + const styles = useStyles2(getStyles); |
| 85 | + const { data, actions } = model.useSceneAiPanel(isDiff, fetchParams); |
| 86 | + |
| 87 | + return ( |
| 88 | + <Panel |
| 89 | + className={styles.sidePanel} |
| 90 | + title="Flame graph analysis" |
| 91 | + isLoading={data.isLoading} |
| 92 | + headerActions={ |
| 93 | + <IconButton |
| 94 | + title="Close panel" |
| 95 | + name="times-circle" |
| 96 | + variant="secondary" |
| 97 | + aria-label="close" |
| 98 | + onClick={onClose} |
| 99 | + /> |
| 100 | + } |
| 101 | + dataTestId="ai-panel" |
| 102 | + > |
| 103 | + <div className={styles.content}> |
| 104 | + {data.validationError && ( |
| 105 | + <InlineBanner severity="error" title="Validation error!" errors={[data.validationError]} /> |
| 106 | + )} |
| 107 | + |
| 108 | + {data.fetchError && ( |
| 109 | + <InlineBanner |
| 110 | + severity="error" |
| 111 | + title="Error while loading profile data!" |
| 112 | + message="Sorry for any inconvenience, please try again later." |
| 113 | + errors={[data.fetchError]} |
| 114 | + /> |
| 115 | + )} |
| 116 | + |
| 117 | + {data.shouldDisplayReply && <AiReply reply={data.reply} />} |
| 118 | + |
| 119 | + {data.isLoading && ( |
| 120 | + <> |
| 121 | + <Spinner inline /> |
| 122 | + Analyzing... |
| 123 | + </> |
| 124 | + )} |
| 125 | + |
| 126 | + {data.llmError && ( |
| 127 | + <Alert title="An error occured while generating content using OpenAI!" severity="warning"> |
| 128 | + <div> |
| 129 | + <div> |
| 130 | + <p>{data.llmError.message}</p> |
| 131 | + <p> |
| 132 | + Sorry for any inconvenience, please retry or if the problem persists, contact your organization |
| 133 | + admin. |
| 134 | + </p> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + <Button className={styles.retryButton} variant="secondary" fill="outline" onClick={() => actions.retry()}> |
| 138 | + Retry |
| 139 | + </Button> |
| 140 | + </Alert> |
| 141 | + )} |
| 142 | + |
| 143 | + {data.shouldDisplayFollowUpForm && <FollowUpForm onSubmit={actions.submitFollowupQuestion} />} |
| 144 | + </div> |
| 145 | + </Panel> |
| 146 | + ); |
| 147 | + }; |
| 148 | +} |
| 149 | + |
| 150 | +const getStyles = (theme: GrafanaTheme2) => ({ |
| 151 | + sidePanel: css` |
| 152 | + flex: 1 0 50%; |
| 153 | + margin-left: 8px; |
| 154 | + max-width: calc(50% - 4px); |
| 155 | + `, |
| 156 | + title: css` |
| 157 | + margin: -4px 0 4px 0; |
| 158 | + `, |
| 159 | + content: css` |
| 160 | + padding: ${theme.spacing(1)}; |
| 161 | + `, |
| 162 | + retryButton: css` |
| 163 | + float: right; |
| 164 | + `, |
| 165 | +}); |
0 commit comments