# 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
public final class WorkMetricsInfoRepository implements ScheduleEventListener, ExecutionEventListener
```

<br />

*** ** * ** ***

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

## Summary

| ### Public fields |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#DEFAULT_RETENTION_TIME_MILLIS()` The default retention time for finished work metrics (7 days). |
| `static final long` | `https://developer.android.com/reference/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/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,java.util.concurrent.Executor)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/java/util/concurrent/Executor.html dbExecutor)` Creates an instance of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository` with a default retention of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#DEFAULT_RETENTION_TIME_MILLIS()`. |
| `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,java.time.Duration,java.util.concurrent.Executor)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html retentionDuration, https://developer.android.com/reference/java/util/concurrent/Executor.html dbExecutor )` Creates an instance of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention duration. |
| `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#WorkMetricsInfoRepository(android.content.Context,kotlin.Long,java.util.concurrent.TimeUnit,java.util.concurrent.Executor)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, long retentionTime, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/TimeUnit.html retentionTimeUnit, https://developer.android.com/reference/java/util/concurrent/Executor.html dbExecutor )` Creates an instance of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository` with a custom retention time. |

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

## Public fields

### DEFAULT_RETENTION_TIME_MILLIS

```
public static final long DEFAULT_RETENTION_TIME_MILLIS
```

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

### MAX_RETENTION_TIME_MILLIS

```
public static final long MAX_RETENTION_TIME_MILLIS
```

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)

```
public WorkMetricsInfoRepository(@NonNull Context context, Executor dbExecutor)
```

Creates an instance of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository` with a default retention of `https://developer.android.com/reference/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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/java/util/concurrent/Executor.html dbExecutor` | 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)
public WorkMetricsInfoRepository(
    @NonNull Context context,
    @NonNull Duration retentionDuration,
    Executor dbExecutor
)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html retentionDuration` | The retention duration. Clamped to a maximum of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#MAX_RETENTION_TIME_MILLIS()`. Must be positive. |
| `https://developer.android.com/reference/java/util/concurrent/Executor.html dbExecutor` | 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)

```
public WorkMetricsInfoRepository(
    @NonNull Context context,
    long retentionTime,
    @NonNull TimeUnit retentionTimeUnit,
    Executor dbExecutor
)
```

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

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

## Public methods

### getFinishedWorkMetricsInfoFlow

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

```
public final @NonNull Flow<@NonNull WorkMetricsInfo> getFinishedWorkMetricsInfoFlow()
```

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/androidx/work/analytics/WorkMetricsInfo` whenever one finishes.

A `https://developer.android.com/reference/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).

### getWorkMetricsInfoById

```
public final @NonNull List<@NonNull WorkMetricsInfo> getWorkMetricsInfoById(@NonNull UUID workId)
```

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

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfo>` | A list of `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfo` records associated with the `https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfoRepository#getWorkMetricsInfoById(java.util.UUID)`. |

### getWorkMetricsInfos

```
public final @NonNull List<@NonNull WorkMetricsInfo> getWorkMetricsInfos(@NonNull WorkMetricsQuery query)
```

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

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/analytics/WorkMetricsInfo>` | A list of `https://developer.android.com/reference/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)

```
public void onCancelled(@NonNull WorkInfo workInfo)
```

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

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

### onEnqueued

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

```
public void onEnqueued(@NonNull WorkInfo workInfo)
```

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

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

### onException

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

```
public void onException(@NonNull Throwable throwable, @NonNull WorkInfo workInfo)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Throwable.html throwable` | Throwable thrown by worker |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/WorkInfo workInfo` | Snapshot of the work info |

### onFinished

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

```
public void onFinished(
    @NonNull ListenableWorker.Result result,
    @NonNull WorkInfo workInfo
)
```

Called when the worker finishes and returns a value.

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

### onPrerequisiteFailed

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

```
public void onPrerequisiteFailed(@NonNull WorkInfo workInfo)
```

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

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

### onStarted

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

```
public void onStarted(@NonNull WorkInfo workInfo)
```

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

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

### onStopped

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

```
public void onStopped(int stopReason, @NonNull WorkInfo workInfo)
```

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

| Parameters |
|---|---|
| `int stopReason` | Reason why work stopped |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/WorkInfo workInfo` | Snapshot of the work info |

### onUnblocked

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

```
public void onUnblocked(@NonNull WorkInfo workInfo)
```

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/androidx/work/ScheduleEventListener#onEnqueued(androidx.work.WorkInfo)`.

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

### onUpdated

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

```
public void onUpdated(@NonNull WorkInfo oldWorkInfo, @NonNull WorkInfo updatedWorkInfo)
```

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

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