diff --git a/spug_api/apps/home/views.py b/spug_api/apps/home/views.py index 5c8a9f2..cb117d9 100644 --- a/spug_api/apps/home/views.py +++ b/spug_api/apps/home/views.py @@ -8,7 +8,8 @@ from apps.schedule.models import Task from apps.monitor.models import Detection from apps.alarm.models import Alarm from apps.deploy.models import Deploy, DeployRequest -from libs.utils import json_response, human_date +from libs.utils import json_response, human_date, parse_time +from libs.parser import JsonParser, Argument from datetime import datetime, timedelta import json @@ -35,11 +36,18 @@ def get_alarm(request): def get_request(request): - data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()} - for req in DeployRequest.objects.filter(created_at__gt=human_date()): - data[req.deploy.app_id]['count'] += 1 - data = sorted(data.values(), key=lambda x: x['count'], reverse=True)[:5] - return json_response(data) + form, error = JsonParser( + Argument('duration', type=list, help='参数错误') + ).parse(request.body) + if error is None: + s_date = form.duration[0] + e_date = (parse_time(form.duration[1]) + timedelta(days=1)).strftime('%Y-%m-%d') + data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()} + for req in DeployRequest.objects.filter(created_at__gt=s_date, created_at__lt=e_date): + data[req.deploy.app_id]['count'] += 1 + data = sorted(data.values(), key=lambda x: x['count'], reverse=True)[:5] + return json_response(data) + return json_response(error=error) def get_deploy(request): diff --git a/spug_web/src/pages/home/RequestTop.js b/spug_web/src/pages/home/RequestTop.js index e592a28..171b883 100644 --- a/spug_web/src/pages/home/RequestTop.js +++ b/spug_web/src/pages/home/RequestTop.js @@ -3,37 +3,70 @@ * Copyright (c) * Released under the AGPL-3.0 License. */ -import React from 'react'; -import { Card } from 'antd'; +import React, { useState, useEffect } from 'react'; +import { Card, DatePicker } from 'antd'; import { Chart, Geom, Axis, Tooltip } from 'bizcharts'; +import styles from './index.module.css'; +import moment from 'moment'; import { http } from 'libs'; -export default class RequestTop extends React.Component { - constructor(props) { - super(props); - this.state = { - loading: true, - res: [] - }; + +export default function () { + const [loading, setLoading] = useState(false); + const [duration, setDuration] = useState([moment(), moment()]); + const [range, setRange] = useState('day'); + const [res, setRes] = useState([]) + + useEffect(() => { + setLoading(true); + const strDuration = duration.map(x => x.format('YYYY-MM-DD')) + http.post('/api/home/request/', {duration: strDuration}) + .then(res => setRes(res)) + .finally(() => setLoading(false)) + }, [duration]) + + function handleClick(val) { + let duration = []; + switch (val) { + case 'day': + setRange('day'); + duration = [moment(), moment()]; + break; + case 'week': + setRange('week'); + duration = [moment().weekday(0), moment().weekday(6)]; + break; + case 'month': + setRange('month'); + const s_date = moment().startOf('month') + const e_date = moment().endOf('month') + duration = [s_date, e_date]; + break; + default: + setRange('custom') + duration = val + } + setDuration(duration) } - componentDidMount() { - http.get('/api/home/request/') - .then(res => this.setState({res})) - .finally(() => this.setState({loading: false})) - } - - render() { - const {res, loading} = this.state; - return ( - - - - - - - - - ) - } + return ( + + handleClick('day')}>今日 + handleClick('week')}>本周 + handleClick('month')}>本月 + + + )}> + + + + + + + + ) } diff --git a/spug_web/src/pages/home/index.module.css b/spug_web/src/pages/home/index.module.css new file mode 100644 index 0000000..57be898 --- /dev/null +++ b/spug_web/src/pages/home/index.module.css @@ -0,0 +1,19 @@ +:global(.ant-card-extra) { + padding: 12px 0; +} + +.spanButton { + cursor: pointer; + margin-right: 24px; + color: rgba(0, 0, 0, .65); +} + +.spanButtonActive { + cursor: pointer; + margin-right: 24px; + color: #1890ff; +} + +.spanButton:hover { + color: #1890ff; +} \ No newline at end of file