Skip to content

Commit

Permalink
Add statistics page
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 14, 2024
1 parent 727c794 commit 9cb8ae2
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 1 deletion.
5 changes: 5 additions & 0 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'moment';
import {setupCodeMirrorForFile} from './legacy/file.js';
import {setupReviewDetails} from './legacy/review.js';
import {fromNow} from './legacy/time.js';
import CavilStatistics from './vue/CavilStatistics.vue';
import ClassifySnippets from './vue/ClassifySnippets.vue';
import EditSnippet from './vue/EditSnippet.vue';
import IgnoredFiles from './vue/IgnoredFiles.vue';
Expand Down Expand Up @@ -107,6 +108,10 @@ window.cavil = {
app.mount('#review-search');
},

setupStatistics() {
createApp(CavilStatistics).mount('#statistics');
},

fromNow,
setupCodeMirrorForFile,
setupReviewDetails
Expand Down
67 changes: 67 additions & 0 deletions assets/vue/CavilStatistics.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<template>
<div class="row">
<div class="col-md-2">
<div class="stats">
<div class="stats-body">{{ openReviews }}</div>
<div class="stats-description">Open Reviews</div>
</div>
</div>
<div class="col-md-2">
<div class="stats">
<div class="stats-body">{{ activePackages }}</div>
<div class="stats-description">Active Packages</div>
</div>
</div>
<div class="col-md-2">
<div class="stats">
<div class="stats-body">{{ embargoedPackages }}</div>
<div class="stats-description">Embargoed Packages</div>
</div>
</div>
</div>
</template>

<script>
import Refresh from './mixins/refresh.js';
export default {
name: 'CavilStatistics',
mixins: [Refresh],
data() {
return {
activePackages: 0,
embargoedPackages: 0,
openReviews: 0,
refreshDelay: 120000,
refreshUrl: '/stats/meta'
};
},
methods: {
refreshData(data) {
this.activePackages = data.active_packages;
this.embargoedPackages = data.embargoed_packages;
this.openReviews = data.open_reviews;
}
}
};
</script>

<style>
div.stats {
border: solid 1px #777;
border-radius: 4px;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
color: #777;
margin-bottom: 20px;
}
div.stats .stats-description {
font-size: 0.8em;
padding: 0 15px 15px;
text-align: center;
}
div.stats .stats-body {
font-size: 26px;
padding: 15px 15px 0;
text-align: center;
}
</style>
3 changes: 3 additions & 0 deletions lib/Cavil.pm
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ sub startup ($self) {
->name('new_snippet');
$admin_or_contributor->post('/snippet/decision/<id:num>')->to('Snippet#decision')->name('snippet_decision');

$logged_in->get('/stats')->to('Stats#index')->name('stats');
$logged_in->get('/stats/meta')->to('Stats#meta')->name('stats_meta');

# Upload (experimental)
$admin->get('/upload')->to('Upload#index')->name('upload');
$admin->post('/upload')->to('Upload#store')->name('store_upload');
Expand Down
28 changes: 28 additions & 0 deletions lib/Cavil/Controller/Stats.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2024 SUSE Linux GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.

package Cavil::Controller::Stats;
use Mojo::Base 'Mojolicious::Controller', -signatures;

sub index ($self) {
$self->render;
}

sub meta ($self) {
my $stats = $self->packages->stats;
$self->render(json => $stats);
}

1;
13 changes: 13 additions & 0 deletions lib/Cavil/Model/Packages.pm
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,19 @@ sub states ($self, $name) {
)->hashes->to_array;
}

sub stats {
my $self = shift;

my $stats = $self->pg->db->query(
"SELECT
(SELECT COUNT(*) FROM bot_packages WHERE obsolete = false) AS active_packages,
(SELECT COUNT(*) FROM bot_packages WHERE obsolete = false AND embargoed = true) AS embargoed_packages,
(SELECT COUNT(*) FROM bot_packages WHERE obsolete = false AND state = 'new') AS open_reviews"
)->hash;

return $stats;
}

sub unpack ($self, @args) { $self->_enqueue('unpack', @args) }

sub unpacked ($self, $id) {
Expand Down
2 changes: 1 addition & 1 deletion t/embargo.t
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ EOF

get '/*whatever' => {whatever => ''} => {text => '', status => 404};

my $cavil_test = Cavil::Test->new(online => $ENV{TEST_ONLINE}, schema => 'manual_review_test');
my $cavil_test = Cavil::Test->new(online => $ENV{TEST_ONLINE}, schema => 'embargo_test');
my $t = Test::Mojo->new(Cavil => $cavil_test->default_config);
$cavil_test->embargo_fixtures($t->app);
my $dir = $cavil_test->checkout_dir;
Expand Down
2 changes: 2 additions & 0 deletions t/login.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ subtest 'Login required' => sub {
$t->get_ok('/licenses/recent')->status_is(401)->content_like(qr/Login Required/);
$t->get_ok('/licenses/recent/meta')->status_is(401)->content_like(qr/Login Required/);
$t->get_ok('/spdx/1')->status_is(401)->content_like(qr/Login Required/);
$t->get_ok('/stats')->status_is(401)->content_like(qr/Login Required/);
$t->get_ok('/stats/meta')->status_is(401)->content_like(qr/Login Required/);
};

subtest 'Not authenticated' => sub {
Expand Down
41 changes: 41 additions & 0 deletions t/stats.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (C) 2024 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.

use Mojo::Base -strict;

use FindBin;
use lib "$FindBin::Bin/lib";

use Test::More;
use Test::Mojo;
use Cavil::Test;

plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE};

my $cavil_test = Cavil::Test->new(online => $ENV{TEST_ONLINE}, schema => 'stats_test');
my $t = Test::Mojo->new(Cavil => $cavil_test->default_config);
$cavil_test->mojo_fixtures($t->app);

subtest 'Statistics' => sub {
$t->get_ok('/login')->status_is(302)->header_is(Location => '/');

$t->get_ok('/stats')->status_is(200)->content_like(qr/id="statistics"/);
$t->get_ok('/stats/meta')->status_is(200)->json_is('/active_packages', 2)->json_is('/embargoed_packages', 0)
->json_is('/open_reviews' => 2);

$t->get_ok('/logout')->status_is(302)->header_is(Location => '/');
};

done_testing;
3 changes: 3 additions & 0 deletions templates/layouts/default.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<li>
%= link_to 'Pattern Performance' => 'recent_patterns', class => 'dropdown-item'
</li>
<li>
%= link_to 'Statistics' => 'stats', class => 'dropdown-item'
</li>
<li><hr class="dropdown-divider"></li>
% if (current_user_has_role 'admin') {
<h3 class="dropdown-header">Administrator Menu</h3>
Expand Down
9 changes: 9 additions & 0 deletions templates/stats/index.html.ep
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
% layout 'default';
% title 'Statistics';

<div id="statistics"></div>

% content_for 'ready_function' => begin
cavil.setupStatistics();
% end

0 comments on commit 9cb8ae2

Please sign in to comment.