Ignore:
Timestamp:
May 15, 2008, 9:55:17 PM (16 years ago)
Author:
ecprice
Message:

Use joins, new xmlist.py

Location:
trunk/packages/sipb-xen-www/code
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-www/code/controls.py

    r522 r535  
    1212import re
    1313import cache_acls
     14import cPickle
    1415
    1516# ... and stolen from xend/uuid.py
     
    114115        machine.type_id = machine_type.type_id
    115116        ctx.current.save(machine)
    116         disk = Disk(machine_id=machine.machine_id, 
     117        disk = Disk(machine_id=machine.machine_id,
    117118                    guest_device_name='hda', size=disk_size)
    118119        open_nics = NIC.select_by(machine_id=None)
     
    123124        nic.machine_id = machine.machine_id
    124125        nic.hostname = name
    125         ctx.current.save(nic)   
     126        ctx.current.save(nic)
    126127        ctx.current.save(disk)
    127128        cache_acls.refreshMachine(machine)
     
    137138    return machine
    138139
    139 def getUptimes(machines=None):
    140     """Return a dictionary mapping machine names to uptime strings"""
    141     value_string = remctl('web', 'listvms')
    142     lines = value_string.splitlines()
    143     d = {}
    144     for line in lines:
    145         lst = line.split()
    146         name, id = lst[:2]
    147         uptime = ' '.join(lst[2:])
    148         d[name] = uptime
    149     ans = {}
    150     for m in machines:
    151         ans[m] = d.get(m.name)
    152     return ans
     140def getList(machines):
     141    """Return a dictionary mapping machine  to dicts."""
     142    value_string = remctl('web', 'listvms', '--pickle')
     143    value_dict = cPickle.loads(value_string)
     144
     145    d = dict((m, value_dict[m.name]) for m in machines if m.name in value_dict)
     146    return d
    153147
    154148def parseStatus(s):
  • trunk/packages/sipb-xen-www/code/main.py

    r516 r535  
    197197def getListDict(user):
    198198    """Gets the list of local variables used by list.tmpl."""
     199    checkpoint.checkpoint('Starting')
    199200    machines = g.machines
    200201    checkpoint.checkpoint('Got my machines')
    201202    on = {}
    202203    has_vnc = {}
    203     on = g.uptimes
     204    xmlist = g.xmlist
    204205    checkpoint.checkpoint('Got uptimes')
    205206    for m in machines:
    206         m.uptime = g.uptimes.get(m)
    207         if not on[m]:
     207        if m not in xmlist:
    208208            has_vnc[m] = 'Off'
    209         elif m.type.hvm:
    210             has_vnc[m] = True
     209            m.uptime = None
    211210        else:
    212             has_vnc[m] = "ParaVM"+helppopup("paravm_console")
     211            m.uptime = xmlist[m]['uptime']
     212            if xmlist[m]['console']:
     213                has_vnc[m] = True
     214            elif m.type.hvm:
     215                has_vnc[m] = "WTF?"
     216            else:
     217                has_vnc[m] = "ParaVM"+helppopup("paravm_console")
    213218    max_memory = validation.maxMemory(user)
    214219    max_disk = validation.maxDisk(user)
     
    228233             defaults=defaults,
    229234             machines=machines,
    230              has_vnc=has_vnc,
    231              uptimes=g.uptimes)
     235             has_vnc=has_vnc)
    232236    return d
    233237
     
    652656        checkpoint.checkpoint('output as a string')
    653657        print output_string
    654         print '<!-- <pre>%s</pre> -->' % checkpoint
     658        if fields.has_key('timedebug'):
     659            print '<pre>%s</pre>' % checkpoint
    655660    except Exception, err:
    656661        if not fields.has_key('js'):
     
    676681if __name__ == '__main__':
    677682    fields = cgi.FieldStorage()
     683
     684    if fields.has_key('sqldebug'):
     685        import logging
     686        logging.basicConfig()
     687        logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
     688        logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.INFO)
     689
    678690    u = getUser()
    679691    g.user = u
  • trunk/packages/sipb-xen-www/code/templates/list.tmpl

    r448 r535  
    11#from skeleton import skeleton
    22#extends skeleton
     3#import datetime
    34
    45
     
    119120<td>#slurp
    120121#if $machine.uptime
    121 $machine.uptime#slurp
     122${datetime.timedelta(seconds=int(machine.uptime))}#slurp
    122123#end if
    123124</td>
  • trunk/packages/sipb-xen-www/code/validation.py

    r440 r535  
    55import re
    66import string
    7 from sipb_xen_database import Machine, NIC, Type
     7from sipb_xen_database import Machine, NIC, Type, Disk
    88from webcommon import InvalidInput, g
    99
     
    4545        return MAX_MEMORY_SINGLE
    4646    machines = getMachinesByOwner(user, machine)
    47     active_machines = [x for x in machines if g.uptimes.get(x)]
     47    active_machines = [x for x in machines if g.xmlist.get(x)]
    4848    mem_usage = sum([x.memory for x in active_machines if x != machine])
    4949    return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage)
     
    5555    return the maximum that a given machine can be changed to.
    5656    """
    57     machines = getMachinesByOwner(user, machine)
    58     disk_usage = sum([sum([y.size for y in x.disks])
    59                       for x in machines if x != machine])
     57    if machine is not None:
     58        machine_id = machine.machine_id
     59    else:
     60        machine_id = None
     61    disk_usage = Disk.query().filter_by(Disk.c.machine_id != machine_id,
     62                                        owner=user).sum(Disk.c.size)
    6063    return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.)
    6164
    6265def cantAddVm(user):
    6366    machines = getMachinesByOwner(user)
    64     active_machines = [x for x in machines if g.uptimes.get(x)]
     67    active_machines = [x for x in machines if g.xmlist.get(x)]
    6568    if len(machines) >= MAX_VMS_TOTAL:
    6669        return 'You have too many VMs to create a new one.'
  • trunk/packages/sipb-xen-www/code/webcommon.py

    r264 r535  
    11"""Exceptions for the web interface."""
    22
     3import time
    34from sipb_xen_database import Machine, MachineAccess
    45
     
    4041    def __init__(self, user):
    4142        self.user = user
    42    
    43     machines = cachedproperty(lambda self:
    44                              [ma.machine for ma in
    45                               MachineAccess.select_by(user=self.user)])
    46     uptimes = cachedproperty(lambda self:
    47                              controls.getUptimes(self.machines))
     43
     44    machines = cachedproperty(lambda self:
     45                                  Machine.query().join('acl').select_by(user=self.user))
     46    xmlist = cachedproperty(lambda self:
     47                                controls.getList(self.machines))
    4848
    4949    def clear(self):
Note: See TracChangeset for help on using the changeset viewer.