| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # This file is meant to be SOURCED |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | function track(){ |
| 18 | # Tracks events by sending HTTP GET requests with query strings to a web server |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 19 | # Input: |
| 20 | # - First argument (mandatory): event name |
| 21 | # - Rest of arguments: |
| 22 | # - If they exist, they correspond to tuples of operation-value-comment-tags |
| 23 | # - operation: particular operation in an event |
| 24 | # - value: particular value for an operation |
| 25 | # - comment |
| 26 | # - none will be passed when empty |
| 27 | # - will be parsed to replace spaces by underscores |
| 28 | # - tags |
| 29 | # - none will be passed when empty |
| 30 | # - will be parsed to replace spaces by ~ |
| 31 | # - If no arguments: |
| 32 | # - operation: like event |
| 33 | # - value: "" |
| 34 | # - comment: "" |
| 35 | # - tags: "" |
| 36 | # Output: |
| 37 | # Sends as many HTTP requests as operations with the following query string |
| 38 | # "&{OSM_TRACK_INSTALLATION_ID}&{TIMESTAMP}&{EVENT}&{OPERATION}&{VALUE}&{COMMENT}&{TAGS}" |
| 39 | # |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 40 | |
| 41 | if [ $# -lt 1 ]; then |
| 42 | echo "Unexpected error in track function. At least 1 arg is expected: event" |
| 43 | return 1 |
| 44 | fi |
| 45 | |
| 46 | osm_track_event_name=$1 |
| 47 | |
| 48 | ctime=`date +%s` |
| 49 | |
| 50 | query_string="" |
| 51 | query_string="${query_string}&installation_id=${OSM_TRACK_INSTALLATION_ID}" |
| 52 | query_string="${query_string}&local_ts=${ctime}" |
| 53 | query_string="${query_string}&event=${osm_track_event_name}" |
| 54 | |
| 55 | shift 1 |
| 56 | if [ $# -eq 0 ]; then |
| 57 | operation="${osm_track_event_name}" |
| 58 | value="" |
| 59 | comment="" |
| 60 | tags="" |
| 61 | final_query_string="${query_string}" |
| 62 | final_query_string="${final_query_string}&operation=${operation}" |
| 63 | final_query_string="${final_query_string}&value=${value}" |
| 64 | final_query_string="${final_query_string}&comment=${comment}" |
| 65 | final_query_string="${final_query_string}&tags=${tags}" |
| 66 | url="https://osm.etsi.org/InstallLog.php?${final_query_string}" |
| 67 | echo "Track $osm_track_event_name $operation: ${url}" |
| garciadeblas | e572aab | 2022-01-14 11:19:16 +0100 | [diff] [blame] | 68 | LANG=C wget -q -O /dev/null "$url" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 69 | else |
| 70 | while (( "$#" > 0 )); do |
| 71 | operation="${1:-${osm_track_event_name}}" |
| 72 | shift 1 |
| 73 | value="${1:-}" |
| garciadeblas | 4d89c37 | 2021-11-25 11:57:18 +0100 | [diff] [blame] | 74 | value="${value// /_}" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 75 | shift 1 |
| 76 | comment="${1:-}" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 77 | comment="${comment// /_}" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 78 | shift 1 |
| garciadeblas | 4d89c37 | 2021-11-25 11:57:18 +0100 | [diff] [blame] | 79 | tags="${1:-}" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 80 | tags="${tags//,/\~}" |
| garciadeblas | 4d89c37 | 2021-11-25 11:57:18 +0100 | [diff] [blame] | 81 | shift 1 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 82 | [ "$value" == "none" ] && value="" |
| 83 | [ "$comment" == "none" ] && comment="" |
| 84 | [ "$tags" == "none" ] && tags="" |
| 85 | final_query_string="${query_string}" |
| 86 | final_query_string="${final_query_string}&operation=${operation}" |
| 87 | final_query_string="${final_query_string}&value=${value}" |
| 88 | final_query_string="${final_query_string}&comment=${comment}" |
| 89 | final_query_string="${final_query_string}&tags=${tags}" |
| 90 | url="https://osm.etsi.org/InstallLog.php?${final_query_string}" |
| 91 | echo "Track $osm_track_event_name $operation: ${url}" |
| garciadeblas | e572aab | 2022-01-14 11:19:16 +0100 | [diff] [blame] | 92 | LANG=C wget -q -O /dev/null "$url" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 93 | done |
| 94 | fi |
| 95 | return 0 |
| 96 | } |