forked from bderenzo/tinystatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincidents.sh
executable file
·113 lines (96 loc) · 2.59 KB
/
incidents.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
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
#!/usr/bin/env bash
# Define input CSV files
INCIDENTS_FILE="incidents.csv"
PAST_INCIDENTS_FILE="past_incidents.csv"
LAST_UPDATED=$(date +"%Y-%m-%dT%H:%M:%S%z")
command_exists(){
if ! command -v "${1}" >/dev/null 2>&1; then
echo >&2 "Error: ${1} missing. Please install it"
exit 1
fi
}
trim_spaces() {
echo "$1" | sed 's/^[ \t]*//;s/[ \t]*$//'
}
# Initialize JSON structure
json_output=$(cat <<EOF
{
"last_updated": "$LAST_UPDATED",
"current_incidents": [],
"past_incidents": []
}
EOF
)
convert_csv_to_json() {
local csv_file=$1
local json_array_name=$2
local json_objects=""
while IFS=',' read -r name impact reported restored; do
# Skip the line if any field is empty
if [ -z "$name" ] || [ -z "$impact" ] || [ -z "$reported" ] || [ -z "$restored" ]; then
continue
fi
# Trim spaces
name=$(trim_spaces "$name")
impact=$(trim_spaces "$impact")
reported=$(trim_spaces "$reported")
restored=$(trim_spaces "$restored" | tr -d '\r')
# Create JSON object for each row
json_object=$(cat <<EOF
{
"name": "$name",
"impact": "$impact",
"reported": "$reported",
"restored": "$restored",
}
EOF
)
if [ -z "$json_objects" ]; then
json_objects="$json_object"
else
json_objects="$json_objects, $json_object"
fi
done < <(tail -n +1 "$csv_file")
# Add JSON objects to the main JSON structure
if [ -n "$json_objects" ]; then
json_output=$(echo "$json_output" | jq ".${json_array_name} = [$json_objects]")
fi
}
load_current_incidents() {
local json_objects=""
while IFS=',' read -r name impact reported; do
# Skip the line if any field is empty
if [ -z "$name" ] || [ -z "$impact" ] || [ -z "$reported" ]; then
continue
fi
# Trim spaces
name=$(trim_spaces "$name")
impact=$(trim_spaces "$impact")
reported=$(trim_spaces "$reported" | tr -d '\r')
# Create JSON object for each row
json_object=$(cat <<EOF
{
"name": "$name",
"impact": "$impact",
"reported": "$reported",
"status": "FAILURE",
}
EOF
)
if [ -z "$json_objects" ]; then
json_objects="$json_object"
else
json_objects="$json_objects, $json_object"
fi
done < <(tail -n +1 "$INCIDENTS_FILE")
# Add JSON objects to the main JSON structure
if [ -n "$json_objects" ]; then
json_output=$(echo "$json_output" | jq ".current_incidents = [$json_objects]")
fi
}
command_exists 'jq'
# Process the CSV files
load_current_incidents
convert_csv_to_json "$PAST_INCIDENTS_FILE" "past_incidents"
# Output the final JSON structure
echo "$json_output"