<commit_before><?php

namespace Moschini\PerfToolBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use DbUtils\SitesDb;

class SitesController extends Controller
{
    /**
     * @Route("/done")
     * @Template()
     */
    public function doneAction()
    {
        return array();
    }

	/**
     * @Route("/")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        $sites = SitesDb::getManagedSites();
        $find = array();
        $site = $request->get('site');

        if($site)
        {
            $find = array('site' => $site);
        }

        $configs = SitesDb::getSitesConfig($find);

        return array(
            'current_site' => $site,
            'sites' => $configs
        );
    }


    private function getUrls($urls)
    {
        $urls = str_replace("\r\n", "\n", $urls);
        $urls = str_replace("\r", "\n", $urls);
        return array_values(
            array_filter(
                array_map('trim', 
                    explode("\n", $urls)
                )
            )
        );
    }

    /**
     * @Route("/new")
     * @Template()
     */
    public function addAction(Request $request)
    {
        $site = $request->get('site');
        $defaultData = array('interval' => 180, 'site' => $site, 'nb' => 10, 'agent' => 'desktop');

        $form = $this->createFormBuilder($defaultData)
            ->add('site', 'text', array(
                'attr' => array(
                    'placeholder' => 'Site name',
                )
            ))
            ->add('urls', 'textarea', array(
                'attr' => array(
                    'placeholder' => 'One urls per line',
                    'rows' => 5,
                    'class' => 'input-xxlarge',
                   )
            ))
            ->add('agent', 'choice', 
                array('choices' => array(
                    'desktop' => 'Desktop', 
                    'mobile' => 'Mobile'), 
                'expanded' => true,
                'label' => 'User-Agent'))
            ->add('nb', 'integer', array(
                'label' => 'Number of requests',
                'attr' => array(
                    'class' => 'input-mini',
                ),
                'constraints' => array(
                    new Range(array('min' => 1, 'max' => 20)),
                ),
            ))
            ->add('interval', 'choice', 
                array('choices' => array(
                    5 => '5 min', 
                    10 => '10 min', 
                    30 => '30 min', 
                    60 => '1 h', 
                    180 => '3 h', 
                    360 => '6 h', 
                    720 => '12 h', 
                    1440 => '24 h'),
                     'attr' => array(
                        'class' => 'input-small'
            )))
            ->getForm();

        if($request->isMethod('POST'))
        {
            $form->bind($request);
            if($form->isValid())
            {
                $data = $form->getData();
                $data['urls'] = $this->getUrls($data['urls']);
                
                if(SitesDb::insertToDb($data))
                {
                    return $this->redirect($this->generateUrl('moschini_perftool_sites_index', array('site' => $site)));
                }
                else
                {
                    echo 'Error while inserting to DB. Please try-again or contact an administrator if the problem persist.';
                }   
            }<commit_msg>Redirect to created site when adding a new<commit_after><?php

namespace Moschini\PerfToolBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use DbUtils\SitesDb;

class SitesController extends Controller
{
    /**
     * @Route("/done")
     * @Template()
     */
    public function doneAction()
    {
        return array();
    }

	/**
     * @Route("/")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        $sites = SitesDb::getManagedSites();
        $find = array();
        $site = $request->get('site');

        if($site)
        {
            $find = array('site' => $site);
        }

        $configs = SitesDb::getSitesConfig($find);

        return array(
            'current_site' => $site,
            'sites' => $configs
        );
    }


    private function getUrls($urls)
    {
        $urls = str_replace("\r\n", "\n", $urls);
        $urls = str_replace("\r", "\n", $urls);
        return array_values(
            array_filter(
                array_map('trim', 
                    explode("\n", $urls)
                )
            )
        );
    }

    /**
     * @Route("/new")
     * @Template()
     */
    public function addAction(Request $request)
    {
        $site = $request->get('site');
        $defaultData = array('interval' => 180, 'site' => $site, 'nb' => 10, 'agent' => 'desktop');

        $form = $this->createFormBuilder($defaultData)
            ->add('site', 'text', array(
                'attr' => array(
                    'placeholder' => 'Site name',
                )
            ))
            ->add('urls', 'textarea', array(
                'attr' => array(
                    'placeholder' => 'One urls per line',
                    'rows' => 5,
                    'class' => 'input-xxlarge',
                   )
            ))
            ->add('agent', 'choice', 
                array('choices' => array(
                    'desktop' => 'Desktop', 
                    'mobile' => 'Mobile'), 
                'expanded' => true,
                'label' => 'User-Agent'))
            ->add('nb', 'integer', array(
                'label' => 'Number of requests',
                'attr' => array(
                    'class' => 'input-mini',
                ),
                'constraints' => array(
                    new Range(array('min' => 1, 'max' => 20)),
                ),
            ))
            ->add('interval', 'choice', 
                array('choices' => array(
                    5 => '5 min', 
                    10 => '10 min', 
                    30 => '30 min', 
                    60 => '1 h', 
                    180 => '3 h', 
                    360 => '6 h', 
                    720 => '12 h', 
                    1440 => '24 h'),
                     'attr' => array(
                        'class' => 'input-small'
            )))
            ->getForm();

        if($request->isMethod('POST'))
        {
            $form->bind($request);
            if($form->isValid())
            {
                $data = $form->getData();
                $data['urls'] = $this->getUrls($data['urls']);
                
                if(SitesDb::insertToDb($data))
                {
                    return $this->redirect($this->generateUrl('moschini_perftool_sites_index', array('site' => $data['site'])));
                }
                else
                {
                    echo 'Error while inserting to DB. Please try-again or contact an administrator if the problem persist.';
                }   
            }
{'fraction_non_alphanumeric': 0.152765126870527, 'fraction_numerical': 0.01067013662979831, 'mean_word_length': 1.2929594272076372, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, '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': '6437506', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2005, 'n_words': 411}