sergehuber commented on code in PR #763: URL: https://github.com/apache/unomi/pull/763#discussion_r3377899804
########## tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/scheduler/PurgeTasksCommand.java: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.dev.commands.scheduler; + +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.unomi.api.tasks.ScheduledTask; + +import java.util.Calendar; +import java.util.Date; + +@Command(scope = "unomi", name = "task-purge", description = "Purges old completed tasks") +@Service +public class PurgeTasksCommand extends BaseSchedulerCommand { + + @Option(name = "-d", aliases = "--days", description = "Number of days to keep completed tasks (default: 7)", required = false) + private int daysToKeep = 7; + + @Option(name = "-f", aliases = "--force", description = "Skip confirmation prompt", required = false) + private boolean force = false; + + @Override + public Object execute() throws Exception { + if (!force) { + String response = session.readLine( + "This will permanently delete all completed tasks older than " + daysToKeep + " days. Continue? (y/n): ", + null + ); + if (!"y".equalsIgnoreCase(response != null ? response.trim() : "n")) { + println("Operation cancelled."); + return null; + } + } + + // Calculate cutoff date + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_MONTH, -daysToKeep); + Date cutoffDate = cal.getTime(); + + // Get completed tasks + int offset = 0; + int batchSize = 100; + int purgedCount = 0; + + while (true) { + var tasks = schedulerService.getTasksByStatus(ScheduledTask.TaskStatus.COMPLETED, offset, batchSize, null); + if (tasks.getList().isEmpty()) { + break; + } + + // Cancel old completed tasks + for (ScheduledTask task : tasks.getList()) { + if (task.getLastExecutionDate() != null && task.getLastExecutionDate().before(cutoffDate)) { + schedulerService.cancelTask(task.getItemId()); + purgedCount++; Review Comment: Correct, `cancelTask` only marks tasks as CANCELLED and keeps them in storage, which is the opposite of purging. We added a proper `deleteTask` method to `SchedulerService` (and down through `SchedulerProvider` and `PersistenceSchedulerProvider`) and switched the purge command to use it. ########## tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantCrudCommand.java: ########## @@ -0,0 +1,278 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.dev.commands.tenants; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.karaf.shell.support.table.ShellTable; + +import java.io.PrintStream; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.tenants.*; +import org.apache.unomi.common.DataTable; +import org.apache.unomi.persistence.spi.CustomObjectMapper; +import org.apache.unomi.shell.dev.services.BaseCrudCommand; +import org.apache.unomi.shell.dev.services.CrudCommand; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * A command to perform CRUD operations on tenants + */ +@Component(service = CrudCommand.class, immediate = true) +public class TenantCrudCommand extends BaseCrudCommand { + + private static final Logger LOGGER = LoggerFactory.getLogger(TenantCrudCommand.class.getName()); + private static final ObjectMapper OBJECT_MAPPER = new CustomObjectMapper(); + private static final List<String> PROPERTY_NAMES = List.of( + "itemId", "name", "description", "status", "creationDate", "lastModificationDate", "resourceQuota", "properties", "restrictedEventPermissions", "authorizedIPs" + ); + + @Reference + private TenantService tenantService; + + @Override + public String getObjectType() { + return "tenant"; + } + + @Override + protected String[] getHeadersWithoutTenant() { + return new String[]{"ID", "Name", "Description", "Status", "Created", "Modified"}; + } + + /** + * Override to skip the tenant column since we're listing tenants themselves. + * The tenant ID would be the same as the ID column, making it redundant. + */ + @Override + public String[] getHeaders() { + return getHeadersWithoutTenant(); + } + + /** + * Override to skip prepending tenant ID to rows since we're listing tenants themselves. + */ + @Override + protected DataTable buildDataTable() { + PrintStream console = getConsole(); + try { + Query query = buildQuery(maxEntries); + PartialList<?> items = getItems(query); + + printPaginationWarning(items, console); + + DataTable dataTable = new DataTable(); + for (Object item : items.getList()) { + Comparable[] rowData = buildRow(item); + dataTable.addRow(rowData); + } + + return dataTable; + } catch (Exception e) { + LOGGER.error("Error building data table", e); + console.println("Error: " + e.getMessage()); + return new DataTable(); + } + } + + /** + * Override to skip prepending tenant ID to rows since we're listing tenants themselves. + */ + @Override + public void buildRows(ShellTable table, int maxEntries) { + PrintStream console = getConsole(); + try { + Query query = buildQuery(maxEntries); + PartialList<?> items = getItems(query); + + printPaginationWarning(items, console); + + for (Object item : items.getList()) { + Comparable[] rowData = buildRow(item); + table.addRow().addContent(rowData); + } + } catch (Exception e) { + console.println("Error: " + e.getMessage()); + LOGGER.error("Error building rows", e); + } + } + + @Override + protected PartialList<?> getItems(Query query) { + List<Tenant> tenants = tenantService.getAllTenants(); + // Filter out system tenant + tenants = tenants.stream() + .filter(tenant -> !TenantService.SYSTEM_TENANT.equals(tenant.getItemId())) + .collect(Collectors.toList()); + return new PartialList<>(tenants, 0, tenants.size(), tenants.size(), PartialList.Relation.EQUAL); + } + + @Override + protected Comparable[] buildRow(Object item) { + Tenant tenant = (Tenant) item; + return new Comparable[]{ + tenant.getItemId(), + tenant.getName(), + tenant.getDescription(), + tenant.getStatus() != null ? tenant.getStatus().toString() : "", + tenant.getCreationDate() != null ? tenant.getCreationDate().toString() : "", + tenant.getLastModificationDate() != null ? tenant.getLastModificationDate().toString() : "" + }; + } + + /** + * Special case for tenants: the tenant ID is the same as the item ID for tenant objects. + */ + @Override + protected String getTenantIdFromItem(Object item) { + if (item instanceof Tenant) { + Tenant tenant = (Tenant) item; + return tenant.getItemId(); + } + return super.getTenantIdFromItem(item); + } + + @Override + public Map<String, Object> read(String id) { + Tenant tenant = tenantService.getTenant(id); + if (tenant == null || TenantService.SYSTEM_TENANT.equals(tenant.getItemId())) { + return null; + } + return OBJECT_MAPPER.convertValue(tenant, Map.class); + } + + @Override + public String create(Map<String, Object> properties) { + String id = (String) properties.remove("itemId"); + if (id == null) { + return null; + } + + try { + // Create the tenant + Tenant tenant = tenantService.createTenant(id, properties); + + // Generate API keys with no expiration + ApiKey publicKey = tenantService.generateApiKeyWithType(tenant.getItemId(), ApiKey.ApiKeyType.PUBLIC, null); + ApiKey privateKey = tenantService.generateApiKeyWithType(tenant.getItemId(), ApiKey.ApiKeyType.PRIVATE, null); + + // Save the tenant with the new API keys + tenantService.saveTenant(tenant); + Review Comment: You're right. `createTenant()` already handles both key generation and persistence internally, so calling `generateApiKeyWithType` again and then `saveTenant` on the stale instance was both redundant and risky (it would overwrite the freshly generated keys). Replaced the whole block with just `createTenant(id, properties)`. ########## tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/scheduler/ListTasksCommand.java: ########## @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.dev.commands.scheduler; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.table.Col; +import org.apache.karaf.shell.support.table.ShellTable; + +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.services.SchedulerService; +import org.apache.unomi.api.tasks.ScheduledTask; +import org.apache.unomi.shell.dev.commands.CommandUtils; + +import java.io.PrintStream; +import java.util.List; + +@Command(scope = "unomi", name = "task-list", description = "Lists scheduled tasks") +@Service +public class ListTasksCommand extends BaseSchedulerCommand { + + @Option(name = "-s", aliases = "--status", description = "Filter by task status (SCHEDULED, RUNNING, COMPLETED, FAILED, CANCELLED, CRASHED)", required = false) + private String status; + + @Option(name = "-t", aliases = "--type", description = "Filter by task type", required = false) + private String type; + + @Option(name = "--limit", description = "Maximum number of tasks to display (default: 50)", required = false) + private int limit = 50; + + @Override + public Object execute() throws Exception { + PrintStream console = getConsole(); + ShellTable table = new ShellTable(); + + // Configure table columns + table.column(new Col("ID").maxSize(36)); + table.column(new Col("Type").maxSize(30)); + table.column(new Col("Status").maxSize(10)); + table.column(new Col("Next Run").maxSize(19)); + table.column(new Col("Last Run").maxSize(19)); + table.column(new Col("Failures").alignRight()); + table.column(new Col("Successes").alignRight()); + table.column(new Col("Total Exec").alignRight()); + table.column(new Col("Persistent").maxSize(10)); + + // Get tasks based on filters + List<ScheduledTask> tasks; + if (status != null) { + try { + ScheduledTask.TaskStatus taskStatus = ScheduledTask.TaskStatus.valueOf(status.toUpperCase()); + // Get persistent tasks + PartialList<ScheduledTask> filteredTasks = schedulerService.getTasksByStatus(taskStatus, 0, limit, null); + tasks = filteredTasks.getList(); + // Add memory tasks with matching status + List<ScheduledTask> memoryTasks = schedulerService.getMemoryTasks(); + for (ScheduledTask task : memoryTasks) { + if (task.getStatus() == taskStatus) { + tasks.add(task); + } + } + } catch (IllegalArgumentException e) { + println("Invalid status: " + status); + return null; + } + } else if (type != null) { + // Get persistent tasks + PartialList<ScheduledTask> filteredTasks = schedulerService.getTasksByType(type, 0, limit, null); + tasks = filteredTasks.getList(); + // Add memory tasks with matching type + List<ScheduledTask> memoryTasks = schedulerService.getMemoryTasks(); + for (ScheduledTask task : memoryTasks) { + if (task.getTaskType().equals(type)) { + tasks.add(task); + } + } Review Comment: Same issue as the status branch above — addressed in the same fix. ########## tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantCrudCommand.java: ########## @@ -0,0 +1,278 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.dev.commands.tenants; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.karaf.shell.support.table.ShellTable; + +import java.io.PrintStream; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.tenants.*; +import org.apache.unomi.common.DataTable; +import org.apache.unomi.persistence.spi.CustomObjectMapper; +import org.apache.unomi.shell.dev.services.BaseCrudCommand; +import org.apache.unomi.shell.dev.services.CrudCommand; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * A command to perform CRUD operations on tenants + */ +@Component(service = CrudCommand.class, immediate = true) +public class TenantCrudCommand extends BaseCrudCommand { + + private static final Logger LOGGER = LoggerFactory.getLogger(TenantCrudCommand.class.getName()); + private static final ObjectMapper OBJECT_MAPPER = new CustomObjectMapper(); + private static final List<String> PROPERTY_NAMES = List.of( + "itemId", "name", "description", "status", "creationDate", "lastModificationDate", "resourceQuota", "properties", "restrictedEventPermissions", "authorizedIPs" + ); + + @Reference + private TenantService tenantService; + + @Override + public String getObjectType() { + return "tenant"; + } + + @Override + protected String[] getHeadersWithoutTenant() { + return new String[]{"ID", "Name", "Description", "Status", "Created", "Modified"}; + } + + /** + * Override to skip the tenant column since we're listing tenants themselves. + * The tenant ID would be the same as the ID column, making it redundant. + */ + @Override + public String[] getHeaders() { + return getHeadersWithoutTenant(); + } + + /** + * Override to skip prepending tenant ID to rows since we're listing tenants themselves. + */ + @Override + protected DataTable buildDataTable() { + PrintStream console = getConsole(); + try { + Query query = buildQuery(maxEntries); + PartialList<?> items = getItems(query); + + printPaginationWarning(items, console); + + DataTable dataTable = new DataTable(); + for (Object item : items.getList()) { + Comparable[] rowData = buildRow(item); + dataTable.addRow(rowData); + } + + return dataTable; + } catch (Exception e) { + LOGGER.error("Error building data table", e); + console.println("Error: " + e.getMessage()); + return new DataTable(); + } + } + + /** + * Override to skip prepending tenant ID to rows since we're listing tenants themselves. + */ + @Override + public void buildRows(ShellTable table, int maxEntries) { + PrintStream console = getConsole(); + try { + Query query = buildQuery(maxEntries); + PartialList<?> items = getItems(query); + + printPaginationWarning(items, console); + + for (Object item : items.getList()) { + Comparable[] rowData = buildRow(item); + table.addRow().addContent(rowData); + } + } catch (Exception e) { + console.println("Error: " + e.getMessage()); + LOGGER.error("Error building rows", e); + } + } + + @Override + protected PartialList<?> getItems(Query query) { + List<Tenant> tenants = tenantService.getAllTenants(); + // Filter out system tenant + tenants = tenants.stream() + .filter(tenant -> !TenantService.SYSTEM_TENANT.equals(tenant.getItemId())) + .collect(Collectors.toList()); + return new PartialList<>(tenants, 0, tenants.size(), tenants.size(), PartialList.Relation.EQUAL); + } + + @Override + protected Comparable[] buildRow(Object item) { + Tenant tenant = (Tenant) item; + return new Comparable[]{ + tenant.getItemId(), + tenant.getName(), + tenant.getDescription(), + tenant.getStatus() != null ? tenant.getStatus().toString() : "", + tenant.getCreationDate() != null ? tenant.getCreationDate().toString() : "", + tenant.getLastModificationDate() != null ? tenant.getLastModificationDate().toString() : "" + }; + } + + /** + * Special case for tenants: the tenant ID is the same as the item ID for tenant objects. + */ + @Override + protected String getTenantIdFromItem(Object item) { + if (item instanceof Tenant) { + Tenant tenant = (Tenant) item; + return tenant.getItemId(); + } + return super.getTenantIdFromItem(item); + } + + @Override + public Map<String, Object> read(String id) { + Tenant tenant = tenantService.getTenant(id); + if (tenant == null || TenantService.SYSTEM_TENANT.equals(tenant.getItemId())) { + return null; + } + return OBJECT_MAPPER.convertValue(tenant, Map.class); + } + + @Override + public String create(Map<String, Object> properties) { + String id = (String) properties.remove("itemId"); + if (id == null) { + return null; + } + + try { + // Create the tenant + Tenant tenant = tenantService.createTenant(id, properties); + + // Generate API keys with no expiration + ApiKey publicKey = tenantService.generateApiKeyWithType(tenant.getItemId(), ApiKey.ApiKeyType.PUBLIC, null); + ApiKey privateKey = tenantService.generateApiKeyWithType(tenant.getItemId(), ApiKey.ApiKeyType.PRIVATE, null); + + // Save the tenant with the new API keys + tenantService.saveTenant(tenant); + + return tenant.getItemId(); + } catch (Exception e) { + return null; + } + } + + @Override + public void update(String id, Map<String, Object> properties) { + Tenant tenant = tenantService.getTenant(id); + if (tenant == null || TenantService.SYSTEM_TENANT.equals(tenant.getItemId())) { + return; + } + + try { + // Update tenant properties + if (properties.containsKey("name")) { + tenant.setName((String) properties.get("name")); + } + if (properties.containsKey("description")) { + tenant.setDescription((String) properties.get("description")); + } + if (properties.containsKey("status")) { + tenant.setStatus(Enum.valueOf(TenantStatus.class, (String) properties.get("status"))); + } + if (properties.containsKey("resourceQuota")) { + tenant.setResourceQuota(OBJECT_MAPPER.convertValue(properties.get("resourceQuota"), ResourceQuota.class)); + } + if (properties.containsKey("properties")) { + @SuppressWarnings("unchecked") + Map<String, Object> props = (Map<String, Object>) properties.get("properties"); + tenant.setProperties(props); + } + if (properties.containsKey("restrictedEventPermissions")) { + @SuppressWarnings("unchecked") + Set<String> permissions = new HashSet<>((List<String>) properties.get("restrictedEventPermissions")); + tenant.setRestrictedEventTypes(permissions); + } + if (properties.containsKey("authorizedIPs")) { + @SuppressWarnings("unchecked") + Set<String> ips = new HashSet<>((List<String>) properties.get("authorizedIPs")); + tenant.setAuthorizedIPs(ips); + } + + tenant.setLastModificationDate(new Date()); + tenantService.saveTenant(tenant); + } catch (Exception e) { + // Handle error + } Review Comment: Agreed, silently swallowing the exception and then printing "Updated …" would make failures very hard to diagnose. Fixed by logging the error and rethrowing as a `RuntimeException`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
