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() ident = identity(request)['identity'] # 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 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(*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() 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'] = row.PerformanceProfile.performance_utilization host['display_performance_score'] = row.PerformanceProfile.display_performance_score host['idling_time'] = row.PerformanceProfile.idling_time host['io_wait'] = row.PerformanceProfile.io_wait 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 )