Skip to content

Commit

Permalink
Add batch delete index (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojun207 committed Jul 8, 2022
1 parent fbc1fa4 commit 9e03510
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
24 changes: 11 additions & 13 deletions pkg/handlers/index/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package index

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"

Expand All @@ -36,23 +37,20 @@ import (
// @Failure 500 {object} meta.HTTPResponseError
// @Router /api/index/{index} [delete]
func Delete(c *gin.Context) {
indexName := c.Param("target")

index, exists := core.GetIndex(indexName)
if !exists {
c.JSON(http.StatusBadRequest, meta.HTTPResponseError{Error: "index " + indexName + " does not exists"})
indexNames := c.Param("target")
if indexNames == "" {
c.JSON(http.StatusBadRequest, meta.HTTPResponseError{Error: "index name cannot be empty"})
return
}

// delete meta
if err := core.DeleteIndex(index.Name); err != nil {
c.JSON(http.StatusInternalServerError, meta.HTTPResponseError{Error: err.Error()})
return
for _, indexName := range strings.Split(indexNames, ",") {
if err := core.DeleteIndex(indexName); err != nil {
c.JSON(http.StatusBadRequest, meta.HTTPResponseError{Error: err.Error()})
return
}
}

c.JSON(http.StatusOK, meta.HTTPResponseIndex{
Message: "deleted",
Index: index.Name,
StorageType: index.StorageType,
c.JSON(http.StatusOK, meta.HTTPResponse{
Message: "deleted",
})
}
2 changes: 1 addition & 1 deletion pkg/handlers/index/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDelete(t *testing.T) {
args: args{
code: http.StatusBadRequest,
params: map[string]string{"target": ""},
result: "does not exists",
result: "index name cannot be empty",
},
wantErr: false,
},
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {
index: {
header: "Indexes",
add: "Add Index",
delete: "Delete Index",
search: "Search Index",
},
template: {
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default {
index: {
header: "索引管理",
add: "添加",
delete: "删除",
search: "查询",
},
template: {
Expand Down
4 changes: 2 additions & 2 deletions web/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var index = {
update: (data: any) => {
return http().put("/api/index/" + data.name, data);
},
delete: (name: string) => {
return http().delete("/api/index/" + name);
delete: (names: string) => {
return http().delete("/api/index/" + names);
},
nameList: (name: string) => {
return http().get("/api/index_name?name=" + name);
Expand Down
56 changes: 52 additions & 4 deletions web/src/views/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
:title="t('index.header')"
:rows="indexes"
:columns="resultColumns"
row-key="id"
row-key="name"
:pagination="pagination"
selection="multiple"
:loading="loading"
v-model:selected="selectedIndexes"
:filter="filterQuery"
:filter-method="filterData"
>
Expand All @@ -28,6 +31,13 @@
:label="t('index.add')"
@click="addIndex"
/>
<q-btn
class="q-ml-sm"
color="negative"
icon="delete"
:label="t('index.delete')"
@click="deleteSelectedIndexes"
/>
</template>

<template #body-cell-no="props">
Expand Down Expand Up @@ -67,6 +77,7 @@
/>
</q-td>
</template>

</q-table>

<q-dialog
Expand All @@ -91,7 +102,7 @@
</template>

<script>
import { defineComponent, ref } from "vue";
import {defineComponent, nextTick, ref} from "vue";
import { useStore } from "vuex";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
Expand All @@ -110,9 +121,11 @@ export default defineComponent({
const store = useStore();
const $q = useQuasar();
const { t } = useI18n();
const loading = ref(false);
const selectedIndexes = ref([]);
const indexes = ref([]);
const getIndexes = () => {
loading.value = true;
indexService.list().then((res) => {
var counter = 1;
indexes.value = res.data.map((data) => {
Expand All @@ -133,6 +146,7 @@ export default defineComponent({
},
};
});
loading.value = false;
});
};
Expand Down Expand Up @@ -221,7 +235,38 @@ export default defineComponent({
html: true,
}).onOk(() => {
indexService.delete(props.row.name).then(() => {
getIndexes();
nextTick(getIndexes);
});
});
};
const deleteSelectedIndexes = () => {
if (!selectedIndexes || selectedIndexes.value.length == 0) {
$q.notify({
position: "top",
color: "warning",
textColor: "white",
icon: "warning",
message: "Please select index for deletion",
})
return
}
const showText = selectedIndexes.value.map(r=> "<li>" + r.name + "</li>").join("")
$q.dialog({
title: "Delete indexes",
message:
"You are about to delete these indexes: <ul>" +
showText +
"</ul>",
cancel: true,
persistent: true,
html: true,
}).onOk(() => {
const indexNames = selectedIndexes.value.map(r=> r.name).join(",")
indexService.delete(indexNames).then((res) => {
selectedIndexes.value = []
nextTick(getIndexes);
});
});
};
Expand All @@ -232,6 +277,9 @@ export default defineComponent({
showPreviewIndexDialog,
resultColumns,
index,
loading,
selectedIndexes,
deleteSelectedIndexes,
indexes,
pagination: {
rowsPerPage: 20,
Expand Down

0 comments on commit 9e03510

Please sign in to comment.