Error: You don't have access to administer databases." unless (LJ::check_priv($dbh, $remote, "siteadmin", "dbweightview")); my $can_save = LJ::check_priv($dbh, $remote, "siteadmin", "dbweightchange"); my $view = $FORM{'view'} eq "role" ? "role" : "host"; my %dbinfo; # dbid my %slaves; # dbid my $sth; $sth = $dbh->prepare("SELECT dbid, name, fdsn, masterid FROM dbinfo"); $sth->execute; while ($_ = $sth->fetchrow_hashref) { next unless $_->{'dbid'}; $dbinfo{$_->{'dbid'}} = $_; push @{$slaves{$_->{'masterid'}}}, $_->{'dbid'}; } my %weights; my %role; my %roletweight; $sth = $dbh->prepare("SELECT dbid, role, norm, curr FROM dbweights"); $sth->execute; while ($_ = $sth->fetchrow_hashref) { next unless defined $dbinfo{$_->{'dbid'}}; $weights{$_->{'dbid'}}->{$_->{'role'}} = $_; push @{$role{$_->{'role'}}}, $_->{'dbid'}; $roletweight{$_->{'role'}} += $_->{'curr'}; } my $ret; my $p = sub { $ret .= shift; }; my $status; if ($can_save && defined $FORM{'action:save'}) { return "Error: Not a POST request." unless LJ::did_post(); my $curr_changed = 0; foreach my $k (keys %FORM) { next unless $k =~ /^set-(\d+)-(\w+?)-(\w+)$/; my ($sid, $role, $what) = ($1, $2, $3); next unless $what eq "norm" or $what eq "curr"; next unless defined $weights{$sid}; next unless defined $weights{$sid}->{$role}; my $val = $FORM{$k}+0; my $old = $weights{$sid}->{$role}->{$what}; next if $val == $old; $dbh->do("UPDATE dbweights SET $what=$val WHERE ". "dbid=$sid AND role='$role'"); $weights{$sid}->{$role}->{$what} = $val; if ($what eq "curr") { $curr_changed = 1; $roletweight{$role} += $val - $old; } } if ($curr_changed) { if ($LJ::DBCONFIG_WRITE_CONFIG) { my $good = 1; my $newcfg = "# auto-generated config from /admin/dbadmin.bml. changes will be overwritten!\n"; $newcfg .= "%LJ::DBINFO = (\n"; foreach my $id (sort keys %dbinfo) { next unless $id > 0; my $dbinf = $dbinfo{$id}; my $name = $dbinf->{'name'}; if ($dbinf->{'masterid'} == 0) { $name = "master"; } $newcfg .= "\t'$name' => {\n"; $newcfg .= "\t\t'_fdsn' => \"$dbinf->{fdsn}\",\n"; $newcfg .= "\t\t'role' => {\n"; foreach my $role (sort keys %{$weights{$id} || {}}) { $newcfg .= "\t\t\t'$role' => $weights{$id}->{$role}->{'curr'},\n"; } $newcfg .= "\t\t},\n"; $newcfg .= "\t},\n"; } $newcfg .= ");\n"; $newcfg .= "\$LJ::DBINFO{'master'}->{'role'}->{'master'} = 1;\n"; $newcfg .= "1;\n"; if (open(CFG, ">$ENV{'LJHOME'}/cgi-bin/dbconfig.pl")) { print CFG $newcfg; close CFG; open (TCH, ">>$ENV{'LJHOME'}/cgi-bin/ljconfig.pl"); close TCH; } $status .= "

Wrote config.

"; } else { my $newserial = LJ::procnotify_add("DBI::Role::reload"); $status .= "

New Serial: $newserial

\n"; } } } my $single = sub { my $role = shift; return @{$role{$role}} == 1; }; my $slaveroleperc = sub { my $sid = shift; my $role = shift; return sprintf("%0.1f%%", 100*$weights{$sid}->{$role}->{'curr'}/($roletweight{$role}||1)); }; my $dumpslaves = sub { my $mid = shift; my $depth = shift; my $rec = shift; return unless $slaves{$mid}; my $indent = " " x ($depth*5); foreach my $sid (sort { $#{$slaves{$a}} <=> $#{$slaves{$b}} } @{$slaves{$mid}}) { my $db = $dbinfo{$sid}; $p->("$indent$db->{'name'} ($sid)"); foreach my $role (sort keys %{$weights{$sid}}) { my $r = $weights{$sid}->{$role}; my $col; if ($r->{'norm'} != $r->{'curr'}) { $col = "bgcolor='#800000'"; } $p->("$indent$role"); $p->(""); $p->(""); $p->("" . $slaveroleperc->($sid, $role) . ""); $p->(""); } $rec->($sid, $depth+1, $rec); } }; my $dumprole = sub { my $role = shift; return if $single->($role); $p->("$role"); foreach my $sid (sort { $weights{$b}->{$role}->{'curr'} <=> $weights{$a}->{$role}->{'curr'} } @{$role{$role}}) { my $r = $weights{$sid}->{$role}; my $col; $col = "bgcolor='#800000'" if $r->{'norm'} != $r->{'curr'}; $p->("$dbinfo{$sid}->{'name'}"); $p->(""); $p->(""); $p->("" . $slaveroleperc->($sid, $role) . ""); $p->(""); } }; $p->('
'); $p->(""); $p->(''); my $hr = "Host / Role"; if ($view eq "role") { $hr = "Host / Role"; } $p->(""); if ($view eq "role") { foreach my $role (sort keys %role) { $dumprole->($role); } } else { $dumpslaves->(0, 0, $dumpslaves); # root } if ($can_save) { $p->(''); } $p->('
$hrNormCurr%
'); $p->('
'); $p->('
'); $p->($status); return $ret; _code?>