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

Live: support streaming results out-of-the-box #32821

Merged
merged 11 commits into from Apr 9, 2021
Merged

Conversation

ryantxu
Copy link
Member

@ryantxu ryantxu commented Apr 9, 2021

This works with the pre-release/WIP:
https://github.com/grafana/signal-generator-datasource

We have many streaming implementations where the data source needs to construct its own rxjs magic to keep things up-to-date. This PR applies the streaming logic by default for DataSourceWithBackend.ts

The front-end implementation for signal generator is now:
image

Peek 2021-04-08 17-42

@ryantxu ryantxu requested a review from a team as a code owner April 9, 2021 00:31
@ryantxu ryantxu requested review from a team, aknuds1, wbrowne, hugohaggmark, natellium, FZambia and leeoniya and removed request for a team April 9, 2021 00:31
go.mod Outdated
@@ -45,7 +45,7 @@ require (
github.com/grafana/grafana-aws-sdk v0.4.0
github.com/grafana/grafana-live-sdk v0.0.4
github.com/grafana/grafana-plugin-model v0.0.0-20190930120109-1fc953a61fb4
github.com/grafana/grafana-plugin-sdk-go v0.91.0
github.com/grafana/grafana-plugin-sdk-go v0.91.1-0.20210408195819-266fa63c3826
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed so that meta.channel is parsed

@ryantxu ryantxu requested a review from toddtreece April 9, 2021 00:32
if (!dsConfig) {
return Promise.reject({ message: `Datasource named ${name} was not found` });
dsConfig = this.settingsMapById[name];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@torkelo any reason this may be a bad idea? The issue I have is that backend plugins do not have access to uid :( and we need something to pass forward that is consistent.

Long term, we should likely replace all use of id with uid, this change can help with that migration -- at least I do not think it will hurt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dam I thought the backend was already using uid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When/where do you need to lookup ds by id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel response from a query is:
ds/${id}/path

Ideally that could be: ds/${uid}/path, BUT backend plugin API does not pass in the UID so it can not be returned from the query easily. This is something we should fix, but is a bigger issue.

For V8, perhaps we should stop returning the internal id entirely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed with @torkelo -- he says it is an OK temporary fix until we get #32853

Copy link
Contributor

@hugohaggmark hugohaggmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting progress with this, splendid work 🎉

Comment on lines 139 to 176
switchMap((raw) => {
const rsp = toDataQueryResponse(raw, queries as DataQuery[]);
// Check if any response should subscribe to a live stream
if (rsp.data?.length && rsp.data.find((f: DataFrame) => f.meta?.channel)) {
const buffer: StreamingFrameOptions = {
maxLength: request.maxDataPoints ?? 500,
};

// For recent queries, clamp to the current time range
if (request.rangeRaw?.to === 'now') {
buffer.maxDelta = request.range.to.valueOf() - request.range.from.valueOf();
}

const staticdata: DataFrame[] = [];
const streams: Array<Observable<DataQueryResponse>> = [];
for (const frame of rsp.data) {
const addr = parseLiveChannelAddress(frame.meta?.channel);
if (addr) {
streams.push(
getLiveDataStream({
addr,
buffer,
frame: frame as DataFrame,
})
);
} else {
staticdata.push(frame);
}
}
if (staticdata.length) {
streams.push(of({ ...rsp, data: staticdata }));
}
if (streams.length === 1) {
return streams[0]; // avoid merge wrapper
}
return merge(...streams);
}
return of(rsp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway we can pull this apart so it becomes testable? And maybe add tests too?

@@ -403,7 +404,14 @@ func (g *GrafanaLive) handlePushScope(_ *models.SignedInUser, namespace string)
func (g *GrafanaLive) handleDatasourceScope(user *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
ds, err := g.DatasourceCache.GetDatasourceByUID(namespace, user, false)
if err != nil {
return nil, fmt.Errorf("error getting datasource: %w", err)
// the namespace may be an ID
Copy link
Contributor

@FZambia FZambia Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a temporary or persistent change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove it when we fix #32853

@ryantxu ryantxu requested a review from FZambia April 9, 2021 17:25
Copy link
Contributor

@FZambia FZambia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@ryantxu ryantxu enabled auto-merge (squash) April 9, 2021 19:07
@ryantxu ryantxu merged commit b96e452 into master Apr 9, 2021
@ryantxu ryantxu deleted the standard-stream-support branch April 9, 2021 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants