Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Andrey Yemelyanov
noc
Commits
15ec9b08
Commit
15ec9b08
authored
Jul 10, 2019
by
Andrey Vertiprahov
Browse files
Add column to LatestChanges report.
parent
4effad53
Changes
1
Hide whitespace changes
Inline
Side-by-side
services/web/apps/cm/reportlatestchanges/views.py
View file @
15ec9b08
...
...
@@ -2,19 +2,21 @@
# ---------------------------------------------------------------------
# Latest Change Report
# ---------------------------------------------------------------------
# Copyright (C) 2007-201
8
The NOC Project
# Copyright (C) 2007-201
9
The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import
datetime
# Third-party modules
from
django
import
forms
from
pymongo
import
ReadPreference
# NOC modules
from
noc.lib.app.simplereport
import
SimpleReport
,
TableColumn
from
noc.
lib.nosql
import
get_db
from
noc.
core.mongo.connection
import
get_db
from
noc.sa.models.useraccess
import
UserAccess
from
noc.sa.models.managedobject
import
ManagedObject
from
noc.sa.models.administrativedomain
import
AdministrativeDomain
...
...
@@ -24,18 +26,17 @@ from noc.core.translation import ugettext as _
class
ReportForm
(
forms
.
Form
):
repo
=
forms
.
ChoiceField
(
label
=
_
(
"Type"
),
choices
=
[(
"config"
,
"config"
),
(
"dnszone"
,
"DNS"
)])
repo
=
forms
.
ChoiceField
(
label
=
_
(
"Type"
),
choices
=
[(
"config"
,
"config"
),
(
"dnszone"
,
"DNS"
)])
days
=
forms
.
IntegerField
(
label
=
_
(
"In Days"
),
min_value
=
1
)
adm_domain
=
forms
.
ModelChoiceField
(
label
=
_
(
"Managed Objects Administrative Domain (for config only)"
),
required
=
False
,
queryset
=
AdministrativeDomain
.
objects
.
order_by
(
"name"
))
queryset
=
AdministrativeDomain
.
objects
.
order_by
(
"name"
),
)
managed_object
=
forms
.
CharField
(
label
=
_
(
"Managed object search (for config only)"
),
required
=
False
,
help_text
=
_
(
"Use name, network (ex 10.0.0.0/8) or MAC"
)
help_text
=
_
(
"Use name, network (ex 10.0.0.0/8) or MAC"
)
,
)
...
...
@@ -43,20 +44,24 @@ class ReportLatestChangesApplication(SimpleReport):
title
=
_
(
"Latest Changes"
)
form
=
ReportForm
def
get_data
(
self
,
request
,
repo
=
"config"
,
days
=
1
,
adm_domain
=
None
,
managed_object
=
None
,
**
kwargs
):
def
get_data
(
self
,
request
,
repo
=
"config"
,
days
=
1
,
adm_domain
=
None
,
managed_object
=
None
,
**
kwargs
):
baseline
=
datetime
.
datetime
.
now
()
-
datetime
.
timedelta
(
days
=
days
)
coll
=
get_db
()[
"noc.gridvcs.%s.files"
%
repo
].
with_options
(
read_preference
=
ReadPreference
.
SECONDARY_PREFERRED
)
read_preference
=
ReadPreference
.
SECONDARY_PREFERRED
)
pipeline
=
[
{
"$match"
:
{
"ts"
:
{
"$gte"
:
baseline
}}},
{
"$group"
:
{
"_id"
:
"$object"
,
"last_ts"
:
{
"$max"
:
"$ts"
}}},
{
"$sort"
:
{
"_id"
:
1
}}
{
"$sort"
:
{
"_id"
:
1
}}
,
]
if
repo
==
"config"
:
objects
=
ManagedObject
.
objects
.
filter
()
if
not
request
.
user
.
is_superuser
:
objects
=
objects
.
filter
(
administrative_domain__in
=
UserAccess
.
get_domains
(
request
.
user
))
administrative_domain__in
=
UserAccess
.
get_domains
(
request
.
user
)
)
if
adm_domain
:
adm_domain
=
AdministrativeDomain
.
get_nested_ids
(
adm_domain
)
objects
=
objects
.
filter
(
administrative_domain__in
=
adm_domain
)
...
...
@@ -66,7 +71,9 @@ class ReportLatestChangesApplication(SimpleReport):
objects
=
objects
.
filter
(
name__contains
=
managed_object
)
else
:
objects
=
objects
.
filter
(
mo_q
)
pipeline
=
[{
"$match"
:
{
"object"
:
{
"$in"
:
list
(
objects
.
values_list
(
"id"
,
flat
=
True
))}}}]
+
pipeline
pipeline
=
[
{
"$match"
:
{
"object"
:
{
"$in"
:
list
(
objects
.
values_list
(
"id"
,
flat
=
True
))}}}
]
+
pipeline
# Perform query
data
=
list
(
coll
.
aggregate
(
pipeline
))
# Resolve names
...
...
@@ -75,21 +82,33 @@ class ReportLatestChangesApplication(SimpleReport):
seen_ids
=
list
(
set
(
d
[
"_id"
]
for
d
in
data
))
n_map
=
{}
if
repo
==
"config"
:
n_map
=
{
x
[
0
]:
x
[
1
:]
for
x
in
ManagedObject
.
objects
.
filter
(
id__in
=
list
(
seen_ids
)).
values_list
(
"id"
,
"name"
,
"profile"
)}
n_map
=
{
x
[
0
]:
x
[
1
:]
for
x
in
ManagedObject
.
objects
.
filter
(
id__in
=
list
(
seen_ids
)).
values_list
(
"id"
,
"name"
,
"address"
,
"profile"
)
}
elif
repo
==
"dns"
:
n_map
=
{
x
[
0
]:
x
[
1
:]
for
x
in
DNSZone
.
objects
.
filter
(
id__in
=
list
(
seen_ids
)).
values_list
(
"id"
,
"name"
,
"profile"
)}
n_map
=
{
x
[
0
]:
x
[
1
:]
for
x
in
DNSZone
.
objects
.
filter
(
id__in
=
list
(
seen_ids
)).
values_list
(
"id"
,
"name"
,
"address"
,
"profile"
)
}
for
d
in
data
:
name
,
profile
=
n_map
.
get
(
d
[
"_id"
],
(
"-"
,
None
))
result
+=
[(
d
[
"_id"
],
name
,
Profile
.
get_by_id
(
profile
)
if
profile
else
"-"
,
d
[
"last_ts"
])]
result
+=
[
(
d
[
"_id"
],
name
,
Profile
.
get_by_id
(
profile
)
if
profile
else
"-"
,
d
[
"last_ts"
])
]
return
self
.
from_dataset
(
title
=
"%s: %s in %d days"
%
(
self
.
title
,
repo
,
days
),
columns
=
[
"ID"
,
"Name"
,
"Address"
,
"Profile"
,
TableColumn
(
_
(
"Last Changed"
),
format
=
"datetime"
)],
TableColumn
(
_
(
"Last Changed"
),
format
=
"datetime"
),
],
data
=
result
,
enumerate
=
True
enumerate
=
True
,
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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