~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/api.py

  • Committer: Tarmac
  • Author(s): Vishvananda Ishaya, Anthony Young
  • Date: 2011-09-01 00:16:34 UTC
  • mfrom: (1468.3.13 os-simple-usage)
  • Revision ID: tarmac-20110901001634-bb3whyoipqxc60k5
Simple usage extension for nova.  Uses db to calculate tenant_usage for specified time periods.

Methods:
    * index: return a list of tenant_usages, with option of incuding detailed server_usage
    * show: returns a specific tenant_usage object

tenant_usage object:
    * tenant_usage.total_memory_mb_usage: sum of memory_mb * hours for all instances in tenant for this period
    * tenant_usage.total_local_gb_usage: sum of local_gb * hours for all instances in tenant for this period
    * tenant_usage.total_vcpus_usage: sum of vcpus * hours for all instances in tenant for this period
    * tenant_usage.total_hours: sum of all instance hours for this period
    * tenant_usage.server_usages: A detailed list of server_usages, which describe the usage of a specific server

For larger instances db tables, indexes on instance.launched_at and instance.terminated_at should significantly help performance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1306
1306
    return instances
1307
1307
 
1308
1308
 
 
1309
@require_context
 
1310
def instance_get_active_by_window(context, begin, end=None, project_id=None):
 
1311
    """Return instances that were continuously active over window."""
 
1312
    session = get_session()
 
1313
    query = session.query(models.Instance).\
 
1314
                    filter(models.Instance.launched_at < begin)
 
1315
    if end:
 
1316
        query = query.filter(or_(models.Instance.terminated_at == None,
 
1317
                                 models.Instance.terminated_at > end))
 
1318
    else:
 
1319
        query = query.filter(models.Instance.terminated_at == None)
 
1320
    if project_id:
 
1321
        query = query.filter_by(project_id=project_id)
 
1322
    return query.all()
 
1323
 
 
1324
 
1309
1325
@require_admin_context
1310
 
def instance_get_active_by_window(context, begin, end=None):
1311
 
    """Return instances that were continuously active over the given window"""
 
1326
def instance_get_active_by_window_joined(context, begin, end=None,
 
1327
                                         project_id=None):
 
1328
    """Return instances and joins that were continuously active over window."""
1312
1329
    session = get_session()
1313
1330
    query = session.query(models.Instance).\
1314
 
                   options(joinedload_all('fixed_ips.floating_ips')).\
1315
 
                   options(joinedload('security_groups')).\
1316
 
                   options(joinedload_all('fixed_ips.network')).\
1317
 
                   options(joinedload('instance_type')).\
1318
 
                   filter(models.Instance.launched_at < begin)
 
1331
                    options(joinedload_all('fixed_ips.floating_ips')).\
 
1332
                    options(joinedload('security_groups')).\
 
1333
                    options(joinedload_all('fixed_ips.network')).\
 
1334
                    options(joinedload('instance_type')).\
 
1335
                    filter(models.Instance.launched_at < begin)
1319
1336
    if end:
1320
1337
        query = query.filter(or_(models.Instance.terminated_at == None,
1321
1338
                                 models.Instance.terminated_at > end))
1322
1339
    else:
1323
1340
        query = query.filter(models.Instance.terminated_at == None)
 
1341
    if project_id:
 
1342
        query = query.filter_by(project_id=project_id)
1324
1343
    return query.all()
1325
1344
 
1326
1345