Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a composition for the task runs count by state api #2784

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compositions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export * from './useTaskRunFavicon'
export * from './useTaskRunResult'
export * from './useTaskRuns'
export * from './useTaskRunsCount'
export * from './useTaskRunsCountByState'
export * from './useTaskRunsHistory'
export * from './useThemeTokens'
export * from './useVariable'
Expand Down
58 changes: 58 additions & 0 deletions src/compositions/useTaskRunsCountByState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { SubscriptionOptions, useSubscriptionWithDependencies } from '@prefecthq/vue-compositions'
import merge from 'lodash.merge'
import { computed, MaybeRefOrGetter, toRef, toValue } from 'vue'
import { useCan, useWorkspaceApi } from '@/compositions'
import { TaskRunsFilter } from '@/models/Filters'
import { UiApi } from '@/services'
import { MaybeRef } from '@/types'
import { Getter } from '@/types/reactivity'
import { UseEntitySubscription } from '@/types/useEntitySubscription'

export type UseTaskRunsCountByState = UseEntitySubscription<UiApi['getTaskRunsCountByState'], 'count'>

export function useTaskRunsCountByState(filter: MaybeRefOrGetter<TaskRunsFilter | null | undefined>, options?: MaybeRef<SubscriptionOptions>): UseTaskRunsCountByState {
const api = useWorkspaceApi()
const can = useCan()

const getter: Getter<[TaskRunsFilter] | null> = () => {
if (!can.read.task_run) {
return null
}

const filterValue = toValue(filter)

if (!filterValue) {
return null
}

const base = getBaseFilter(filterValue)

// merge here is important to track changes to `filter` if it is a reactive
const parameter = merge({}, base, filterValue)

return [parameter]
}

const parameters = toRef(getter)
const subscription = useSubscriptionWithDependencies(api.ui.getTaskRunsCountByState, parameters, options)
const count = computed(() => subscription.response)

return {
subscription,
count,
}
}

function getBaseFilter(filter: TaskRunsFilter): TaskRunsFilter {
// makes sure that if subFlowRunsExists but was set to undefined (to get both all tasks)
// it doesn't get overridden to `false`
if ('subFlowRunsExist' in (filter.taskRuns ?? {})) {
return {}
}

return {
taskRuns: {
subFlowRunsExist: false,
},
}
}
Loading