# WorkMetricsInfoRepository

Artifact: [androidx.work:work-analytics](https://developer.android.com/jetpack/androidx/releases/work) [View Source](https://cs.android.com/search?q=file:androidx/work/analytics/WorkMetricsInfoRepository.kt+class:androidx.work.analytics.WorkMetricsInfoRepository) Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

*** ** * ** ***


```
@ExperimentalEventsApi
@ExperimentalWorkMetricsApi
class WorkMetricsInfoRepository : ScheduleEventListener, ExecutionEventListener
```

<br />

*** ** * ** ***

Repository class that calculates and stores metrics info and analytics about workers.

## Summary

| ### Public companion properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#DEFAULT_RETENTION_TIME_MILLIS()` The default retention time for finished work metrics (7 days). |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#MAX_RETENTION_TIME_MILLIS()` The maximum allowed retention time for finished work metrics (30 days). |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,java.util.concurrent.Executor)(context: https://developer.android.com/reference/android/content/Context.html, dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html?)` Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a default retention of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#DEFAULT_RETENTION_TIME_MILLIS()`. |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,java.time.Duration,java.util.concurrent.Executor)( context: https://developer.android.com/reference/android/content/Context.html, retentionDuration: https://developer.android.com/reference/java/time/Duration.html, dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html? )` Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention duration. |
| `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,kotlin.Long,java.util.concurrent.TimeUnit,java.util.concurrent.Executor)( context: https://developer.android.com/reference/android/content/Context.html, retentionTime: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, retentionTimeUnit: https://developer.android.com/reference/java/util/concurrent/TimeUnit.html, dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html? )` Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention time. |

| ### Public functions |
|---|---|
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo>` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#getWorkMetricsInfoById(java.util.UUID)(workId: https://developer.android.com/reference/java/util/UUID.html)` Gets a list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` snapshots for a given work id. |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo>` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#getWorkMetricsInfos(androidx.work.analytics.WorkMetricsQuery)(query: https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsQuery)` Gets a list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` matching the query criteria. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onCancelled(androidx.work.WorkInfo)(workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request is canceled from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#cancelWorkById(java.util.UUID)`. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onEnqueued(androidx.work.WorkInfo)(workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request is enqueued from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#enqueue(androidx.work.WorkRequest)`. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onException(kotlin.Throwable,androidx.work.WorkInfo)(throwable: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html, workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when the work request fails from an exception after it has started. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onFinished(androidx.work.ListenableWorker.Result,androidx.work.WorkInfo)(result: https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result, workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when the worker finishes and returns a value. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onPrerequisiteFailed(androidx.work.WorkInfo)(workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request fails because a prerequisite work fails. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onStarted(androidx.work.WorkInfo)(workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request starts executing i.e. `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#startWork()`. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onStopped(kotlin.Int,androidx.work.WorkInfo)(stopReason: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request is stopped by the system after it has started. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onUnblocked(androidx.work.WorkInfo)(workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request is no longer waiting on any of its prerequisite work. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#onUpdated(androidx.work.WorkInfo,androidx.work.WorkInfo)(oldWorkInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo, updatedWorkInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo)` Called when a work request is updated from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#updateWork(androidx.work.WorkRequest)`. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo>` | `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#finishedWorkMetricsInfoFlow()` A hot `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` that emits a `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` whenever one finishes. |

## Public companion properties

### DEFAULT_RETENTION_TIME_MILLIS

```
val DEFAULT_RETENTION_TIME_MILLIS: Long
```

The default retention time for finished work metrics (7 days).

### MAX_RETENTION_TIME_MILLIS

```
val MAX_RETENTION_TIME_MILLIS: Long
```

The maximum allowed retention time for finished work metrics (30 days).

## Public constructors

### WorkMetricsInfoRepository

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
WorkMetricsInfoRepository(context: Context, dbExecutor: Executor? = null)
```

Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a default retention of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#DEFAULT_RETENTION_TIME_MILLIS()`.

It is recommended that the `https://developer.android.com/reference/java/util/concurrent/Executor.html` passed here is the same as the one passed into `https://developer.android.com/reference/kotlin/androidx/work/Configuration.Builder#setTaskExecutor(java.util.concurrent.Executor)`. If no executor is provided, Room's default executors will be used instead.

Note that this executor is only used for database operations and not for running the event hooks themselves.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The application `https://developer.android.com/reference/android/content/Context.html`. |
| `dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html? = null` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` passed to Room on which database queries and transactions will be run. |

### WorkMetricsInfoRepository

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
@RequiresApi(value = 26)
WorkMetricsInfoRepository(
    context: Context,
    retentionDuration: Duration,
    dbExecutor: Executor? = null
)
```

Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention duration.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The application `https://developer.android.com/reference/android/content/Context.html`. |
| `retentionDuration: https://developer.android.com/reference/java/time/Duration.html` | The retention duration. Clamped to a maximum of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#MAX_RETENTION_TIME_MILLIS()`. Must be positive. |
| `dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html? = null` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` passed to Room on which database queries and transactions will be run. |

### WorkMetricsInfoRepository

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
WorkMetricsInfoRepository(
    context: Context,
    retentionTime: Long,
    retentionTimeUnit: TimeUnit,
    dbExecutor: Executor? = null
)
```

Creates an instance of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention time.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The application `https://developer.android.com/reference/android/content/Context.html`. |
| `retentionTime: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The retention time. Clamped to a maximum of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#MAX_RETENTION_TIME_MILLIS()`. Must be positive. |
| `retentionTimeUnit: https://developer.android.com/reference/java/util/concurrent/TimeUnit.html` | The `https://developer.android.com/reference/java/util/concurrent/TimeUnit.html` for `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#(android.content.Context,kotlin.Long,java.util.concurrent.TimeUnit,java.util.concurrent.Executor)`. |
| `dbExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html? = null` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` passed to Room on which database queries and transactions will be run. |

## Public functions

### getWorkMetricsInfoById

```
suspend fun getWorkMetricsInfoById(workId: UUID): List<WorkMetricsInfo>
```

Gets a list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` snapshots for a given work id.

| Parameters |
|---|---|
| `workId: https://developer.android.com/reference/java/util/UUID.html` | The identifier of the `https://developer.android.com/reference/kotlin/androidx/work/WorkRequest`. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo>` | A list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` records associated with the `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfoRepository#getWorkMetricsInfoById(java.util.UUID)`. |

### getWorkMetricsInfos

```
suspend fun getWorkMetricsInfos(query: WorkMetricsQuery): List<WorkMetricsInfo>
```

Gets a list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` matching the query criteria.

| Parameters |
|---|---|
| `query: https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsQuery` | The `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsQuery` containing filter parameters. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo>` | A list of `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` records matching the query parameters. |

### onCancelled

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onCancelled(workInfo: WorkInfo): Unit
```

Called when a work request is canceled from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#cancelWorkById(java.util.UUID)`.

| Parameters |
|---|---|
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onEnqueued

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onEnqueued(workInfo: WorkInfo): Unit
```

Called when a work request is enqueued from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#enqueue(androidx.work.WorkRequest)`.

| Parameters |
|---|---|
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onException

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onException(throwable: Throwable, workInfo: WorkInfo): Unit
```

Called when the work request fails from an exception after it has started.

| Parameters |
|---|---|
| `throwable: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html` | Throwable thrown by worker |
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onFinished

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onFinished(result: ListenableWorker.Result, workInfo: WorkInfo): Unit
```

Called when the worker finishes and returns a value.

| Parameters |
|---|---|
| `result: https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result` | `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result` from work finishing |
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onPrerequisiteFailed

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onPrerequisiteFailed(workInfo: WorkInfo): Unit
```

Called when a work request fails because a prerequisite work fails.

| Parameters |
|---|---|
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onStarted

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onStarted(workInfo: WorkInfo): Unit
```

Called when a work request starts executing i.e. `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#startWork()`.

| Parameters |
|---|---|
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onStopped

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onStopped(stopReason: Int, workInfo: WorkInfo): Unit
```

Called when a work request is stopped by the system after it has started.

| Parameters |
|---|---|
| `stopReason: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Reason why work stopped |
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onUnblocked

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onUnblocked(workInfo: WorkInfo): Unit
```

Called when a work request is no longer waiting on any of its prerequisite work. If the work has no prerequisite work, this is called immediately after `https://developer.android.com/reference/kotlin/androidx/work/ScheduleEventListener#onEnqueued(androidx.work.WorkInfo)`.

| Parameters |
|---|---|
| `workInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info |

### onUpdated

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
open suspend fun onUpdated(oldWorkInfo: WorkInfo, updatedWorkInfo: WorkInfo): Unit
```

Called when a work request is updated from the app e.g. `https://developer.android.com/reference/kotlin/androidx/work/WorkManager#updateWork(androidx.work.WorkRequest)`.

| Parameters |
|---|---|
| `oldWorkInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info before the update |
| `updatedWorkInfo: https://developer.android.com/reference/kotlin/androidx/work/WorkInfo` | Snapshot of the work info after the update |

## Public properties

### finishedWorkMetricsInfoFlow

Added in [2.12.0](https://developer.android.com/jetpack/androidx/releases/work#2.12.0)

```
val finishedWorkMetricsInfoFlow: Flow<WorkMetricsInfo>
```

A hot `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` that emits a `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` whenever one finishes.

A `https://developer.android.com/reference/kotlin/androidx/work/analytics/WorkMetricsInfo` is considered finished when the request period is complete or obsolete, either because the work finished or a new request period started (e.g. if the work request is updated or a periodic request completes a period).