<?php

require dirname(dirname(__FILE__)) . "/bootstrap.php";

$batch_size = 2500;  // 10k

$config = parse_ini_file(ROOT . '/../config/backend.ini', true);

if (is_array($config) && isset($config['bootstrap']))
{
    $databaseconfig = $config['bootstrap'];

    if (!isset($databaseconfig['username']))
    {
        $databaseconfig['username'] = get_current_user();
    }

    $dbconn = pg_pconnect(
      sprintf(
          'dbname=%s host=%s port=%s user=%s password=%s',
          $databaseconfig['database'],
          $databaseconfig['host'],
          $databaseconfig['port'],
          $databaseconfig['username'],
          $databaseconfig['password']
      )
    );

    if ($dbconn === false) die('Could not connect DB!');
} else {
  die('Invalid DB Config!');
}

$recv = [];

// Process all tables individually for speed
$res = pg_query($dbconn,
"SELECT table_name
  FROM information_schema.tables
   WHERE table_schema='public'
     AND table_type='BASE TABLE'
     AND table_name LIKE 'packet20______'
  ORDER BY table_name ASC;");

 while (($table = pg_fetch_assoc($res)) !== false)
 {
    echo 'Processing receiver IDs in table ' . $table['table_name'] . "... \r\n";

    $process = true;

    // Select all packets
    while($process)
    {
      $process = false;
      echo "Starting batch...\r\n";

      $start_time = microtime(true);
      pg_query($dbconn,'BEGIN;');
      $packet_res = pg_query($dbconn, "SELECT id, station_id, raw_path FROM {$table['table_name']} WHERE receiver_id IS NULL ORDER BY id ASC LIMIT $batch_size;");

      $total_packets = pg_num_rows($packet_res);
      if ($total_packets== 0)
      {
        echo "No unmodified records found... Skipping {$table['table_name']}.\r\n\n";
        pg_query($dbconn,'ABORT;');
        continue;
      }
      while (($record = pg_fetch_assoc($packet_res)) !== false)
      {
        $process = true;

        $parsed = explode(',', $record['raw_path']);
        if (is_array($parsed) && sizeof($parsed) > 0)
        {
          $receiver_id = 0;

          // Try cache
          if (isset($recv[$parsed[0]]))
          {
            $receiver_id = $recv[$parsed[0]];
          }
          else
          {
            $rcvr_res = pg_query($dbconn, "SELECT * FROM receiver WHERE name = '{$parsed[0]}' LIMIT 1;");
            if (($receiver = pg_fetch_assoc($rcvr_res)) !== false)
            {
              // Receiver found, add to cache
              $recv[$receiver['name']] = $receiver['id'];
              $receiver_id = $receiver['id'];
            }
            else
            {
              // Receiver not found
              $rcvr_insert = pg_query($dbconn, "INSERT INTO receiver (name) VALUES('{$parsed[0]}') LIMIT 1 RETURNING Currval('receiver_id_seq');");
              $receiver_id = pg_fetch_row($rcvr_insert);
              $receiver_id = $receiver_id[0];
              $recv[$parsed[0]] = $receiver_id;
            }
          }

          // Update the packet record
          if ($receiver_id > 0)
          {
            pg_query($dbconn, "UPDATE {$table['table_name']} SET receiver_id = $receiver_id WHERE id = {$record['id']};");
            echo '.';
          }
        }
      }
      pg_query($dbconn,'END;');
      $dbtime = round(microtime(true) - $start_time, 4);
      $tps = $batch_size / $dbtime;
      echo "\r\nBatch completed in {$dbtime} seconds. ($tps tps)\r\n";
    }
    if ($total_packets > 0)
    {
      echo "Table update complete, running full vacuum...";
      pg_query($dbconn, "VACUUM FULL {$table['table_name']};");
      echo "Vacuum complete, all processing for table {$table['table_name']} complete.\r\n\n";
    }
}

echo "Nothing to do...\r\nProcessing complete!\r\n";
