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

[Usage Collection] Report nodes feature usage #70108

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ describe('get_nodes_usage', () => {
it('calls fetchNodesUsage', async () => {
const callCluster = jest.fn();
callCluster.mockResolvedValueOnce(mockedNodesFetchResponse);
jest.fn(async () => ({
cluster_name: 'test',
nodes: mockedNodesResponse,
}));
await getNodesUsage(callCluster);
expect(callCluster).toHaveBeenCalledWith('transport.request', {
path: '/_nodes/usage',
Expand All @@ -84,10 +80,6 @@ describe('get_nodes_usage', () => {
it('returns a modified array of node usage data', async () => {
const callCluster = jest.fn();
callCluster.mockResolvedValueOnce(mockedNodesFetchResponse);
jest.fn(async () => ({
cluster_name: 'test',
nodes: mockedNodesResponse,
}));
const result = await getNodesUsage(callCluster);
expect(result.nodes).toEqual([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@ export interface NodeObj {
[key: string]: NodeAggregation;
};
}
export interface NodesStats {
total: number;
successful: number;
failed: number;
}

export interface NodesFeatureUsageResponse {
cluster_name: string;
nodes: {
[key: string]: NodeObj;
};
}

export type NodesUsageFetcher = (callCluster: APICaller) => Promise<NodesFeatureUsageResponse>;

export type NodesUsageGetter = (
callCluster: APICaller
) => Promise<{ nodes: NodeObj[] | Array<{}> }>;
Expand All @@ -59,7 +53,7 @@ export type NodesUsageGetter = (
*
* The Nodes usage API was introduced in v6.0.0
*/
export async function fetchNodesUsage<NodesUsageFetcher>(callCluster: APICaller) {
export async function fetchNodesUsage(callCluster: APICaller): Promise<NodesFeatureUsageResponse> {
const response = await callCluster('transport.request', {
method: 'GET',
path: '/_nodes/usage',
Expand All @@ -76,13 +70,10 @@ export async function fetchNodesUsage<NodesUsageFetcher>(callCluster: APICaller)
* @returns Object containing array of modified usage information with the node_id nested within the data for that node.
*/
export const getNodesUsage: NodesUsageGetter = async (callCluster) => {
const result = await fetchNodesUsage<NodesUsageFetcher>(callCluster);
let transformedNodes = [{}];
if (result && result.nodes) {
transformedNodes = Object.entries(result.nodes).map(([key, value]) => ({
...(value as NodeObj),
node_id: key,
}));
}
const result = await fetchNodesUsage(callCluster);
const transformedNodes = Object.entries(result?.nodes || {}).map(([key, value]) => ({
...(value as NodeObj),
Copy link
Member

Choose a reason for hiding this comment

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

NIT: I think there is no need for the cast now that fetchNodesUsage is properly typed :)

node_id: key,
}));
return { nodes: transformedNodes };
};