<commit_before>package MediaWords::Controller::Api::V2::Topics::Stories;
use Modern::Perl "2015";
use MediaWords::CommonLibs;

use strict;
use warnings;
use base 'Catalyst::Controller';
use JSON;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use Moose;
use namespace::autoclean;
use List::Compare;
use Carp;
use Readonly;

use MediaWords::DBI::ApiLinks;
use MediaWords::Solr;
use MediaWords::TM::Snapshot;

Readonly my $DEFAULT_STORY_LIMIT => 10;

BEGIN { extends 'MediaWords::Controller::Api::V2::MC_Controller_REST' }

__PACKAGE__->config( action => { list_GET => { Does => [ qw( ~PublicApiKeyAuthenticated ~Throttled ~Logged ) ] }, } );

sub link_paging_key { return 'stories'; }

sub pre_deserialize($$)
{
    my ( $self, $c ) = @_;

    $c->stash->{ timespan } = MediaWords::TM::set_timespans_id_param( $c );
}

sub apibase : Chained('/') : PathPart('api/v2/topics') : CaptureArgs(1)
{
    my ( $self, $c, $topics_id ) = @_;

    $c->stash->{ topics_id } = $topics_id;
}

sub stories : Chained('apibase') : PathPart('stories') : CaptureArgs(0)
{

}

sub list : Chained('stories') : Args(0) : ActionClass('MC_REST')
{

}

