java.lang.Object
org.gcube.informationsystem.resourceregistry.statistics.StatisticsWorker

public class StatisticsWorker extends Object
Computes the groups of the public statistics in the background and keeps the last result of each, which is what GET /statistics returns: a public request never touches the database.

The light groups (contexts, types, queryTemplates) are counts over data the registry already caches and are refreshed every LIGHT_INTERVAL_MINUTES minutes; the heavy ones (instances, connectivity) scan every instance and are refreshed every HEAVY_INTERVAL_HOURS hours, in one pass.

The worker never delays the startup of the service nor a request: it runs on its own thread, which checks every TICK_MINUTES minute which groups are due. A group is due when its interval has elapsed or its last computation failed, so a computation that fails at startup, before the database is ready, is retried at the next check; a failed computation keeps the previous result of the group.

Author:
Luca Frosini (ISTI - CNR)
  • Field Details

    • LIGHT_INTERVAL_MINUTES

      public static final long LIGHT_INTERVAL_MINUTES
      The refresh interval of the light groups.
      See Also:
    • HEAVY_INTERVAL_HOURS

      public static final long HEAVY_INTERVAL_HOURS
      The refresh interval of the heavy groups.
      See Also:
    • TICK_MINUTES

      public static final long TICK_MINUTES
      How often the worker checks which groups are due.
      See Also:
    • COMPUTED_AT_PROPERTY

      public static final String COMPUTED_AT_PROPERTY
      The name of the field each group carries with the time it was computed.
      See Also:
    • GROUPS

      public static final String[] GROUPS
      The groups, in the order of the document.
  • Method Details

    • getInstance

      public static StatisticsWorker getInstance()
      Returns the singleton instance of StatisticsWorker.
      Returns:
      the singleton instance
    • start

      public void start()
      Starts the background thread, which computes every group at its first check. Idempotent.
    • shutdown

      public void shutdown()
      Stops the background thread. Idempotent.
    • computeLightGroups

      public boolean computeLightGroups()
      Computes the contexts, types and queryTemplates groups.
      Returns:
      true if every group was computed
    • computeHeavyGroups

      public boolean computeHeavyGroups()
      Computes the instances and connectivity groups, in one scan.
      Returns:
      true if both groups were computed
    • document

      public org.gcube.com.fasterxml.jackson.databind.node.ObjectNode document()
      Returns the statistics document: every group, with computedAt null for a group whose first computation has not ended yet.
      Returns:
      the document