$rows * @return array{orders:int,revenue:float,lines:array} */ public static function summarize( array $rows ) { $groups = array(); $orders = 0; $revenue = 0.0; foreach ( $rows as $row ) { $verdict = isset( $row['verdict'] ) ? (string) $row['verdict'] : ''; if ( '' === $verdict ) { continue; } $result = array( 'verdict' => $verdict, 'name' => isset( $row['name'] ) ? (string) $row['name'] : '', 'confidence' => isset( $row['confidence'] ) ? (string) $row['confidence'] : '', 'detail' => '', ); $label = AgentStamp_Classifier::label( $result ); $total = isset( $row['total'] ) ? (float) $row['total'] : 0.0; if ( ! isset( $groups[ $label ] ) ) { $groups[ $label ] = array( 'label' => $label, 'verdict' => $verdict, 'orders' => 0, 'revenue' => 0.0, 'share' => 0.0, ); } $groups[ $label ]['orders']++; $groups[ $label ]['revenue'] += $total; $orders++; $revenue += $total; } foreach ( $groups as $label => $group ) { $groups[ $label ]['share'] = $orders > 0 ? $group['orders'] / $orders : 0.0; } $lines = array_values( $groups ); usort( $lines, static function ( $a, $b ) { if ( $a['revenue'] === $b['revenue'] ) { return $b['orders'] - $a['orders']; } return $a['revenue'] < $b['revenue'] ? 1 : -1; } ); return array( 'orders' => $orders, 'revenue' => $revenue, 'lines' => $lines, ); } /** * Orders that were not a person. * * @param array{lines:array} $summary * @return array{orders:int,revenue:float} */ public static function not_human( array $summary ) { $orders = 0; $revenue = 0.0; foreach ( $summary['lines'] as $line ) { if ( 'human' === $line['verdict'] ) { continue; } $orders += $line['orders']; $revenue += $line['revenue']; } return array( 'orders' => $orders, 'revenue' => $revenue, ); } }