<s> import json <EOL> import logging <EOL> import sys <EOL> from monitor import dbutil <EOL> from monitor . models import Table , HBaseCluster <EOL> OPERATION_NUM_OPS = '<STR_LIT>' <EOL> OPERATION_AVG_TIME = '<STR_LIT>' <EOL> OPERATION_MIN_TIME = '<STR_LIT>' <EOL> OPERATION_MAX_TIME = '<STR_LIT>' <EOL> OPERATION_TOTAL_TIME = '<STR_LIT>' <EOL> logger = logging . getLogger ( __name__ ) <EOL> def make_empty_operation_metric ( ) : <EOL> operationMetric = { } <EOL> operationMetric [ OPERATION_NUM_OPS ] = <NUM_LIT:0> <EOL> operationMetric [ OPERATION_TOTAL_TIME ] = <NUM_LIT:0> <EOL> operationMetric [ OPERATION_MAX_TIME ] = <NUM_LIT:0> <EOL> operationMetric [ OPERATION_MIN_TIME ] = sys . maxint <EOL> return operationMetric <EOL> def aggregate_one_region_operation_metric ( aggregateMetric , deltaMetric ) : <EOL> if OPERATION_NUM_OPS in deltaMetric : <EOL> aggregateMetric [ OPERATION_NUM_OPS ] += deltaMetric [ OPERATION_NUM_OPS ] <EOL> aggregateMetric [ OPERATION_TOTAL_TIME ] += ( deltaMetric [ OPERATION_AVG_TIME ] <EOL> * deltaMetric [ OPERATION_NUM_OPS ] ) <EOL> if aggregateMetric [ OPERATION_MAX_TIME ] < deltaMetric [ OPERATION_MAX_TIME ] : <EOL> aggregateMetric [ OPERATION_MAX_TIME ] = deltaMetric [ OPERATION_MAX_TIME ] <EOL> if aggregateMetric [ OPERATION_MIN_TIME ] > deltaMetric [ OPERATION_MIN_TIME ] : <EOL> aggregateMetric [ OPERATION_MIN_TIME ] = deltaMetric [ OPERATION_MIN_TIME ] <EOL> def compute_avg_time_and_num_ops_after_aggregation ( operationMetrics ) : <EOL> for operationName in operationMetrics . keys ( ) : <EOL> if operationMetrics [ operationName ] [ OPERATION_NUM_OPS ] > <NUM_LIT:0> : <EOL> operationMetrics [ operationName ] [ OPERATION_AVG_TIME ] = ( operationMetrics [ operationName ] [ OPERATION_TOTAL_TIME ] <EOL> / operationMetrics [ operationName ] [ OPERATION_NUM_OPS ] ) <EOL> operationMetrics [ operationName ] [ OPERATION_NUM_OPS ] = operationMetrics [ operationName ] [ OPERATION_NUM_OPS ] / <NUM_LIT:10> <EOL> else : <EOL> operationMetrics [ operationName ] [ OPERATION_AVG_TIME ] = <NUM_LIT:0> <EOL> def aggregate_region_operation_metric_in_process ( output_queue , task_data ) : <EOL> allClusterOperationMetric = { } <EOL> tables = Table . objects . all ( ) <EOL> for table in tables : <EOL> clusterName = table . cluster . name <EOL> clusterOperationMetric = allClusterOperationMetric . setdefault ( clusterName , { } ) <EOL> tableOperationMetric = { } <EOL> regions = dbutil . get_region_by_table ( table ) <EOL> logger . info ( <EOL> "<STR_LIT>" "<STR_LIT>" , len ( regions ) , <EOL> table . name , clusterName ) <EOL> for region in regions : <EOL> if region . operationMetrics is None or region . operationMetrics == '<STR_LIT>' : <EOL> continue ; <EOL> regionOperationMetrics = json . loads ( region . operationMetrics ) <EOL> for regionOperationName in regionOperationMetrics . keys ( ) : <EOL> regionOperation = regionOperationMetrics [ regionOperationName ] <EOL> aggregate_one_region_operation_metric ( <EOL> tableOperationMetric . setdefault ( regionOperationName , <EOL> make_empty_operation_metric ( ) ) , regionOperation ) <EOL> aggregate_one_region_operation_metric ( <EOL> clusterOperationMetric . setdefault ( regionOperationName , <EOL> make_empty_operation_metric ( ) ) , regionOperation ) <EOL> compute_avg_time_and_num_ops_after_aggregation ( tableOperationMetric ) <EOL> table . operationMetrics = json . dumps ( tableOperationMetric ) <EOL> table . save ( ) <EOL> clusters = HBaseCluster . objects . all ( ) <EOL> for cluster in clusters : <EOL> clusterName = cluster . cluster . name <EOL> if clusterName in allClusterOperationMetric : <EOL> clusterOperationMetric = allClusterOperationMetric [ clusterName ] <EOL> compute_avg_time_and_num_ops_after_aggregation ( clusterOperationMetric ) <EOL> cluster . operationMetrics = json . dumps ( clusterOperationMetric ) <EOL> cluster . save ( ) <EOL> return </s>