# get any where clauses for media_id, link_to_stories_id, link_from_stories_id, stories_id params
sub _get_extra_where_clause($$)
{
    my ( $c, $timespans_id ) = @_;

    my $clauses = [];

    if ( my $media_id = $c->req->params->{ media_id } )
    {
        $media_id += 0;
        push( @{ $clauses }, "s.media_id = $media_id" );
    }

    if ( my $stories_id = $c->req->params->{ stories_id } )
    {
        $stories_id += 0;
        push( @{ $clauses }, "s.stories_id = $stories_id" );
    }

    if ( my $link_to_stories_id = $c->req->params->{ link_to_stories_id } )
    {
        $link_to_stories_id += 0;
        $timespans_id       += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.source_stories_id
        from snap.story_links sl
        where
            sl.ref_stories_id = $link_to_stories_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_from_stories_id = $c->req->params->{ link_from_stories_id } )
    {
        $link_from_stories_id += 0;
        $timespans_id         += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.ref_stories_id
        from snap.story_links sl
        where
            sl.source_stories_id = $link_from_stories_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_to_media_id = $c->req->params->{ link_to_media_id } )
    {
        $link_to_media_id += 0;
        $timespans_id     += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.source_stories_id
        from snap.story_links sl
            join timespans t using ( timespans_id )
            join snap.stories s on ( sl.ref_stories_id = s.stories_id and s.snapshots_id = t.snapshots_id )
        where
            s.media_id = $link_to_media_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_from_media_id = $c->req->params->{ link_from_media_id } )
    {
        $link_from_media_id += 0;
        $timespans_id       += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.ref_stories_id
        from snap.story_links sl
            join timespans t using ( timespans_id )
            join snap.stories s on ( sl.source_stories_id = s.stories_id and s.snapshots_id = t.snapshots_id )
        where
            s.media_id = $link_from_media_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    return '' unless ( @{ $clauses } );

    return 'and ' . join( ' and ', map { "( $_ ) " } @{ $clauses } );
}

sub list_GET
{
    my ( $self, $c ) = @_;

    my $timespan = MediaWords::TM::set_timespans_id_param( $c );<commit_msg>add support for q parameter for stories/list<commit_after>package MediaWords::Controller::Api::V2::Topics::Stories;
use Modern::Perl "2015";
use MediaWords::CommonLibs;

use strict;
use warnings;
use base 'Catalyst::Controller';
use JSON;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use Moose;
use namespace::autoclean;
use List::Compare;
use Carp;
use Readonly;

use MediaWords::DBI::ApiLinks;
use MediaWords::Solr;
use MediaWords::TM::Snapshot;

Readonly my $DEFAULT_STORY_LIMIT => 10;

BEGIN { extends 'MediaWords::Controller::Api::V2::MC_Controller_REST' }

__PACKAGE__->config( action => { list_GET => { Does => [ qw( ~PublicApiKeyAuthenticated ~Throttled ~Logged ) ] }, } );

sub link_paging_key { return 'stories'; }

sub pre_deserialize($$)
{
    my ( $self, $c ) = @_;

    $c->stash->{ timespan } = MediaWords::TM::set_timespans_id_param( $c );
}

sub apibase : Chained('/') : PathPart('api/v2/topics') : CaptureArgs(1)
{
    my ( $self, $c, $topics_id ) = @_;

    $c->stash->{ topics_id } = $topics_id;
}

sub stories : Chained('apibase') : PathPart('stories') : CaptureArgs(0)
{

}

sub list : Chained('stories') : Args(0) : ActionClass('MC_REST')
{

}

# get any where clauses for media_id, link_to_stories_id, link_from_stories_id, stories_id params
sub _get_extra_where_clause($$)
{
    my ( $c, $timespans_id ) = @_;

    my $clauses = [];

    if ( my $media_id = $c->req->params->{ media_id } )
    {
        $media_id += 0;
        push( @{ $clauses }, "s.media_id = $media_id" );
    }

    if ( my $stories_id = $c->req->params->{ stories_id } )
    {
        $stories_id += 0;
        push( @{ $clauses }, "s.stories_id = $stories_id" );
    }

    if ( my $link_to_stories_id = $c->req->params->{ link_to_stories_id } )
    {
        $link_to_stories_id += 0;
        $timespans_id       += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.source_stories_id
        from snap.story_links sl
        where
            sl.ref_stories_id = $link_to_stories_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_from_stories_id = $c->req->params->{ link_from_stories_id } )
    {
        $link_from_stories_id += 0;
        $timespans_id         += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.ref_stories_id
        from snap.story_links sl
        where
            sl.source_stories_id = $link_from_stories_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_to_media_id = $c->req->params->{ link_to_media_id } )
    {
        $link_to_media_id += 0;
        $timespans_id     += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.source_stories_id
        from snap.story_links sl
            join timespans t using ( timespans_id )
            join snap.stories s on ( sl.ref_stories_id = s.stories_id and s.snapshots_id = t.snapshots_id )
        where
            s.media_id = $link_to_media_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $link_from_media_id = $c->req->params->{ link_from_media_id } )
    {
        $link_from_media_id += 0;
        $timespans_id       += 0;
        push( @{ $clauses }, <<SQL );
s.stories_id in (
    select
            sl.ref_stories_id
        from snap.story_links sl
            join timespans t using ( timespans_id )
            join snap.stories s on ( sl.source_stories_id = s.stories_id and s.snapshots_id = t.snapshots_id )
        where
            s.media_id = $link_from_media_id and
            sl.timespans_id = $timespans_id
)
SQL

    }

    if ( my $q = $c->req->params->{ q } )
    {
        $q = "{ topic-timespan:$timespans_id } and ( $q )";
        my $solr_stories_id = MediaWords::Solr::search_for_stories_ids( $c->dbis, { q => $q } );

        my $ids_table = $c->dbis->get_temporary_ids_table( $solr_stories_id );
        push( @{ $clauses }, "s.stories_id in ( select id from $ids_table )" );
    }

    return '' unless ( @{ $clauses } );

    return 'and ' . join( ' and ', map { "( $_ ) " } @{ $clauses } );
}

sub list_GET
{
    my ( $self, $c ) = @_;

    my $timespan = MediaWords::TM::set_timespans_id_param( $c );
{'fraction_non_alphanumeric': 0.14085554866707997, 'fraction_numerical': 0.005455672659640421, 'mean_word_length': 2.5533039647577094, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5005976', 'n_tokens_mistral': 3099, 'n_tokens_neox': 3008, 'n_words': 595}