#!/bin/bash
# http = httpie.org

# -------------------------
# ETAPA 1 - LOGIN
# -------------------------
# curl -X POST \
#   -H "Content-Type: application/json" \
#   -d '{"username": "idufsc@ufsc.br", "passowrd": "pass"}' \
#   https://dashboards.setic.ufsc.br/api/session

#   http POST https://dashboards.setic.ufsc.br/api/session username=idufsc@ufsc.br password=pass

# {
#   "id": "70dd00e9-aea2-452f-8de1-ead9390e5"
# }


# -------------------------
# ETAPA 2 (opcional) - USER
# -------------------------
# curl -X GET \
#   -H "Content-Type: application/json" \
#   -H "X-Metabase-Session: 70dd00e9-aea2-452f-8de1-ead9390e5" \
#   https://dashboards.setic.ufsc.br/api/user/current

# http https://dashboards.setic.ufsc.br/api/user/current X-Metabase-Session:70dd00e9-aea2-452f-8de1-ead9390e5

# {
#     "common_name": "Crineu Tres",
#     "date_joined": "2016-12-22T14:00:06.442Z",
#     "email": "crineu.tres@ufsc.br",
#     "first_name": "Crineu",
#     "google_auth": false,
#     "group_ids": [
#         1,
#         2
#     ],
#     "id": 50,
#     "is_active": true,
#     "is_qbnewb": false,
#     "is_superuser": true,
#     "last_login": "2020-11-06T01:12:36.80123Z",
#     "last_name": "Tres",
#     "ldap_auth": false,
#     "login_attributes": null,
#     "personal_collection_id": 149,
#     "updated_at": "2020-11-06T01:12:36.80123"
# }



# -------------------------
# ETAPA 3 - DOWNLOAD
# -------------------------

ANO=2020
FORMATO=csv
# FORMATO=json
ARQUIVO=resultados_"${ANO}"."${FORMATO}"


rm -f "${ARQUIVO}"
touch "${ARQUIVO}"
for i in `seq 12`
do
  echo "parameters=[{\"type\":\"category\",\"target\":[\"variable\",[\"template-tag\",\"ano\"]],\"value\":\"${ANO}\"},{\"type\":\"category\",\"target\":[\"variable\",[\"template-tag\",\"mes\"]],\"value\":\"$i\"}]" \
  | http --form POST https://dashboards.setic.ufsc.br/api/card/978/query/csv X-Metabase-Session:70dd00e9-aea2-452f-8de1-ead9390e5a20 \
  | sed '1d' \
  >> "${ARQUIVO}"
done


# echo 'parameters=[{"type":"category","target":["variable",["template-tag","ano"]],"value":"2020"},{"type":"category","target":["variable",["template-tag","mes"]],"value":"6"}]' | \
# curl -v  -X POST \
#   -H "Content-Type: application/json" \
#   -H "X-Metabase-Session: 70dd00e9-aea2-452f-8de1-ead9390e5" \
#   --data-binary @- \
#   https://dashboards.setic.ufsc.br/api/card/978/query/csv
# > result.csv


