-
Notifications
You must be signed in to change notification settings - Fork 936
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
feat(vis_type_vega): support reading time field #9152
base: main
Are you sure you want to change the base?
Changes from 3 commits
a4be69c
6e2791c
0ad3147
5bbbb2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- Vega visualization with ppl now supports reading time field ([#9152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9152)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,13 @@ | |
*/ | ||
|
||
import { i18n } from '@osd/i18n'; | ||
import moment from 'moment'; | ||
|
||
import { Data, UrlObject, PPLQueryRequest } from './types'; | ||
import { SearchAPI } from './search_api'; | ||
import { TimeCache } from './time_cache'; | ||
|
||
const TIMEFIELD = '%timefield%'; | ||
|
||
const getRequestName = (request: PPLQueryRequest, index: number) => | ||
request.dataObject.name || | ||
|
@@ -15,13 +20,29 @@ | |
}); | ||
|
||
export class PPLQueryParser { | ||
searchAPI: SearchAPI; | ||
|
||
constructor(searchAPI: SearchAPI) { | ||
constructor(private readonly timeCache: TimeCache, private readonly searchAPI: SearchAPI) { | ||
this.searchAPI = searchAPI; | ||
} | ||
|
||
injectTimeFilter(query: string, timefield: string) { | ||
if (this.timeCache._timeRange) { | ||
const [source, ...others] = query.split('|'); | ||
const bounds = this.timeCache.getTimeBounds(); | ||
const from = moment(bounds.min).format('YYYY-MM-DD HH:mm:ss.SSS'); | ||
const to = moment(bounds.max).format('YYYY-MM-DD HH:mm:ss.SSS'); | ||
const timeFilter = `where \`${timefield}\` >= '${from}' and \`${timefield}\` <= '${to}'`; | ||
if (others.length > 0) { | ||
return `${source.trim()} | ${timeFilter} | ${others.map((s) => s.trim()).join(' | ')}`; | ||
} | ||
return `${source.trim()} | ${timeFilter}`; | ||
} | ||
return query; | ||
} | ||
|
||
parseUrl(dataObject: Data, url: UrlObject) { | ||
const timefield = url[TIMEFIELD]; | ||
delete url[TIMEFIELD]; | ||
|
||
// data.url.body.query must be defined | ||
if (!url.body || !url.body.query || typeof url.body.query !== 'string') { | ||
throw new Error( | ||
|
@@ -34,6 +55,11 @@ | |
); | ||
} | ||
|
||
if (timefield) { | ||
const query = this.injectTimeFilter(url.body.query, timefield); | ||
url.body.query = query; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the url that vega uses to query the cluster? I wouldn't expect the url parser to format/mutate the url, but I'm new to this area so it may be a gap in my understanding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes,
Could you please share your concern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. No concerns, my expectation was that the parser is a pure function but it looks like this is more of a parser and formatter of the original url if I'm understanding correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got your point now, I think we can also make it a pure function, for example we can deep copy the |
||
} | ||
|
||
return { dataObject, url }; | ||
} | ||
|
||
|
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.
will this synced to other apps(discovery/dashboards)?
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.
This is not the current browser url, it's the
url
property of vega-lite config object, the TIMEFIELD defines the time field name of current index, for example,timestamp
, and it will used as the time filter of the query.