Skip to content

Commit

Permalink
Merge pull request #25 from tikazyq/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Marvin Zhang authored May 10, 2019
2 parents 7c492ee + bc4a04b commit 7ed7fb1
Show file tree
Hide file tree
Showing 31 changed files with 963 additions and 34 deletions.
4 changes: 4 additions & 0 deletions crawlab/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask_cors import CORS
from flask_restful import Api
# from flask_restplus import Api
from routes.sites import SiteApi
from utils.log import other
from constants.node import NodeStatus
from db.manager import db_manager
Expand Down Expand Up @@ -68,6 +69,9 @@
api.add_resource(ScheduleApi,
'/api/schedules',
'/api/schedules/<string:id>')
api.add_resource(SiteApi,
'/api/sites',
'/api/sites/<string:id>')


def monitor_nodes_status(celery_app):
Expand Down
2 changes: 1 addition & 1 deletion crawlab/db/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DbManager(object):
"""

def __init__(self):
self.mongo = MongoClient(host=MONGO_HOST, port=MONGO_PORT)
self.mongo = MongoClient(host=MONGO_HOST, port=MONGO_PORT, connect=False)
self.db = self.mongo[MONGO_DB]

def save(self, col_name: str, item: dict, **kwargs) -> None:
Expand Down
72 changes: 72 additions & 0 deletions crawlab/routes/sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json

from bson import ObjectId
from pymongo import ASCENDING

from db.manager import db_manager
from routes.base import BaseApi
from utils import jsonify


class SiteApi(BaseApi):
col_name = 'sites'

arguments = (
('keyword', str),
('category', str),
)

def get(self, id: str = None, action: str = None):
# action by id
if action is not None:
if not hasattr(self, action):
return {
'status': 'ok',
'code': 400,
'error': 'action "%s" invalid' % action
}, 400
return getattr(self, action)(id)

elif id is not None:
site = db_manager.get(col_name=self.col_name, id=id)
return jsonify(site)

# list tasks
args = self.parser.parse_args()
page_size = args.get('page_size') or 10
page_num = args.get('page_num') or 1
filter_str = args.get('filter')
keyword = args.get('keyword')
filter_ = {}
if filter_str is not None:
filter_ = json.loads(filter_str)
if keyword is not None:
filter_['$or'] = [
{'description': {'$regex': keyword}},
{'name': {'$regex': keyword}},
{'domain': {'$regex': keyword}}
]

items = db_manager.list(
col_name=self.col_name,
cond=filter_,
limit=page_size,
skip=page_size * (page_num - 1),
sort_key='rank',
sort_direction=ASCENDING
)

sites = []
for site in items:
# get spider count
site['spider_count'] = db_manager.count('spiders', {'site': site['_id']})

sites.append(site)

return {
'status': 'ok',
'total_count': db_manager.count(self.col_name, filter_),
'page_num': page_num,
'page_size': page_size,
'items': jsonify(sites)
}
9 changes: 9 additions & 0 deletions crawlab/routes/spiders.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class SpiderApi(BaseApi):

# spider schedule cron enabled
('envs', str),

# spider site
('site', str),
)

def get(self, id=None, action=None):
Expand Down Expand Up @@ -125,6 +128,12 @@ def get(self, id=None, action=None):
if last_task is not None:
spider['task_ts'] = last_task['create_ts']

# get site
if spider.get('site') is not None:
site = db_manager.get('sites', spider['site'])
if site is not None:
spider['site_name'] = site['name']

# file stats
stats = get_file_suffix_stats(dir_path)

Expand Down
4 changes: 0 additions & 4 deletions crawlab/routes/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def get(self, id: str = None, action: str = None):
'code': 400,
'error': 'action "%s" invalid' % action
}, 400
# other.info(f"到这了{action},{id}")
return getattr(self, action)(id)

elif id is not None:
Expand Down Expand Up @@ -78,9 +77,6 @@ def get(self, id: str = None, action: str = None):
sort_key='create_ts')
items = []
for task in tasks:
# celery tasks
# _task = db_manager.get('tasks_celery', id=task['_id'])

# get spider
_spider = db_manager.get(col_name='spiders', id=str(task['spider_id']))

Expand Down
2 changes: 1 addition & 1 deletion crawlab/tasks/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class Scheduler(object):
mongo = MongoClient(host=MONGO_HOST, port=MONGO_PORT)
mongo = MongoClient(host=MONGO_HOST, port=MONGO_PORT, connect=False)
task_col = 'apscheduler_jobs'

# scheduler jobstore
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crawlab",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"scripts": {
"serve": "cross-env NODE_ENV=development vue-cli-service serve --ip=0.0.0.0",
Expand Down
48 changes: 28 additions & 20 deletions frontend/src/components/InfoView/SpiderInfoView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
<el-input v-model="spiderForm.col" :placeholder="$t('Results Collection')"
:disabled="isView"></el-input>
</el-form-item>
<el-form-item :label="$t('Site')">
<el-autocomplete v-model="spiderForm.site"
:placeholder="$t('Site')"
:fetch-suggestions="fetchSiteSuggestions"
clearable
@select="onSiteSelect">
</el-autocomplete>
</el-form-item>
<el-form-item :label="$t('Spider Type')">
<el-select v-model="spiderForm.type" :placeholder="$t('Spider Type')" :disabled="isView" clearable>
<el-option value="scrapy" label="Scrapy"></el-option>
Expand All @@ -38,26 +46,6 @@
<el-option value="go" label="Go"></el-option>
</el-select>
</el-form-item>
<!--<el-form-item :label="$t('Schedule Enabled')">-->
<!--<el-switch v-model="spiderForm.cron_enabled" :disabled="isView">-->
<!--</el-switch>-->
<!--</el-form-item>-->
<!--<el-form-item :label="$t('Schedule Cron')" v-if="spiderForm.cron_enabled"-->
<!--prop="cron"-->
<!--:rules="cronRules"-->
<!--:inline-message="true">-->
<!--<template slot="label">-->
<!--<el-tooltip :content="$t('Cron Format: [second] [minute] [hour] [day of month] [month] [day of week]')"-->
<!--placement="top">-->
<!--<span>-->
<!--{{$t('Schedule Cron')}}-->
<!--<i class="fa fa-exclamation-circle"></i>-->
<!--</span>-->
<!--</el-tooltip>-->
<!--</template>-->
<!--<el-input v-model="spiderForm.cron" :placeholder="$t('Schedule Cron')"-->
<!--:disabled="isView"></el-input>-->
<!--</el-form-item>-->
</el-form>
</el-row>
<el-row class="button-container" v-if="!isView">
Expand Down Expand Up @@ -172,6 +160,22 @@ export default {
})
}
})
},
fetchSiteSuggestions (keyword, callback) {
this.$request.get('/sites', {
keyword: keyword,
page_num: 1,
page_size: 100
}).then(response => {
const data = response.data.items.map(d => {
d.value = `${d.name} | ${d.domain}`
return d
})
callback(data)
})
},
onSiteSelect (item) {
this.spiderForm.site = item._id
}
}
}
Expand All @@ -187,4 +191,8 @@ export default {
width: 100%;
text-align: right;
}
.el-autocomplete {
width: 100%;
}
</style>
12 changes: 11 additions & 1 deletion frontend/src/i18n/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
'Task Detail': '任务详情',
'Schedules': '定时任务',
'Deploys': '部署',
'Sites': '网站',

// 标签
Overview: '概览',
Expand Down Expand Up @@ -70,7 +71,7 @@ export default {

// 节点状态
Online: '在线',
Offline: '在线',
Offline: '离线',
Unavailable: '未知',

// 爬虫
Expand Down Expand Up @@ -130,6 +131,15 @@ export default {
'Parameters': '参数',
'Add Schedule': '添加定时任务',

// 网站
'Site': '网站',
'Rank': '排名',
'Domain': '域名',
'Category': '类别',
'Select': '请选择',
'Select Category': '请选择类别',
'Spider Count': '爬虫数',

// 文件
'Choose Folder': '选择文件',

Expand Down
20 changes: 20 additions & 0 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ export const constantRouterMap = [
}
]
},
{
name: 'Site',
path: '/sites',
component: Layout,
meta: {
title: 'Site',
icon: 'fa fa-sitemap'
},
children: [
{
path: '',
name: 'SiteList',
component: () => import('../views/site/SiteList'),
meta: {
title: 'Sites',
icon: 'fa fa-sitemap'
}
}
]
},

{ path: '*', redirect: '/404', hidden: true }
]
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import task from './modules/task'
import file from './modules/file'
import schedule from './modules/schedule'
import lang from './modules/lang'
import site from './modules/site'
import getters from './getters'

Vue.use(Vuex)
Expand All @@ -27,7 +28,8 @@ const store = new Vuex.Store({
task,
file,
schedule,
lang
lang,
site
},
getters
})
Expand Down
67 changes: 67 additions & 0 deletions frontend/src/store/modules/site.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import request from '../../api/request'

const state = {
siteList: [],

// filter
filter: {
category: undefined
},
keyword: '',

// pagination
pageNum: 1,
pageSize: 10,
totalCount: 0
}

const getters = {}

const mutations = {
SET_KEYWORD (state, value) {
state.keyword = value
},
SET_SITE_LIST (state, value) {
state.siteList = value
},
SET_PAGE_NUM (state, value) {
state.pageNum = value
},
SET_PAGE_SIZE (state, value) {
state.pageSize = value
},
SET_TOTAL_COUNT (state, value) {
state.totalCount = value
}
}

const actions = {
editSite ({ state, dispatch }, payload) {
const { id, category } = payload
return request.post(`/sites/${id}`, {
category
})
},
getSiteList ({ state, commit }) {
return request.get('/sites', {
page_num: state.pageNum,
page_size: state.pageSize,
keyword: state.keyword || undefined,
filter: {
category: state.filter.category || undefined
}
})
.then(response => {
commit('SET_SITE_LIST', response.data.items)
commit('SET_TOTAL_COUNT', response.data.total_count)
})
}
}

export default {
namespaced: true,
state,
getters,
mutations,
actions
}
Loading

0 comments on commit 7ed7fb1

Please sign in to comment.