Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
synnefo
Commits
2eb58a60
Commit
2eb58a60
authored
Dec 02, 2013
by
Christos Stavrakakis
Browse files
cyclades: Optimize server-list command
Optimize server-list command by prefetching related fields if needed.
parent
802bf225
Changes
2
Hide whitespace changes
Inline
Side-by-side
snf-cyclades-app/synnefo/logic/management/commands/server-list.py
View file @
2eb58a60
...
...
@@ -32,6 +32,7 @@
# or implied, of GRNET S.A.
from
optparse
import
make_option
from
functools
import
partial
from
snf_django.management.commands
import
ListCommand
from
synnefo.db.models
import
VirtualMachine
...
...
@@ -77,13 +78,13 @@ class Command(ListCommand):
astakos_auth_url
=
ASTAKOS_AUTH_URL
astakos_token
=
ASTAKOS_TOKEN
def
get_ip
v4
(
vm
):
return
vm
.
nics
.
filter
(
ips__subnet__ipversion
=
4
)
\
.
values_list
(
"ips__address"
,
flat
=
True
)
def
g
et
_
ipv
6
(
vm
)
:
return
vm
.
nics
.
filter
(
ips__subnet__ipversion
=
6
)
\
.
values_list
(
"ips__address"
,
flat
=
True
)
def
get_ip
s
(
version
,
vm
):
ips
=
[]
for
nic
in
vm
.
nics
.
all
():
for
ip
in
nic
.
ips
.
all
():
if
ip
.
subn
et
.
ipv
ersion
==
version
:
ips
.
append
(
ip
.
address
)
return
ips
def
format_vm_state
(
vm
):
if
vm
.
operstate
==
"BUILD"
:
...
...
@@ -100,8 +101,10 @@ class Command(ListCommand):
"image.id"
:
(
"imageid"
,
"The ID of the server's image"
),
"image.name"
:
(
"image"
,
"The name of the server's image"
),
"state"
:
(
format_vm_state
,
"The current state of the server"
),
"ipv4"
:
(
get_ipv4
,
"The IPv4 addresses of the server"
),
"ipv6"
:
(
get_ipv6
,
"The IPv6 addresses of the server"
),
"ipv4"
:
(
partial
(
get_ips
,
4
),
"The IPv4 addresses of the server"
),
"ipv6"
:
(
partial
(
get_ips
,
6
),
"The IPv6 addresses of the server"
),
"created"
:
(
"created"
,
"The date the server was created"
),
"deleted"
:
(
"deleted"
,
"Whether the server is deleted or not"
),
"suspended"
:
(
"suspended"
,
"Whether the server is administratively"
...
...
@@ -126,6 +129,9 @@ class Command(ListCommand):
self
.
fields
=
[
"image.name"
if
x
==
"image.id"
else
x
for
x
in
self
.
fields
]
if
"ipv4"
in
self
.
fields
or
"ipv6"
in
self
.
fields
:
self
.
prefetch_related
.
append
(
"nics__ips__subnet"
)
def
handle_db_objects
(
self
,
rows
,
*
args
,
**
kwargs
):
if
"image.name"
in
self
.
fields
:
icache
=
ImageCache
()
...
...
snf-django-lib/snf_django/management/commands/__init__.py
View file @
2eb58a60
...
...
@@ -113,6 +113,10 @@ class ListCommand(SynnefoCommand):
astakos_auth_url
=
None
astakos_token
=
None
# Optimize DB queries
prefetch_related
=
[]
select_related
=
[]
help
=
"Generic List Command"
option_list
=
SynnefoCommand
.
option_list
+
(
make_option
(
...
...
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