def _normalize_time(self, request): params = request.params.copy() start = params.pop("start", None) end = params.pop("end", None) try: start = ( timeutils.local_to_utc(timeutils.timestamp_to_datetime(start)) if start is not None else timeutils.seconds_ago(DEFAULT_CHANGE_SINCE_SECONDS) ) end = timeutils.local_to_utc(timeutils.timestamp_to_datetime(end)) if end is not None else None except ValueError: raise exception.Invalid(_("Not invalid datetime format.")) query_params = {"start": start, "end": end} return query_params
def _statistic_instance_operation(self, req, tenant_id, start=None, end=None): """ list all instance include deleted and error, then statistic for operations. """ c = client.NovaClient() SERVERS_PATH = SERVERS_REQUEST_PATH.replace("{tenant_id}", tenant_id) params = {"all_tenants": True} if start: params.update({"changes-since": start}) else: # default from a month ago. params.update({"changes-since": timeutils.seconds_ago(DEFAULT_CHANGE_SINCE_SECONDS)}) params.update({"vm_state": "ERROR"}) result, headers = c.response("GET", SERVERS_PATH, params, req.headers, req.body) successes = [] errors = [] for server in result["servers"]: created = server["created"] if not _is_in_time(created, start, end): continue if server["OS-EXT-STS:vm_state"] == "error": fault = {} fault.update(tenant_id=server["tenant_id"]) fault.update(uuid=server["id"]) fault.update(name=server["name"]) # FIXME(hzzhoushaoyu): fault code should be string # as filter should match string but int. if "fault" in server: code = server["fault"].get("code", "500") code = str(code) server["fault"].update(code=code) fault.update(server["fault"]) errors.append(fault) else: fault.update({"message": "Unknow", "code": "500", "created": created}) errors.append(fault) else: # FIXME(hzzhoushaoyu) dead code. successes.append(server) return successes, errors
def _statistic_snapshot_operation(self, req, start=None, end=None): """ list all snapshots created, then statistic for operations. """ c = client.GlanceClient() params = {"is_public": "none"} if start: params.update({"changes-since": start}) else: # default from a month ago. params.update({"changes-since": timeutils.seconds_ago(DEFAULT_CHANGE_SINCE_SECONDS)}) params.update({"property-image_type": "snapshot"}) result, headers = c.response("GET", "/images/detail", params, req.headers, req.body) error = [] success = [] for image in result["images"]: created = image["created_at"] if not _is_in_time(created, start, end): continue if ( image["checksum"] is None and "image_type" in image["properties"] and image["properties"]["image_type"] == "snapshot" ): err_info = { "id": image["id"], "name": image["name"], "owner": image["owner"], "created": created, "desc": "Instance %s create snapshot failed." % image["properties"]["instance_uuid"], } error.append(err_info) else: # FIXME(hzzhoushaoyu) dead code. success.append(image) return success, error