<commit_before><?php
namespace Moschini\PerfToolBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use HarUtils\HarFile;

use DbUtils\SitesDb;

class DefaultController extends Controller
{
	/**
     * @Route("/")
     * @Route("/index")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        return array(
            'files' => $this->getFilesFromDb($request), 
        );
    }

	/**
     * @Route("/send")
     * @Template()
     */
    public function sendAction(Request $request)
    {
        $site = $request->get('site');
        $defaultData = array('type' => 'har', 'site' => $site);

        $form = $this->createFormBuilder($defaultData)
            //->add('type', 'choice', array('choices' => array('har' => 'har', 'loadtime' => 'loadtime')))
            ->add('site', 'text', array(
                'attr' => array(
                    'placeholder' => 'Site name',
                )
            ))
            ->add('url', 'text', array(
                'attr' => array(
                    'placeholder' => 'http://www.google.com',
                )
            ))
            ->getForm();

        if($request->isMethod('POST'))
        {
            $form->bind($request);
            if($form->isValid())
            {
                $data = $form->getData();
                $msg = array(
                    'url' => $data['url'],
                    'site' => $data['site'],
                    'account' => 'me',
                    'type' => 'har', //$data['type'],
                );
                $this->get('old_sound_rabbit_mq.upload_picture_producer')->publish(json_encode($msg), 'perftest');
                return $this->redirect($this->generateUrl('moschini_perftool_default_done', array('site' => $site)));
            }
        }
        return array('form' => $form->createView());
        
    }
	
    /**
     * @Route("/done")
     * @Template()
     */
    public function doneAction()
    {
        return array();
    }
	
	/**
     * @Route("/graph")
     * @Template()
     */
    public function graphAction(Request $request)
	{
		$urls = array();
		$datas = array();

        $files = $this->getFilesFromDb($request);

        if($files)
        {
            foreach($files as $har)
            {
                foreach($har->getPages() as $page)
                {
                    $url = $page->getUrl();
                    $url_key = $url->getUid();
                    if(!array_key_exists($url_key, $urls))
                    {
                        $urls[ $url_key ] = $url;
                    }

                    if(!array_key_exists($url_key, $datas)){
                        $datas[ $url_key ] = array();
                    }

                    $datas[ $url_key ][ ] = $page->getLoadTime() / 1000;
                }
            }
        }
        
		return array(
			'datas' => $datas,
			'urls' => $urls,
			);
    }

    private function getFilesFromDb($request)
    {
        return SitesDb::getFilesFromFilter($request->get('site'), $request->get('url'));
    }

	/**
     * @Route("/time")
     * @Template()
     */
    public function timeAction(Request $request)
	{

        $files = $this->getFilesFromDb($request);
        $values = array();
        $urls = array();

        foreach($files as $har)<commit_msg>Fix bug when no files are returned in controller<commit_after><?php
namespace Moschini\PerfToolBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use HarUtils\HarFile;

use DbUtils\SitesDb;

class DefaultController extends Controller
{
	/**
     * @Route("/")
     * @Route("/index")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        return array(
            'files' => $this->getFilesFromDb($request), 
        );
    }

	/**
     * @Route("/send")
     * @Template()
     */
    public function sendAction(Request $request)
    {
        $site = $request->get('site');
        $defaultData = array('type' => 'har', 'site' => $site);

        $form = $this->createFormBuilder($defaultData)
            //->add('type', 'choice', array('choices' => array('har' => 'har', 'loadtime' => 'loadtime')))
            ->add('site', 'text', array(
                'attr' => array(
                    'placeholder' => 'Site name',
                )
            ))
            ->add('url', 'text', array(
                'attr' => array(
                    'placeholder' => 'http://www.google.com',
                )
            ))
            ->getForm();

        if($request->isMethod('POST'))
        {
            $form->bind($request);
            if($form->isValid())
            {
                $data = $form->getData();
                $msg = array(
                    'url' => $data['url'],
                    'site' => $data['site'],
                    'account' => 'me',
                    'type' => 'har', //$data['type'],
                );
                $this->get('old_sound_rabbit_mq.upload_picture_producer')->publish(json_encode($msg), 'perftest');
                return $this->redirect($this->generateUrl('moschini_perftool_default_done', array('site' => $site)));
            }
        }
        return array('form' => $form->createView());
        
    }
	
    /**
     * @Route("/done")
     * @Template()
     */
    public function doneAction()
    {
        return array();
    }
	
	/**
     * @Route("/graph")
     * @Template()
     */
    public function graphAction(Request $request)
	{
		$urls = array();
		$datas = array();

        $files = $this->getFilesFromDb($request);

        if($files)
        {
            foreach($files as $har)
            {
                foreach($har->getPages() as $page)
                {
                    $url = $page->getUrl();
                    $url_key = $url->getUid();
                    if(!array_key_exists($url_key, $urls))
                    {
                        $urls[ $url_key ] = $url;
                    }

                    if(!array_key_exists($url_key, $datas)){
                        $datas[ $url_key ] = array();
                    }

                    $datas[ $url_key ][ ] = $page->getLoadTime() / 1000;
                }
            }
        }
        
		return array(
			'datas' => $datas,
			'urls' => $urls,
			);
    }

    private function getFilesFromDb($request)
    {
        return SitesDb::getFilesFromFilter($request->get('site'), $request->get('url'));
    }

	/**
     * @Route("/time")
     * @Template()
     */
    public function timeAction(Request $request)
	{

        $files = $this->getFilesFromDb($request);
        $values = array();
        $urls = array();

        foreach((array)$files as $har)
{'fraction_non_alphanumeric': 0.16578874410867758, 'fraction_numerical': 0.0011089548100914888, 'mean_word_length': 1.6094032549728752, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1562615', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2009, 'n_words': 349}