-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccum-traffic-clones.sh
151 lines (117 loc) · 4.77 KB
/
accum-traffic-clones.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
function print_error()
{
[[ -n "$write_error_to_file" ]] && echo "$*" >> "$write_error_to_file"
echo "$*" >&2
}
function print_warning()
{
[[ -n "$write_warning_to_file" ]] && echo "$*" >> "$write_warning_to_file"
echo "$*" >&2
}
[[ -z "$traffic_clones_dir" ]] && traffic_clones_dir='traffic/clones'
[[ -z "$traffic_clones_by_year_dir" ]] && traffic_clones_by_year_dir="$traffic_clones_dir/by_year"
[[ -z "$traffic_clones_json" ]] && traffic_clones_json="$traffic_clones_dir/latest.json"
[[ -z "$traffic_clones_accum_json" ]] && traffic_clones_accum_json="$traffic_clones_dir/latest-accum.json"
current_date_time_utc=$(date --utc +%FT%TZ)
clones_accum_timestamp=()
clones_accum_count=()
clones_accum_uniques=()
IFS=$'\n' read -r -d '' count_outdated_prev uniques_outdated_prev count_prev uniques_prev <<< "$(jq -c -r ".count_outdated,.uniques_outdated,.count,.uniques" $traffic_clones_accum_json)"
clones_timestamp_prev_seq=""
clones_count_prev_seq=""
clones_unique_prev_seq=""
for i in $(jq '.clones|keys|.[]' $traffic_clones_accum_json); do
IFS=$'\n' read -r -d '' timestamp count uniques <<< "$(jq -c -r ".clones[$i].timestamp,.clones[$i].count,.clones[$i].uniques" $traffic_clones_accum_json)"
clones_accum_timestamp[${#clones_accum_timestamp[@]}]="$timestamp"
clones_accum_count[${#clones_accum_count[@]}]=$count
clones_accum_uniques[${#clones_accum_uniques[@]}]=$uniques
clones_timestamp_prev_seq="$clones_timestamp_prev_seq|$timestamp"
clones_count_prev_seq="$clones_count_prev_seq|$count"
clones_uniques_prev_seq="$clones_uniques_prev_seq|$uniques"
done
# CAUTION:
# Sometimes the json data file comes empty for some reason.
# We must check that special case to avoid invalid accumulation!
#
IFS=$'\n' read -r -d '' count uniques clones_length <<< $(jq '.count,.uniques,.clones|length' $traffic_clones_json)
(( ! count && ! uniques && ! clones_length )) && {
print_error "$0: error: json data is invalid or empty."
exit 255
} >&2
first_clones_timestamp=""
clones_timestamp=()
clones_count=()
clones_uniques=()
clones_timestamp_next_seq=""
clones_count_next_seq=""
clones_unique_next_seq=""
for i in $(jq '.clones|keys|.[]' $traffic_clones_json); do
IFS=$'\n' read -r -d '' timestamp count uniques <<< "$(jq -c -r ".clones[$i].timestamp,.clones[$i].count,.clones[$i].uniques" $traffic_clones_json)"
(( ! i )) && first_clones_timestamp="$timestamp"
clones_timestamp[${#clones_timestamp[@]}]="$timestamp"
clones_count[${#clones_count[@]}]=$count
clones_uniques[${#clones_uniques[@]}]=$uniques
clones_timestamp_next_seq="$clones_timestamp_next_seq|$timestamp"
clones_count_next_seq="$clones_count_next_seq|$count"
clones_uniques_next_seq="$clones_uniques_next_seq|$uniques"
timestamp_date_time_utc=${timestamp//:/-}
timestamp_date_utc=${timestamp_date_time_utc/%T*}
timestamp_year_utc=${timestamp_date_utc/%-*}
timestamp_year_dir="$traffic_clones_by_year_dir/$timestamp_year_utc"
[[ ! -d "$timestamp_year_dir" ]] && mkdir -p "$timestamp_year_dir"
echo "\
{
\"timestamp\" : \"$timestamp\",
\"count\" : $count,
\"uniques\" : $uniques
}" > "$timestamp_year_dir/$timestamp_date_utc.json"
done
# accumulate statistic
count_outdated_next=$count_outdated_prev
uniques_outdated_next=$uniques_outdated_prev
for (( i=0; i < ${#clones_accum_timestamp[@]}; i++)); do
if [[ -z "$first_clones_timestamp" || "${clones_accum_timestamp[i]}" < "$first_clones_timestamp" ]]; then
(( count_outdated_next += ${clones_accum_count[i]} ))
(( uniques_outdated_next += ${clones_accum_uniques[i]} ))
fi
done
count_next=0
uniques_next=0
for (( i=0; i < ${#clones_timestamp[@]}; i++)); do
(( count_next += ${clones_count[i]} ))
(( uniques_next += ${clones_uniques[i]} ))
done
(( count_next += count_outdated_next ))
(( uniques_next += uniques_outdated_next ))
(( count_outdated_prev == count_outdated_next && uniques_outdated_prev == uniques_outdated_next && \
count_prev == count_next && uniques_prev == uniques_next )) && [[ \
"$clones_timestamp_next_seq" == "$clones_timestamp_prev_seq" && \
"$clones_count_next_seq" == "$clones_count_prev_seq" && \
"$clones_uniques_next_seq" == "$clones_uniques_prev_seq" ]] && {
print_warning "$0: warning: nothing is changed, no new clones."
exit 255
} >&2
{
echo -n "\
{
\"timestamp\" : \"$current_date_time_utc\",
\"count_outdated\" : $count_outdated_next,
\"uniques_outdated\" : $uniques_outdated_next,
\"count\" : $count_next,
\"uniques\" : $uniques_next,
\"clones\" : ["
for (( i=0; i < ${#clones_timestamp[@]}; i++)); do
(( i )) && echo -n ','
echo ''
echo -n "\
{
\"timestamp\": \"${clones_timestamp[i]}\",
\"count\": ${clones_count[i]},
\"uniques\": ${clones_uniques[i]}
}"
done
(( i )) && echo -e -n "\n "
echo ']
}'
} > $traffic_clones_accum_json || exit $?