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 12, 2021
1 parent 87fe68d commit 3acd397
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to Kafka extension will be documented in this file.
- Improved the "New cluster" and "New topic" wizards: now include validation and a back button. See [#21](https://github.com/jlandersen/vscode-kafka/issues/21).
- Newly created topic or cluster is automatically selected in the Kafka Explorer. See [#61](https://github.com/jlandersen/vscode-kafka/issues/61).
- Internal topics are now hidden by default. See [#29](https://github.com/jlandersen/vscode-kafka/issues/29) and [#74](https://github.com/jlandersen/vscode-kafka/pull/74).
- Elements should be sorted alphabetically in the explorer. See [#63](https://github.com/jlandersen/vscode-kafka/issues/63).

## [0.10.0] - 2021-01-02
### Added
Expand Down
24 changes: 19 additions & 5 deletions src/explorer/models/consumerGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ export class ConsumerGroupsItem extends NodeBase {
super(parent);
}

async computeChildren() : Promise<NodeBase[]> {
async computeChildren(): Promise<NodeBase[]> {
const client = this.getParent().client;
let consumerGroupIds = await client.getConsumerGroupIds();
const settings = getWorkspaceSettings();
consumerGroupIds = consumerGroupIds.filter(cg => this.isDisplayed(cg, settings.consumerFilters));

const consumerGroupIds = (await client.getConsumerGroupIds())
.filter(cg => this.isDisplayed(cg, settings.consumerFilters))
.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 @@ -51,12 +58,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 3acd397

Please sign in to comment.