added batch set projects

This commit is contained in:
marvzhang
2020-07-18 10:58:06 +08:00
parent 31c3c91545
commit d973aa39a7
4 changed files with 89 additions and 5 deletions

View File

@@ -74,6 +74,7 @@ type Spider struct {
Config entity.ConfigSpiderData `json:"config"` // 可配置爬虫配置
LatestTasks []Task `json:"latest_tasks"` // 最近任务列表
Username string `json:"username"` // 用户名称
ProjectName string `json:"project_name"` // 项目名称
// 时间
UserId bson.ObjectId `json:"user_id" bson:"user_id"`
@@ -183,7 +184,6 @@ func GetSpiderList(filter interface{}, skip int, limit int, sortStr string) ([]S
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
// 获取正在运行的爬虫
@@ -191,17 +191,27 @@ func GetSpiderList(filter interface{}, skip int, limit int, sortStr string) ([]S
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
// 获取用户
var user User
if spider.UserId.Valid() {
if spider.UserId.Valid() && spider.UserId.Hex() != constants.ObjectIdNull {
user, err = GetUser(spider.UserId)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
}
// 获取项目
var project Project
if spider.ProjectId.Valid() && spider.ProjectId.Hex() != constants.ObjectIdNull {
project, err = GetProject(spider.ProjectId)
if err != nil {
if err != mgo.ErrNotFound {
log.Errorf(err.Error())
debug.PrintStack()
}
}
}
@@ -210,6 +220,7 @@ func GetSpiderList(filter interface{}, skip int, limit int, sortStr string) ([]S
spiders[i].LastStatus = task.Status
spiders[i].LatestTasks = latestTasks
spiders[i].Username = user.Username
spiders[i].ProjectName = project.Name
}
count, _ := c.Find(filter).Count()

View File

@@ -236,6 +236,7 @@ export default {
'De-Duplication': '去重',
'Same Above': '同上',
'Batch Run': '批量运行',
'Set Projects': '设置项目',
// 爬虫列表
'Name': '名称',

View File

@@ -343,6 +343,13 @@ const actions = {
const id = payload ? payload.id : state.spiderForm._id
const res = await request.get(`/spiders/${id}/file/tree`)
commit('SET_FILE_TREE', res.data.data)
},
async setProjects({ state }, payload) {
const { projectId, spiderIds } = payload
await request.post(`/spiders-set-projects`, {
project_id: projectId,
spider_ids: spiderIds
})
}
}

View File

@@ -323,6 +323,38 @@
/>
<!--./batch crawl dialog-->
<!--set projects dialog-->
<el-dialog
:title="$t('Set Projects')"
:visible.sync="setProjectsVisible"
:before-close="() => setProjectsVisible = false"
width="580px"
>
<el-form
ref="set-projects-form"
label-width="120px"
:model="form"
>
<el-form-item :label="$t('Project')" prop="projectId" required>
<el-select v-model="form.projectId" size="small" :placeholder="$t('Project')">
<el-option
v-for="op in projectList"
:key="op._id"
:label="op.name"
:value="op._id"
/>
</el-select>
</el-form-item>
</el-form>
<template slot="footer">
<el-button type="plain" size="small" @click="setProjectsVisible = false">{{ $t('Cancel') }}</el-button>
<el-button type="primary" size="small" @click="onSetProjectsConfirm">
{{ $t('Confirm') }}
</el-button>
</template>
</el-dialog>
<!--./set projects dialog-->
<el-card style="border-radius: 0">
<!--filter-->
<div class="filter">
@@ -396,6 +428,17 @@
>
{{ $t('Batch Run') }}
</el-button>
<el-button
v-if="this.selectedSpiders.length"
size="small"
type="primary"
icon="el-icon-s-operation"
class="btn set-projects"
style="font-weight: bolder"
@click="onSetProjects"
>
{{ $t('Set Projects') }}
</el-button>
<el-button
v-if="this.selectedSpiders.length"
size="small"
@@ -720,6 +763,7 @@
addDialogVisible: false,
crawlConfirmDialogVisible: false,
batchCrawlDialogVisible: false,
setProjectsVisible: false,
isRunningTasksDialogVisible: false,
activeSpiderId: undefined,
activeSpider: undefined,
@@ -867,7 +911,10 @@
isStopLoading: false,
isRemoveLoading: false,
isMultiple: false,
copyDialogVisible: false
copyDialogVisible: false,
form: {
projectId: null
}
}
},
computed: {
@@ -906,6 +953,7 @@
const columns = []
columns.push({ name: 'display_name', label: 'Name', width: '160', align: 'left', sortable: true })
columns.push({ name: 'type', label: 'Spider Type', width: '120', sortable: true })
columns.push({ name: 'project_name', label: 'Project', width: '120' })
columns.push({ name: 'is_long_task', label: 'Is Long Task', width: '80' })
columns.push({ name: 'is_scrapy', label: 'Is Scrapy', width: '80' })
columns.push({ name: 'latest_tasks', label: 'Latest Tasks', width: '180' })
@@ -1350,6 +1398,23 @@
},
isDisabled(row) {
return row.is_public && row.username !== this.userInfo.username && this.userInfo.role !== 'admin'
},
onSetProjects() {
this.setProjectsVisible = true
this.isMultiple = true
this.form.projectId = null
},
onSetProjectsConfirm() {
this.$refs['set-projects-form'].validate(async valid => {
if (!valid) return
await this.$store.dispatch('spider/setProjects', {
projectId: this.form.projectId,
spiderIds: this.selectedSpiders.map(d => d._id)
})
this.setProjectsVisible = false
this.isMultiple = false
await this.getList()
})
}
}
}