Skip to content

Commit

Permalink
✨ Allow to sort results in admin stats page (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldidry committed Apr 26, 2022
1 parent c173959 commit a3bce8f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for Perl application Lstu

0.24-0 ????-??-??
- ✨ Allow to sort results in admin stats page (#91)

0.23-1 2021-04-??
- Fix disabled URLs still showing in admin interface (#79)
Expand Down
20 changes: 18 additions & 2 deletions lib/Lstu/Controller/Stats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,22 @@ sub fullstats {
}

sub stats {
my $c = shift;
my $c = shift;
my $order = $c->param('order') // 'counter';
my $dir = $c->param('dir') // '-desc';

my %orders = (
'short' => 1,
'url' => 1,
'counter' => 1,
'created_by' => 1,
);
my %dirs = (-desc => 1, -asc => 1);

$order = 'counter' unless $orders{$order};
$dir = '-desc' unless $dirs{$dir};


if ((!defined($c->config('ldap')) && !defined($c->config('htpasswd'))) || $c->is_user_authenticated) {
my $db_session = Lstu::DB::Session->new(
app => $c,
Expand All @@ -77,7 +92,8 @@ sub stats {

my @urls = Lstu::DB::URL->new(
app => $c,
)->paginate($page, $c->config('page_offset'));
)->paginate($page, $c->config('page_offset'), $order, $dir);

$c->respond_to(
json => sub {
my $c = shift;
Expand Down
6 changes: 4 additions & 2 deletions lib/Lstu/DB/URL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ sub paginate {
my $c = shift;
my $page = shift;
my $page_offset = shift;
my $order = shift // 'counter';
my $dir = shift // '-desc';

return @{$c->app->dbi->db->query('SELECT * FROM lstu WHERE url IS NOT NULL AND disabled = 0 ORDER BY counter DESC LIMIT ? offset ?', $page_offset, $page * $page_offset)->hashes->to_array};
return @{$c->app->dbi->db->select('lstu', undef, { url => { '!=', undef }, disabled => 0 }, { order_by => { $dir => $order }, limit => $page_offset, offset => $page * $page_offset })->hashes->to_array};
}

=head2 get_a_lot
Expand Down Expand Up @@ -353,7 +355,7 @@ eg: C<COUNT(short) WHERE url IS NOT NULL>
sub total {
my $c = shift;

return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE url IS NOT NULL')->hashes->first->{count};
return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE url IS NOT NULL AND disabled = 0')->hashes->first->{count};
}

=head2 delete_all
Expand Down
11 changes: 11 additions & 0 deletions lib/Lstu/DB/URL/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ sub new {
return $c;
}

sub paginate {
my $c = shift;
my $page = shift;
my $page_offset = shift;
my $order = shift // 'counter';
my $dir = shift // '-desc';
$dir =~ s/^-//;

return @{$c->app->dbi->db->query("SELECT * FROM lstu WHERE url IS NOT NULL AND disabled = 0 ORDER BY $order $dir LIMIT ? OFFSET ?", $page_offset, $page * $page_offset)->hashes->to_array};
}

1;
32 changes: 16 additions & 16 deletions themes/default/lib/Lstu/I18N/lstu.pot
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ msgstr ""
msgid "About"
msgstr ""

#: themes/default/templates/stats.html.ep:13 themes/default/templates/stats.html.ep:14
#: themes/default/templates/stats.html.ep:18 themes/default/templates/stats.html.ep:19
msgid "Admin password"
msgstr ""

Expand All @@ -44,23 +44,23 @@ msgstr ""
msgid "Copy to clipboard"
msgstr ""

#: themes/default/templates/stats.html.ep:46
#: themes/default/templates/stats.html.ep:61
msgid "Counter"
msgstr ""

#: themes/default/templates/stats.html.ep:47
#: themes/default/templates/stats.html.ep:66
msgid "Created"
msgstr ""

#: themes/default/templates/api.html.ep:44 themes/default/templates/index.html.ep:10 themes/default/templates/index.html.ep:9
msgid "Custom shortened text"
msgstr ""

#: themes/default/templates/stats.html.ep:52
#: themes/default/templates/stats.html.ep:73
msgid "Delete"
msgstr ""

#: themes/default/templates/stats.html.ep:36
#: themes/default/templates/stats.html.ep:41
msgid "Export your URLs"
msgstr ""

Expand All @@ -76,15 +76,15 @@ msgstr ""
msgid "Get the details (visit counter, creation date, etc.) of a shortened URL."
msgstr ""

#: themes/default/templates/index.html.ep:12 themes/default/templates/stats.html.ep:17
#: themes/default/templates/index.html.ep:12 themes/default/templates/stats.html.ep:22
msgid "Go!"
msgstr ""

#: themes/default/templates/partial/lstu.js.ep:30
msgid "Hit Enter, then Ctrl+C to copy the short link"
msgstr ""

#: themes/default/templates/stats.html.ep:89
#: themes/default/templates/stats.html.ep:110
msgid "Home"
msgstr ""

Expand All @@ -109,7 +109,7 @@ msgstr ""
msgid "If you are logged in as admin, you can provide a \"page\" parameter"
msgstr ""

#: themes/default/templates/stats.html.ep:32 themes/default/templates/stats.html.ep:35
#: themes/default/templates/stats.html.ep:37 themes/default/templates/stats.html.ep:40
msgid "Import URLs"
msgstr ""

Expand Down Expand Up @@ -137,11 +137,11 @@ msgstr ""
msgid "Logout"
msgstr ""

#: themes/default/templates/stats.html.ep:10
#: themes/default/templates/stats.html.ep:15
msgid "Logout from admin stats"
msgstr ""

#: themes/default/templates/stats.html.ep:84
#: themes/default/templates/stats.html.ep:105
msgid "Next"
msgstr ""

Expand All @@ -162,11 +162,11 @@ msgstr ""
msgid "Please, check your credentials: unable to authenticate."
msgstr ""

#: themes/default/templates/stats.html.ep:82
#: themes/default/templates/stats.html.ep:103
msgid "Previous"
msgstr ""

#: themes/default/templates/stats.html.ep:50
#: themes/default/templates/stats.html.ep:71
msgid "QRCode"
msgstr ""

Expand All @@ -182,7 +182,7 @@ msgstr ""
msgid "Response:"
msgstr ""

#: themes/default/templates/stats.html.ep:45
#: themes/default/templates/stats.html.ep:56
msgid "Shortened URL"
msgstr ""

Expand All @@ -198,7 +198,7 @@ msgstr ""
msgid "Sorry, unable to parse the provided file"
msgstr ""

#: themes/default/templates/layouts/default.html.ep:60 themes/default/templates/layouts/default.html.ep:66 themes/default/templates/stats.html.ep:4
#: themes/default/templates/layouts/default.html.ep:60 themes/default/templates/layouts/default.html.ep:66 themes/default/templates/stats.html.ep:9
msgid "Statistics"
msgstr ""

Expand All @@ -223,7 +223,7 @@ msgid "The URL you want to shorten comes from a domain (%1) that is blacklisted
msgstr ""

#. ($c->url_for('/')
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:184 lib/Lstu/Controller/URL.pm:242
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:200 lib/Lstu/Controller/URL.pm:242
msgid "The shortened URL %1 doesn't exist."
msgstr ""

Expand All @@ -249,7 +249,7 @@ msgstr ""
msgid "Too many bad passwords. You're banned."
msgstr ""

#: themes/default/templates/api.html.ep:104 themes/default/templates/api.html.ep:95 themes/default/templates/stats.html.ep:44
#: themes/default/templates/api.html.ep:104 themes/default/templates/api.html.ep:95 themes/default/templates/stats.html.ep:51
msgid "URL"
msgstr ""

Expand Down
44 changes: 40 additions & 4 deletions themes/default/templates/stats.html.ep
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
% title 'Lstu stats';
% use Mojo::Date;
% my $order = $self->param('order') // 'counter';
% my $dir = $self->param('dir') // '-desc';
% my $arrow = ($dir eq '-desc') ? '↓' : '↑';
% my $adir = sprintf('&dir=%s', ($dir eq '-desc') ? '-asc' : '-desc');
% $dir = sprintf('&dir=%s', $dir);
<h2><%= l('Statistics') %></h2>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
% if (!flash('banned')) {
Expand Down Expand Up @@ -41,10 +46,41 @@
<table class="table table-striped">
<tr>
<th>#</th>
<th><%= l('URL') %></th>
<th><%= l('Shortened URL') %></th>
<th><%= l('Counter') %></th>
<th><%= l('Created') %></th>
% if ((defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) && defined(stash('admin'))) {
<th>
<a href="<%= url_for('stats') %>?order=url<%= ($order eq 'url') ? $adir : $dir %>">
<%= l('URL') %><%= $arrow if ($order eq 'url') %>
</a>
</th>
<th>
<a href="<%= url_for('stats') %>?order=short<%= ($order eq 'short') ? $adir : $dir %>">
<%= l('Shortened URL') %><%= $arrow if ($order eq 'short') %>
</a>
</th>
<th>
<a href="<%= url_for('stats') %>?order=counter<%= ($order eq 'counter') ? $adir : $dir %>">
<%= l('Counter') %><%= $arrow if ($order eq 'counter') %>
</a>
</th>
<th>
<a href="<%= url_for('stats') %>?order=created_by<%= ($order eq 'created_by') ? $adir : $dir %>">
<%= l('Created') %><%= $arrow if ($order eq 'created_by') %>
</a>
</th>
% } else {
<th>
<%= l('URL') %>
</th>
<th>
<%= l('Shortened URL') %>
</th>
<th>
<%= l('Counter') %>
</th>
<th>
<%= l('Created') %>
</th>
% }
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
% unless (defined(stash('admin'))) {
<th><%= l('QRCode') %></th>
Expand Down

0 comments on commit a3bce8f

Please sign in to comment.