Class CapabilitiesHelper

java.lang.Object
ai.wanaku.cli.main.support.CapabilitiesHelper

public final class CapabilitiesHelper extends Object
Utility class providing capabilities management functionality for the Wanaku CLI.

This class handles the fetching, merging, and processing of service capabilities from various sources including management tools and resource providers. It provides methods to combine activity states, format data for display, and create printable representations of service capabilities.

The class follows a reactive programming model using Mutiny's Uni for asynchronous operations and provides comprehensive error handling for API calls.

Since:
1.0
Version:
1.0
Author:
Wanaku Team
  • Field Details

    • API_TIMEOUT

      public static final Duration API_TIMEOUT
      Default timeout duration for API calls.
    • DEFAULT_STATUS

      public static final String DEFAULT_STATUS
      Default status value used when actual status is unavailable.
      See Also:
    • ACTIVE_STATUS

      public static final String ACTIVE_STATUS
      Status value indicating an active service.
      See Also:
    • INACTIVE_STATUS

      public static final String INACTIVE_STATUS
      Status value indicating an inactive service.
      See Also:
    • VERBOSE_TIMESTAMP_FORMATTER

      public static final DateTimeFormatter VERBOSE_TIMESTAMP_FORMATTER
      Formatter for verbose timestamp display. Format: "EEE, MMM dd, yyyy 'at' HH:mm:ss" (e.g., "Mon, Jan 15, 2024 at 14:30:45")
    • COLUMNS

      public static final String[] COLUMNS
      Standard column names for capability display.

      This array defines the order and names of columns when displaying capabilities in table or map format. The order matches the fields in CapabilitiesHelper.PrintableCapability.

  • Method Details

    • fetchAndMergeCapabilities

      public static io.smallrye.mutiny.Uni<List<CapabilitiesHelper.PrintableCapability>> fetchAndMergeCapabilities(TargetsService targetsService)
      Fetches and merges capabilities from multiple sources asynchronously.

      This method combines data from management tools and resource providers, along with their respective activity states, to create a unified list of printable capabilities.

      Parameters:
      targetsService - the service used to fetch target information
      Returns:
      a Uni emitting a list of CapabilitiesHelper.PrintableCapability objects
      Throws:
      NullPointerException - if targetsService is null
      See Also:
    • combineDataIntoCapabilities

      public static List<CapabilitiesHelper.PrintableCapability> combineDataIntoCapabilities(List<?> responses)
      Combines fetched data from multiple sources into a list of printable capabilities.

      This method expects exactly 4 responses in the following order:

      1. Management tools list
      2. Tools activity state map
      3. Resource providers list
      4. Resources activity state map
      Parameters:
      responses - list containing exactly 4 responses from API calls
      Returns:
      list of CapabilitiesHelper.PrintableCapability objects
      Throws:
      IndexOutOfBoundsException - if responses list doesn't contain exactly 4 elements
      ClassCastException - if response types don't match expected types
    • mergeActivityStates

      public static Map<String,List<ActivityRecord>> mergeActivityStates(Map<String,List<ActivityRecord>> toolsActivityState, Map<String,List<ActivityRecord>> resourcesActivityState)
      Merges two activity state maps into a single map.

      When duplicate keys are found, the activity records from both maps are combined into a single list for that key.

      Parameters:
      toolsActivityState - activity state map for management tools
      resourcesActivityState - activity state map for resource providers
      Returns:
      merged activity state map with immutable value lists
      Throws:
      NullPointerException - if either parameter is null
    • findActivityRecord

      public static ActivityRecord findActivityRecord(ServiceTarget serviceTarget, Map<String,List<ActivityRecord>> activityStates)
      Finds the activity record for a specific service target.

      Searches through the activity states map to find a matching activity record based on service name and target ID.

      Parameters:
      serviceTarget - the service target to find activity record for
      activityStates - map of activity states indexed by service name
      Returns:
      the matching ActivityRecord or null if not found
      Throws:
      NullPointerException - if either parameter is null
    • createPrintableCapability

      public static CapabilitiesHelper.PrintableCapability createPrintableCapability(ServiceTarget serviceTarget, Map<String,List<ActivityRecord>> activityStates)
      Creates a printable capability from a service target and activity states.

      This method combines service target information with its corresponding activity record to create a formatted representation suitable for display.

      Parameters:
      serviceTarget - the service target containing basic service information
      activityStates - map of activity states to find matching activity record
      Returns:
      a CapabilitiesHelper.PrintableCapability with formatted service information
      Throws:
      NullPointerException - if either parameter is null
    • fetchManagementTools

      public static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchManagementTools(TargetsService targetsService)
      Fetches the list of management tools from the targets service.
      Parameters:
      targetsService - the service to fetch management tools from
      Returns:
      a Uni emitting a list of ServiceTarget objects
      Throws:
      NullPointerException - if targetsService is null
    • fetchToolsActivityState

      public static io.smallrye.mutiny.Uni<Map<String,List<ActivityRecord>>> fetchToolsActivityState(TargetsService targetsService)
      Fetches the activity state for management tools.
      Parameters:
      targetsService - the service to fetch activity state from
      Returns:
      a Uni emitting a map of service names to activity records
      Throws:
      NullPointerException - if targetsService is null
    • fetchResourceProviders

      public static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchResourceProviders(TargetsService targetsService)
      Fetches the list of resource providers from the targets service.
      Parameters:
      targetsService - the service to fetch resource providers from
      Returns:
      a Uni emitting a list of ServiceTarget objects
      Throws:
      NullPointerException - if targetsService is null
    • fetchResourcesActivityState

      public static io.smallrye.mutiny.Uni<Map<String,List<ActivityRecord>>> fetchResourcesActivityState(TargetsService targetsService)
      Fetches the activity state for resource providers.
      Parameters:
      targetsService - the service to fetch activity state from
      Returns:
      a Uni emitting a map of service names to activity records
      Throws:
      NullPointerException - if targetsService is null
    • executeApiCall

      public static <T> io.smallrye.mutiny.Uni<T> executeApiCall(Supplier<WanakuResponse<T>> apiCall, T defaultValue)
      Executes an API call with error handling and fallback to default value.

      This method provides a consistent way to handle API calls that return WanakuResponse objects. If the response contains an error or if an exception occurs, the method returns the provided default value.

      Type Parameters:
      T - the type of data returned by the API call
      Parameters:
      apiCall - supplier that performs the API call
      defaultValue - value to return in case of error or exception
      Returns:
      a Uni emitting either the API response data or the default value
      Throws:
      NullPointerException - if apiCall is null
      WanakuException - if the API call fails with an exception
    • determineServiceStatus

      public static String determineServiceStatus(ActivityRecord activityRecord)
      Determines the service status based on activity record.
      Parameters:
      activityRecord - the activity record to check (may be null)
      Returns:
      ACTIVE_STATUS, INACTIVE_STATUS, or DEFAULT_STATUS
    • formatLastSeenTimestamp

      public static String formatLastSeenTimestamp(ActivityRecord activityRecord)
      Formats the last seen timestamp from an activity record.

      The timestamp is formatted using the system default timezone and the VERBOSE_TIMESTAMP_FORMATTER pattern.

      Parameters:
      activityRecord - the activity record containing the timestamp (may be null)
      Returns:
      formatted timestamp string or empty string if record is null or has no timestamp
    • processServiceConfigurations

      public static List<CapabilitiesHelper.PrintableCapabilityConfiguration> processServiceConfigurations(Map<String,String> configurations)
      Processes service configurations into printable format.

      Note: This method currently returns an empty list as configuration processing requirements are not yet finalized.

      Parameters:
      configurations - map of configuration key-value pairs
      Returns:
      list of CapabilitiesHelper.PrintableCapabilityConfiguration objects (currently empty)
    • printCapabilities

      public static void printCapabilities(List<CapabilitiesHelper.PrintableCapability> capabilities, WanakuPrinter printer)
      Prints a list of capabilities in table format.
      Parameters:
      capabilities - list of capabilities to print
      printer - the printer to use for output
      Throws:
      NullPointerException - if either parameter is null
    • printCapability

      public static void printCapability(CapabilitiesHelper.PrintableCapability capability, WanakuPrinter printer)
      Prints a single capability in map format.
      Parameters:
      capability - the capability to print
      printer - the printer to use for output
      Throws:
      NullPointerException - if either parameter is null