[Stack Monitoring] Convert node roles into array (#167628)

## 📓 Summary

When the `elasticsearch.node.roles` property is set to a single role,
the API returns a single string instead of a list, failing when
computing the role list client side and resulting in an empty list.

This fix is to parse the node roles into a list if the API returns a
single role string and correctly displays the roles.

E.g. Assuming we have the following roles configuration
```
[
      {
        "_source": {
          "elasticsearch": {
            "node": {
              "roles": [
                "data",
                "master"
              ]
            }
          }
        }
      },
      {
        "_source": {
          "elasticsearch": {
            "node": {
              "roles": "master"
            }
          }
        }
      }
    ]

```

It would fail at parsing the second node roles and fall back into the
"coordinating only" default.

<img width="814" alt="Screenshot 2023-09-29 at 13 05 14"
src="bf49a974-650a-44cc-827b-b3dc834d6cee">

Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co>
This commit is contained in:
Marco Antonio Ghiani 2023-10-04 10:50:07 +02:00 committed by GitHub
parent 7baab02533
commit a69957e0fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -186,15 +186,17 @@ export const ElasticsearchNodesPage: React.FC<ComponentProps> = ({ clusters }) =
);
};
function sortNodeRoles(roles: string[] | undefined): string[] | undefined {
function sortNodeRoles(roles: string[] | string | undefined): string[] | undefined {
if (!roles) {
return undefined;
}
if (roles.length === 0) {
const rolesList = Array.isArray(roles) ? roles : [roles];
if (rolesList.length === 0) {
return [];
}
const rolesAsSet = new Set(roles);
const rolesAsSet = new Set(rolesList);
return rolesByImportance.filter((role) => rolesAsSet.has(role));
}