kibana/examples/search_examples/server/fibonacci_strategy.ts
Davis McPhee 11a80ce559
[Search Sessions] Fix flaky partial results test in search examples (#155816)
## Summary

Unskip and fix flaky partial results test in search examples plugin.

Flaky test runner x100:
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/2172.

Fixes #116038.

### Checklist

- [ ] ~Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)~
- [ ]
~[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials~
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] ~Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard
accessibility](https://webaim.org/techniques/keyboard/))~
- [ ] ~Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))~
- [ ] ~If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)~
- [ ] ~This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))~
- [ ] ~This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)~

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
2023-04-27 20:27:59 -03:00

50 lines
1.7 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { v4 as uuidv4 } from 'uuid';
import { ISearchStrategy } from '@kbn/data-plugin/server';
import { of } from 'rxjs';
import { FibonacciRequest, FibonacciResponse } from '../common/types';
export const fibonacciStrategyProvider = (): ISearchStrategy<
FibonacciRequest,
FibonacciResponse
> => {
const responseMap = new Map<string, [number[], number, number]>();
return {
search: (request: FibonacciRequest) => {
const id = request.id ?? uuidv4();
const [sequence, total, started] = responseMap.get(id) ?? [
[],
request.params?.n ?? 0,
Date.now(),
];
if (sequence.length < 2) {
if (total > 0) sequence.push(sequence.length);
} else {
const [a, b] = sequence.slice(-2);
sequence.push(a + b);
}
const loaded = sequence.length;
responseMap.set(id, [sequence, total, started]);
if (loaded >= total) {
responseMap.delete(id);
}
const isRunning = loaded < total;
const isPartial = isRunning;
const took = Date.now() - started;
const values = sequence.slice(0, loaded);
return of({ id, loaded, total, isRunning, isPartial, rawResponse: { took, values } });
},
cancel: async (id: string) => {
responseMap.delete(id);
},
} as unknown as ISearchStrategy<FibonacciRequest, FibonacciResponse>;
};