Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update existing country/app/host csv logs to add headers #953

Closed
grischard opened this issue Sep 15, 2023 · 1 comment
Closed

Update existing country/app/host csv logs to add headers #953

grischard opened this issue Sep 15, 2023 · 1 comment
Labels
service:tiles The raster map on tile.openstreetmap.org

Comments

@grischard
Copy link
Collaborator

grischard commented Sep 15, 2023

On norbert, run:

#!/bin/bash

# robustness
set -euo pipefail
# Verify that at least one file exists before proceeding for each type
shopt -s nullglob

sudo apt install moreutils

# Function to add headers only if they don't exist
add_header_if_not_exists() {
  local filename=$1
  local header=$2

  # Check if header already exists
  if ! head -n 1 "$filename" | grep -q "^${header}$"; then
    { echo "$header"; cat "$filename"; } | sponge "$filename"
  fi
}

# Add headers to hosts-* files
hosts_files=(hosts-*.csv)
if [ "${#hosts_files[@]}" -gt 0 ]; then
  for filename in "${hosts_files[@]}"; do
  add_header_if_not_exists "$filename" "\"host\",\"tps\",\"tps_miss\""
  done
fi

# Add headers to apps-* files
apps_files=(apps-*.csv)
if [ "${#apps_files[@]}" -gt 0 ]; then
  for filename in "${apps_files[@]}"; do
    add_header_if_not_exists "$filename" "\"app\",\"tps\",\"tps_miss\""
  done
fi

# Add headers to country-* files
country_files=(country-*.csv)
if [ "${#country_files[@]}" -gt 0 ]; then
  for filename in "${country_files[@]}"; do
    add_header_if_not_exists "$filename" "\"country\",\"ips\",\"tps\",\"tps_miss\""
  done
fi
@pnorman pnorman added the service:tiles The raster map on tile.openstreetmap.org label Sep 27, 2023
@Firefishy
Copy link
Member

Done. I made a few modifications + bug fixes to the script. I also retained timestamps.

#!/bin/bash

# robustness
set -euo pipefail

# Verify that input argument (filename) is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

filename=$1

# Function to add headers only if they don't exist and retain mtime
add_header_if_not_exists() {
  local filename=$1
  local header=$2

  # Store current mtime of the file
  local mtime=$(stat -c %y "$filename")

  # Check if header already exists
  if ! head -n 1 "$filename" | grep -q "^${header}$"; then
    { echo "$header"; cat "$filename"; } | sponge "$filename"
    # Restore the original mtime
    touch -d "$mtime" "$filename"
  fi
}

# Determine file type and add appropriate header
case "$filename" in
  hosts-*.csv)
    add_header_if_not_exists "$filename" "\"host\",\"tps\",\"tps_miss\""
    ;;
  apps-*.csv)
    add_header_if_not_exists "$filename" "\"app\",\"tps\",\"tps_miss\""
    ;;
  countries-*.csv)
    add_header_if_not_exists "$filename" "\"country\",\"ips\",\"tps\",\"tps_miss\""
    ;;
  *)
    echo "Unknown file type: $filename"
    exit 1
    ;;
esac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service:tiles The raster map on tile.openstreetmap.org
Projects
None yet
Development

No branches or pull requests

3 participants