Repository navigation
Expand file tree
/
Copy pathprocess-json.sh
More file actions
executable file
·137 lines (121 loc) · 3.75 KB
/
Copy pathprocess-json.sh
File metadata and controls
executable file
·137 lines (121 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Function to print script usage
print_usage() {
echo "Usage: $0 [options] <json_file> [key (setup, clean, compile, run{default})] [value (pass{default}, fail, missing)]"
echo "Options:"
echo " -s, --stats Print vital statistics after each test"
echo " -l Print output on a single line"
echo " -c Print output in csv format (ignore filters)"
echo " -h, --help Print this help message"
}
# Function to parse JSON and extract the test values
parse_json() {
local json_file="$1"
local print_on_single_line="$2"
local key="$3"
local value="$4"
local show_stats="$5"
# Extract relevant entries and filter them
local filtered_entries=$(jq -c --arg key "$key" --arg value "$value" '
.[] | select(
.test != null and
(.test_stages[$key] == $value or ($value == "fail" and .test_stages[$key] == "timeout"))
)
' "$json_file")
# Loop through each filtered entry and print the directory path
while IFS= read -r entry; do
[[ -z "$entry" ]] && continue
local test_value=$(echo "$entry" | jq -r '.test')
local directory_path=$(basename "$test_value")
if [[ "$show_stats" == "true" ]]; then
local stats_str=$(echo "$entry" | jq -r --arg key "$key" '
(.metrics[$key] // .metrics.run // null) as $m |
if $m != null then
"time: \($m.elapsed_sec // 0)s | cpu: \($m.cpu_percent // 0)% | user: \($m.user_cpu_sec // 0)s | sys: \($m.sys_cpu_sec // 0)s | maxrss: \(if ($m.max_rss_kb != null and $m.max_rss_kb > 0) then "\((($m.max_rss_kb / 10.24 | round) / 100))MB" else "0MB" end) | ctx: \($m.vol_ctx_switches // 0)v/\($m.invol_ctx_switches // 0)i"
else
"(no metrics)"
end
')
if [[ "$print_on_single_line" == "-l" ]]; then
echo -n "$directory_path [$stats_str] "
else
echo "$directory_path"
echo " $stats_str"
fi
else
if [[ "$print_on_single_line" == "-l" ]]; then
echo -n "$directory_path "
else
echo "$directory_path"
fi
fi
done <<< "$filtered_entries"
if [[ "$print_on_single_line" == "-l" ]]; then
echo
fi
}
to_csv() {
local json_file="$1"
local show_stats="$2"
if [[ "$show_stats" == "true" ]]; then
jq -r 'map(select(.test != null)) | .[] | [
.test,
(.test_stages | to_entries[-1].value),
(if (.test_stages | to_entries[-1].value) == "fail" then (.test_stages | to_entries[-1].key) else "" end),
(.metrics.run.elapsed_sec // ""),
(.metrics.run.cpu_percent // ""),
(.metrics.run.max_rss_kb // "")
] | @csv' "$json_file" | tr -d '"' | tr -d '/'
else
jq -r 'map(select(.test != null)) | .[] | [.test, (.test_stages | to_entries[-1].value), if (.test_stages | to_entries[-1].value) == "fail" then (.test_stages | to_entries[-1].key) else "" end] | @csv' "$json_file" | tr -d '"' | tr -d '/'
fi
}
show_stats=false
print_single_line=""
csv_mode=false
positional_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
-l)
print_single_line="-l"
shift
;;
-s|--stats)
show_stats=true
shift
;;
-c)
csv_mode=true
shift
;;
-*)
echo "Unknown option: $1" >&2
print_usage
exit 1
;;
*)
positional_args+=("$1")
shift
;;
esac
done
if [[ ${#positional_args[@]} -eq 0 ]]; then
print_usage
exit 1
fi
json_file="${positional_args[0]}"
key="${positional_args[1]:-run}"
value="${positional_args[2]:-pass}"
if [[ ! -f "$json_file" ]]; then
echo "Error: JSON file '$json_file' does not exist." >&2
exit 1
fi
if [[ "$csv_mode" == true ]]; then
to_csv "$json_file" "$show_stats"
exit 0
fi
parse_json "$json_file" "$print_single_line" "$key" "$value" "$show_stats"