1515# under the License.
1616
1717import datetime
18+ import operator
1819
1920import dateutil .parser
2021from dateutil import tz
@@ -69,8 +70,15 @@ def extract_for_tenant(self, tenant, lastrun):
6970 tenant_id = conn .client .tenant_id
7071 servers = conn .servers .list (search_opts = {"changes-since" : lastrun })
7172
72- # FIXME(aloga): use start and end from the retreived servers
73- aux = conn .usage .get (tenant_id , lastrun , end )
73+ servers = sorted (servers , key = operator .attrgetter ("created" ))
74+
75+ if servers :
76+ start = dateutil .parser .parse (servers [0 ].created )
77+ start .replace (tzinfo = None )
78+ else :
79+ start = lastrun
80+
81+ aux = conn .usage .get (tenant_id , start , end )
7482 usages = getattr (aux , "server_usages" , [])
7583
7684 images = conn .images .list ()
@@ -109,7 +117,6 @@ def extract_for_tenant(self, tenant, lastrun):
109117 records [instance_id ].memory = usage ["memory_mb" ]
110118 records [instance_id ].cpu_count = usage ["vcpus" ]
111119 records [instance_id ].disk = usage ["local_gb" ]
112- records [instance_id ].cpu_duration = int (usage ["hours" ] * 3600 )
113120
114121 started = dateutil .parser .parse (usage ["started_at" ])
115122 records [instance_id ].start_time = int (started .strftime ("%s" ))
@@ -120,5 +127,11 @@ def extract_for_tenant(self, tenant, lastrun):
120127 else :
121128 wall = now - started
122129
123- records [instance_id ].wall_duration = int (wall .total_seconds ())
130+ wall = int (wall .total_seconds ())
131+ records [instance_id ].wall_duration = wall
132+
133+ cput = int (usage ["hours" ] * 3600 )
134+ # NOTE(aloga): in some cases there may be rounding errors and cput
135+ # may be larger than wall.
136+ records [instance_id ].cpu_duration = cput if cput < wall else wall
124137 return records
0 commit comments