Fixed#2730: added status filter support for webhooks (#5214)

* Fixed#2730: added status filter support for webhooks

* added status param

* addressing comments

* postgres query change
This commit is contained in:
Parth Panchal 2022-05-30 15:53:23 +05:30 committed by GitHub
parent ffebbb2d28
commit f8f79ebb2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View File

@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS type_entity (
); );
ALTER TABLE webhook_entity ALTER TABLE webhook_entity
ADD status VARCHAR(256) GENERATED ALWAYS AS (json ->> '$.status') NOT NULL,
DROP COLUMN deleted; DROP COLUMN deleted;
ALTER TABLE entity_relationship ALTER TABLE entity_relationship

View File

@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS type_entity (
); );
ALTER TABLE webhook_entity ALTER TABLE webhook_entity
ADD status VARCHAR(256) GENERATED ALWAYS AS (json ->> 'status') STORED NOT NULL,
DROP COLUMN deleted; DROP COLUMN deleted;
DROP INDEX entity_relationship_edge_index; DROP INDEX entity_relationship_edge_index;

View File

@ -1,6 +1,9 @@
package org.openmetadata.catalog.jdbi3; package org.openmetadata.catalog.jdbi3;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.openmetadata.catalog.Entity; import org.openmetadata.catalog.Entity;
import org.openmetadata.catalog.type.Include; import org.openmetadata.catalog.type.Include;
@ -39,6 +42,7 @@ public class ListFilter {
condition = addCondition(condition, getServiceCondition(tableName)); condition = addCondition(condition, getServiceCondition(tableName));
condition = addCondition(condition, getParentCondition(tableName)); condition = addCondition(condition, getParentCondition(tableName));
condition = addCondition(condition, getCategoryCondition(tableName)); condition = addCondition(condition, getCategoryCondition(tableName));
condition = addCondition(condition, getWebhookCondition(tableName));
return condition.isEmpty() ? "WHERE TRUE" : "WHERE " + condition; return condition.isEmpty() ? "WHERE TRUE" : "WHERE " + condition;
} }
@ -73,6 +77,11 @@ public class ListFilter {
return category == null ? "" : getCategoryPrefixCondition(tableName, escape(category)); return category == null ? "" : getCategoryPrefixCondition(tableName, escape(category));
} }
public String getWebhookCondition(String tableName) {
String webhookStatus = queryParams.get("status");
return webhookStatus == null ? "" : getStatusPrefixCondition(tableName, escape(webhookStatus));
}
private String getFqnPrefixCondition(String tableName, String fqnPrefix) { private String getFqnPrefixCondition(String tableName, String fqnPrefix) {
return tableName == null return tableName == null
? String.format("fullyQualifiedName LIKE '%s%s%%'", fqnPrefix, Entity.SEPARATOR) ? String.format("fullyQualifiedName LIKE '%s%s%%'", fqnPrefix, Entity.SEPARATOR)
@ -85,6 +94,21 @@ public class ListFilter {
: String.format("%s.category LIKE '%s%s%%'", tableName, category, ""); : String.format("%s.category LIKE '%s%s%%'", tableName, category, "");
} }
private String getStatusPrefixCondition(String tableName, String statusPrefix) {
if (!statusPrefix.isEmpty()) {
List<String> statusList = new ArrayList<>(Arrays.asList(statusPrefix.split(",")));
List<String> condition = new ArrayList<>();
for (String s : statusList) {
String format = "\"" + s + "\"";
condition.add(format);
}
return "status in (" + String.join(",", condition) + ")";
}
return tableName == null
? String.format("status LIKE '%s%s%%'", statusPrefix, "")
: String.format("%s.status LIKE '%s%s%%'", tableName, statusPrefix, "");
}
private String addCondition(String condition1, String condition2) { private String addCondition(String condition1, String condition2) {
if (condition1.isEmpty()) { if (condition1.isEmpty()) {
return condition2; return condition2;

View File

@ -104,6 +104,9 @@ public class WebhookResource extends EntityResource<Webhook, WebhookRepository>
public ResultList<Webhook> list( public ResultList<Webhook> list(
@Context UriInfo uriInfo, @Context UriInfo uriInfo,
@Context SecurityContext securityContext, @Context SecurityContext securityContext,
@Parameter(description = "Filter webhooks by status", schema = @Schema(type = "string", example = "active"))
@QueryParam("status")
String statusParam,
@Parameter(description = "Limit the number webhooks returned. (1 to 1000000, default = " + "10) ") @Parameter(description = "Limit the number webhooks returned. (1 to 1000000, default = " + "10) ")
@DefaultValue("10") @DefaultValue("10")
@Min(0) @Min(0)
@ -123,7 +126,7 @@ public class WebhookResource extends EntityResource<Webhook, WebhookRepository>
@DefaultValue("non-deleted") @DefaultValue("non-deleted")
Include include) Include include)
throws IOException { throws IOException {
ListFilter filter = new ListFilter(Include.ALL); ListFilter filter = new ListFilter(Include.ALL).addQueryParam("status", statusParam);
return listInternal(uriInfo, securityContext, "", filter, limitParam, before, after); return listInternal(uriInfo, securityContext, "", filter, limitParam, before, after);
} }