> For the complete documentation index, see [llms.txt](https://obsrv.sunbird.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://obsrv.sunbird.org/guides/connectors-developer-guide/classes/connectorstats-class.md).

# ConnectorStats Class

## ConnectorStats

### 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

Ref: <https://github.com/Sunbird-Obsrv/connector-sdk-scala/blob/main/connector-sdk-core/src/main/scala/org/sunbird/obsrv/connector/model/ConnectorStats.scala>

### Arguments

#### `connectorInstanceId: String`

* **Description**: The unique identifier for the specific instance of the connector.

#### `statsJson: Option[String]`

* **Description**: The JSON representation of the statistics for the connector instance.

#### `stats: mutable.Map[String, AnyRef]`

* **Description**: A mutable map that holds the statistics metrics for the connector instance.

### Methods

#### `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 `Option` containing the value of the metric, if it exists.

#### `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`

* **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]`

* **Description**: Removes the specified metric from the statistics.
* **Parameters**: `metric` - The name of the metric to remove.
* **Returns**: An `Option` containing the removed value, if it existed.

#### `toJson(): String`

* **Description**: Serializes the statistics to a JSON string.
* **Returns**: A JSON string representation of the statistics.

#### `saveStats(): Unit`

* **Description**: Saves the current statistics to the database.
* **Throws**: `ObsrvException` if the statistics could not be saved.

### Usage

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

```scala
import org.sunbird.obsrv.connector.model.ConnectorStats
import org.sunbird.obsrv.job.util.PostgresConnectionConfig

implicit val postgresConfig: PostgresConnectionConfig = // initialize config

val stats = new ConnectorStats("connectorInstanceId", None)

// Accessing and updating stats
stats.putStat("metric1", 100.asInstanceOf[AnyRef])
println(stats.getStat[Int]("metric1"))
stats.saveStats()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://obsrv.sunbird.org/guides/connectors-developer-guide/classes/connectorstats-class.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
