function xform_rows_to_top5($resArray, $column_vector) { global $top5_page_list; global $top5_function_list; global $top5_data; $ts_col = array_search("timestamp", $column_vector); $page_col = array_search("page", $column_vector); $fn_col = array_search("function", $column_vector); if ($ts_col === FALSE || $page_col === FALSE || $fn_col === FALSE) { header('HTTP/1.1 500 Internal Server Error'); print_r($column_vector); die("column names messed up."); } $palette = new Palette(); $last_ts = NULL; $last_page = NULL; $chunk = array(); $top5_data = "\n\n[\n"; foreach ($resArray as $row) { $page = $row[$page_col]; $ts = $row[$ts_col]; $fn = $row[$fn_col]; # continue current chunk if ts and page are same if ($ts == $last_ts && $page == $last_page) { if (count($chunk) == 2 + 5) { # 2 is for timestamp and page # Already 5 entries continue; } else { $chunk[] = $palette->format($fn); } } else { # # either ts or page changed, insert current chunk # and reset trackers # # If there are no entries in current chunk # this is the very first entry, special case - # don't we hate those? # if (count($chunk) != 0) { while (count($chunk) < 5 + 2) { $chunk[] = "'no_entry'"; } $top5_data .= json_encode($chunk) . ",\n"; } $chunk = array(); $chunk = array($ts, $page, $palette->format($fn)); $last_ts = $ts; $last_page = $page; $top5_page_list[$page] = TRUE; } } $top5_data .= "\n],\n"; $top5_data .= json_encode($palette->elements) . "\n\n"; }