A live bird collage card for your Home Assistant dashboard.
New feat : Disk usage summary (#318)
Alexandre465e9739ce6e
diff --git a/scripts/advanced.php b/scripts/advanced.phpindex 7a077afa..dab63399 100644--- a/scripts/advanced.php+++ b/scripts/advanced.php@@ -5,9 +5,18 @@ error_reporting(E_ERROR);require_once "scripts/common.php";$home = get_home();$config = get_config();+$user = get_user();ensure_authenticated();+if (isset($_GET['run_species_count'])) {+ echo "<script>";+ $output = shell_exec("sudo -u $user ".$home."/BirdNET-Pi/scripts/disk_species_count.sh 2>&1");+ $escaped_output = htmlspecialchars($output, ENT_QUOTES | ENT_SUBSTITUTE);+ echo "alert(`$escaped_output`);";+ echo "</script>";+ }+if(isset($_GET['submit'])) {$contents = file_get_contents('/etc/birdnet/birdnet.conf');$restart_livestream = false;@@ -307,14 +316,15 @@ $newconfig = get_config();<label for="purge_threshold">Purge Threshold (Disk Used %):</label><input name="purge_threshold" type="number" style="width:6em;" min="20" max="99" step="1" value="<?php print($newconfig['PURGE_THRESHOLD']);?>"/><p>Defines how full the disk should be before the purge operations occur.<br>Note: This variable is still active if Keep is set. This means that the servies will be stopped at the purge threshold.</p><br>- <label for="max_files_species">Amount of files to keep for each species :</label>+ <label for="max_files_species">Number of files to keep for each species :</label><input name="max_files_species" type="number" style="width:6em;" min="0" step="1" value="<?php print($newconfig['MAX_FILES_SPECIES']);?>"/></td></tr><tr><td>- If different than 0 (keep all), defines the maximum number of files to be kept for each species, with priority give to files with highest confidence.- This value does not take into account the last 7 days (protected by default).+ If different than 0 (keep all), defines the number of files to keep for each species, with priority given to files with higher confidence. This value does not include files from the last 7 days, these new files are protected against auto-deletion.</td></tr><tr><td>Note only the spectrogram and audio files are deleted, the obsevation data remains in the database.The files protected through the "lock" icon are also not affected.+ <br>+ <button type="submit" name="run_species_count" value="1" onclick="{this.innerHTML = 'Loading ... please wait.';this.classList.add('disabled')}"><i>[Click here for disk usage summary]</i></button></td></tr></table><br><table class="settingstable"><tr><td>diff --git a/scripts/disk_species_count.sh b/scripts/disk_species_count.shnew file mode 100755index 00000000..2f15a7dd--- /dev/null+++ b/scripts/disk_species_count.sh@@ -0,0 +1,75 @@+#!/bin/bash++# Get default values+source /etc/birdnet/birdnet.conf+base_dir="$(readlink -f "$HOME/BirdSongs/Extracted/By_Date")"+cd "$base_dir" || exit 1++# Function to format numbers to k if ≥1000+format_k() {+ local value=$1+ if [ "$value" -ge 1000 ]; then+ awk -v v="$value" 'BEGIN { printf "%.1fk", v/1000 }'+ else+ echo "$value"+ fi+}++# Get bird names from the database+bird_names=$(sqlite3 -readonly "$HOME"/BirdNET-Pi/scripts/birds.db <<EOF+.headers off+.mode list+SELECT DISTINCT Com_Name FROM detections;+EOF+)++# Sanitize names for folder matching+sanitized_names="$(echo "$bird_names" | tr ' ' '_' | tr -d "'" | grep '[[:alnum:]]')"+sanitized_names=$(echo "$sanitized_names" | sed 's/_*$//')++# Count species+species_count=$(echo "$sanitized_names" | wc -l)+total_file_count=0++# Temp files+data_file=$(mktemp)+output_file=$(mktemp)++# Loop through each species+while read -r species; do+ # Count total files+ total=$(find */"$species" -type f -name "*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*.*" \+ -not -iname "*.png" 2>/dev/null | wc -l)+ total_file_count=$((total_file_count + total))++ # Format total+ total_display=$(format_k "$total")++ # Clean species name for display+ species_display=$(echo "$species" | tr '_' ' ')++ # Save padded sort key + display line+ printf "%05d %s : %s\n" "$total" "$total_display" "$species_display" >> "$data_file"+done <<<"$sanitized_names"++# Avoid TERM error if not running in a terminal+[ -t 1 ] && clear++# Build final output+{+ echo "BirdSongs stored on your drive"+ echo " "+ echo "Location : $base_dir: "+ echo "Free space : $(df -h "$base_dir" | awk 'NR==2 {print $4}' | sed 's/G/ GB/; s/M/ MB/; s/K/ KB/')"+ echo "Total species : $species_count"+ echo "Total files : $(format_k "$total_file_count")"+ echo "Total size : $(du -sh . | sed 's/G/ GB/; s/M/ MB/; s/K/ KB/' | cut -f1)"+ echo " "+ sort -r "$data_file" | sed 's/^[0-9]* //'+} > "$output_file"++# Show results+cat "$output_file"++# Clean up+rm -f "$data_file" "$output_file"