<commit_before>#!/usr/bin/env perl
use strict;
use warnings;
use JSON::XS;
use LWP::UserAgent;
use POSIX qw/floor strftime/;
use DateTime;
use CGI "meta";
use File::Temp qw/tempfile/;
use Config::Simple;
use DBD::mysql;
use Time::HiRes qw/gettimeofday tv_interval/;
use encoding qw/utf8/;

my $t0 = [gettimeofday];

my $REFRESH = 60;
my $WWWFOLDER = "/var/www/rift/"; # include trailing slash
my $TIMEOUT = 5; # Timeout per HTTP request to the Rift server (one per shard)
my $CONFIGFILE = "/home/alucard/rift/ret.conf";

# DO NOT EDIT BELOW THIS LINE
my $cfg = new Config::Simple($CONFIGFILE) or die "Failed to read config. $!\n";
my $usesql = $cfg->param('SQLENABLE');
my ($dsn, $dbh, $sth);

# Set up SQL connection
if ((defined $usesql) && (lc $usesql eq "true") && (defined $cfg->param('SQLDB')) && (defined $cfg->param('SQLLOC')) && (defined $cfg->param('SQLUSER')) && (defined $cfg->param('SQLPASS'))) {
  $dsn = "DBI:mysql:database=" . $cfg->param('SQLDB') . ";host=" . $cfg->param('SQLLOC') . ";";
  $dbh = DBI->connect($dsn, $cfg->param('SQLUSER'), $cfg->param('SQLPASS'), { mysql_enable_utf8 => 1, });
  if (!defined $dbh) {
    print STDERR "Error connecting to databse. " . $DBI::errstr . "\n";
    undef $usesql;
  }
}
else { undef $usesql; }

# Get & store event definitions
my %eventsbyname;
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name FROM eventnames");
  $sth->execute() or die "Can't get event names. $DBI::errstr\n";

  while (my $row = $sth->fetchrow_hashref()) {
    $eventsbyname{$row->{'name'}} = $row->{'id'};
  }
}
# Get & store zone definitions
my %zonesbyname;
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name FROM zones");
  $sth->execute() or die "Can't get zone names. $DBI::errstr\n";

  while (my $row = $sth->fetchrow_hashref()) {
    $zonesbyname{$row->{'name'}} = $row->{'id'};
  }
}
# Get & store shards to reference by name and ID
my (%eubyid, %nabyid, %eubyname, %nabyname, %pvps);
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name, dc, pvp FROM shards");
  my $success = $sth->execute();
  if (!$success) { print STDERR "Can't get shards. $DBI::errstr\n"; }
  else {
    while (my $row = $sth->fetchrow_hashref()) {
      if ($row->{'dc'} eq "eu") {
        $eubyid{$row->{'id'}} = $row->{'name'};
        $eubyname{$row->{'name'}} = $row->{'id'};
      }
      elsif ($row->{'dc'} eq "na") {
        $nabyid{$row->{'id'}} = $row->{'name'};
        $nabyname{$row->{'name'}} = $row->{'id'};
      }
# Keep track of PvP servers for later in/exclusion from average times
      $pvps{$row->{'name'}} = $row->{'pvp'};
    }
  }
}
# Hard code shards for fallback
# Only if SQL not in use
if (!%nabyid) {
  %nabyid = (
      1704 => "Deepwood",
      1707 => "Faeblight",
      1702 => "Greybriar",
      1721 => "Hailol",
      1708 => "Laethys",
      1701 => "Seastone",
      1706 => "Wolfsbane",
      );
  %nabyname = reverse %nabyid;
}
if (!%eubyid) {
  %eubyid = (
      2702 => "Bloodiron",
      2714 => "Brisesol",
      2711 => "Brutwacht",
      2721 => "Gelidra",
      2741 => "Typhiria",
      2722 => "Zaviel",
      );
  %eubyname = reverse %eubyid;
}
my %nadc = (
    url => "http://chat-us.riftgame.com:8080/chatservice/zoneevent/list?shardId=",
    shortname => "na",
    shardsbyid => \%nabyid,
    shardsbyname => \%nabyname,
    tz => "America/Los_Angeles",
    );
my %eudc = (
    url => "http://chat-eu.riftgame.com:8080/chatservice/zoneevent/list?shardId=",
    shortname => "eu",
    shardsbyid => \%eubyid,
    shardsbyname => \%eubyname,
    tz => "GMT",
    );

my @dcs = ();
push(@dcs, \%nadc);
push(@dcs, \%eudc);

# REALLY DO NOT EDIT BELOW THIS LINE

# Set up
my $ua = LWP::UserAgent->new(
    agent => 'Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16',
    timeout => $TIMEOUT,
    );

my $json = JSON::XS->new();

