Skip to content

Commit

Permalink
Add topic and consumer group filters
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Jan 11, 2021
1 parent ee31a90 commit e2f725a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ All notable changes to Kafka extension will be documented in this file.
### Added
- Newly created topic or cluster is automatically selected in the Kafka Explorer. See [#61](https://github.com/jlandersen/vscode-kafka/issues/61).
- Click on empty Kafka explorer to add a new cluster. See [#76](https//github.com/jlandersen/vscode-kafka/pull/76).
- Added glob patterns to filter topics (`kafka.explorer.topics.filters`) and consumer groups (`kafka.explorer.consumers.filters`) out of the Kafka explorer. See [#74](https://github.com/jlandersen/vscode-kafka/pull/74).

### Changed
- 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).

## [0.10.0] - 2021-01-02
### Added
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
"default": "name",
"description": "Choose sorting for topics in explorer"
},
"kafka.explorer.topics.filter": {
"type": "array",
"default": ["__consumer_offsets", "__transaction_state", "_schemas"],
"markdownDescription": "Glob patterns filtering topics out of the Kafka explorer. `*` matches any string, `?` matches a single character."
},
"kafka.explorer.consumers.filter": {
"type": "array",
"default": [],
"markdownDescription": "Glob patterns filtering consumer groups out of the Kafka explorer. `*` matches any string, `?` matches a single character."
},
"kafka.producers.fakerjs.enabled":{
"type": "boolean",
"default":true,
Expand Down
15 changes: 14 additions & 1 deletion src/explorer/models/consumerGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as vscode from "vscode";

import { ConsumerGroupMember } from "../../client";
import { Icons } from "../../constants";
import { getWorkspaceSettings } from "../../settings";
import { NodeBase } from "./nodeBase";
import { ClusterItem } from "./cluster";
import * as minimatch from "minimatch";

export class ConsumerGroupsItem extends NodeBase {
public label = "Consumer Groups";
Expand All @@ -16,13 +18,24 @@ export class ConsumerGroupsItem extends NodeBase {

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

return Promise.resolve(
consumerGroupIds.map((consumerGroupId) => (new ConsumerGroupItem(consumerGroupId, this))));
}
getParent(): ClusterItem {
return <ClusterItem>super.getParent();
}

private isDisplayed(consumerGroup: string, filters: string[]): boolean {
if (!filters) {
return true;
}
const id = consumerGroup.toLowerCase();
return !filters.find( f => minimatch(id, f));
}
}

class ConsumerGroupItem extends NodeBase {
Expand Down
12 changes: 11 additions & 1 deletion src/explorer/models/topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TopicSortOption } from "../../settings/workspace";
import { ClusterItem } from "./cluster";
import { ConfigsItem } from "./common";
import { NodeBase } from "./nodeBase";
import * as minimatch from "minimatch";

export class TopicGroupItem extends NodeBase {
public label = "Topics";
Expand All @@ -21,6 +22,8 @@ export class TopicGroupItem extends NodeBase {
const client = this.getParent().client;
const settings = getWorkspaceSettings();
let topics = await client.getTopics();
//Filter topics before sorting them
topics = topics.filter(t => this.isDisplayed(t, settings.topicFilters));

switch (settings.topicSortOption) {
case TopicSortOption.Name:
Expand All @@ -30,7 +33,6 @@ export class TopicGroupItem extends NodeBase {
topics = topics.sort(this.sortByPartitionsAscending);
break;
}

return topics.map((topic) => {
return new TopicItem(topic, this);
});
Expand All @@ -50,6 +52,14 @@ export class TopicGroupItem extends NodeBase {
if (a.partitionCount > b.partitionCount) { return 1; }
return 0;
}

private isDisplayed(t: Topic, filters: string[]): boolean {
if (!filters) {
return true;
}
const id = t.id.toLowerCase();
return !filters.find( f => minimatch(id, f));
}
}

export class TopicItem extends NodeBase {
Expand Down
6 changes: 6 additions & 0 deletions src/settings/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export enum TopicSortOption {
export type InitialConsumerOffset = "latest" | "earliest";

const DEFAULT_PRODUCER_LOCALE = 'en';
const DEFAULT_TOPIC_FILTER = ['__consumer_offsets', '__transaction_state', '_schemas'];
const DEFAULT_CONSUMER_FILTER:string[] = [];

export interface WorkspaceSettings extends vscode.Disposable {
consumerOffset: InitialConsumerOffset;
Expand All @@ -35,6 +37,8 @@ class VsCodeWorkspaceSettings implements WorkspaceSettings {
public topicSortOption: TopicSortOption = TopicSortOption.Name;
public producerFakerJSEnabled = true;
public producerFakerJSLocale = DEFAULT_PRODUCER_LOCALE;
public topicFilters = DEFAULT_TOPIC_FILTER;
public consumerFilters = DEFAULT_CONSUMER_FILTER;

private constructor() {
this.configurationChangeHandlerDisposable = vscode.workspace.onDidChangeConfiguration(
Expand All @@ -56,6 +60,8 @@ class VsCodeWorkspaceSettings implements WorkspaceSettings {
this.topicSortOption = configuration.get<TopicSortOption>("explorer.topics.sort", TopicSortOption.Name);
this.producerFakerJSEnabled = configuration.get<boolean>("producers.fakerjs.enabled", true);
this.producerFakerJSLocale = configuration.get<string>("producers.fakerjs.locale", DEFAULT_PRODUCER_LOCALE);
this.topicFilters = configuration.get<string[]>("explorer.topics.filter", DEFAULT_TOPIC_FILTER);
this.consumerFilters = configuration.get<string[]>("explorer.consumers.filter", DEFAULT_CONSUMER_FILTER);
}

dispose(): void {
Expand Down

0 comments on commit e2f725a

Please sign in to comment.