11<script setup lang="ts">
22import { formatDate } from " ~/utils"
3+ import { useIntervalFn } from " @vueuse/core"
4+ import type { TaskSelect } from " ~/server/db/schema"
35
46const route = useRoute ()
57const router = useRouter ()
@@ -11,34 +13,70 @@ if (!route.query.page) {
1113const perPage = 10
1214const searchRef = ref (" " )
1315
16+ const state = computed (() => route .query .state )
1417const search = computed (() => route .query .search || " " )
1518const page = computed (() => Number (route .query .page ) || 1 )
1619
17- const { data } = useAsyncData (
20+ const { data, refresh } = useAsyncData (
1821 " tasks" ,
1922 () =>
2023 $fetch (` /api/tasks ` , {
2124 params: {
2225 limit: perPage ,
26+ state: state .value ,
2327 search: search .value ,
2428 offset: (page .value - 1 ) * perPage ,
2529 },
2630 }),
2731 {
28- watch: [page , search ],
32+ watch: [page , search , state ],
2933 }
3034)
3135
36+ useIntervalFn (() => {
37+ console .log (" REFRESHING..." )
38+ refresh ()
39+ }, 3000 )
40+
3241const totalPages = computed (() => Math .ceil ((data .value ?.count || 0 ) / perPage ))
3342
34- function limitText(text : string , length : number ) {
43+ const limitText = (text : string , length : number ) => {
3544 if (text .length > length ) {
3645 return text .slice (0 , length ).trim () + " ..."
3746 }
3847 return text
3948}
4049
41- function searchSubmit() {
50+ const formatReturnValue = (task : TaskSelect ) => {
51+ if (task .returnValue ?.return_value ) {
52+ return task .returnValue .return_value
53+ } else {
54+ return " null"
55+ }
56+ }
57+
58+ // temporarily, while dishka hasn't fixed it's module naming bug
59+ const formatTaskName = (taskName : string ) => {
60+ if (taskName .includes (" :" )) {
61+ const parts = taskName .split (" :" )
62+ return parts [parts .length - 1 ]
63+ } else {
64+ return taskName
65+ }
66+ }
67+
68+ const stateHandler = (state : " pending" | " success" | " failure" ) => {
69+ router .push ({
70+ path: " /tasks" ,
71+ query: {
72+ page: 1 ,
73+ state: state ,
74+ search: searchRef .value || undefined ,
75+ },
76+ })
77+ }
78+
79+ const searchSubmit = () => {
4280 if (searchRef .value ) {
4381 router .push ({
4482 path: " /tasks" ,
@@ -50,24 +88,26 @@ function searchSubmit() {
5088 }
5189}
5290
53- function handleNext() {
91+ const handleNext = () => {
5492 if (page .value < totalPages .value ) {
5593 router .push ({
5694 path: " /tasks" ,
5795 query: {
5896 page: page .value + 1 ,
97+ state: state .value ,
5998 search: searchRef .value || undefined ,
6099 },
61100 })
62101 }
63102}
64103
65- function handlePrev() {
104+ const handlePrev = () => {
66105 if (page .value > 1 ) {
67106 router .push ({
68107 path: " /tasks" ,
69108 query: {
70109 page: page .value - 1 ,
110+ state: state .value ,
71111 search: searchRef .value || undefined ,
72112 },
73113 })
@@ -113,21 +153,22 @@ function handlePrev() {
113153 <th >Result</th >
114154 <th >Started</th >
115155 <th >Finished</th >
116- <th >Runtime</th >
156+ <th >Runtime (s) </th >
117157 <th >Worker</th >
118158 </tr >
119159 </thead >
120160 <tbody >
121161 <tr v-if =" data" v-for =" task in data.tasks" >
122- <td class =" task-name-td" >{{ task.name }}</td >
162+ <td class =" task-name-td" >{{ formatTaskName( task.name) }}</td >
123163 <td >
124164 <NuxtLink :to =" { name: 'tasks-id', params: { id: task.id } }" >
125165 {{ task.id }}
126166 </NuxtLink >
127167 </td >
128168 <td >
129169 <span
130- class =" py-1 px-2 badge"
170+ @click =" stateHandler(task.state)"
171+ class =" py-1 px-2 badge cursor-pointer"
131172 :class =" {
132173 'bg-success': task.state === 'success',
133174 'bg-danger': task.state === 'failure',
@@ -138,10 +179,12 @@ function handlePrev() {
138179 </span >
139180 </td >
140181 <td >{{ task.args }}</td >
141- <td >{{ limitText(JSON.stringify(task.kwargs), 50 ) }}</td >
142- <td >{{ task.returnValue }}</td >
182+ <td >{{ limitText(JSON.stringify(task.kwargs), 40 ) }}</td >
183+ <td >{{ formatReturnValue( task) }}</td >
143184 <td >{{ formatDate(task.startedAt) }}</td >
144- <td >{{ task.finishedAt ? formatDate(task.finishedAt) : null }}</td >
185+ <td >
186+ {{ task.finishedAt ? formatDate(task.finishedAt) : null }}
187+ </td >
145188 <td >{{ task.executionTime }}</td >
146189 <td >{{ task.worker }}</td >
147190 </tr >
@@ -153,23 +196,25 @@ function handlePrev() {
153196 <nav >
154197 <ul class =" pagination" >
155198 <li class =" page-item" >
156- <span
199+ <button
157200 @click =" handlePrev"
158201 :class =" { disabled: page === 1 }"
159202 class =" page-link cursor-pointer"
160- >Previous</span
161203 >
204+ Previous
205+ </button >
162206 </li >
163207 <div class =" flex justify-center items-center px-2" >
164208 <span >{{ page }} / {{ totalPages }}</span >
165209 </div >
166210 <li class =" page-item" >
167- <span
211+ <button
168212 @click =" handleNext"
169213 class =" page-link cursor-pointer"
170214 :class =" { disabled: page === totalPages }"
171- >Next</span
172215 >
216+ Next
217+ </button >
173218 </li >
174219 </ul >
175220 </nav >
0 commit comments