#!/usr/bin/env perl use strict; use warnings; use Mojo::File qw(curfile); use lib curfile->dirname->sibling('lib')->to_string; use FAPG::DAQ::Dashboard; my $app = FAPG::DAQ::Dashboard->new; my $weather_config = $app->config->{weather} // {}; if ( !$weather_config->{enabled} ) { print "Weather ingestion is disabled in dashboard.yml\n"; exit 0; } my ( $fetched, $stored ); my $success = eval { my $rows = $app->weather_fetcher->fetch; $fetched = scalar @$rows; $stored = $app->weather->store_batch($rows) // 0; 1; }; if ( !$success ) { my $error = $@ || 'Unknown weather ingestion failure'; chomp $error; print STDERR "$error\n"; exit 1; } print "Weather ingestion fetched $fetched points and stored $stored points\n"; # Backfill history if the database has less than ~365 days of data # or if the extended columns (solar, cloud, soil, pressure) are empty. my $one_year_ago = time - ( 365 * 24 * 60 * 60 ); my $oldest = $app->weather->oldest_epoch; my $needs_history = !defined $oldest || $oldest > $one_year_ago; my $needs_extended = !$app->weather->has_extended_data; if ( $needs_history || $needs_extended ) { require POSIX; my $target_start = $one_year_ago; my $end_epoch = $needs_extended ? time - ( 7 * 24 * 60 * 60 ) : ( defined $oldest ? $oldest - 3600 : time - ( 7 * 24 * 60 * 60 ) ); my $chunk_days = 90; print "Backfilling weather history" . ( $needs_extended ? " (extended columns)" : "" ) . "...\n"; my $cursor = $target_start; while ( $cursor < $end_epoch ) { my $chunk_end = $cursor + ( $chunk_days * 24 * 60 * 60 ); $chunk_end = $end_epoch if $chunk_end > $end_epoch; my $start_date = POSIX::strftime( '%Y-%m-%d', gmtime($cursor) ); my $end_date = POSIX::strftime( '%Y-%m-%d', gmtime($chunk_end) ); my $ok = eval { my $rows = $app->weather_fetcher->fetch_history( $start_date, $end_date ); my $n = $app->weather->store_batch($rows) // 0; print " $start_date to $end_date: $n points\n"; 1; }; if ( !$ok ) { my $err = $@ || 'Unknown error'; chomp $err; print STDERR " Backfill chunk $start_date failed: $err\n"; last; } $cursor = $chunk_end + ( 24 * 60 * 60 ); sleep 1; # Be polite to the archive API } print "Backfill complete.\n"; } exit 0;