Skip to content

Commit

Permalink
add public explainquery
Browse files Browse the repository at this point in the history
  • Loading branch information
hexylena committed Mar 15, 2024
1 parent 6dd3784 commit cc11a7b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions parts/03-query-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,58 @@ query_expj() {
EOF
}

query_exp_pub() {
date=$(date --rfc-3339=seconds)
sql_query="$1"
explain_json=$(
psql -qAt <<-EOF
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) $sql_query
EOF
)

if [[ "$explain_json" == "" ]]; then
exit 1;
fi

data=$(jq -n \
--arg PLAN "$explain_json" \
--arg TITLE "$USER's gxadmin plan $date" \
--arg QUERY "$sql_query" \
'{"title": $ARGS.named.TITLE, "query": $ARGS.named.QUERY, "plan": $ARGS.named.PLAN}')

echo "⚠️ WARNING ⚠️ "
echo "We will be sending the data to a PUBLIC service that we do not control!"
echo "Are you sure you want to do this? And no user IDs or emails are present in the query?"
echo "[N/y]"
read -r answer
if [[ "$answer" != "y" ]]; then
echo "Aborting"
exit 1;
fi

resp=$(curl --silent 'https://explain.dalibo.com/new.json' \
-X POST \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' \
-H 'Content-Type: application/json;charset=utf-8' \
-H 'Origin: https://explain.dalibo.com' \
-H 'DNT: 1' -H 'Connection: keep-alive' \
-H 'Referer: https://explain.dalibo.com/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
-H 'Accept: application/json' \
--data-raw "$data")

echo "Here is the delete key, I haven't found the URL to make it work yet: "
echo "$resp" | jq .deleteKey
echo "Here is your nice-to-view plan:"
echo "$resp" | jq '"https://explain.dalibo.com/plan/" + .id' -r
}

query_echo() {
echo "$1"
}
Expand Down
1 change: 1 addition & 0 deletions parts/70-look-for.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ look_for() {
iquery ) query_influx "$QUERY" "$query_name" "$fields" "$tags" "$timestamp";;
explainquery ) query_exp "$QUERY";;
explainjsonquery ) query_expj "$QUERY";;
publicexplainquery ) query_exp_pub "$QUERY";;
echoquery ) query_echo "$QUERY";;
# default
* ) usage "Error";;
Expand Down

0 comments on commit cc11a7b

Please sign in to comment.