<commit_before><?php
/*
 * This file is part of the Goteo Package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the README.md
 * and LICENSE files that was distributed with this source code.
 */

namespace Goteo\Controller\Api;

use Symfony\Component\HttpFoundation\Request;
use Goteo\Application\Exception\ControllerAccessDeniedException;
use Goteo\Application\Exception\ControllerException;
use Goteo\Application\Config;
use Goteo\Library\Text;
use Goteo\Application\Currency;
use Goteo\Payment\Payment;
use Goteo\Core\Model;
use Goteo\Model\Project;
use Goteo\Model\Call;
use Goteo\Model\Invest;
use Goteo\Model\Image;
use Goteo\Model\Origin;
use Goteo\Util\Stats\Stats;


class AdminChartsApiController extends ChartsApiController {

    public function __construct() {
        parent::__construct();
        // Activate replica read for this controller (cache already activated in AbstractApiController)
        \Goteo\Core\DB::replica(true);
        if(!$this->user || !$this->user->hasPerm('view-any-project')) {
            throw new ControllerAccessDeniedException();
        }
    }


    protected function getAggregatesSQLFilter($type, Request $request) {
        $values = $filter = [];
        $project_key = $type === 'invests' ? 'invest.project' : 'project.id';
        if($request->query->has('call')) {
            $filter[] = "$project_key IN (SELECT project FROM call_project WHERE call_project.call=:call)";
            $values[':call'] = $request->query->get('call');
        }
        return [$filter, $values];
    }

    /**
     * Groups data in time units (5min, 1 hour, 1 day, etc)
     * @param  Request $request [description]
     */<commit_msg>Add matcher,channel,user,consultant filters to aggregates timeline<commit_after><?php
/*
 * This file is part of the Goteo Package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the README.md
 * and LICENSE files that was distributed with this source code.
 */

namespace Goteo\Controller\Api;

use Symfony\Component\HttpFoundation\Request;
use Goteo\Application\Exception\ControllerAccessDeniedException;
use Goteo\Application\Exception\ControllerException;
use Goteo\Application\Config;
use Goteo\Library\Text;
use Goteo\Application\Currency;
use Goteo\Payment\Payment;
use Goteo\Core\Model;
use Goteo\Model\Project;
use Goteo\Model\Call;
use Goteo\Model\Invest;
use Goteo\Model\Image;
use Goteo\Model\Origin;
use Goteo\Util\Stats\Stats;


class AdminChartsApiController extends ChartsApiController {

    public function __construct() {
        parent::__construct();
        // Activate replica read for this controller (cache already activated in AbstractApiController)
        \Goteo\Core\DB::replica(true);
        if(!$this->user || !$this->user->hasPerm('view-any-project')) {
            throw new ControllerAccessDeniedException();
        }
    }


    protected function getAggregatesSQLFilter($type, Request $request) {
        $values = $filter = [];
        $project_key = $type === 'invests' ? 'invest.project' : 'project.id';
        $user_key = $type === 'invests' ? 'invest.user' : 'project.owner';
        if($request->query->has('call')) {
            $filter[] = "$project_key IN (SELECT project FROM call_project a WHERE a.call=:call)";
            $values[':call'] = $request->query->get('call');
        }
        if($request->query->has('matcher')) {
            $filter[] = "$project_key IN (SELECT project_id FROM matcher_project b WHERE b.matcher_id=:matcher)";
            $values[':matcher'] = $request->query->get('matcher');
        }
        if($request->query->has('channel')) {
            if($type === 'invests')
                $filter[] = "invest.project IN (SELECT id FROM project c WHERE c.node=:channel)";
            else
                $filter[] = "project.node=:channel";
            $values[':channel'] = $request->query->get('channel');
        }
        if($request->query->has('project')) {
            $filter[] = "$project_key=:project";
            $values[':project'] = $request->query->get('project');
        }
        if($request->query->has('user')) {
            $filter[] = "$user_key=:user";
            $values[':user'] = $request->query->get('user');
        }
        if($request->query->has('consultant')) {
            $filter[] = "$project_key IN (SELECT project FROM user_project d WHERE d.user=:consultant)";
            $values[':consultant'] = $request->query->get('consultant');
        }
        return [$filter, $values];
    }

    /**
     * Groups data in time units (5min, 1 hour, 1 day, etc)
     * @param  Request $request [description]
     */
{'fraction_non_alphanumeric': 0.14589989350372737, 'fraction_numerical': 0.0012779552715654952, 'mean_word_length': 3.108486439195101, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '1082174', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1386, 'n_words': 325}