-
Notifications
You must be signed in to change notification settings - Fork 58
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
Implement saved query substitution for S3 integrations #1705
Merged
YANG-DB
merged 10 commits into
opensearch-project:main
from
Swiddis:feature/integ_saved_queries
Apr 17, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3cf1a2a
Add example queries for cloudfront
Swiddis 23bfb17
Add Flint S3 label to haproxy
Swiddis 9501271
Add query substitution method to build process
Swiddis 7e18cde
Fix tests
Swiddis e6f69aa
Update build API with new data source fields
Swiddis c1e8fdd
Update frontend to use new API term
Swiddis 27890d0
Use new fields in original integration creation request
Swiddis e0ad0e7
Fix broken data source usage ref
Swiddis be58780
Merge remote-tracking branch 'upstream/main' into feature/integ_saved…
Swiddis efe7b42
Merge branch 'main' into feature/integ_saved_queries
Swiddis 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
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
8 changes: 8 additions & 0 deletions
8
...ptors/integrations/__data__/repository/aws_cloudfront/assets/example_queries-1.0.0.ndjson
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,8 @@ | ||
{"attributes":{"createdTimeMs":1713289099101,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Top IPs by Request Count","query":"SELECT c_ip, COUNT(*) AS request_count FROM {table_name} GROUP BY c_ip ORDER BY request_count DESC LIMIT 10;","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Top IPs by Request Count","version":1},"id":"1d07d010-fc18-11ee-99c9-43e5dbd0692c","references":[],"type":"observability-search","updated_at":"2024-04-16T17:52:30.414Z","version":"WzI3NTEsMV0="} | ||
{"attributes":{"createdTimeMs":1713293044079,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Top Status by Count","query":"SELECT sc_status, COUNT(*) AS status_count FROM {table_name} GROUP BY sc_status ORDER BY status_count DESC LIMIT 10;","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Top Status by Count","version":1},"id":"4c6b8820-fc21-11ee-ab45-d3075d0510e6","references":[],"type":"observability-search","updated_at":"2024-04-16T18:44:47.956Z","version":"WzI4MzAsMV0="} | ||
{"attributes":{"createdTimeMs":1713290175184,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Number of Requests","query":"SELECT COUNT(*) AS request_count FROM {table_name};","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Number of Requests","version":1},"id":"9e6a9b40-fc1a-11ee-99c9-43e5dbd0692c","references":[],"type":"observability-search","updated_at":"2024-04-16T17:56:15.220Z","version":"WzI3NTIsMV0="} | ||
{"attributes":{"createdTimeMs":1713293161193,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Total Bytes Served","query":"SELECT SUM(sc_bytes) AS total_bytes_served FROM {table_name};","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Total Bytes Served","version":1},"id":"92398eb0-fc21-11ee-ab45-d3075d0510e6","references":[],"type":"observability-search","updated_at":"2024-04-16T18:46:01.242Z","version":"WzI4MzEsMV0="} | ||
{"attributes":{"createdTimeMs":1713293269224,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Average Time Taken","query":"SELECT AVG(time_taken) AS average_time_taken FROM {table_name};","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Average Time Taken","version":1},"id":"d2a038a0-fc21-11ee-ab45-d3075d0510e6","references":[],"type":"observability-search","updated_at":"2024-04-16T18:47:49.290Z","version":"WzI4MzIsMV0="} | ||
{"attributes":{"createdTimeMs":1713293425335,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Slow Requests from Average Time threshold","query":"WITH avg_time AS (SELECT AVG(time_to_first_byte) AS avg_time FROM {table_name}) SELECT * FROM {table_name} CROSS JOIN avg_time WHERE time_to_first_byte > 1 * avg_time LIMIT 10;","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Slow Requests from Average Time threshold","version":1},"id":"2fac4250-fc22-11ee-ab45-d3075d0510e6","references":[],"type":"observability-search","updated_at":"2024-04-16T18:59:34.785Z","version":"WzI4MzQsMV0="} | ||
{"attributes":{"createdTimeMs":1713294061574,"savedQuery":{"data_sources":"[{\"name\":\"mys3\",\"type\":\"s3glue\",\"label\":\"mys3\",\"value\":\"mys3\"}]","description":"","name":"Requests by User Agent","query":"SELECT * FROM {table_name} WHERE cs_user_agent LIKE '%Chrome%' LIMIT 10;","query_lang":"SQL","selected_date_range":{"end":"now","start":"now-15m","text":""},"selected_fields":{"text":"","tokens":[]},"selected_timestamp":{"name":"","type":"timestamp"}},"title":"Requests by User Agent","version":1},"id":"aae73c80-fc23-11ee-ab45-d3075d0510e6","references":[],"type":"observability-search","updated_at":"2024-04-16T19:01:01.640Z","version":"WzI4MzUsMV0="} | ||
{"exportedCount":7,"missingRefCount":0,"missingReferences":[]} |
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
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 |
---|---|---|
|
@@ -11,8 +11,10 @@ import { deepCheck } from './repository/utils'; | |
|
||
interface BuilderOptions { | ||
name: string; | ||
dataSource: string; | ||
indexPattern: string; | ||
workflows?: string[]; | ||
dataSource?: string; | ||
tableName?: string; | ||
} | ||
|
||
interface SavedObject { | ||
|
@@ -42,12 +44,67 @@ export class IntegrationInstanceBuilder { | |
return Promise.reject(assets.error); | ||
} | ||
const remapped = this.remapIDs(this.getSavedObjectBundles(assets.value, options.workflows)); | ||
const withDataSource = this.remapDataSource(remapped, options.dataSource); | ||
const refs = await this.postAssets(withDataSource); | ||
const withDataSource = this.remapDataSource(remapped, options.indexPattern); | ||
const withSubstitutedQueries = this.substituteQueries( | ||
withDataSource, | ||
options.dataSource, | ||
options.tableName | ||
); | ||
const refs = await this.postAssets(withSubstitutedQueries as SavedObjectsBulkCreateObject[]); | ||
const builtInstance = await this.buildInstance(integration, refs, options); | ||
return builtInstance; | ||
} | ||
|
||
// If we have a data source or table specified, hunt for saved queries and update them with the | ||
// new DS/table. | ||
substituteQueries(assets: SavedObject[], dataSource?: string, tableName?: string): SavedObject[] { | ||
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. Heart of the substitution updates: here's where we search for saved queries to update |
||
if (!dataSource) { | ||
return assets; | ||
} | ||
|
||
assets = assets.map((asset) => { | ||
if (asset.type === 'observability-search') { | ||
const savedQuery = ((asset.attributes as unknown) as { | ||
savedQuery: { | ||
// The actual SavedSearchAttributes type uses "dataSources", but when exporting it's | ||
// "data_sources". I'm not sure why the discrepancy exists but since that's the exported | ||
// format we need to define our own type here. | ||
data_sources: string; | ||
query: string; | ||
query_lang: string; | ||
}; | ||
}).savedQuery; | ||
if (!savedQuery.data_sources) { | ||
return asset; | ||
} | ||
const dataSources = JSON.parse(savedQuery.data_sources) as Array<{ | ||
name: string; | ||
type: string; | ||
label: string; | ||
value: string; | ||
}>; | ||
for (const ds of dataSources) { | ||
if (ds.type !== 's3glue') { | ||
continue; // Nothing to do | ||
} | ||
// TODO is there a distinction between these where we should only set one? They're all | ||
// equivalent in every export I've seen. | ||
ds.name = dataSource; | ||
ds.label = dataSource; | ||
ds.value = dataSource; | ||
} | ||
savedQuery.data_sources = JSON.stringify(dataSources); | ||
|
||
if (savedQuery.query_lang === 'SQL' && tableName) { | ||
savedQuery.query = savedQuery.query.replaceAll('{table_name}', tableName); | ||
} | ||
} | ||
return asset; | ||
}); | ||
|
||
return assets; | ||
} | ||
|
||
getSavedObjectBundles( | ||
assets: ParsedIntegrationAsset[], | ||
includeWorkflows?: string[] | ||
|
@@ -69,18 +126,14 @@ export class IntegrationInstanceBuilder { | |
.flat() as SavedObject[]; | ||
} | ||
|
||
remapDataSource( | ||
assets: SavedObject[], | ||
dataSource: string | undefined | ||
): Array<{ type: string; attributes: { title: string } }> { | ||
remapDataSource(assets: SavedObject[], dataSource: string | undefined): SavedObject[] { | ||
if (!dataSource) return assets; | ||
assets = assets.map((asset) => { | ||
return assets.map((asset) => { | ||
if (asset.type === 'index-pattern') { | ||
asset.attributes.title = dataSource; | ||
} | ||
return asset; | ||
}); | ||
return assets; | ||
} | ||
|
||
remapIDs(assets: SavedObject[]): SavedObject[] { | ||
|
@@ -136,7 +189,10 @@ export class IntegrationInstanceBuilder { | |
return Promise.resolve({ | ||
name: options.name, | ||
templateName: config.value.name, | ||
dataSource: options.dataSource, | ||
// Before data sources existed we called the index pattern a data source. Now we need the old | ||
// name for BWC but still use the new data sources in building, so we map the variable only | ||
// for returned output here | ||
dataSource: options.indexPattern, | ||
creationDate: new Date().toISOString(), | ||
assets: refs, | ||
}); | ||
|
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.
For the front-end we need to add this pair of params to send to the back-end, so we do so. Todo: this method has a lot of parameters and should probably be broken up somehow.