Skip to content

Commit

Permalink
Do not create records for instances outside the reporting period
Browse files Browse the repository at this point in the history
If we passed the extract-to and extract-from options we were creating
bogus records without a usage and start and end times. This change fixes
that behaviour, and only creates records corresponding to that period.
  • Loading branch information
alvarolopez committed Aug 3, 2017
1 parent 345da26 commit 11520c0
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions caso/extract/nova.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def extract_for_project(self, project, extract_from, extract_to):
# Iter over results until we do not have more to get
while True:
aux = nova.servers.list(
search_opts={"changes-since": extract_from},
search_opts={"changes-since": extract_from,
"deleted": True},
limit=limit,
marker=marker
)
Expand All @@ -134,6 +135,10 @@ def extract_for_project(self, project, extract_from, extract_to):

servers = sorted(servers, key=operator.attrgetter("created"))

# We are going to query for the sever usages below. It will return the
# usages for the specified period. However, usages should be absolute
# values, not deltas. Therefore we need to change the start time for
# the query with that of the oldest server.
if servers:
start = dateutil.parser.parse(servers[0].created)
start = start.replace(tzinfo=None)
Expand All @@ -149,18 +154,16 @@ def extract_for_project(self, project, extract_from, extract_to):
vo = self.voms_map.get(project)

for server in servers:
server_start = dateutil.parser.parse(server.created)
server_start = server_start.replace(tzinfo=None)
if server_start > extract_to:
continue
records[server.id] = self.build_record(server, vo, images,
flavors, users)

for usage in usages:
if usage["instance_id"] not in records:
try:
server = nova.servers.get(usage["instance_id"])
except novaclient.exceptions.ClientException:
# Maybe the instance is completely missing
continue
records[server.id] = self.build_record(server, vo, images,
flavors, users)
continue
instance_id = usage["instance_id"]
records[instance_id].memory = usage["memory_mb"]
records[instance_id].cpu_count = usage["vcpus"]
Expand Down

0 comments on commit 11520c0

Please sign in to comment.