| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | region=eu-west-1 |
| 6 | |
| 7 | from_date_incl="$1" |
| 8 | to_date_excl="$2" |
| 9 | |
| 10 | [[ -n $from_date_incl ]] |
| 11 | [[ -n $to_date_excl ]] |
| 12 | |
| 13 | run_query() { |
| 14 | local name="$1" |
| 15 | local query="$2" |
| 16 | |
| 17 | res=$(aws athena start-query-execution \ |
| 18 | --region $region \ |
| 19 | --result-configuration "OutputLocation=s3://nixos-athena/ingestion/$name/" \ |
| 20 | --query-string "$query") |
| 21 | |
| 22 | execution_id="$(printf "%s" "$res" | jq -r -e .QueryExecutionId)" |
| 23 | [[ -n $execution_id ]] |
| 24 | |
| 25 | echo "Started query $name as $execution_id." |
| 26 | |
| 27 | printf "Waiting..." |
| 28 | while true; do |
| 29 | res="$(aws athena get-query-execution --region $region --query-execution-id "$execution_id")" |
| 30 | status="$(printf %s "$res" | jq -r -e .QueryExecution.Status.State)" |
| 31 | if [[ $status == RUNNING || $status == QUEUED ]]; then |
| 32 | printf "." |
| 33 | sleep 1 |
| 34 | continue |
| 35 | fi |
| 36 | if [[ $status == SUCCEEDED ]]; then |
| 37 | printf " done.\n" |
| 38 | break |
| 39 | fi |
| 40 | printf "\nFailed: %s (%s)\n" "$status" "$res" |
| 41 | exit 1 |
| 42 | done |
| 43 | } |
| 44 | |
| 45 | run_query fill-urls \ |
| 46 | " |
| 47 | insert into urls |
| 48 | with requests2 as (select *, date_format(date_parse(timestamp, '%Y-%m-%dT%T+0000'), '%Y-%m-%d') as day from requests) |
| 49 | select url, count(*) as nr, sum(response_body_size) as total_bytes, sum(elapsed_usec) as total_elapsed, host, day |
| 50 | from requests2 |
| 51 | where (response_status >= '200' and response_status <= '399') and (day >= '$from_date_incl' and day < '$to_date_excl') |
| 52 | group by host, day, url; |
| 53 | " |
| 54 | |
| 55 | run_query fill-nix-cache-info \ |
| 56 | " |
| 57 | insert into nix_cache_info |
| 58 | with requests2 as (select *, date_format(date_parse(timestamp, '%Y-%m-%dT%T+0000'), '%Y-%m-%d') as day from requests) |
| 59 | select count(*) as nr, asn, geo_country, geo_region, request_user_agent, day |
| 60 | from requests2 |
| 61 | where host = 'cache.nixos.org' and url = '/nix-cache-info' and (day >= '$from_date_incl' and day < '$to_date_excl') |
| 62 | group by day, asn, geo_country, geo_region, request_user_agent; |
| 63 | " |
| 64 | |
| 65 | run_query fill-clients \ |
| 66 | " |
| 67 | insert into clients |
| 68 | with requests2 as (select *, date_format(date_parse(timestamp, '%Y-%m-%dT%T+0000'), '%Y-%m-%d') as day from requests) |
| 69 | select asn, geo_country, geo_region, count(*) as nr, sum(response_body_size) as total_bytes, sum(elapsed_usec) as total_elapsed, host, day |
| 70 | from requests2 |
| 71 | where (day >= '$from_date_incl' and day < '$to_date_excl') |
| 72 | group by host, day, asn, geo_country, geo_region; |
| 73 | " |