-
Notifications
You must be signed in to change notification settings - Fork 0
/
profilingStep.sh
40 lines (34 loc) · 1.44 KB
/
profilingStep.sh
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
#!/bin/bash
function profiling_func() {
local stepno="$1"
local description="$2"
local timestamp=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
if [ -f "profiling.json" ]; then
# Read data from profiling.json if it exists
data=$(jq -c '.' profiling.json)
else
# Initialize with default data if profiling.json doesn't exist
data='{
"input": {
},
"stepsProfile": [],
"totalTime": {
"minutes": 0,
"seconds": 0
}
}'
fi
# Check if a step with the same number exists in the "stepsProfile" array
step_exists=$(echo "$data" | jq --arg stepno "$stepno" '.stepsProfile[] | has($stepno)')
if [ "$step_exists" = "true" ]; then
# Update the existing step with the same number
updated_data=$(echo "$data" | jq --arg stepno "$stepno" --arg description "$description" --arg timestamp "$timestamp" '.stepsProfile[] |= if has($stepno) then .[$stepno].description = $description | .[$stepno].timestamp = $timestamp else . end')
else
# Construct the step object
local step_object="{\"step$stepno\": {\"description\": \"$description\", \"timestamp\": \"$timestamp\"}}"
# Add the step object to 'stepsProfile' array
updated_data=$(echo "$data" | jq --argjson step "$step_object" '.stepsProfile += [$step]')
fi
# Write to file
echo "$updated_data" > profiling.json
}