Commit c97a70e8 authored by Luc Didry's avatar Luc Didry

Better admin stats page (frontend and backend)

parent 39b444da
......@@ -174,7 +174,7 @@ sub startup {
if ($msg) {
$c->respond_to(
json => { json => { success => Mojo::JSON->false, msg => $msg } },
any => sub {
any => sub {
my $c = shift;
$c->flash('msg' => $msg);
......@@ -197,7 +197,7 @@ sub startup {
$c->respond_to(
json => { json => { success => Mojo::JSON->true, url => $url, short => $prefix.$short } },
any => sub {
any => sub {
my $c = shift;
$c->flash('url' => $url);
......@@ -217,44 +217,10 @@ sub startup {
$r->get('/stats' => sub {
my $c = shift;
my $u = (defined($c->cookie('url'))) ? decode_json $c->cookie('url') : [];
my $p = join ",", (('?') x @{$u});
my @urls = LstuModel::Lstu->select("WHERE short IN ($p) ORDER BY counter DESC", @{$u});
my $prefix = $c->prefix;
$c->respond_to(
json => sub {
my @struct;
for my $url (@urls) {
push @struct, {
short => $prefix.$url->{short},
url => $url->{url},
counter => $url->{counter},
created_at => $url->{timestamp}
};
}
$c->render( json => \@struct );
},
any => sub {
shift->render(
template => 'stats',
prefix => $prefix,
urls => \@urls
)
}
)
})->name('stats');
$r->post('/stats' => sub {
my $c = shift;
my $pwd = $c->param('adminpwd');
my $page = $c->param('page') || 0;
$page = 0 if ($page < 0);
if (defined($c->config('adminpwd')) && $pwd eq $c->config('adminpwd')) {
if (defined($c->app->{token}) && defined($c->session('token')) && $c->session('token') eq $c->app->{token}) {
my $total = LstuModel::Lstu->count("WHERE url IS NOT NULL");
my $page = $c->param('page') || 0;
$page = 0 if ($page < 0);
$page = $page - 1 if ($page * $c->config('page_offset') > $total);
my ($first, $last) = (!$page, ($page * $c->config('page_offset') <= $total && $total < ($page + 1) * $c->config('page_offset')));
......@@ -267,9 +233,57 @@ sub startup {
first => $first,
last => $last,
page => $page,
adminpwd => $pwd,
admin => 1,
total => $total
)
} else {
my $u = (defined($c->cookie('url'))) ? decode_json $c->cookie('url') : [];
my $p = join ",", (('?') x @{$u});
my @urls = LstuModel::Lstu->select("WHERE short IN ($p) ORDER BY counter DESC", @{$u});
my $prefix = $c->prefix;
$c->respond_to(
json => sub {
my @struct;
for my $url (@urls) {
push @struct, {
short => $prefix.$url->{short},
url => $url->{url},
counter => $url->{counter},
created_at => $url->{timestamp}
};
}
$c->render( json => \@struct );
},
any => sub {
shift->render(
template => 'stats',
prefix => $prefix,
urls => \@urls
)
}
)
}
})->name('stats');
$r->post('/stats' => sub {
my $c = shift;
my $pwd = $c->param('adminpwd');
my $act = $c->param('action');
if (defined($c->config('adminpwd')) && defined($pwd) && $pwd eq $c->config('adminpwd')) {
my $token = $c->shortener(32);
$c->app->{token} = $token;
$c->session('token' => $token);
$c->redirect_to('stats');
} elsif (defined($act) && $act eq 'logout') {
#$c->signed_cookie('token' => '', {expires => 1});
delete $c->app->{token};
delete $c->session->{token};
$c->redirect_to('stats');
} else {
$c->flash('msg' => $c->l('Bad password'));
$c->redirect_to('stats');
......
......@@ -26,19 +26,19 @@ msgstr ""
msgid "About"
msgstr ""
#: templates/stats.html.ep:10 templates/stats.html.ep:9
#: templates/stats.html.ep:12 templates/stats.html.ep:13
msgid "Admin password"
msgstr ""
#: lib/Lstu.pm:274
#: lib/Lstu.pm:288
msgid "Bad password"
msgstr ""
#: templates/stats.html.ep:26
#: templates/stats.html.ep:29
msgid "Counter"
msgstr ""
#: templates/stats.html.ep:27
#: templates/stats.html.ep:30
msgid "Created"
msgstr ""
......@@ -50,7 +50,7 @@ msgstr ""
msgid "Failure reason"
msgstr ""
#: templates/index.html.ep:12 templates/stats.html.ep:13
#: templates/index.html.ep:12 templates/stats.html.ep:16
msgid "Go!"
msgstr ""
......@@ -70,7 +70,11 @@ msgstr ""
msgid "License:"
msgstr ""
#: templates/stats.html.ep:51
#: templates/stats.html.ep:9
msgid "Logout from admin stats"
msgstr ""
#: templates/stats.html.ep:53
msgid "Next"
msgstr ""
......@@ -83,7 +87,7 @@ msgstr ""
msgid "Parameters:"
msgstr ""
#: templates/stats.html.ep:50
#: templates/stats.html.ep:51
msgid "Previous"
msgstr ""
......@@ -95,7 +99,7 @@ msgstr ""
msgid "Response:"
msgstr ""
#: templates/stats.html.ep:25
#: templates/stats.html.ep:28
msgid "Shortened URL"
msgstr ""
......@@ -104,7 +108,7 @@ msgid "Statistics"
msgstr ""
#. ($c->url_for('/')
#: lib/Lstu.pm:300
#: lib/Lstu.pm:314
msgid "The shortened URL %1 doesn't exist."
msgstr ""
......@@ -118,7 +122,7 @@ msgstr ""
msgid "The shortened text can contain only numbers, letters and the - and _ characters and can't be \"a\", \"api\" or \"stats\". Your URL to shorten: %1"
msgstr ""
#: templates/stats.html.ep:24
#: templates/stats.html.ep:27
msgid "URL"
msgstr ""
......
# Lstu
# Copyright (C) 2013 - 2015 Luc Didry
# This file is distributed under the same license as the Lstu package.
# Luc Didry <luc@didry.org>, 2015.
#
#
# Translators:
# Luc Didry <luc@didry.org>, 2015
msgid ""
msgstr ""
"Project-Id-Version: Lstu - 0.04\n"
"Project-Id-Version: Lstu\n"
"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n"
"PO-Revision-Date: 2015-05-28 23:52+0100\n"
"PO-Revision-Date: 2015-05-31 21:50+0000\n"
"Last-Translator: Luc Didry <luc@didry.org>\n"
"Language-Team: French (http://www.transifex.com/projects/p/lstu/language/fr/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"Last-Translator: Luc Didry <luc@didry.org>\n"
"Language-Team: Luc Didry <admin@lstu.fr>\n"
"Language: fr\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 1.7.7\n"
"X-Poedit-SourceCharset: UTF-8\n"
#. ($url)
#: lib/Lstu.pm:172
......@@ -26,19 +28,19 @@ msgstr "%1 n’est pas une URL valide."
msgid "About"
msgstr "À propos"
#: templates/stats.html.ep:10 templates/stats.html.ep:9
#: templates/stats.html.ep:12 templates/stats.html.ep:13
msgid "Admin password"
msgstr "Mot de passe administrateur"
#: lib/Lstu.pm:274
#: lib/Lstu.pm:288
msgid "Bad password"
msgstr "Mot de passe incorrect"
#: templates/stats.html.ep:26
#: templates/stats.html.ep:29
msgid "Counter"
msgstr "Compteur"
#: templates/stats.html.ep:27
#: templates/stats.html.ep:30
msgid "Created"
msgstr "Créé le"
......@@ -51,12 +53,13 @@ msgstr "Texte du raccourci personnalisé"
msgid "Failure reason"
msgstr "Raison de l’échec"
#: templates/index.html.ep:12 templates/stats.html.ep:13
#: templates/index.html.ep:12 templates/stats.html.ep:16
msgid "Go!"
msgstr "Allons-y !"
#: templates/api.html.ep:16
msgid "If equal to \"json\", response will be in JSON format, HTML format otherwise"
msgid ""
"If equal to \"json\", response will be in JSON format, HTML format otherwise"
msgstr "Si égal à \"json\", la réponse sera au format JSON, sinon ce sera une réponse en HTML"
#: templates/api.html.ep:21 templates/api.html.ep:39
......@@ -71,20 +74,26 @@ msgstr "JSON : succès"
msgid "License:"
msgstr "Licence :"
#: templates/stats.html.ep:51
#: templates/stats.html.ep:9
msgid "Logout from admin stats"
msgstr "Déconnexion des stats admin"
#: templates/stats.html.ep:53
msgid "Next"
msgstr "Suivant"
#. ($c->config('contact')
#: lib/Lstu.pm:165
msgid "No shortened URL available. Please retry or contact the administrator at %1. Your URL to shorten: %2"
msgid ""
"No shortened URL available. Please retry or contact the administrator at %1."
" Your URL to shorten: %2"
msgstr "Il n’y a plus d’URL raccourcie disponible. Veuillez réessayer ou contactez l’administrateur sur %1. Rappel de l’URL à raccourcir : %2"
#: templates/api.html.ep:12
msgid "Parameters:"
msgstr "Paramètres :"
#: templates/stats.html.ep:50
#: templates/stats.html.ep:51
msgid "Previous"
msgstr "Précédent"
......@@ -96,7 +105,7 @@ msgstr "Réponse pour le format HTML"
msgid "Response:"
msgstr "Réponse :"
#: templates/stats.html.ep:25
#: templates/stats.html.ep:28
msgid "Shortened URL"
msgstr "URL raccourcie"
......@@ -105,7 +114,7 @@ msgid "Statistics"
msgstr "Statistiques"
#. ($c->url_for('/')
#: lib/Lstu.pm:300
#: lib/Lstu.pm:314
msgid "The shortened URL %1 doesn't exist."
msgstr "L’URL raccourcie %1 n’existe pas"
......@@ -116,10 +125,12 @@ msgstr "Le texte du raccourci (%1) est déjà utilisé. Veuillez en choisir un a
#. ($url)
#: lib/Lstu.pm:133
msgid "The shortened text can contain only numbers, letters and the - and _ characters and can't be \"a\", \"api\" or \"stats\". Your URL to shorten: %1"
msgid ""
"The shortened text can contain only numbers, letters and the - and _ "
"characters and can't be \"a\", \"api\" or \"stats\". Your URL to shorten: %1"
msgstr "Le texte du raccourci ne doit contenir que des chiffres, des lettres et les caractères - et _ et ne peut être \"a\", \"api\" ou \"stats\". Rappel de l’URL à raccourcir : %2"
#: templates/stats.html.ep:24
#: templates/stats.html.ep:27
msgid "URL"
msgstr "URL"
......@@ -129,11 +140,15 @@ msgid "URL to shorten"
msgstr "URL à raccourcir"
#: templates/api.html.ep:22
msgid "You will be redirected to the Lstu interface with a message giving the shortened URL"
msgid ""
"You will be redirected to the Lstu interface with a message giving the "
"shortened URL"
msgstr "Vous aurez l’interface normale de Lstu avec un message indiquant l’URL raccourcie"
#: templates/api.html.ep:30
msgid "You will be redirected to the targeted URL or to the Lstu interface with a message giving the failure reason"
msgid ""
"You will be redirected to the targeted URL or to the Lstu interface with a "
"message giving the failure reason"
msgstr "Vous serez redirigé vers l’URL cible ou vers la page d’accueil de Lstu avec un message indiquant la raison de l’échec"
#: templates/api.html.ep:14
......
......@@ -3,20 +3,23 @@
% use Mojo::Date;
<h2><%= l('Statistics') %></h2>
% if (defined(config('adminpwd'))) {
% if (!defined(stash('adminpwd'))) {
<form class="form" role="form" method="POST" action="<%== url_for 'stats' %>">
% if (defined(stash('admin'))) {
<input type="hidden" value="logout" name="action"></input>
<%= submit_button l('Logout from admin stats'), class => 'btn btn-default btn-primary' %>
% } else {
<div class="form-group">
<label class="sr-only" for="adminpwd"><%= l('Admin password') %></label>
<input type="password" name="adminpwd" id="adminpwd" class="form-control" placeholder="<%= l('Admin password') %>" required></input>
</div>
<input type="hidden" value="0" name="page"></input>
<%= submit_button l('Go!'), class => 'btn btn-default btn-primary' %>
% }
</form>
% if (defined(flash('msg'))) {
<h3 class="alert alert-danger"><%= flash('msg') %><h3>
% }
<br>
% }
% }
<table class="table table-striped">
<tr>
......@@ -30,6 +33,7 @@
% if (defined(stash('page')) && stash('page') >= 0) {
% $i = 1 + config('page_offset') * stash('page');
% }
% my $j = $i;
% for my $url (@{$urls}) {
<tr>
<td><%= $i++ %></td>
......@@ -41,22 +45,13 @@
% }
</table>
% if (defined(config('adminpwd'))) {
% if (defined(stash('adminpwd'))) {
% if (defined(stash('admin'))) {
<nav>
<form class="form" role="form" method="POST" action="<%== url_for 'stats' %>" id="form">
<input type="hidden" value="<%= stash('adminpwd') %>" name="adminpwd">
<input type="hidden" value="<%= stash('page') %>" name="page" id="page">
<ul class="pager">
<li class="previous<%= (stash('first')) ? ' disabled' : '' %>"><a href="javascript: sub(<%= (stash('first')) ? 0 : stash('page') - 1 %>)"><span aria-hidden="true">&larr;</span> <%= l('Previous') %></a></li>
<li class="next<%= (stash('last')) ? ' disabled' : '' %>"><a href="javascript: sub(<%= (stash('last')) ? stash('page') : stash('page') + 1 %>)"><%= l('Next') %> <span aria-hidden="true">&rarr;</span></a></li>
<li class="previous<%= (stash('first')) ? ' disabled' : '' %>"><%= link_to url_for->query(page => (stash('first')) ? 0 : stash('page') - 1) => begin %><span aria-hidden="true">&larr;</span> <%= l('Previous') %><% end %></li>
<li class="center"><%= $j.'-'.--$i.'/'.stash('total') %></li>
<li class="next<%= (stash('last')) ? ' disabled' : '' %>"><%= link_to url_for->query(page => (stash('last')) ? stash('page') : stash('page') + 1) => begin %><%= l('Next') %> <span aria-hidden="true">&rarr;</span><% end %></li>
</ul>
</form>
</nav>
%= javascript begin
function sub(val) {
document.getElementById("page").value = val;
document.getElementById("form").submit();
}
% end
% }
% }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment