Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 9d3f241

Browse files
committed
[WIP] broken data plugin usage, can't resolve promise
Signed-off-by: inge4pres <[email protected]>
1 parent ac06ea3 commit 9d3f241

File tree

5 files changed

+112
-53
lines changed

5 files changed

+112
-53
lines changed

src/plugins/profiling/public/app.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ function App({ fetchTopN, fetchElasticFlamechart, fetchPixiFlamechart, fetchTopN
3838
samples: [],
3939
series: new Map(),
4040
});
41+
const [topnData, setTopNData] = useState({
42+
samples: [],
43+
series: new Map(),
44+
});
4145

4246
const [elasticFlamegraph, setElasticFlamegraph] = useState({ leaves: [] });
4347
const [pixiFlamegraph, setPixiFlamegraph] = useState({});
4448

4549
const tabs = [
4650
{
4751
id: 'stacktrace-elastic',
48-
name: 'Stack Traces (Elastic)',
52+
name: 'Stack Traces (API)',
4953
content: (
5054
<>
5155
<EuiSpacer />
@@ -57,6 +61,20 @@ function App({ fetchTopN, fetchElasticFlamechart, fetchPixiFlamechart, fetchTopN
5761
</>
5862
),
5963
},
64+
{
65+
id: 'stacktrace-elastic-data',
66+
name: 'Stack Traces (Data Plugin)',
67+
content: (
68+
<>
69+
<EuiSpacer />
70+
<TopNContext.Provider value={topnData}>
71+
<StackTraceNavigation fetchTopN={fetchTopNData} setTopN={setTopNData} />
72+
<StackedBarChart id="topn-data" name="topn-data" height={400} x="x" y="y" category="g" />
73+
<ChartGrid maximum={10} />
74+
</TopNContext.Provider>
75+
</>
76+
),
77+
},
6078
{
6179
id: 'flamegraph-elastic',
6280
name: 'FlameGraph (Elastic)',

src/plugins/profiling/public/components/contexts/topn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
import { createContext } from 'react';
1010

11-
export const TopNContext = createContext();
11+
export const TopNContext = createContext({});

src/plugins/profiling/public/components/stacktrace-nav.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,19 @@ export const StackTraceNavigation = ({ fetchTopN, setTopN }) => {
101101
});
102102

103103
console.log(new Date().toISOString(), 'started payload retrieval');
104-
fetchTopN(topnValue[0].value, dateValue[0].value).then((response) => {
105-
console.log(new Date().toISOString(), 'finished payload retrieval');
106-
const samples = getTopN(response);
107-
const series = groupSamplesByCategory(samples);
108-
setTopN({ samples, series });
109-
console.log(new Date().toISOString(), 'updated local state');
110-
});
104+
fetchTopN(topnValue[0].value, dateValue[0].value)
105+
.then((response) => {
106+
console.log(new Date().toISOString(), 'finished payload retrieval');
107+
const samples = getTopN(response);
108+
const series = groupSamplesByCategory(samples);
109+
console.log('sample %o', samples);
110+
console.log('series %o', series);
111+
setTopN({ samples, series });
112+
console.log(new Date().toISOString(), 'updated local state');
113+
})
114+
.catch((err) => {
115+
console.log('error when reading topN data: ' + err.message);
116+
});
111117
}, [toggleTopNSelected, toggleDateSelected]);
112118

113119
return (

src/plugins/profiling/public/services.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
import { CoreStart, HttpFetchError, HttpFetchQuery } from 'kibana/public';
1010
import { getRemoteRoutePaths } from '../common';
1111
import { ProdfilerPluginStartDeps } from './plugin';
12-
import { DownsampledRequest, DownsampledTopNResponse } from '../common/types';
12+
import {
13+
DOWNSAMPLED_TOPN_STRATEGY,
14+
DownsampledRequest,
15+
DownsampledTopNResponse,
16+
TopNAggregateResponse,
17+
} from '../common/types';
1318

1419
export interface Services {
1520
fetchTopN: (type: string, seconds: string) => Promise<any[] | HttpFetchError>;
1621
fetchElasticFlamechart: (seconds: string) => Promise<any[] | HttpFetchError>;
1722
fetchPixiFlamechart: (seconds: string) => Promise<any[] | HttpFetchError>;
18-
// FIXME
19-
fetchTopNData?: (searchField: string, seconds: string) => Promise<DownsampledTopNResponse>;
23+
fetchTopNData: (searchField: string, seconds: string) => Promise<TopNAggregateResponse>;
2024
}
2125

2226
function getFetchQuery(seconds: string): HttpFetchQuery {
@@ -36,28 +40,44 @@ export function getServices(core: CoreStart, data?: ProdfilerPluginStartDeps): S
3640
const paths = getRemoteRoutePaths();
3741

3842
return {
39-
fetchTopNData: (searchField: string, seconds: string): Promise<DownsampledTopNResponse> => {
43+
fetchTopNData: async (searchField: string, seconds: string): Promise<TopNAggregateResponse> => {
4044
const unixTime = Math.floor(Date.now() / 1000);
41-
return (
42-
data!.data.search
43-
.search<DownsampledRequest, DownsampledTopNResponse>(
44-
{
45-
params: {
46-
projectID: 5,
47-
timeFrom: unixTime - parseInt(seconds, 10),
48-
timeTo: unixTime,
49-
// FIXME remove hard-coded value for topN items length and expose it through the UI
50-
topNItems: 100,
51-
searchField,
52-
},
45+
const response: TopNAggregateResponse = { topN: { histogram: { buckets: [] } } };
46+
data!.data.search
47+
.search<DownsampledRequest, DownsampledTopNResponse>(
48+
{
49+
params: {
50+
projectID: 5,
51+
timeFrom: unixTime - parseInt(seconds, 10),
52+
timeTo: unixTime,
53+
// FIXME remove hard-coded value for topN items length and expose it through the UI
54+
topNItems: 100,
55+
searchField,
5356
},
54-
{
55-
strategy: 'downsampledTopN',
56-
}
57-
)
58-
// get the results and prepare the Promise
59-
.toPromise<DownsampledTopNResponse>()
60-
);
57+
},
58+
{
59+
strategy: DOWNSAMPLED_TOPN_STRATEGY,
60+
}
61+
)
62+
.subscribe({
63+
next: (result) => {
64+
console.log('subscription data plugin rawResponse: %o', result.rawResponse);
65+
response.topN.histogram = result.rawResponse.aggregations.histogram;
66+
},
67+
// TODO error handling
68+
error: (err) => {
69+
console.log('subscription error: %o', err);
70+
},
71+
// FIXME remove this, used for debugging only
72+
complete: () => {
73+
console.log('subscription completed');
74+
},
75+
});
76+
77+
console.log('returning Promise of TopNAggregateResponse');
78+
return await new Promise<TopNAggregateResponse>((resolve, _) => {
79+
return resolve(response);
80+
});
6181
},
6282

6383
fetchTopN: async (type: string, seconds: string) => {

src/plugins/profiling/server/search/strategy.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,62 @@
88

99
import { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types';
1010
import { IEsSearchRequest, ISearchStrategy, PluginStart } from '../../../data/server';
11-
import { autoHistogramSumCountOnGroupByField, newProjectTimeQuery } from '../routes/mappings';
11+
import {
12+
autoHistogramSumCountOnGroupByField,
13+
newProjectTimeQuery,
14+
ProjectTimeQuery,
15+
} from '../routes/mappings';
1216
import { downsampledIndex, getSampledTraceEventsIndex } from '../routes/search_flamechart';
1317
import { DownsampledRequest, DownsampledTopNResponse } from '../../common/types';
1418

1519
export const DownsampledTopNFactory = (
1620
data: PluginStart
1721
): ISearchStrategy<DownsampledRequest, DownsampledTopNResponse> => {
1822
const es = data.search.getSearchStrategy();
23+
24+
// FIXME these 2 constants should be configurable?
25+
const initialExp = 6;
26+
const targetSampleSize = 20000; // minimum number of samples to get statistically sound results
27+
28+
// Calculate the right down-sampled index to query data from
29+
const sampleCountFromInitialExp = (filter: ProjectTimeQuery, options, deps): number => {
30+
// By default, we return no samples and use the un-sampled index
31+
let sampleCount = 0;
32+
es.search(
33+
{
34+
params: {
35+
index: downsampledIndex + initialExp,
36+
body: {
37+
query: filter,
38+
size: 0,
39+
track_total_hits: true,
40+
},
41+
},
42+
},
43+
options,
44+
deps
45+
).subscribe({
46+
next: (value) => {
47+
sampleCount = (value.rawResponse.hits.total as SearchTotalHits).value;
48+
},
49+
});
50+
return sampleCount;
51+
};
1952
return {
20-
search: async (request, options, deps) => {
53+
search: (request, options, deps) => {
2154
const { projectID, timeFrom, timeTo, topNItems, searchField } = request.params!;
2255
const filter = newProjectTimeQuery(
2356
projectID.toString(),
2457
timeFrom.toString(),
2558
timeTo.toString()
2659
);
2760

28-
// FIXME these 2 constants should be configurable?
29-
const initialExp = 6;
30-
const targetSampleSize = 20000; // minimum number of samples to get statistically sound results
31-
// Calculate the right down-sampled index to query data from
32-
const sampleCountFromInitialExp = async (): Promise<number> => {
33-
return await deps.esClient.asInternalUser
34-
.search({
35-
index: downsampledIndex + initialExp,
36-
body: {
37-
query: filter,
38-
size: 0,
39-
track_total_hits: true,
40-
},
41-
})
42-
.then((resp) => {
43-
return (resp.body.hits?.total as SearchTotalHits).value;
44-
});
45-
};
4661
// Create the query for the actual data
4762
const downsampledReq = {
4863
params: {
4964
index: getSampledTraceEventsIndex(
5065
targetSampleSize,
51-
await sampleCountFromInitialExp(),
66+
sampleCountFromInitialExp(filter, options, deps),
5267
initialExp
5368
).name,
5469
body: {

0 commit comments

Comments
 (0)