]> git.ais-ucla.org Git - stats.ais-ucla.org.git/commitdiff
split up leaderboards
authorChristopher Milan <chrismilan@ucla.edu>
Mon, 17 Mar 2025 06:00:31 +0000 (23:00 -0700)
committerChristopher Milan <chrismilan@ucla.edu>
Mon, 17 Mar 2025 06:00:31 +0000 (23:00 -0700)
leader.cgi

index a6be70c37bca84210ab184c9adcae4c55d9fd92a..d356c0e04ebe60efca410590dc612c08c99544be 100755 (executable)
@@ -4,6 +4,8 @@ use warnings;
 use CGI qw(:standard);
 use DBI;
 
+$ENV{TZ} = 'America/Los_Angeles';
+
 print header(-type => 'text/html', -charset => 'utf-8');
 print start_html(-title => "GPU Leaderboard",
                  -style => {-src => 'index.css'});
@@ -14,23 +16,105 @@ my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", "", "", {
   AutoCommit => 1
 }) or die $DBI::errstr;
 
-my $query = qq{
+my $query_today = qq{
+  SELECT u.name, SUM(g.util * (1.0/60.0)) AS gpu_hours
+  FROM usage g
+  JOIN users u ON g.uid = u.uid
+  WHERE u.time >= datetime('now', 'start of day')
+  GROUP BY u.uid
+  ORDER BY gpu_hours DESC;
+};
+
+my $query_week = qq{
   SELECT u.name, SUM(g.util * (1.0/60.0)) AS gpu_hours
   FROM usage g
   JOIN users u ON g.uid = u.uid
+  WHERE u.time >= datetime('now', 'start of day', '-' || strftime('%w', 'now') || ' days')
   GROUP BY u.uid
   ORDER BY gpu_hours DESC;
 };
 
-my $sth = $dbh->prepare($query);
-$sth->execute();
+my $query_month = qq{
+  SELECT u.name, SUM(g.util * (1.0/60.0)) AS gpu_hours
+  FROM usage g
+  JOIN users u ON g.uid = u.uid
+  WHERE u.time >= datetime('now', 'start of month')
+  GROUP BY u.uid
+  ORDER BY gpu_hours DESC;
+};
+
+my $query_alltime = qq{
+  SELECT u.name, SUM(g.util * (1.0/60.0)) AS gpu_hours
+  FROM usage g
+  JOIN users u ON g.uid = u.uid
+  GROUP BY u.uid
+  ORDER BY gpu_hours DESC;
+};
+
+my $sth_today = $dbh->prepare($query_today);
+$sth_today->execute();
+
+my $sth_week = $dbh->prepare($query_week);
+$sth_week->execute();
+
+my $sth_month = $dbh->prepare($query_month);
+$sth_month->execute();
+
+my $sth_alltime = $dbh->prepare($query_alltime);
+$sth_alltime->execute();
 
 print h1("GPU Leaderboard");
 
+print "<table border='0'><tr>";
+
+# Today's leaderboard
+print "<td valign='top'>";
+print "<h2>Today</h2>";
+print "<table border='1' cellspacing='0' cellpadding='5'>";
+print "<tr><th>Name</th><th>GPU Hours</th></tr>";
+while (my $row = $sth_today->fetchrow_hashref) {
+    print "<tr>";
+    print "<td>$row->{name}</td>";
+    print "<td>" . sprintf("%.2f", $row->{gpu_hours} || 0) . "</td>";
+    print "</tr>";
+}
+print "</table>";
+print "</td>";
+
+# This Week's leaderboard
+print "<td valign='top'>";
+print "<h2>This Week</h2>";
 print "<table border='1' cellspacing='0' cellpadding='5'>";
 print "<tr><th>Name</th><th>GPU Hours</th></tr>";
+while (my $row = $sth_week->fetchrow_hashref) {
+    print "<tr>";
+    print "<td>$row->{name}</td>";
+    print "<td>" . sprintf("%.2f", $row->{gpu_hours} || 0) . "</td>";
+    print "</tr>";
+}
+print "</table>";
+print "</td>";
 
-while (my $row = $sth->fetchrow_hashref) {
+# This Month's leaderboard
+print "<td valign='top'>";
+print "<h2>This Month</h2>";
+print "<table border='1' cellspacing='0' cellpadding='5'>";
+print "<tr><th>Name</th><th>GPU Hours</th></tr>";
+while (my $row = $sth_month->fetchrow_hashref) {
+    print "<tr>";
+    print "<td>$row->{name}</td>";
+    print "<td>" . sprintf("%.2f", $row->{gpu_hours} || 0) . "</td>";
+    print "</tr>";
+}
+print "</table>";
+print "</td>";
+
+# All time leaderboard
+print "<td valign='top'>";
+print "<h2>All time</h2>";
+print "<table border='1' cellspacing='0' cellpadding='5'>";
+print "<tr><th>Name</th><th>GPU Hours</th></tr>";
+while (my $row = $sth_alltime->fetchrow_hashref) {
     print "<tr>";
     print "<td>$row->{name}</td>";
     print "<td>" . sprintf("%.2f", $row->{gpu_hours} || 0) . "</td>";
@@ -38,6 +122,10 @@ while (my $row = $sth->fetchrow_hashref) {
 }
 
 print "</table>";
+print "</td>";
+
+
+print "</tr></table>";
 
 print <<'END';
 <hr />