'Update stats after an instance is changed.'

def update_stats_for_instance(self, instance, is_removed=False):

    uuid = instance['uuid']
    if (uuid in self.states):
        old_state = self.states[uuid]
        self._decrement(('num_vm_%s' % old_state['vm_state']))
        self._decrement(('num_task_%s' % old_state['task_state']))
        self._decrement(('num_os_type_%s' % old_state['os_type']))
        self._decrement(('num_proj_%s' % old_state['project_id']))
    else:
        self._increment('num_instances')
    (vm_state, task_state, os_type, project_id) = self._extract_state_from_instance(instance)
    if (is_removed or (vm_state in vm_states.ALLOW_RESOURCE_REMOVAL)):
        self._decrement('num_instances')
        self.states.pop(uuid)
    else:
        self._increment(('num_vm_%s' % vm_state))
        self._increment(('num_task_%s' % task_state))
        self._increment(('num_os_type_%s' % os_type))
        self._increment(('num_proj_%s' % project_id))
    self['io_workload'] = self.io_workload
