Skip to content

Commit 5d2c4c6

Browse files
committed
feat: 1.1.3 version
1 parent 08fa5bc commit 5d2c4c6

File tree

7 files changed

+125
-29
lines changed

7 files changed

+125
-29
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nuxt-app",
33
"private": true,
44
"type": "module",
5-
"version": "1.1.2",
5+
"version": "1.1.3",
66
"scripts": {
77
"build": "nuxt build",
88
"dev": "nuxt dev",
@@ -12,6 +12,7 @@
1212
"db:push": "drizzle-kit push"
1313
},
1414
"dependencies": {
15+
"@vueuse/core": "^12.0.0",
1516
"better-sqlite3": "^11.6.0",
1617
"bootstrap": "^5.3.3",
1718
"dayjs": "^1.11.13",

pnpm-lock.yaml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/tasks/index.vue

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script setup lang="ts">
22
import { formatDate } from "~/utils"
3+
import { useIntervalFn } from "@vueuse/core"
4+
import type { TaskSelect } from "~/server/db/schema"
35
46
const route = useRoute()
57
const router = useRouter()
@@ -11,34 +13,70 @@ if (!route.query.page) {
1113
const perPage = 10
1214
const searchRef = ref("")
1315
16+
const state = computed(() => route.query.state)
1417
const search = computed(() => route.query.search || "")
1518
const 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+
3241
const 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>

src/server/api/tasks/index.get.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { getTasksQueryParamsSchema } from "../../schemas/tasks"
55
export default defineEventHandler(async (event) => {
66
const query = await getValidatedQuery(event, getTasksQueryParamsSchema.parse)
77

8-
const { tasks, count } = await tasksRepository.getAll(
9-
query.search ? query.search : null,
10-
query.limit,
11-
query.offset
12-
)
8+
const { tasks, count } = await tasksRepository.getAll({
9+
name: query.search ? query.search : null,
10+
limit: query.limit,
11+
offset: query.offset,
12+
state: query.state,
13+
})
1314

1415
return { tasks, count }
1516
})

src/server/db/schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export const tasksTable = sqliteTable(
1414
finishedAt: int("finished_at", { mode: "timestamp" }),
1515
args: text({ mode: "json" }).$type<Array<any>>(),
1616
kwargs: text({ mode: "json" }).$type<Record<string, any>>(),
17-
returnValue: text("return_value", { mode: "json" }).$type<
18-
Record<string, any>
19-
>(),
17+
returnValue: text("return_value", { mode: "json" }).$type<{
18+
return_value: any
19+
}>(),
2020
},
2121
(t) => ({
2222
idxStartedAt: index("idx_tasks__started_at").on(t.startedAt),

src/server/repositories/tasks.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import { db } from "../db"
22
import { tasksTable } from "../db/schema"
33
import { takeUniqueOrThrow } from "../utils"
4-
import { count, eq, desc, like } from "drizzle-orm"
4+
import { count, eq, desc, like, and } from "drizzle-orm"
55

66
export type TaskState = "pending" | "success" | "failed"
77

88
class TasksRepository {
9-
async getAll(name: string | null, limit: number, offset: number) {
9+
async getAll({
10+
name,
11+
state,
12+
limit,
13+
offset,
14+
}: {
15+
limit: number
16+
offset: number
17+
name: string | null
18+
state?: "success" | "pending" | "failure"
19+
}) {
1020
const whereCondition = name
1121
? like(tasksTable.name, `%${name.toLowerCase()}%`)
1222
: undefined
@@ -16,13 +26,17 @@ class TasksRepository {
1626
count: count(),
1727
})
1828
.from(tasksTable)
19-
.where(whereCondition)
29+
.where(
30+
and(whereCondition, state ? eq(tasksTable.state, state) : undefined)
31+
)
2032
.then(takeUniqueOrThrow)
2133

2234
const tasks = await db
2335
.select()
2436
.from(tasksTable)
25-
.where(whereCondition)
37+
.where(
38+
and(whereCondition, state ? eq(tasksTable.state, state) : undefined)
39+
)
2640
.orderBy(desc(tasksTable.startedAt))
2741
.limit(limit)
2842
.offset(offset)

src/server/schemas/tasks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export const getTasksQueryParamsSchema = z.object({
2323
search: z.string().optional(),
2424
limit: z.coerce.number().gte(0),
2525
offset: z.coerce.number().gte(0),
26+
state: z.enum(["success", "pending", "failure"]).optional(),
2627
})

0 commit comments

Comments
 (0)