-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdelete_date_from_reports.sh
executable file
·120 lines (99 loc) · 2.62 KB
/
delete_date_from_reports.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
#!/bin/bash
#
# Removes a particular date JSON from timeseries reports on Google Storage.
#
# Usage:
#
# $ sql/delete_date_from_reports.sh -d YYYY_MM_DD
# $ sql/delete_date_from_reports.sh -d YYYY_MM_DD -l top1k
# $ sql/delete_date_from_reports.sh -d YYYY_MM_DD -l top1k -r "*crux*"
#
# Flags:
#
# -l: Optional name of the report lens to generate, eg "top10k".
#
# -r: Optional name of the report files to generate, eg "*crux*".
#
set -o pipefail
LENS_ARG=""
REPORTS="*"
VERBOSE=0
NO_CHANGES=0
# Read the flags.
while getopts ":nvd:l:r:" opt; do
case "${opt}" in
d)
YYYY_MM_DD=${OPTARG}
;;
v)
VERBOSE=1
;;
n)
NO_CHANGES=1
;;
l)
LENS_ARG=${OPTARG}
;;
r)
REPORTS=${OPTARG}
;;
esac
done
if [[ "${YYYY_MM_DD}" == "" ]]; then
echo "Usage $0 -d 2021_12_01"
exit 1
fi
echo "${YYYY_MM_DD}"
# Run all timeseries queries.
for query in sql/timeseries/$REPORTS.sql; do
if [[ ! -f $query ]]; then
echo "Nothing to do"
continue;
fi
# Extract the metric name from the file path.
metric=$(echo $(basename $query) | cut -d"." -f1)
if [[ "${LENS_ARG}" == "" ]]; then
LENSES=("")
echo "Deleting ${metric} report for base"
elif [[ "${LENS_ARG}" == "ALL" ]]; then
LENSES=("" $(ls sql/lens))
echo "Deleting ${metric} report for base and all lenses"
else
LENSES=("${LENS_ARG}")
echo "Deleting ${metric} report for one lens"
fi
for LENS in "${LENSES[@]}"
do
gs_lens_dir=""
if [[ $LENS != "" ]]; then
gs_lens_dir="$LENS/"
fi
current_contents=""
gs_url="gs://httparchive/reports/$gs_lens_dir${metric}.json"
gsutil ls $gs_url &> /dev/null
if [ $? -eq 0 ]; then
echo "Updating this query: ${metric} for LENS: ${LENS}"
# The file exists, so remove the requested date
current_contents=$(gsutil cat $gs_url)
if [ ${VERBOSE} -eq 1 ]; then
echo "Current JSON:"
echo "${current_contents}\n"
fi
new_contents=$(echo "$current_contents" | jq -c --indent 1 --arg date "${YYYY_MM_DD}" '.[] | select(.date!=$date)' | tr -d '\n' | sed 's/^/[ /' | sed 's/}$/ } ]\n/' | sed 's/}{/ }, {/g')
if [ ${VERBOSE} -eq 1 ]; then
echo "New JSON:"
echo "${new_contents}"
fi
# Make sure the removal succeeded.
if [ $? -eq 0 ] && [ ${NO_CHANGES} -eq 0 ]; then
# Upload the response to Google Storage.
echo "Uploading new file to Google Storage"
echo $new_contents \
| gsutil -h "Content-Type:application/json" cp - $gs_url
else
echo $new_contents >&2
fi
fi
done
done
echo -e "Done"