Skip to content

Commit

Permalink
Elements should be sorted alphabetically in the explorer
Browse files Browse the repository at this point in the history
Fixes jlandersen#63

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jan 11, 2021
1 parent ee31a90 commit d5661b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/explorer/models/consumerGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ export class ConsumerGroupsItem extends NodeBase {
super(parent);
}

async computeChildren() : Promise<NodeBase[]> {
async computeChildren(): Promise<NodeBase[]> {
const client = this.getParent().client;
const consumerGroupIds = await client.getConsumerGroupIds();
const consumerGroupIds = (await client.getConsumerGroupIds())
.sort(this.sortByAscending);
return Promise.resolve(
consumerGroupIds.map((consumerGroupId) => (new ConsumerGroupItem(consumerGroupId, this))));
}

private sortByAscending(a: string, b: string): -1 | 0 | 1 {
if (a.toLowerCase() < b.toLowerCase()) { return -1; }
if (a.toLowerCase() > b.toLowerCase()) { return 1; }
return 0;
}

getParent(): ClusterItem {
return <ClusterItem>super.getParent();
}
Expand All @@ -38,12 +46,19 @@ class ConsumerGroupItem extends NodeBase {
async computeChildren(): Promise<NodeBase[]> {
const client = this.getParent().getParent().client;
const groupDetails = await client.getConsumerGroupDetails(this.consumerGroupId);
const members = groupDetails.members.sort(this.sortByMemberIdAscending);
return [
new ConsumerGroupDetailsItem("State", groupDetails.state, this),
new ConsumerGroupMembersItem(groupDetails.members, this),
new ConsumerGroupMembersItem(members, this),
];
}

private sortByMemberIdAscending(a: ConsumerGroupMember, b: ConsumerGroupMember): -1 | 0 | 1 {
if (a.memberId.toLowerCase() < b.memberId.toLowerCase()) { return -1; }
if (a.memberId.toLowerCase() > b.memberId.toLowerCase()) { return 1; }
return 0;
}

getParent(): ConsumerGroupsItem {
return <ConsumerGroupsItem>super.getParent();
}
Expand Down
13 changes: 10 additions & 3 deletions src/explorer/models/kafka.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Disposable, TreeItemCollapsibleState } from "vscode";
import { ClientAccessor } from "../../client";
import { ClientAccessor, Cluster } from "../../client";
import { ClusterSettings } from "../../settings";
import { ClusterItem, NoClusterItem } from "./cluster";
import { NodeBase } from "./nodeBase";
Expand All @@ -16,7 +16,8 @@ export class KafkaModel extends NodeBase implements Disposable {
}

public async computeChildren(): Promise<NodeBase[]> {
const clusters = this.clusterSettings.getAll();
const clusters = this.clusterSettings.getAll()
.sort(this.sortByNameAscending);
if (clusters.length === 0) {
return [new NoClusterItem(this)];
}
Expand All @@ -25,6 +26,12 @@ export class KafkaModel extends NodeBase implements Disposable {
});
}

private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 {
if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; }
if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; }
return 0;
}

public dispose(): void {
this.children?.forEach(child => (<ClusterItem>child).dispose());
}
Expand All @@ -33,6 +40,6 @@ export class KafkaModel extends NodeBase implements Disposable {
return this.getChildren()
.then(clusters =>
clusters.find(child => (<ClusterItem>child).cluster.name === clusterName)
);
);
}
}

0 comments on commit d5661b0

Please sign in to comment.