# Get last known state of events from SQL
my $laststate = ();
if ($usesql) {
  $sth = $dbh->prepare("SELECT * FROM events WHERE endtime = 0");
  $sth->execute() or die $DBI::errstr;
  $laststate = $sth->fetchall_arrayref({});
}

# Go through each DC and construct web pages
foreach my $dc (@dcs) {
# Safely use a temp file (moved later)
  my ($temp, $filename) = tempfile("retXXXXX", TMPDIR => 1, UNLINK => 0);
  binmode $temp, ':utf8';
  my $html = new CGI;

  print $temp $html->start_html(
      -title => "YARET",
      -encoding => 'UTF-8',
      -style => { -src => 'ret.css'},
      -head => meta({-http_equiv => 'Refresh',
        -content => "$REFRESH"}),
      );
  print $temp "<h2 class=\"normal\">Yet Another Rift Event Tracker</h2>";

# Insert links to other datacenter's pages
  print $temp "<p class=\"normal\">Switch to:\n";
  foreach my $otherdc (@dcs) {
    if ($dc != $otherdc) {
      print $temp '<a href="'. $otherdc->{"shortname"} . '.html">' . uc($otherdc->{"shortname"}) . "</a> ";
    }
    else { print $temp uc($dc->{"shortname"}) . " "; }
  }
  print $temp "</p>\n";

# Construct table
  print $temp '<span class="caption">' . "\n";
  print $temp '<div><span class="relevant">Nightmare Tide content</span>';
  print $temp '<br /><span class="oldnews olddesc">Old content (SL + Vanilla)</span></div>';
  print $temp '<br /><div class="new">Newly started event</div> <div class="nearend">Nearing its average run time</div>';
  print $temp '<br /><div class="behemoth">Bloodfire Behemoth</div> <div class="pony">Unicorns</div> <div class="unstable">Unstable Artifact</div>';
  print $temp '</span>' . "\n";

  my @headers = ("Shard", "Zone", "Event Name", "Elapsed Time");
  print $temp "<table class='ret'>";

  print $temp "<thead><tr>\n";
  foreach my $header (@headers) {
    print $temp "<th>$header</th>";<commit_msg>Add caption for Fae Yule<commit_after>#!/usr/bin/env perl
use strict;
use warnings;
use JSON::XS;
use LWP::UserAgent;
use POSIX qw/floor strftime/;
use DateTime;
use CGI "meta";
use File::Temp qw/tempfile/;
use Config::Simple;
use DBD::mysql;
use Time::HiRes qw/gettimeofday tv_interval/;
use encoding qw/utf8/;

my $t0 = [gettimeofday];

my $REFRESH = 60;
my $WWWFOLDER = "/var/www/rift/"; # include trailing slash
my $TIMEOUT = 5; # Timeout per HTTP request to the Rift server (one per shard)
my $CONFIGFILE = "/home/alucard/rift/ret.conf";

# DO NOT EDIT BELOW THIS LINE
my $cfg = new Config::Simple($CONFIGFILE) or die "Failed to read config. $!\n";
my $usesql = $cfg->param('SQLENABLE');
my ($dsn, $dbh, $sth);

# Set up SQL connection
if ((defined $usesql) && (lc $usesql eq "true") && (defined $cfg->param('SQLDB')) && (defined $cfg->param('SQLLOC')) && (defined $cfg->param('SQLUSER')) && (defined $cfg->param('SQLPASS'))) {
  $dsn = "DBI:mysql:database=" . $cfg->param('SQLDB') . ";host=" . $cfg->param('SQLLOC') . ";";
  $dbh = DBI->connect($dsn, $cfg->param('SQLUSER'), $cfg->param('SQLPASS'), { mysql_enable_utf8 => 1, });
  if (!defined $dbh) {
    print STDERR "Error connecting to databse. " . $DBI::errstr . "\n";
    undef $usesql;
  }
}
else { undef $usesql; }

# Get & store event definitions
my %eventsbyname;
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name FROM eventnames");
  $sth->execute() or die "Can't get event names. $DBI::errstr\n";

  while (my $row = $sth->fetchrow_hashref()) {
    $eventsbyname{$row->{'name'}} = $row->{'id'};
  }
}
# Get & store zone definitions
my %zonesbyname;
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name FROM zones");
  $sth->execute() or die "Can't get zone names. $DBI::errstr\n";

  while (my $row = $sth->fetchrow_hashref()) {
    $zonesbyname{$row->{'name'}} = $row->{'id'};
  }
}
# Get & store shards to reference by name and ID
my (%eubyid, %nabyid, %eubyname, %nabyname, %pvps);
if ($usesql) {
  $sth = $dbh->prepare("SELECT id, name, dc, pvp FROM shards");
  my $success = $sth->execute();
  if (!$success) { print STDERR "Can't get shards. $DBI::errstr\n"; }
  else {
    while (my $row = $sth->fetchrow_hashref()) {
      if ($row->{'dc'} eq "eu") {
        $eubyid{$row->{'id'}} = $row->{'name'};
        $eubyname{$row->{'name'}} = $row->{'id'};
      }
      elsif ($row->{'dc'} eq "na") {
        $nabyid{$row->{'id'}} = $row->{'name'};
        $nabyname{$row->{'name'}} = $row->{'id'};
      }
# Keep track of PvP servers for later in/exclusion from average times
      $pvps{$row->{'name'}} = $row->{'pvp'};
    }
  }
}
# Hard code shards for fallback
# Only if SQL not in use
if (!%nabyid) {
  %nabyid = (
      1704 => "Deepwood",
      1707 => "Faeblight",
      1702 => "Greybriar",
      1721 => "Hailol",
      1708 => "Laethys",
      1701 => "Seastone",
      1706 => "Wolfsbane",
      );
  %nabyname = reverse %nabyid;
}
if (!%eubyid) {
  %eubyid = (
      2702 => "Bloodiron",
      2714 => "Brisesol",
      2711 => "Brutwacht",
      2721 => "Gelidra",
      2741 => "Typhiria",
      2722 => "Zaviel",
      );
  %eubyname = reverse %eubyid;
}
my %nadc = (
    url => "http://chat-us.riftgame.com:8080/chatservice/zoneevent/list?shardId=",
    shortname => "na",
    shardsbyid => \%nabyid,
    shardsbyname => \%nabyname,
    tz => "America/Los_Angeles",
    );
