record last_collected timestamp for tenants

Otherwise, we keep rescanning empty hours every run,
which is stupid.

Change-Id: Ifb52065edb600e4b8bb157b5fab083a17af63e60
This commit is contained in:
Chris Forbes
2014-04-17 14:08:00 +12:00
parent 8e0ee3ee8b
commit 58510d928f
3 changed files with 16 additions and 8 deletions

View File

@@ -79,10 +79,9 @@ def collect_usage(tenant, db, session, resp, end):
timestamp = datetime.now()
session.begin(subtransactions=True)
print 'collect_usage for %s %s' % (tenant.id, tenant.name)
db.insert_tenant(tenant.id, tenant.name,
db_tenant = db.insert_tenant(tenant.id, tenant.name,
tenant.description, timestamp)
start = session.query(func.max(UsageEntry.end).label('end')).\
filter(UsageEntry.tenant_id == tenant.id).first().end
start = db_tenant.last_collected
if not start:
print 'failed to find any previous usageentry for this tenant; starting at %s' % dawn_of_time
start = dawn_of_time
@@ -112,6 +111,10 @@ def collect_usage(tenant, db, session, resp, end):
db.insert_usage(tenant.id, res, transformed,
window_start, window_end, timestamp)
# update the timestamp for the tenant so we won't examine this timespan again.
db_tenant.last_collected = window_end
session.add(db_tenant)
session.commit()
resp["tenants"].append(
{"id": tenant.id,

View File

@@ -17,12 +17,16 @@ class Database(object):
query = self.session.query(Tenant).\
filter(Tenant.id == tenant_id)
if query.count() == 0:
self.session.add(Tenant(id=tenant_id,
info=metadata,
name=tenant_name,
created=timestamp
))
tenant = Tenant(id=tenant_id,
info=metadata,
name=tenant_name,
created=timestamp
)
self.session.add(tenant)
self.session.flush() # can't assume deferred constraints.
return tenant
else:
return query[0]
def insert_resource(self, tenant_id, resource_id, resource_type, timestamp):
query = self.session.query(Resource).\

View File

@@ -81,6 +81,7 @@ class Tenant(Base):
name = Column(Text, nullable=False)
info = Column(Text)
created = Column(DateTime, nullable=False)
last_collected = Column(DateTime, nullable=True)
resources = relationship(Resource, backref="tenant")