X-Git-Url: http://git.rot13.org/?p=angular-drzb;a=blobdiff_plain;f=angular-server.pl;h=3da071c45c29a2d0b81e77e43ae2e0b91a83e1a7;hp=458337a0bb3b32834a0d4bc52a621c54410b8df0;hb=HEAD;hpb=c26e9a237701fe63be1fd9bf898942c15f96ef7e diff --git a/angular-server.pl b/angular-server.pl index 458337a..3da071c 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -6,7 +6,8 @@ use Time::HiRes; use Clone qw(clone); use Mojo::UserAgent; -our $VERSION = '0.3'; +our $VERSION = `git describe`; +chomp $VERSION; sub new_uuid { Time::HiRes::time * 100000 } @@ -18,9 +19,13 @@ my $couchdb_database = 'drzb2013v2'; 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 ); @@ -68,6 +73,16 @@ helper locale => sub { return $locale{ $lang } || "MISSING $lang $_[1]"; }; +helper ip => sub { + my $self = shift; + return + $self->req->headers->header('X-Forwarded-For') + || $self->req->headers->header('X-Real-IP') + || $self->tx->{remote_address} + ; +}; + + get '/js/services.js' => sub { my $self = shift; $self->stash( VERSION => $VERSION ); @@ -95,6 +110,12 @@ get '/lang/:lang/.template' => sub { $self->render( $self->stash('template') , lang => $self->stash('lang') ); }; +get '/lang/:lang/template/*template' => sub { # angular-ui templates + my $self = shift; + my $path = '/template/' . $self->stash('template'); + warn "# render_static $path"; + $self->render_static( $path ); +}; get '/data/' => sub { my $self = shift; @@ -174,6 +195,12 @@ any [ 'post' ] => '/data/:database/:entity' => sub { $json->{entity} = $entity; warn "NEW $id\n"; } + + $json->{x_audit} = { + t => Time::HiRes::time, + ip => $self->ip(), + }; + warn "## $database $entity $id body ",dump($self->req->body, $json); my $new = _couchdb_put "/$database/$entity.$id" => $json; @@ -200,6 +227,19 @@ get '/:database/_design/:design/_view/:view' => sub { if ( my $param = $self->req->url->query->clone->remove('callback')->remove('format')->to_string ) { $url .= '?' . $param } + + if ( exists $view_cache->{$url}->{time} ) { + if ( time() - $view_cache->{$url}->{time} < 60 ) { + warn "HIT CouchDB cache $url"; + $view_cache->{$url}->{hit}++; + return _render_jsonp( $self, $view_cache->{$url}->{json} ); + } else { + warn "REFRESH CouchDB cache $url"; + $view_cache->{$url}->{refresh}++; + } + } else { + $view_cache->{$url}->{miss}++; + } warn "CouchDB proxy $url"; my $json = _couchdb_get($url); @@ -215,8 +255,8 @@ get '/:database/_design/:design/_view/:view' => sub { 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); }); @@ -224,7 +264,29 @@ if ( doc.user.organization != '' ) { } }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) @@ -233,7 +295,33 @@ if ( doc.user.organization != '' ) { 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(); + $view_cache->{$url}->{json} = $json; + + warn "# view_cache ",dump($view_cache); + _render_jsonp( $self, $json ); }; @@ -249,15 +337,13 @@ hook after_dispatch => sub { return if $self->res->headers->header('ETag'); -warn "## body $body"; - my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum; $self->res->headers->header('ETag' => $our_etag); my $browser_etag = $self->req->headers->header('If-None-Match'); return unless $browser_etag && $browser_etag eq $our_etag; - $self->app->log->info("HTTP cache hit ", dump( $self->req->url->to_string ), $our_etag ); + $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" ); $self->res->code(304); $self->res->body('');