my %eudc = (
    url => "http://chat-eu.riftgame.com:8080/chatservice/zoneevent/list?shardId=",
    shortname => "eu",
    shardsbyid => \%eubyid,
    shardsbyname => \%eubyname,
    tz => "GMT",
    );

my @dcs = ();
push(@dcs, \%nadc);
push(@dcs, \%eudc);

# REALLY DO NOT EDIT BELOW THIS LINE

# Set up
my $ua = LWP::UserAgent->new(
    agent => 'Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16',
    timeout => $TIMEOUT,
    );

my $json = JSON::XS->new();

# Get last known state of events from SQL
my $laststate = ();
if ($usesql) {
  $sth = $dbh->prepare("SELECT * FROM events WHERE endtime = 0");
  $sth->execute() or die $DBI::errstr;
  $laststate = $sth->fetchall_arrayref({});
}

# Go through each DC and construct web pages
foreach my $dc (@dcs) {
# Safely use a temp file (moved later)
  my ($temp, $filename) = tempfile("retXXXXX", TMPDIR => 1, UNLINK => 0);
  binmode $temp, ':utf8';
  my $html = new CGI;

  print $temp $html->start_html(
      -title => "YARET",
      -encoding => 'UTF-8',
      -style => { -src => 'ret.css'},
      -head => meta({-http_equiv => 'Refresh',
        -content => "$REFRESH"}),
      );
  print $temp "<h2 class=\"normal\">Yet Another Rift Event Tracker</h2>";

# Insert links to other datacenter's pages
  print $temp "<p class=\"normal\">Switch to:\n";
  foreach my $otherdc (@dcs) {
    if ($dc != $otherdc) {
      print $temp '<a href="'. $otherdc->{"shortname"} . '.html">' . uc($otherdc->{"shortname"}) . "</a> ";
    }
    else { print $temp uc($dc->{"shortname"}) . " "; }
  }
  print $temp "</p>\n";

# Construct table
  print $temp '<span class="caption">' . "\n";
  print $temp '<div><span class="relevant">Nightmare Tide content</span>';
  print $temp '<br /><span class="oldnews olddesc">Old content (SL + Vanilla)</span></div>';
  print $temp '<br /><div class="new">Newly started event</div> <div class="nearend">Nearing its average run time</div>';
  print $temp '<br /><div class="behemoth">Bloodfire Behemoth</div> <div class="unstable">Unstable Artifact</div> <div class="pony">Unicorns</div> <div class="yule">Fae Yule</div>';
  print $temp '</span>' . "\n";

  my @headers = ("Shard", "Zone", "Event Name", "Elapsed Time");
  print $temp "<table class='ret'>";

  print $temp "<thead><tr>\n";
  foreach my $header (@headers) {
    print $temp "<th>$header</th>";
{'fraction_non_alphanumeric': 0.21331927604990336, 'fraction_numerical': 0.016341591987348445, 'mean_word_length': 3.4517012123582322, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '279011', 'n_tokens_mistral': 4490, 'n_tokens_neox': 4324, 'n_words': 1102}