my $couchdb_view = "http://10.60.0.92:5984/_utils/document.html?$couchdb_database";
my $client = Mojo::UserAgent->new;
+our $view_cache;
+
sub _couchdb_put {
my ( $url, $data ) = @_;
+ $view_cache = undef;
+
$data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
my $json = Mojo::JSON->new->encode( $data );
# CouchDB proxy for _design _view
-our $view_cache;
-
get '/:database/_design/:design/_view/:view' => sub {
my $self = shift;
my $format = $self->param('format');
if ( doc.user.organization != '' ) {
if ( doc.user.organization )
emit(doc.user.organization, 1);
- if ( doc.user.persons ) {
- doc.user.persons.forEach( function(person) {
+ if ( doc.work.persons ) {
+ doc.work.persons.forEach( function(person) {
if ( person.organization )
emit(person.organization, 1);
});
}
}q|,
reduce => q| function(keys,values,rereduce) { return sum(values); } |,
- }
+ },
+ authors => {
+ map => q|
+function(doc) {
+ if ( doc.work.persons ) {
+ doc.work.persons.forEach( function(person) {
+ emit(person.surname+person.firstname ,
+ person
+ );
+ });
+ }
+ if ( doc.work.symposium_works ) {
+ doc.work.symposium_works.forEach( function(work) {
+ work.persons.forEach( function(person) {
+ emit(person.surname+person.firstname ,
+ person
+ );
+ });
+ });
+ }
+}
+ |,
+ },
}
};
$json = _couchdb_get($url)
if ( $format eq 'key_array' ) { # array of keys sorted by value
$json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ];
+ } elsif ( $format eq 'key_distinct' ) {
+
+ my $found;
+ my @rows = grep {
+ push @{ $found->{ $_->{key} } }, $_->{id};
+ $#{ $found->{ $_->{key} } } == 0; # take just first registration
+ } @{ $json->{rows} };
+
+ $json->{rows} = [ map {
+ $_->{distinct_ids} = $found->{ $_->{key} };
+ $_;
+ } @rows ];
+
+ $json->{distinct_rows} = scalar @{ $json->{rows} };
+ warn "## distinct stats ", dump( $found );
+
+ } elsif ( $format ) {
+
+ die "unknown format: $format";
+
}
$view_cache->{$url}->{time} = time();