def get(self, host_id): limit = limit_value() offset = offset_value() if not is_valid_uuid(host_id): abort(404, message='Invalid host_id, Id should be in form of UUID4') account_number = identity(request)['identity']['account_number'] system_query = system_ids_by_account(account_number).filter( System.inventory_id == host_id).subquery() query = PerformanceProfile.query.filter( PerformanceProfile.system_id.in_(system_query)).order_by( PerformanceProfile.report_date.desc()) count = query.count() query = query.limit(limit).offset(offset) query_results = query.all() if not query_results: abort(404, message="System {} doesn't exist".format(host_id)) performance_history = [] for profile in query_results: performance_record = sort_io_dict(profile.performance_utilization) performance_record['report_date'] = profile.report_date performance_history.append(performance_record) paginated_response = build_paginated_system_list_response( limit, offset, performance_history, count) paginated_response['inventory_id'] = host_id return paginated_response
def get(self): limit = limit_value() offset = offset_value() order_by = (request.args.get('order_by') or 'display_name').strip().lower() order_how = (request.args.get('order_how') or 'asc').strip().lower() account_number = identity(request)['identity']['account_number'] # Note that When using LIMIT, it is important to use an ORDER BY clause # that constrains the result rows into a unique order. # Otherwise you will get an unpredictable subset of the query's rows. # Refer - https://www.postgresql.org/docs/13/queries-limit.html system_query = system_ids_by_account(account_number).filter( *self.build_system_filters()) last_reported = (db.session.query( PerformanceProfile.system_id, func.max(PerformanceProfile.report_date).label('max_date')).filter( PerformanceProfile.system_id.in_( system_query.subquery())).group_by( PerformanceProfile.system_id).subquery()) sort_expression = self.build_sort_expression(order_how, order_by) query = (db.session.query(PerformanceProfile, System, RhAccount).join( last_reported, (last_reported.c.max_date == PerformanceProfile.report_date) & (PerformanceProfile.system_id == last_reported.c.system_id)).join( System, System.id == last_reported.c.system_id).join( RhAccount, RhAccount.id == System.account_id).order_by( *sort_expression)) count = query.count() # NOTE: Override limit value to get all the systems when it is -1 if limit == -1: limit = count query = query.limit(limit).offset(offset) query_results = query.all() hosts = [] for row in query_results: try: system_dict = row.System.__dict__ host = {skey: system_dict[skey] for skey in SYSTEM_COLUMNS} host['account'] = row.RhAccount.account host['performance_utilization'] = sort_io_dict( row.PerformanceProfile.performance_utilization) host['idling_time'] = row.PerformanceProfile.idling_time host['os'] = row.System.deserialize_host_os_data hosts.append(host) except Exception as err: LOG.error('An error occured while fetching the host. %s', repr(err)) count -= 1 return build_paginated_system_list_response(limit, offset, hosts, count)
def get(self, host_id): if not is_valid_uuid(host_id): abort(404, message='Invalid host_id, Id should be in form of UUID4') ident = identity(request)['identity'] user = user_data_from_identity(ident) username = user['username'] if 'username' in user else None account_query = db.session.query(RhAccount.id).filter( RhAccount.account == ident['account_number']).subquery() system_query = db.session.query(System.id) \ .filter(System.account_id.in_(account_query)).filter(System.inventory_id == host_id).subquery() profile = PerformanceProfile.query.filter( PerformanceProfile.system_id.in_(system_query)).order_by( PerformanceProfile.report_date.desc()).first() rating_record = RecommendationRating.query.filter( RecommendationRating.system_id.in_(system_query), RecommendationRating.rated_by == username).first() system = db.session.query(System).filter( System.inventory_id == host_id).first() record = None if profile: record = {key: system.__dict__[key] for key in SYSTEM_COLUMNS} record['performance_utilization'] = sort_io_dict( profile.performance_utilization) record['rating'] = rating_record.rating if rating_record else None record['report_date'] = profile.report_date record['idling_time'] = profile.idling_time record['io_wait'] = profile.io_wait else: abort(404, message="System {} doesn't exist".format(host_id)) return record