Friday release

This commit is contained in:
nemunaire 2013-12-14 06:11:14 +01:00
parent 10eb72688f
commit c349769425
16 changed files with 319 additions and 89 deletions

View File

@ -88,23 +88,26 @@ for my $f (readdir $dh)
# Register solve # Register solve
if ($good == -1) { if ($good == -1) {
say localtime().": Exercice $exercice doesn't exist ; given by team $team in theme $theme."; say STDERR localtime().": Exercice $exercice doesn't exist ; given by team $team in theme $theme.";
} }
elsif ($good == 1) elsif ($good == 1)
{ {
# Check if the exercice exists # Check if the exercice exists
$sth = query($dbh, "SELECT `require` FROM exercices E WHERE E.id_theme = $theme AND E.id = ".$dbh->quote($exercice)); $sth = query($dbh, "SELECT `require` FROM exercices E WHERE E.id_theme = $theme AND E.id = ".$dbh->quote($exercice));
if ($sth->rows && (my $row = get_row($sth))) if ($sth->rows)
{ {
my $row = get_row($sth);
# Check if the team has solved dependancies # Check if the team has solved dependancies
$sth = query($dbh, "SELECT COUNT(S.id) FROM solved S WHERE S.id_exercice = ".$dbh->quote(@$row[0])) if @$row[0]; $sth = query($dbh, "SELECT S.id FROM solved S WHERE S.id_exercice = ".$dbh->quote(@$row[0])) if @$row[0];
if (! (@$row[0] && $sth->rows)) if (! @$row[0] || $sth->rows)
{ {
# Check if the team has not already solved this exercice # Check if the team has not already solved this exercice
$sth = query($dbh, "SELECT COUNT(S.id) FROM solved S WHERE S.id_exercice = ".$dbh->quote($exercice)); $sth = query($dbh, "SELECT S.id FROM solved S WHERE S.id_exercice = ".$dbh->quote($exercice));
if (! $sth->rows) if (! $sth->rows)
{ {
say localtime().": Team $team solve exercice $exercice in $theme"; say $team;
say STDERR localtime().": Team $team solve exercice $exercice in $theme";
query($dbh, "INSERT INTO solved (id_team, id_exercice, time) VALUES ($team, ".$dbh->quote($exercice).", CURRENT_TIMESTAMP);"); query($dbh, "INSERT INTO solved (id_team, id_exercice, time) VALUES ($team, ".$dbh->quote($exercice).", CURRENT_TIMESTAMP);");
$exit++; $exit++;
} }
@ -121,7 +124,7 @@ for my $f (readdir $dh)
} }
} }
else { else {
say localtime().": Team $team didn't give the correct answer for exercice $exercice."; say STDERR localtime().": Team $team didn't give the correct answer for exercice $exercice.";
} }
# Remove the file # Remove the file

15
clear_cache.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
cd `dirname "$0"`
for n in "$@"
do
MD5=`echo -n $n | md5sum | cut -d " " -f 1`
if [ -f "onyx/cache/$MD5.cache.php" ]
then
rm -f "onyx/cache/$MD5.cache.php"
echo "--- $n deleted"
else
echo "/!\\ $n not found ($MD5)"
fi
done

View File

@ -4,7 +4,7 @@ BASEURL="localhost"
SALT_TEAM="connected" SALT_TEAM="connected"
OUT_TEAM="./teams" OUT_TEAM="./teams"
MAX_PARAL=9 MAX_PARAL=10
DEBUG=0 DEBUG=0
@ -34,8 +34,13 @@ then
WGET_OPT="-q" WGET_OPT="-q"
fi fi
./clear_cache.sh top
mkdir -p out mkdir -p out
cd out
ORIG_DIR=`pwd`
MYTMPDIR=`mktemp -d`
cd "$MYTMPDIR"
# First, remove existing version if any # First, remove existing version if any
rm -rf "$BASEURL" "$OUT_TEAM" rm -rf "$BASEURL" "$OUT_TEAM"
@ -43,15 +48,38 @@ rm -rf "$BASEURL" "$OUT_TEAM"
wget $WGET_OPT -m -b "http://$BASEURL/" -o /dev/null wget $WGET_OPT -m -b "http://$BASEURL/" -o /dev/null
mkdir -p "$BASEURL" mkdir -p "$BASEURL"
ln -sf "`pwd`/../files/" "$BASEURL/files" ln -sf "$ORIG_DIR/files/" "$BASEURL/files"
# Get list of teams
TEAMS=
if [ $# -gt 0 ]
then
while [ $# -gt 0 ]
do
TEAMS="$TEAMS /$SALT_TEAM/$1/"
shift
done
FULLSYNC=0
else
for l in $(curl -k "http://$BASEURL/$SALT_TEAM/" 2> /dev/null | grep -oE "/[^/]+/[0-9]+/")
do
TEAMS="$TEAMS $l"
done
FULLSYNC=1
fi
echo "Team list to generate: $TEAMS"
NB=0 NB=0
PIDLIST= PIDLIST=
# Get list of teams and fetch them in parallel # Fetch them in parallel
for l in $(curl -k "http://$BASEURL/$SALT_TEAM/" 2> /dev/null | grep -oE "/[^/]+/[0-9]+/") for l in $TEAMS
do do
( (
wget $WGET_OPT -m "http://$BASEURL/$l" if ! wget $WGET_OPT -m "http://$BASEURL/$l"
then
exit 1
fi
for m in $(grep -R "<form " "$BASEURL/$l" | grep -oE "/[^/]+/([^/]+)/([0-9]+)-[^/]+/([a-zA-Z0-9_]+)/submission") for m in $(grep -R "<form " "$BASEURL/$l" | grep -oE "/[^/]+/([^/]+)/([0-9]+)-[^/]+/([a-zA-Z0-9_]+)/submission")
do do
@ -60,8 +88,13 @@ do
done done
# Remove /connected/XY # Remove /connected/XY
sed -Ei "s#/[^/]+/([0-9]+)/#/#" "$BASEURL/$l/"* if sed -Ei "s#/[^/]+/([0-9]+)/#/#" "$BASEURL/$l/"* &&
sed -Ei "s#/([0-9]+)-[^/]*/([a-zA-Z0-9_]+)/submission#/submission-\1-\2.html#" "$BASEURL/$l/"* sed -Ei "s#/([0-9]+)-[^/]*/([a-zA-Z0-9_]+)/submission#/submission-\1-\2.html#" "$BASEURL/$l/"*
then
exit 0
else
exit 1
fi
) & ) &
PIDLIST="$PIDLIST $!" PIDLIST="$PIDLIST $!"
@ -77,9 +110,15 @@ do
done done
echo "Generating teams ...$PIDLIST" echo "Generating teams ...$PIDLIST"
wait $PIDLIST
rm /tmp/generate_site ERR=0
for i in $PIDLIST
do
if ! wait $i
then
ERR=$(($ERR + 1))
fi
done
# Move connected/ at root # Move connected/ at root
mv "$BASEURL/$SALT_TEAM/" "$OUT_TEAM" mv "$BASEURL/$SALT_TEAM/" "$OUT_TEAM"
@ -89,3 +128,27 @@ find . -name robots.txt -exec rm {} \;
# Remove useless symlink # Remove useless symlink
rm "$BASEURL/files" rm "$BASEURL/files"
# Ready to launch another gen_site
rm /tmp/generate_site
if [ $ERR -gt 0 ]
then
cd "$ORIG_DIR"
rm -rf "$MYTMPDIR"
echo "Some errors occurs" 1>&2
exit $ERR
else
MOREOPT=
if [ "$FULL" -eq "1" ]
then
MOREOPT="--delete"
fi
# Ok, now, sync files with prod
rsync -av $MOREOPT * "$ORIG_DIR/out"
cd "$ORIG_DIR"
rm -rf "$MYTMPDIR"
fi

53
launch.sh Executable file
View File

@ -0,0 +1,53 @@
#!/bin/sh
cd `dirname "$0"`
if [ "$UID" = "0" ]
then
SCRIPT=`pwd`/`basename "$0"`
su -c "sh $SCRIPT" synchro
exit $?
fi
touch ./logs/checks.log
tail -f ./logs/checks.log &
FULLREGEN=0
while true;
do
if [ "$FULLREGEN" != "0" ]
then
./synchro.sh
else
./synchro.sh delete
fi
if [ `ls submission | wc -l` -gt 1 ]
then
TMPF=`mktemp`
if ! ./check.pl 2>> ./logs/checks.log > "$TMPF"
then
FULLREGEN=1
fi
if [ `cat "$TMPF" | wc -l` -gt 0 ]
then
while ! cat "$TMPF" | xargs ./gen_site.sh
do
echo "FAIL regeneration, retry..." 1>&2
done
fi
rm "$TMPF"
elif [ "$FULLREGEN" != "0" ]
then
while ! ./gen_site.sh; do
echo "FAIL regeneration, retry..." 1>&2
done
FULLREGEN=0
else
sleep 1
fi
done

View File

@ -21,6 +21,7 @@ server {
location / location /
{ {
default_type text/html; default_type text/html;
expires epoch;
set $team 0; set $team 0;

View File

@ -18,6 +18,8 @@ function remove_themes($id)
$db->query("DELETE FROM exercices WHERE id_theme = ".$id); $db->query("DELETE FROM exercices WHERE id_theme = ".$id);
$db->query("DELETE FROM themes WHERE id = ".$id); $db->query("DELETE FROM themes WHERE id = ".$id);
$db->deconnexion(); $db->deconnexion();
Cache::del("ordered_th".$id);
} }
if (!empty($_GET["delete"])) if (!empty($_GET["delete"]))

View File

@ -75,6 +75,23 @@ class Exercice
} }
} }
static function __set_state(array $array)
{
$tmp = new Exercice();
$tmp->id = $array["id"];
$tmp->number = $array["number"];
$tmp->theme = $array["theme"];
$tmp->require = $array["require"];
$tmp->level = $array["level"];
$tmp->points = $array["points"];
$tmp->statement = $array["statement"];
$tmp->files = $array["files"];
$tmp->keys = $array["keys"];
return $tmp;
}
function get_id() function get_id()
{ {
return $this->id; return $this->id;
@ -89,10 +106,12 @@ class Exercice
// trié par date // trié par date
function get_solved() function get_solved()
{ {
$db = new BDD(); $id = $this->id;
$db = new BDD();
$db->escape($id);
$res = $db->query("SELECT `id_team`, `time` FROM solved $res = $db->query("SELECT `id_team`, `time` FROM solved
WHERE id_exercice = '$this->id' WHERE id_exercice = '$id'
ORDER BY time"); ORDER BY time");
$db->deconnexion(); $db->deconnexion();
@ -105,11 +124,13 @@ class Exercice
if ($this->require == "") if ($this->require == "")
return 1; return 1;
$db = new BDD(); $req = $this->require;
$db = new BDD();
$db->escape($req);
$res = $db->unique_query("SELECT `id` FROM solved $res = $db->unique_query("SELECT `id` FROM solved
WHERE id_team = '".intval($team->id)."' WHERE id_team = '".intval($team->id)."'
AND id_exercice = '$this->require'"); AND id_exercice = '$req'");
$db->deconnexion(); $db->deconnexion();
if (empty($res)) if (empty($res))
return 0; return 0;
@ -118,9 +139,11 @@ class Exercice
function has_solved($team) function has_solved($team)
{ {
$db = new BDD(); $id = $this->id;
$res = $db->unique_query("SELECT `time` FROM solved WHERE id_exercice = '$this->id' $db = new BDD();
$db->escape($id);
$res = $db->unique_query("SELECT `time` FROM solved WHERE id_exercice = '$id'
AND id_team = ".intval($team->get_id())); AND id_team = ".intval($team->get_id()));
$db->deconnexion(); $db->deconnexion();
@ -143,6 +166,7 @@ class Exercice
do do
{ {
array_push($checked, $exo); array_push($checked, $exo);
$db->escape($exo);
$res = $db->unique_query("SELECT `require` FROM exercices WHERE id = '".$exo."'"); $res = $db->unique_query("SELECT `require` FROM exercices WHERE id = '".$exo."'");
$exo = $res['require']; $exo = $res['require'];
$ret++; $ret++;
@ -278,6 +302,28 @@ class Exercice
return $res['max']; return $res['max'];
} }
public function first_to_solve_exercice()
{
$db = new BDD();
$id = $this->id;
$db->escape($id);
$res = $db->unique_query("SELECT t3.team_name as result
FROM solved AS t1
INNER JOIN (
SELECT MIN(s.time) AS minTime
FROM solved AS s
WHERE s.id_exercice = '".$id."'
) AS t2
INNER JOIN teams AS t3 ON t1.id_team = t3.id
WHERE t1.time = t2.minTime");
$db->deconnexion();
return $res['result'];
}
} }
class ExerciceNotFoundException extends Exception class ExerciceNotFoundException extends Exception

View File

@ -60,6 +60,22 @@ class Team
} }
} }
static function __set_state(array $array)
{
$tmp = new Team();
$tmp->id = $array["id"];
$tmp->team_name = $array["team_name"];
$tmp->key_hash = $array["key_hash"];
$tmp->auth_level = $array["auth_level"];
$tmp->slogan = $array["slogan"];
$tmp->members = $array["members"];
$tmp->points = $array["points"];
$tmp->revoked = $array["revoked"];
return $tmp;
}
// Class methods // Class methods
function update() function update()
{ {
@ -274,14 +290,20 @@ class Team
public static function get_top($nb=0) public static function get_top($nb=0)
{ {
$teams = Team::get_teams(); $top = Cache::read("top");
if (empty($top))
{
$top = Team::get_teams();
usort($teams, "cmp_team_pts"); usort($top, "cmp_team_pts");
Cache::set("top", $top);
}
if ($nb != 0) if ($nb != 0)
$teams = array_slice($teams, 0, $nb); $top = array_slice($top, 0, $nb);
return $teams; return $top;
} }
public static function get_nb_teams() public static function get_nb_teams()
@ -292,21 +314,4 @@ class Team
return $res['count_teams']; return $res['count_teams'];
} }
public static function first_to_solve_exercice($id_exercice)
{
$db = new BDD();
$res = $db->unique_query("SELECT t3.team_name as result
FROM solved AS t1
INNER JOIN (
SELECT MIN(s.time) AS minTime
FROM solved AS s
WHERE s.id_exercice = ".$id_exercice."
) AS t2
INNER JOIN teams AS t3 ON t1.id_team = t3.id
WHERE t1.time = t2.minTime");
$db->deconnexion();
return $res['result'];
}
} }

View File

@ -29,6 +29,16 @@ class Theme
} }
} }
static function __set_state(array $array)
{
$tmp = new Theme();
$tmp->id = $array["id"];
$tmp->name = $array["name"];
return $tmp;
}
function update() function update()
{ {
$name = $this->name; $name = $this->name;
@ -52,6 +62,8 @@ class Theme
} }
$db->deconnexion(); $db->deconnexion();
Cache::del("ordered_th".$this->id);
return ($aff == 1); return ($aff == 1);
} }
@ -93,6 +105,10 @@ class Theme
function get_exercices_ordered() function get_exercices_ordered()
{ {
$res = Cache::read("ordered_th".$this->id);
if (!empty($res))
return $res;
$db = new BDD(); $db = new BDD();
$res = $db->query("SELECT E.id, E.require FROM exercices E $res = $db->query("SELECT E.id, E.require FROM exercices E
INNER JOIN themes T ON T.id = E.id_theme INNER JOIN themes T ON T.id = E.id_theme
@ -130,13 +146,14 @@ class Theme
foreach($res as &$r) foreach($res as &$r)
$r = new Exercice($r["id"]); $r = new Exercice($r["id"]);
Cache::set("ordered_th".$this->id, $res);
return $res; return $res;
} }
public static function get_themes() public static function get_themes()
{ {
$db = new BDD(); $db = new BDD();
$ids = $db->query("SELECT `id` FROM `themes`"); $ids = $db->query("SELECT `id` FROM `themes` ORDER BY `name`");
$db->deconnexion(); $db->deconnexion();
$array = array(); $array = array();

View File

@ -2,34 +2,45 @@
class Cache class Cache
{ {
static function set($id,$var) static function set($id,$var)
{ {
$file = '<?php $cache = '.var_export($var,TRUE).'; ?>'; $file = '<?php $cache = '.var_export($var,TRUE).'; ?>';
file_put_contents(ONYX.'cache/'.md5($id).'.cache.php',$file) or trigger_error('dossier cache inaccessible en écriture.',E_USER_ERROR); $tmpfname = tempnam("/tmp", "cache");
if(file_put_contents($tmpfname, $file, LOCK_EX) !== FALSE)
rename($tmpfname, ONYX.'cache/'.md5($id).'.cache.php') or trigger_error('dossier cache inaccessible en écriture.',E_USER_ERROR);
else
trigger_error('impossible d\'?crire dans '.$tmpfname,E_USER_ERROR);
} }
static function read($id) static function read($id)
{ {
if(!is_readable(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE; if(!is_readable(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
include(ONYX.'cache/'.md5($id).'.cache.php'); $tmpfname = tempnam("/tmp", "cache");
if(!$cache) return FALSE; copy(ONYX.'cache/'.md5($id).'.cache.php', $tmpfname);
include($tmpfname);
unlink($tmpfname);
if(empty($cache)) return FALSE;
return $cache; return $cache;
} }
static function del($id) static function del($id)
{ {
if(!is_file(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE; if(!is_file(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
return unlink(ONYX.'cache/'.md5($id).'.cache.php'); return unlink(ONYX.'cache/'.md5($id).'.cache.php');
} }
static function flush() static function flush()
{ {
foreach(glob(ONYX.'cache/*.cache.php') as $file) unlink($file); foreach(glob(ONYX.'cache/*.cache.php') as $file) unlink($file);
} }
} }
?> ?>

View File

@ -1,9 +1,13 @@
{extends file="public/layout.tpl"} {extends file="public/layout.tpl"}
{block name=main} {block name=main}
<div class="well"> {if isset($teams)}
<div id="carousel-team" class="carousel slide">
<table class="table" style="font-size: 12px;"> <div class="carousel-inner">
<div class="item active">
<div class="well">
<table class="table" style="font-size: 14px;">
<thead> <thead>
<tr> <tr>
<th></th> <th></th>
@ -21,7 +25,7 @@
{foreach from=$theme->get_exercices_ordered() item=exo} {foreach from=$theme->get_exercices_ordered() item=exo}
{$teamName=""} {$teamName=""}
{for $i=0 to $nbExoMax} {for $i=0 to $nbExoMax}
{$teamName=Team::first_to_solve_exercice($exo->get_id())} {$teamName=$exo->first_to_solve_exercice()}
{/for} {/for}
{if $teamName != ""} {if $teamName != ""}
<td class="success text-center">{$teamName}</td> <td class="success text-center">{$teamName}</td>
@ -33,6 +37,20 @@
{/foreach} {/foreach}
</tbody> </tbody>
</table> </table>
</div>
</div>
{foreach from=$teams item=my_team key=k}
<div class="item">
<div class="well">
{include file="summary.tpl"}
</div>
</div> </div>
{/foreach}
</div>
</div>
{/if}
{/block} {/block}

View File

@ -17,32 +17,23 @@
{/if} {/if}
<div class="row"> <div class="row">
<div class="col-md-2"> <div class="col-md-3">
<h3>Top 10</h3> <h3>Top 10</h3>
<div class="list-group"> <div class="list-group">
{foreach from=$top item=t key=k} {foreach from=$top item=t key=k}
<div class="list-group-item">{$k+1}. {link href="{$t->id}-{$t->get_name()}" href_prefix="/" label=$t->get_name()}</div> <div class="list-group-item">{$k+1}. {link href="{$t->id}-{$t->get_name()}" href_prefix="/" label=$t->get_name()}
<span class="badge">
{$t->get_pts()}
</span>
</div>
{/foreach} {/foreach}
</div> </div>
</div> </div>
<div class="col-md-10"> <div class="col-md-9">
{block name=main}{/block} {block name=main}{/block}
</div> </div>
</div> </div>
{if isset($teams)}
<div id="carousel-team" class="carousel slide">
<div class="carousel-inner">
{foreach from=$teams item=my_team key=k}
<div class="item{if $k == 0} active{/if}">
<div class="well">
{include file="summary.tpl"}
</div>
</div>
{/foreach}
</div>
</div>
{/if}
</div> </div>
{/block} {/block}
@ -54,7 +45,7 @@
$(document).ready(function() { $(document).ready(function() {
update_end(); update_end();
$('#carousel-team').carousel({ $('#carousel-team').carousel({
interval: 5000 }); interval: 10000 });
setInterval( function() { setInterval( function() {
update_end(); update_end();

View File

@ -2,7 +2,6 @@
{block name=main} {block name=main}
<div> <div>
<h1>{$my_team->get_name()}</h1>
{include file="summary.tpl"} {include file="summary.tpl"}
</div> </div>
{/block} {/block}

View File

@ -1,7 +1,7 @@
<table class="table" style="font-size: 12px; padding: 10px; margin-bottom: 10px;"> <table class="table" style="font-size: 14px; padding: 10px; margin-bottom: 10px;">
<thead> <thead>
<tr> <tr>
<th style="font-size: 15px">{$my_team->get_name()}</th> <th style="font-size: 30px">{$my_team->get_name()}</th>
{for $i=1 to $nbExoMax} {for $i=1 to $nbExoMax}
<th class="text-center">Exercice {$i}</th> <th class="text-center">Exercice {$i}</th>
{/for} {/for}
@ -12,7 +12,7 @@
{$total=0} {$total=0}
{foreach from=$themes item=theme} {foreach from=$themes item=theme}
<tr> <tr>
<th style="padding: 0px">{$theme->get_name()}</th> <th>{$theme->get_name()}</th>
{$sum=0} {$sum=0}
{$cpt=0} {$cpt=0}
{$themeID=$theme->get_id()} {$themeID=$theme->get_id()}
@ -43,8 +43,8 @@
</tbody> </tbody>
<tfoot> <tfoot>
<tr> <tr>
<th colspan="{$nbExoMax+1}" style="text-align: right">Total :</th> <th colspan="{$nbExoMax+1}" style="text-align: right; font-size: 30px">Total :</th>
<th class="active text-center">{$total}</th> <th class="active text-center" style="font-size: 30px">{$total}</th>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>

View File

@ -8,7 +8,7 @@
<div class="panel-body"> <div class="panel-body">
<ul> <ul>
<li><strong>Gain :</strong> {$cur_exercice->points}</li> <li><strong>Gain :</strong> {$cur_exercice->points}</li>
<li><strong>Description :</strong> {$cur_exercice->statement}</li> <li><strong>Description :</strong> {$cur_exercice->statement|escape|nl2br}</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -5,17 +5,23 @@ cd `dirname "$0"`
if [ "$UID" = "0" ] if [ "$UID" = "0" ]
then then
SCRIPT=`pwd`/`basename "$0"` SCRIPT=`pwd`/`basename "$0"`
su -c "sh $SCRIPT" synchro su -c "sh $SCRIPT $@" synchro
exit $? exit $?
fi fi
rsync -e ssh -av --delete out/localhost/* phobos:~/htdocs/ OPTS=
rsync -e ssh -av --delete out/teams phobos:~/ if [ "$1" = "delete" ]
rsync -e ssh -av --delete files phobos:~/ then
rsync -e ssh -av --delete misc phobos:~/ OPTS="$OPTS --delete"
fi
rsync -e ssh -av $OPTS out/localhost/* phobos:~/htdocs/
rsync -e ssh -av $OPTS out/teams phobos:~/
rsync -e ssh -av $OPTS files phobos:~/
rsync -e ssh -av $OPTS misc phobos:~/
scp nginx.conf submission.php phobos:~/ scp nginx.conf submission.php phobos:~/
rsync -e ssh -av --delete out/localhost/* phobos:~/htdocs/ rsync -e ssh -av $OPTS out/localhost/* phobos:~/htdocs/
rsync -e ssh -av phobos:~/submission/ submission/ rsync -e ssh -av phobos:~/submission/ submission/
ssh phobos "rm ~/submission/*" ssh phobos "rm ~/submission/*"