WorkMetricsInfoRepository


@ExperimentalEventsApi
@ExperimentalWorkMetricsApi
class WorkMetricsInfoRepository : ScheduleEventListener, ExecutionEventListener


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

Summary

Public companion properties

Long

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

Long

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

Public constructors

WorkMetricsInfoRepository(context: Context, dbExecutor: Executor?)

Creates an instance of WorkMetricsInfoRepository with a default retention of DEFAULT_RETENTION_TIME_MILLIS.

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

Creates an instance of WorkMetricsInfoRepository with a custom retention duration.

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

Creates an instance of WorkMetricsInfoRepository with a custom retention time.

Public functions

suspend List<WorkMetricsInfo>

Gets a list of WorkMetricsInfo snapshots for a given work id.

suspend List<WorkMetricsInfo>

Gets a list of WorkMetricsInfo matching the query criteria.

open suspend Unit
onCancelled(workInfo: WorkInfo)

Called when a work request is canceled from the app e.g. WorkManager.cancelWorkById.

open suspend Unit
onEnqueued(workInfo: WorkInfo)

Called when a work request is enqueued from the app e.g. WorkManager.enqueue.

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

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

open suspend Unit

Called when the worker finishes and returns a value.

open suspend Unit

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

open suspend Unit
onStarted(workInfo: WorkInfo)

Called when a work request starts executing i.e. ListenableWorker.startWork.

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

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

open suspend Unit
onUnblocked(workInfo: WorkInfo)

Called when a work request is no longer waiting on any of its prerequisite work.

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

Called when a work request is updated from the app e.g. WorkManager.updateWork.

Public properties

Flow<WorkMetricsInfo>

A hot Flow that emits a WorkMetricsInfo whenever one finishes.

Public companion properties

DEFAULT_RETENTION_TIME_MILLIS

val DEFAULT_RETENTION_TIME_MILLISLong

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

MAX_RETENTION_TIME_MILLIS

val MAX_RETENTION_TIME_MILLISLong

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

Public constructors

WorkMetricsInfoRepository

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

Creates an instance of WorkMetricsInfoRepository with a default retention of DEFAULT_RETENTION_TIME_MILLIS.

It is recommended that the Executor passed here is the same as the one passed into androidx.work.Configuration.Builder.setTaskExecutor. 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: Context

The application Context.

dbExecutor: Executor? = null

The Executor passed to Room on which database queries and transactions will be run.

WorkMetricsInfoRepository

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

Creates an instance of WorkMetricsInfoRepository with a custom retention duration.

Parameters
context: Context

The application Context.

retentionDuration: Duration

The retention duration. Clamped to a maximum of MAX_RETENTION_TIME_MILLIS. Must be positive.

dbExecutor: Executor? = null

The Executor passed to Room on which database queries and transactions will be run.

WorkMetricsInfoRepository

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

Creates an instance of WorkMetricsInfoRepository with a custom retention time.

Parameters
context: Context

The application Context.

retentionTime: Long

The retention time. Clamped to a maximum of MAX_RETENTION_TIME_MILLIS. Must be positive.

retentionTimeUnit: TimeUnit

The TimeUnit for retentionTime.

dbExecutor: Executor? = null

The Executor 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 WorkMetricsInfo snapshots for a given work id.

Parameters
workId: UUID

The identifier of the androidx.work.WorkRequest.

Returns
List<WorkMetricsInfo>

A list of WorkMetricsInfo records associated with the workId.

getWorkMetricsInfos

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

Gets a list of WorkMetricsInfo matching the query criteria.

Parameters
query: WorkMetricsQuery

The WorkMetricsQuery containing filter parameters.

Returns
List<WorkMetricsInfo>

A list of WorkMetricsInfo records matching the query parameters.

onCancelled

open suspend fun onCancelled(workInfo: WorkInfo): Unit

Called when a work request is canceled from the app e.g. WorkManager.cancelWorkById.

Parameters
workInfo: WorkInfo

Snapshot of the work info

onEnqueued

open suspend fun onEnqueued(workInfo: WorkInfo): Unit

Called when a work request is enqueued from the app e.g. WorkManager.enqueue.

Parameters
workInfo: WorkInfo

Snapshot of the work info

onException

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

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

Parameters
throwable: Throwable

Throwable thrown by worker

workInfo: WorkInfo

Snapshot of the work info

onFinished

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

Called when the worker finishes and returns a value.

Parameters
result: ListenableWorker.Result

Result from work finishing

workInfo: WorkInfo

Snapshot of the work info

onPrerequisiteFailed

open suspend fun onPrerequisiteFailed(workInfo: WorkInfo): Unit

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

Parameters
workInfo: WorkInfo

Snapshot of the work info

onStarted

open suspend fun onStarted(workInfo: WorkInfo): Unit

Called when a work request starts executing i.e. ListenableWorker.startWork.

Parameters
workInfo: WorkInfo

Snapshot of the work info

onStopped

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: Int

Reason why work stopped

workInfo: WorkInfo

Snapshot of the work info

onUnblocked

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 onEnqueued.

Parameters
workInfo: WorkInfo

Snapshot of the work info

onUpdated

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

Called when a work request is updated from the app e.g. WorkManager.updateWork.

Parameters
oldWorkInfo: WorkInfo

Snapshot of the work info before the update

updatedWorkInfo: WorkInfo

Snapshot of the work info after the update

Public properties

finishedWorkMetricsInfoFlow

val finishedWorkMetricsInfoFlowFlow<WorkMetricsInfo>

A hot Flow that emits a WorkMetricsInfo whenever one finishes.

A 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).