-
Notifications
You must be signed in to change notification settings - Fork 57
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] Enable ppl visualization in Chatbot #1304
Merged
joshuali925
merged 6 commits into
opensearch-project:main
from
SuZhou-Joe:feature/enable_ppl_visualization_on_main
Dec 21, 2023
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b2d818
temp: enable_ppl_visualization
SuZhou-Joe 357b083
feat: add some fix
SuZhou-Joe 51ab4aa
fix: type declaration
SuZhou-Joe 1f9cc08
fix: public type error
SuZhou-Joe 6e63c4d
fix: type error
SuZhou-Joe aea073d
fix: render chart
SuZhou-Joe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"visualizations" | ||
], | ||
"optionalPlugins": [ | ||
"managementOverview" | ||
"managementOverview", | ||
"assistantDashboards" | ||
] | ||
} |
78 changes: 78 additions & 0 deletions
78
public/dependencies/components/ppl_visualization_model.tsx
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,78 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
EuiButton, | ||
EuiButtonEmpty, | ||
EuiCodeBlock, | ||
EuiModalBody, | ||
EuiModalFooter, | ||
EuiModalHeader, | ||
EuiModalHeaderTitle, | ||
} from '@elastic/eui'; | ||
import React from 'react'; | ||
import { SavedVisualization } from '../../../common/types/explorer'; | ||
import { SavedObjectVisualization } from '../../components/visualizations/saved_object_visualization'; | ||
import { PPLSavedVisualizationClient } from '../../services/saved_objects/saved_object_client/ppl'; | ||
|
||
interface PPLVisualizationModelProps { | ||
savedVisualization: SavedVisualization; | ||
onClose: () => void; | ||
} | ||
|
||
export const PPLVisualizationModal: React.FC<PPLVisualizationModelProps> = (props) => { | ||
return ( | ||
<> | ||
<EuiModalHeader> | ||
<EuiModalHeaderTitle style={{ fontSize: '1.25rem' }}> | ||
{props.savedVisualization.name} | ||
</EuiModalHeaderTitle> | ||
</EuiModalHeader> | ||
|
||
<EuiModalBody> | ||
<div> | ||
<EuiCodeBlock isCopyable>{props.savedVisualization.query}</EuiCodeBlock> | ||
<SavedObjectVisualization | ||
savedVisualization={props.savedVisualization} | ||
timeRange={{ | ||
from: props.savedVisualization.selected_date_range.start, | ||
to: props.savedVisualization.selected_date_range.end, | ||
}} | ||
/> | ||
</div> | ||
</EuiModalBody> | ||
|
||
<EuiModalFooter> | ||
<EuiButton | ||
onClick={async () => { | ||
const response = await savePPLVisualization(props.savedVisualization); | ||
props.onClose(); | ||
window.open(`./observability-logs#/explorer/${response.objectId}`, '_blank'); | ||
}} | ||
fill | ||
> | ||
Save | ||
</EuiButton> | ||
<EuiButtonEmpty onClick={props.onClose}>Close</EuiButtonEmpty> | ||
</EuiModalFooter> | ||
</> | ||
); | ||
}; | ||
|
||
const savePPLVisualization = (savedVisualization: SavedVisualization) => { | ||
const createParams = { | ||
query: savedVisualization.query, | ||
name: savedVisualization.name, | ||
dateRange: [ | ||
savedVisualization.selected_date_range.start, | ||
savedVisualization.selected_date_range.end, | ||
], | ||
fields: [], | ||
timestamp: '', | ||
type: savedVisualization.type, | ||
sub_type: 'visualization', | ||
}; | ||
return PPLSavedVisualizationClient.getInstance().create(createParams); | ||
}; |
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,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { merge } from 'lodash'; | ||
import React from 'react'; | ||
import { toMountPoint } from '../../../../src/plugins/opensearch_dashboards_react/public'; | ||
import { SavedVisualization } from '../../common/types/explorer'; | ||
import { SavedObjectVisualization } from '../components/visualizations/saved_object_visualization'; | ||
import { coreRefs } from '../framework/core_refs'; | ||
import { AssistantSetup } from '../types'; | ||
import { PPLVisualizationModal } from './components/ppl_visualization_model'; | ||
|
||
export const registerAsssitantDependencies = (setup?: AssistantSetup) => { | ||
if (!setup) return; | ||
|
||
setup.registerContentRenderer('ppl_visualization', (content) => { | ||
const params = content as Partial<SavedVisualization>; | ||
const savedVisualization = createSavedVisualization(params); | ||
return ( | ||
<SavedObjectVisualization | ||
savedVisualization={savedVisualization} | ||
timeRange={{ | ||
from: savedVisualization.selected_date_range.start, | ||
to: savedVisualization.selected_date_range.end, | ||
}} | ||
/> | ||
); | ||
}); | ||
|
||
setup.registerActionExecutor('view_ppl_visualization', async (params) => { | ||
const savedVisualization = createSavedVisualization(params as Partial<SavedVisualization>); | ||
const modal = coreRefs.core!.overlays.openModal( | ||
toMountPoint( | ||
<PPLVisualizationModal | ||
savedVisualization={savedVisualization} | ||
onClose={() => modal.close()} | ||
/> | ||
) | ||
); | ||
}); | ||
}; | ||
|
||
const createSavedVisualization = (params: Partial<SavedVisualization>) => { | ||
return merge( | ||
{ | ||
query: params.query, | ||
selected_date_range: { start: 'now-14d', end: 'now', text: '' }, | ||
selected_timestamp: { name: 'timestamp', type: 'timestamp' }, | ||
selected_fields: { tokens: [], text: '' }, | ||
name: params.name, | ||
description: '', | ||
type: 'line', | ||
sub_type: 'visualization', | ||
}, | ||
{ | ||
selected_date_range: params.selected_date_range, | ||
selected_timestamp: params.selected_timestamp, | ||
type: params.type, | ||
} | ||
) as SavedVisualization; | ||
}; |
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
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,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { MessageParser } from '../types'; | ||
|
||
const extractPPLQueries = (content: string) => { | ||
return Array.from(content.matchAll(/(^|[\n\r]|:)\s*(source\s*=\s*.+)/gi)).map( | ||
(match) => match[2] | ||
); | ||
}; | ||
|
||
export const PPLParsers: MessageParser = { | ||
id: 'ppl_visualization_message', | ||
async parserProvider(interaction) { | ||
const ppls: string[] = (interaction.additional_info?.["PPLTool.output"] as string[] | null)?.flatMap((item: string) => { | ||
let ppl: string = "" | ||
try { | ||
const outputResp = JSON.parse(item); | ||
ppl = outputResp.ppl; | ||
} catch (e) { | ||
ppl = item; | ||
} | ||
|
||
return extractPPLQueries(ppl); | ||
}) || []; | ||
|
||
if (!ppls.length) return []; | ||
|
||
const statsPPLs = ppls.filter((ppl) => /\|\s*stats\s+[^|]+\sby\s/i.test(ppl)); | ||
if (!statsPPLs.length) { | ||
return []; | ||
} | ||
|
||
return statsPPLs.map((query) => ({ | ||
type: 'output', | ||
content: query | ||
.replace(/`/g, '') // workaround for https://github.com/opensearch-project/dashboards-observability/issues/509, https://github.com/opensearch-project/dashboards-observability/issues/557 | ||
.replace(/\bSPAN\(/g, 'span('), // workaround for https://github.com/opensearch-project/dashboards-observability/issues/759 | ||
contentType: 'ppl_visualization', | ||
suggestedActions: [ | ||
{ | ||
message: 'View details', | ||
actionType: 'view_ppl_visualization', | ||
metadata: { query, question: interaction.input }, | ||
}, | ||
], | ||
})); | ||
}, | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add this to
dashboards-observability/public/types.ts
Lines 23 to 29 in 1f9cc08
assistantDashboards?: AssistantSetup;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done for this.