<commit_before><?php

namespace CtSearchBundle\Controller;

use CtSearchBundle\CtSearchBundle;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use CtSearchBundle\Classes\IndexManager;
use Symfony\Component\HttpFoundation\Response;

class ConsoleController extends Controller {

  /**
   * @Route("/console", name="console")
   */
  public function consoleAction(Request $request) {
    $indexes = IndexManager::getInstance()->getElasticInfo();
    $targetChoices = array();
    foreach ($indexes as $indexName => $info) {
      $choices = array();
      if(isset($info['mappings'])){
        foreach ($info['mappings'] as $mapping) {
          $choices[$indexName . '.' . $mapping['name']] = $indexName . '.' . $mapping['name'];
        }
      }
      $targetChoices[$indexName] = $choices;
    }
    $listener = function(\Symfony\Component\Form\FormEvent $event) {
      $data = $event->getData();
      $data["searchQuery"] = json_encode(json_decode($data["searchQuery"]), JSON_PRETTY_PRINT);
      $event->setData($data);
    };
    if($request->get('id') != null){
      $savedQuery = IndexManager::getInstance()->getSavedQuery($request->get('id'));
    }
    $values = array(
      'mapping' => isset($savedQuery['target']) ? $savedQuery['target'] : null,
      'searchQuery' => isset($savedQuery['definition']) ? $savedQuery['definition'] : json_encode(array('query' => array('match_all' => new \stdClass())), JSON_PRETTY_PRINT),
      'deleteByQuery' => false,
    );
    $form = $this->createFormBuilder($values)
        ->add('mapping', 'choice', array(
          'label' => $this->get('translator')->trans('Target'),
          'choices' => array('' => $this->get('translator')->trans('Select a target')) + $targetChoices,
          'required' => true,
        ))
        ->add('searchQuery', 'textarea', array(
          'label' => $this->get('translator')->trans('Search query (JSON)'),
          'required' => true
        ))
        ->add('deleteByQuery', 'checkbox', array(
          'label' => $this->get('translator')->trans('Delete records matching this query'),
          'required' => false
        ))
        ->add('execute', 'submit', array('label' => $this->get('translator')->trans('Execute')))
        ->addEventListener(\Symfony\Component\Form\FormEvents::PRE_SUBMIT, $listener)
        ->getForm();

    $form->handleRequest($request);
    $params = array(
      'title' => $this->get('translator')->trans('Console'),
      'form' => $form->createView(),
      'main_menu_item' => 'console',
    );
    if ($form->isValid()) {
      $data = $form->getData();
      $query = $data['searchQuery'];
      $query_r = json_decode($data['searchQuery'], true);
      $index = strpos($data['mapping'], '.') !== 0 ? explode('.', $data['mapping'])[0] : '.' . explode('.', $data['mapping'])[1];
      $mapping = strpos($data['mapping'], '.') !== 0 ? explode('.', $data['mapping'])[1] : explode('.', $data['mapping'])[2];
      try {
        if (!$data['deleteByQuery']) {
          $res = IndexManager::getInstance()->search($index, $query, isset($query_r['from']) ? $query_r['from'] : 0, isset($query_r['size']) ? $query_r['size'] : 20);
          $params['results'] = $this->dumpVar($res);
          if(isset($res['aggregations']) && count($res['aggregations']) > 0){
            $params['facets'] = json_encode($res['aggregations'], JSON_PRETTY_PRINT);
          }
          $params['engine_response'] = $this->getFormattedEngineReponse($res);
          $saveUrlParams = array();
          if($request->get('id') != null){
            $saveUrlParams['id'] = $request->get('id');
          }
          $saveUrlParams['target'] = $data['mapping'];
          $saveUrlParams['query'] = $data['searchQuery'];
          $saveUrl = $this->generateUrl('console-save', $saveUrlParams);
          $params['save_url'] = $saveUrl;<commit_msg>Migrating console controller to s3<commit_after><?php

namespace CtSearchBundle\Controller;

use CtSearchBundle\CtSearchBundle;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\HttpFoundation\Request;
use CtSearchBundle\Classes\IndexManager;
use Symfony\Component\HttpFoundation\Response;

class ConsoleController extends Controller {

  /**
   * @Route("/console", name="console")
   */
  public function consoleAction(Request $request) {
    $indexes = IndexManager::getInstance()->getElasticInfo();
    $targetChoices = array();
    foreach ($indexes as $indexName => $info) {
      $choices = array();
      if(isset($info['mappings'])){
        foreach ($info['mappings'] as $mapping) {
          $choices[$indexName . '.' . $mapping['name']] = $indexName . '.' . $mapping['name'];
        }
      }
      $targetChoices[$indexName] = $choices;
    }
    $listener = function(\Symfony\Component\Form\FormEvent $event) {
      $data = $event->getData();
      $data["searchQuery"] = json_encode(json_decode($data["searchQuery"]), JSON_PRETTY_PRINT);
      $event->setData($data);
    };
    if($request->get('id') != null){
      $savedQuery = IndexManager::getInstance()->getSavedQuery($request->get('id'));
    }
    $values = array(
      'mapping' => isset($savedQuery['target']) ? $savedQuery['target'] : null,
      'searchQuery' => isset($savedQuery['definition']) ? $savedQuery['definition'] : json_encode(array('query' => array('match_all' => new \stdClass())), JSON_PRETTY_PRINT),
      'deleteByQuery' => false,
    );
    $form = $this->createFormBuilder($values)
        ->add('mapping', ChoiceType::class, array(
          'label' => $this->get('translator')->trans('Target'),
          'choices' => array($this->get('translator')->trans('Select a target') => '') + $targetChoices,
          'required' => true,
        ))
        ->add('searchQuery', TextareaType::class, array(
          'label' => $this->get('translator')->trans('Search query (JSON)'),
          'required' => true
        ))
        ->add('deleteByQuery', CheckboxType::class, array(
          'label' => $this->get('translator')->trans('Delete records matching this query'),
          'required' => false
        ))
        ->add('execute', SubmitType::class, array('label' => $this->get('translator')->trans('Execute')))
        ->addEventListener(\Symfony\Component\Form\FormEvents::PRE_SUBMIT, $listener)
        ->getForm();

    $form->handleRequest($request);
    $params = array(
      'title' => $this->get('translator')->trans('Console'),
      'form' => $form->createView(),
      'main_menu_item' => 'console',
    );
    if ($form->isValid()) {
      $data = $form->getData();
      $query = $data['searchQuery'];
      $query_r = json_decode($data['searchQuery'], true);
      $index = strpos($data['mapping'], '.') !== 0 ? explode('.', $data['mapping'])[0] : '.' . explode('.', $data['mapping'])[1];
      $mapping = strpos($data['mapping'], '.') !== 0 ? explode('.', $data['mapping'])[1] : explode('.', $data['mapping'])[2];
      try {
        if (!$data['deleteByQuery']) {
          $res = IndexManager::getInstance()->search($index, $query, isset($query_r['from']) ? $query_r['from'] : 0, isset($query_r['size']) ? $query_r['size'] : 20);
          $params['results'] = $this->dumpVar($res);
          if(isset($res['aggregations']) && count($res['aggregations']) > 0){
            $params['facets'] = json_encode($res['aggregations'], JSON_PRETTY_PRINT);
          }
          $params['engine_response'] = $this->getFormattedEngineReponse($res);
          $saveUrlParams = array();
          if($request->get('id') != null){
            $saveUrlParams['id'] = $request->get('id');
          }
          $saveUrlParams['target'] = $data['mapping'];
          $saveUrlParams['query'] = $data['searchQuery'];
          $saveUrl = $this->generateUrl('console-save', $saveUrlParams);
          $params['save_url'] = $saveUrl;
{'fraction_non_alphanumeric': 0.20542117418256958, 'fraction_numerical': 0.002552570803452048, 'mean_word_length': 3.588957055214724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '5895092', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2442, 'n_words': 384}