Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
inspect_report
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengjinlei
inspect_report
Commits
2e7fdf7b
Commit
2e7fdf7b
authored
Dec 11, 2019
by
zhengjinlei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
概览统计加速
parent
f2f867e9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
6 deletions
+54
-6
tasksapi.py
src/inspect_report/api/tasksapi.py
+53
-5
cron.py
src/inspect_report/cron.py
+1
-1
No files found.
src/inspect_report/api/tasksapi.py
View file @
2e7fdf7b
...
...
@@ -5,7 +5,7 @@ from rest_framework import viewsets
from
rest_framework.decorators
import
action
from
rest_framework.request
import
Request
from
rest_framework.response
import
Response
from
inspect_report.models
import
Tasks
,
CheckSession
from
inspect_report.models
import
Tasks
,
CheckSession
,
RulesStat
import
json
from
datetime
import
datetime
,
timedelta
from
config.config
import
TABLE_PRE
...
...
@@ -493,6 +493,58 @@ class TasksApi(viewsets.ViewSet):
@
action
([
'post'
],
detail
=
False
)
def
static_rule
(
self
,
req
:
Request
):
"""
首页概述-违规项统计
:param req:
:return:
"""
task_id
=
req
.
data
.
get
(
'task'
,
''
)
start_date
=
req
.
data
.
get
(
'start_date'
,
(
datetime
.
now
()
+
timedelta
(
days
=-
1
))
.
strftime
(
'
%
Y-
%
m-
%
d'
))
end_date
=
req
.
data
.
get
(
'end_date'
,
datetime
.
now
()
.
strftime
(
'
%
Y-
%
m-
%
d'
))
task_condition
=
{
'hasCheck'
:
1
,
'createdAt__gte'
:
start_date
,
'createdAt__lt'
:
end_date
+
' 23:59:59'
}
if
task_id
:
task_condition
[
'name__startswith'
]
=
task_id
tasks
=
Tasks
.
objects
.
filter
(
**
task_condition
)
.
values
(
'id'
,
'sessionCollectionId'
)
task_dict
=
{}
for
t
in
tasks
:
if
t
[
'sessionCollectionId'
]
in
task_dict
.
keys
():
task_dict
[
t
[
'sessionCollectionId'
]]
.
append
(
t
[
'id'
])
else
:
task_dict
[
t
[
'sessionCollectionId'
]]
=
[
t
[
'id'
],
]
return_data
=
{}
call_count
=
0
for
k
,
v
in
task_dict
.
items
():
table_name
=
TABLE_PRE
+
k
tn
=
CheckSession
.
set_table
(
table_name
)
session_condition
=
{
'taskId__in'
:
v
,
'violationRuleCount__gt'
:
0
}
validate_all
=
tn
.
objects
.
filter
(
**
session_condition
)
.
aggregate
(
Count
(
"id"
))
call_count
+=
validate_all
[
'id__count'
]
q_start_date
=
datetime
.
strptime
(
start_date
,
'
%
Y-
%
m-
%
d'
)
.
date
()
q_end_date
=
(
datetime
.
strptime
(
end_date
,
'
%
Y-
%
m-
%
d'
)
+
+
timedelta
(
days
=
1
))
.
date
()
task_condition
=
{
'create_date__gte'
:
q_start_date
,
'create_date__lt'
:
q_end_date
}
if
task_id
:
task_condition
[
'rule_name'
]
=
task_id
tasks
=
RulesStat
.
objects
.
filter
(
**
task_condition
)
.
values
(
'rule_name'
,
'from_table'
,
'rule_num'
)
for
t
in
tasks
:
name
=
t
[
'rule_name'
]
if
name
in
return_data
.
keys
():
return_data
[
name
][
'count'
]
+=
int
(
t
[
'rule_num'
])
else
:
return_data
[
name
]
=
{
'count'
:
1
,
'tables'
:
[]}
if
t
[
'from_table'
]
not
in
return_data
[
name
][
'tables'
]:
return_data
[
name
][
'tables'
]
.
append
(
t
[
'from_table'
])
data_sort
=
[]
for
k
,
v
in
return_data
.
items
():
v
[
'rule'
]
=
k
v
[
'ratio'
]
=
round
(
v
[
'count'
]
/
call_count
,
2
)
if
call_count
else
0
v
[
'call_count'
]
=
call_count
data_sort
.
append
(
v
)
data_sort
=
sorted
(
data_sort
,
key
=
lambda
x
:
x
[
'count'
],
reverse
=
True
)
return
Response
({
'code'
:
0
,
'msg'
:
'success'
,
'data'
:
data_sort
})
@
action
([
'post'
],
detail
=
False
)
def
static_rule_bak
(
self
,
req
:
Request
):
"""
首页概述-违规项统计
:param req:
...
...
@@ -525,10 +577,6 @@ class TasksApi(viewsets.ViewSet):
for
check
in
checks
:
call_count
+=
1
result
=
check
[
'checkResult'
]
# detail = {'agentName': check['agentName'], 'customName': check['customName'],
# 'remainTime': check['remainTime'], 'score': check['score'], 'sessionId': check['sessionId'],
# 'sessionCollectionId': k, 'taskId': check['taskId']}
# session_id = check['id']
if
result
:
data
=
json
.
loads
(
result
)
for
d
in
data
:
...
...
src/inspect_report/cron.py
View file @
2e7fdf7b
...
...
@@ -24,7 +24,7 @@ def rule_stat(start_date, end_date):
return_data
=
{}
table_name
=
TABLE_PRE
+
t
[
'sessionCollectionId'
]
tn
=
CheckSession
.
set_table
(
table_name
)
session_condition
=
{
'taskId'
:
t
[
'id'
]}
session_condition
=
{
'taskId'
:
t
[
'id'
]
,
'violationRuleCount__gt'
:
0
}
checks
=
tn
.
objects
.
filter
(
**
session_condition
)
.
values
(
'id'
,
'checkResult'
)
for
check
in
checks
:
result
=
check
[
'checkResult'
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment