Skip to content

Commit

Permalink
Merge pull request #622 from mwiencek/nix-timeline-hacks
Browse files Browse the repository at this point in the history
Replace hacky TT timeline code
  • Loading branch information
mwiencek committed Mar 28, 2018
2 parents ac0421e + fe0867a commit 9e815be
Show file tree
Hide file tree
Showing 13 changed files with 1,033 additions and 321 deletions.
14 changes: 14 additions & 0 deletions lib/MusicBrainz/Server.pm
Expand Up @@ -431,6 +431,20 @@ has json => (
}
);

has json_canonical_utf8 => (
is => 'ro',
default => sub {
return JSON->new->allow_blessed->canonical->convert_blessed->utf8;
}
);

has json_utf8 => (
is => 'ro',
default => sub {
return JSON->new->allow_blessed->convert_blessed->utf8;
}
);

sub TO_JSON {
my $self = shift;

Expand Down
43 changes: 0 additions & 43 deletions lib/MusicBrainz/Server/Controller/JS.pm

This file was deleted.

41 changes: 29 additions & 12 deletions lib/MusicBrainz/Server/Controller/Statistics.pm
@@ -1,4 +1,5 @@
package MusicBrainz::Server::Controller::Statistics;
use Digest::MD5 qw( md5_hex );
use Moose;
use MusicBrainz::Server::Data::Statistics::ByDate;
use MusicBrainz::Server::Data::Statistics::ByName;
Expand Down Expand Up @@ -56,15 +57,34 @@ sub statistics : Path('')
);
}

sub timeline_type_data : Path('timeline/type-data') {
my ($self, $c) = @_;

my @countries = $c->model('CountryArea')->get_all;
my %countries = map { $_->country_code => $_ } @countries;
my %languages = map { $_->iso_code_3 => $_ }
grep { defined $_->iso_code_3 } $c->model('Language')->get_all;
my %scripts = map { $_->iso_code => $_ } $c->model('Script')->get_all;
my %formats = map { $_->id => $_ } $c->model('MediumFormat')->get_all;
my @rel_pairs = $c->model('Relationship')->all_pairs;

my $body = $c->json_canonical_utf8->encode({
countries => \%countries,
formats => \%formats,
languages => \%languages,
relationships => \@rel_pairs,
scripts => \%scripts,
});
$c->res->body($body);
$c->res->content_type('application/json; charset=utf-8');
$c->res->headers->etag(md5_hex($body));
}

sub timeline : Path('timeline/main')
{
my ($self, $c) = @_;

my @stats = qw( count.area count.artist count.place count.release count.medium count.releasegroup count.label count.work count.recording count.series count.instrument count.event count.edit count.edit.open count.edit.perday count.edit.perweek count.vote count.vote.perday count.vote.perweek count.editor count.editor.valid count.editor.deleted count.editor.valid.active count.editor.editlastweek count.editor.votelastweek count.editor.activelastweek count.coverart count.release.has_caa );
$c->stash(
template => 'statistics/timeline.tt',
stats => \@stats
)
$c->stash(template => 'statistics/timeline.tt');
}

sub timeline_redirect : Path('timeline')
Expand All @@ -77,12 +97,10 @@ sub individual_timeline : Path('timeline') Args(1)
{
my ($self, $c, $stat) = @_;

my @stats = split /\+/, $stat;
$c->stash(
template => 'statistics/timeline.tt',
stats => \@stats,
show_all => 1,
)
);
}

sub dataset : Local Args(1)
Expand All @@ -92,10 +110,9 @@ sub dataset : Local Args(1)
$c->res->content_type('application/json; charset=utf-8');
my $tomorrow = Date_to_Time(Add_Delta_Days(Today(1), 1), 0, 0, 0);
$c->res->headers->expires($tomorrow);
$c->stash(
template => 'statistics/dataset.tt',
statistic => $c->model('Statistics::ByName')->get_statistic($dataset)
)

my $statistic = $c->model('Statistics::ByName')->get_statistic($dataset);
$c->res->body($c->json_utf8->encode($statistic));
}

sub countries : Local
Expand Down
9 changes: 4 additions & 5 deletions lib/MusicBrainz/Server/Data/Statistics/ByName.pm
Expand Up @@ -25,13 +25,12 @@ sub get_statistic {
FROM " . $self->_table . "
WHERE name = ?";

my @stats = @{ $self->sql->select_list_of_hashes($query, $statistic) }
or return undef;
my @stats = @{ $self->sql->select_list_of_hashes($query, $statistic) };
my $stats = MusicBrainz::Server::Entity::Statistics::ByName->new(
name => $statistic,
);

my $stats = MusicBrainz::Server::Entity::Statistics::ByName->new();
for my $row (@stats) {
$stats->name($row->{name})
unless $stats->name;
$stats->data->{$row->{date_collected}} = $row->{value};
}

Expand Down
11 changes: 7 additions & 4 deletions po/Makefile
Expand Up @@ -4,10 +4,12 @@ POTFILES=$(sort $(shell find ../lib -type f -name \*.pm)) \
JSFILES=$(sort $(shell find ../root -type f -name \*.js \
-not -path '../root/static/scripts/tests/*' \
-not -path '../root/static/build/*' \
-not -path '../root/static/lib/*'))
-not -path '../root/static/lib/*' \
-not -path '../root/statistics/*'))

TT=$(sort $(shell find ../root -path ../root/statistics -prune -o -type f -name \*.tt -print))
STATTT=$(sort $(shell find ../root/statistics -type f -name \*.tt -print))
STATJS=$(sort $(shell find ../root/statistics -type f -name \*.js -print))

POFILES=$(wildcard *.po)
MOFILES=$(POFILES:.po=.mo)
Expand Down Expand Up @@ -45,15 +47,16 @@ mb_server.pot: $(POTFILES) $(JSFILES) $(TT) $(wildcard extract_pot_templates)
@msgcat -o mb_server.pot mb_server.pot javascript.pot
@rm javascript.pot

statistics.pot: $(STATTT) $(wildcard extract_pot_db) $(wildcard extract_pot_templates)
statistics.pot: $(STATTT) $(STATJS) $(wildcard extract_pot_db) $(wildcard extract_pot_templates)
@echo "Rebuilding the statistics.pot file";
@echo "- Update templates .pot";
@find ../root/statistics -type f -name \*.tt -print | sort | ./extract_pot_templates | msguniq -s > statistics_tmpl.pot;
@xgettext --from-code utf-8 --keyword=l --keyword=lp:1,2c --keyword=ln:1,2 -L javascript -o statistics_js.pot --add-comments=translators $(STATJS)
@echo "- Update database .pot";
@./extract_pot_db statistics | msguniq -s > statistics_db.pot
@echo "- Merge";
@msgcat -o statistics.pot statistics_db.pot statistics_tmpl.pot
@rm statistics_db.pot statistics_tmpl.pot
@msgcat -o statistics.pot statistics_db.pot statistics_tmpl.pot statistics_js.pot
@rm statistics_db.pot statistics_tmpl.pot statistics_js.pot

countries.pot: $(wildcard extract_pot_db)
@echo "Rebuilding the countries.pot file";
Expand Down
19 changes: 19 additions & 0 deletions root/static/scripts/common/i18n/statistics.js
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2018 MetaBrainz Foundation
*
* This file is part of MusicBrainz, the open internet music database,
* and is licensed under the GPL version 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl-2.0.txt
*/

import wrapGettext from './wrapGettext';

const l_statistics = wrapGettext('dgettext', 'statistics');
const ln_statistics = wrapGettext('dngettext', 'statistics');
const lp_statistics = wrapGettext('dpgettext', 'statistics');

export {
l_statistics,
ln_statistics,
lp_statistics,
};
85 changes: 73 additions & 12 deletions root/static/scripts/timeline.js
@@ -1,15 +1,50 @@
const $ = require('jquery');
const _ = require('lodash');
const ko = require('knockout');

const debounce = require('./common/utility/debounce');
const parseDate = require('./common/utility/parseDate');
import stats, {buildTypeStats, getStat} from '../../statistics/stats';

require('../lib/flot/jquery.flot');
require('../lib/flot/jquery.flot.selection');
require('../lib/flot/jquery.flot.time');
require('./jquery.flot.musicbrainz_events');

MB.Timeline = {};
const defaultLines = [
'count.area',
'count.artist',
'count.coverart',
'count.edit',
'count.edit.open',
'count.edit.perday',
'count.edit.perweek',
'count.editor',
'count.editor.activelastweek',
'count.editor.deleted',
'count.editor.editlastweek',
'count.editor.valid',
'count.editor.valid.active',
'count.editor.votelastweek',
'count.event',
'count.instrument',
'count.label',
'count.medium',
'count.place',
'count.recording',
'count.release',
'count.release.has_caa',
'count.releasegroup',
'count.series',
'count.vote',
'count.vote.perday',
'count.vote.perweek',
'count.work',
];

class TimelineViewModel {

constructor(initialLines) {
constructor() {
var self = this;
self.categories = ko.observableArray([]);
self.enabledCategories = ko.computed(function () {
Expand Down Expand Up @@ -107,7 +142,15 @@ class TimelineViewModel {
return bounds;
});

self.addLines(initialLines);
let lines = document.location.pathname.match(
/^\/statistics\/timeline\/(.+)$/
)[1].split('+');

if (lines.length === 1 && lines[0] === 'main') {
lines = defaultLines;
}

self.addLines(lines);
self._getLocationHashSettings();

self.hash = debounce(function () {
Expand Down Expand Up @@ -187,15 +230,15 @@ class TimelineViewModel {
}

addLine(name) {
var newLine = MB.text.Timeline.Stat(name)
var category = _.find(this.categories(), { name: newLine.Category });
var newLine = getStat(name)
var category = _.find(this.categories(), { name: newLine.category });

if (!category) {
var newCategory = MB.text.Timeline.Category[newLine.Category];
category = this.addCategory(new TimelineCategory(newLine.Category, newCategory.Label, !newCategory.Hide));
var newCategory = stats.category[newLine.category];
category = this.addCategory(new TimelineCategory(newLine.category, newCategory.label, !newCategory.hide));
}

category.addLine(new TimelineLine(name, newLine.Label, newLine.Color, !newLine.Hide));
category.addLine(new TimelineLine(name, newLine.label, newLine.color, !newLine.hide));
}

addLines(names) {
Expand Down Expand Up @@ -287,7 +330,16 @@ class TimelineLine {
url: '../../statistics/dataset/' + self.name,
dataType: 'json'
}).done(function (data, status, jqxhr) {
self.data(data);
data = data.data;

const serial = [];
for (const key in data) {
const {year, month, day} = parseDate(key);
serial.push([Date.UTC(year, month, day), data[key]]);
}
serial.sort((a, b) => a[0] - b[0]);

self.data(serial);
self.loaded(true);
}).fail(function () {
self.data(null);
Expand Down Expand Up @@ -364,8 +416,6 @@ class TimelineLine {
}
}

MB.Timeline.TimelineViewModel = TimelineViewModel;

(function () {
// Closure over utility functions.
var showTooltip = function (x, y, contents) {
Expand Down Expand Up @@ -441,7 +491,7 @@ MB.Timeline.TimelineViewModel = TimelineViewModel;
reset();
previousPoint = item.dataIndex;
setItemTooltip(item,
graph === 'rate' ? MB.text.Timeline.RateTooltipCloser : undefined,
graph === 'rate' ? stats.rateTooltipCloser : undefined,
graph === 'rate' ? 2 : undefined);
}
} else if ($(element).data('plot').getEvent(pos)) {
Expand Down Expand Up @@ -556,3 +606,14 @@ MB.Timeline.TimelineViewModel = TimelineViewModel;
}
};
})();

$.ajax({
dataType: 'json',
url: './type-data',
}).done(function (data) {
buildTypeStats(data);

$(function () {
ko.applyBindings(new TimelineViewModel());
});
});
6 changes: 0 additions & 6 deletions root/statistics/dataset.tt

This file was deleted.

0 comments on commit 9e815be

Please sign in to comment.