ConnectorStats Class
ConnectorStats
Section titled “ConnectorStats”Overview
Section titled “Overview”The ConnectorStats class is a part of the org.sunbird.obsrv.connector.model package. It encapsulates the statistics information for a connector instance, allowing for the tracking and management of various metrics associated with the connector’s performance.
Class Definition
Section titled “Class Definition”Arguments
Section titled “Arguments”connectorInstanceId: String
Section titled “connectorInstanceId: String”- Description: The unique identifier for the specific instance of the connector.
statsJson: Option[String]
Section titled “statsJson: Option[String]”- Description: The JSON representation of the statistics for the connector instance.
stats: mutable.Map[String, AnyRef]
Section titled “stats: mutable.Map[String, AnyRef]”- Description: A mutable map that holds the statistics metrics for the connector instance.
Methods
Section titled “Methods”getStat[T](metric: String): Option[T]
Section titled “getStat[T](metric: String): Option[T]”- Description: Retrieves the value of the specified metric.
- Parameters:
metric- The name of the metric to retrieve. - Returns: An
Optioncontaining the value of the metric, if it exists.
getStat[T](metric: String, defaultValue: T): T
Section titled “getStat[T](metric: String, defaultValue: T): T”- Description: Retrieves the value of the specified metric, or returns a default value if the metric does not exist.
- Parameters:
metric- The name of the metric to retrieve.defaultValue- The default value to return if the metric does not exist.
- Returns: The value of the metric, or the default value.
putStat[T <: AnyRef](metric: String, value: T): Unit
Section titled “putStat[T <: AnyRef](metric: String, value: T): Unit”- Description: Adds or updates the value of the specified metric.
- Parameters:
metric- The name of the metric to add or update.value- The value to set for the metric.
removeStat(metric: String): Option[AnyRef]
Section titled “removeStat(metric: String): Option[AnyRef]”- Description: Removes the specified metric from the statistics.
- Parameters:
metric- The name of the metric to remove. - Returns: An
Optioncontaining the removed value, if it existed.
toJson(): String
Section titled “toJson(): String”- Description: Serializes the statistics to a JSON string.
- Returns: A JSON string representation of the statistics.
saveStats(): Unit
Section titled “saveStats(): Unit”- Description: Saves the current statistics to the database.
- Throws:
ObsrvExceptionif the statistics could not be saved.
The ConnectorStats class is used to manage and track the performance metrics of a connector instance. It provides methods to access, update, and persist these metrics.
Example
Section titled “Example”import org.sunbird.obsrv.connector.model.ConnectorStatsimport org.sunbird.obsrv.job.util.PostgresConnectionConfig
implicit val postgresConfig: PostgresConnectionConfig = // initialize config
val stats = new ConnectorStats("connectorInstanceId", None)
// Accessing and updating statsstats.putStat("metric1", 100.asInstanceOf[AnyRef])println(stats.getStat[Int]("metric1"))stats.saveStats()