<commit_before><?php namespace Exolnet\Instruments;

use App;
use Auth;
use Cache;
use Closure;
use Exception;
use Exolnet\Instruments\Drivers\Driver;
use Exolnet\Instruments\Middleware\InstrumentsMiddleware;
use Illuminate\Container\Container;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Session;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Str;
use Swift_Message;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Instruments
{
	/**
	 * @var \Exolnet\Instruments\Drivers\Driver
	 */
	private $driver;
	/**
	 * @var \Illuminate\Container\Container
	 */
	private $app;

	/**
	 * Instruments constructor.
	 *
	 * @param \Illuminate\Container\Container $app
	 */
	public function __construct(Container $app)
	{
		$this->app = $app;
		$this->driver = $app['instruments.driver'];

		$this->driver->tags([
			'app' => config('instruments.application') ?: Str::slug(config('app.name')),
			'env' => App::environment(),
		]);
	}

	/**
	 * @return \Exolnet\Instruments\Drivers\Driver
	 */
	public function getDriver()
	{
		return $this->driver;
	}

	/**
	 * @return void
	 */
	public function boot()
	{
		$listeners = config('instruments.listeners');

		foreach ($listeners as $listener) {
			$listenMethod = 'listen'. Str::studly($listener);
			$this->$listenMethod();
		}
	}

	/**
	 * @return void
	 */
	protected function listenHttp()
	{
		/** @var \Illuminate\Foundation\Http\Kernel $kernel */
		$kernel = app(Kernel::class);

		$kernel->pushMiddleware(InstrumentsMiddleware::class);
	}

	/**
	 * @return void
	 */
	protected function listenDatabase()
	{
		app('events')->listen('illuminate.query', function($query, $bindings, $time, $connection) {
			$query = trim($query);

			if (preg_match('/^(SELECT|UPDATE|DELETE)/i', $query, $match)) {
				$type  = strtolower($match[1]);
				$table = $this->extractTableName($query, 'FROM');
			} elseif (preg_match('/^(INSERT|REPLACE)/i', $query, $match)) {
				$type  = strtolower($match[1]);
				$table = $this->extractTableName($query, 'INTO');
			} else {
				$type  = 'questions';
				$table = 'null';
			}

			$this->driver->flashTags(compact('connection', 'table', 'type'))->timing('sql.query_time', $time);
		});
	}

	/**
	 * @param string $query
	 * @param string $afterKeyword<commit_msg>Add missing app namespace on queries metrics<commit_after><?php namespace Exolnet\Instruments;

use App;
use Auth;
use Cache;
use Closure;
use Exception;
use Exolnet\Instruments\Drivers\Driver;
use Exolnet\Instruments\Middleware\InstrumentsMiddleware;
use Illuminate\Container\Container;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Session;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Str;
use Swift_Message;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Instruments
{
	/**
	 * @var \Exolnet\Instruments\Drivers\Driver
	 */
	private $driver;
	/**
	 * @var \Illuminate\Container\Container
	 */
	private $app;

	/**
	 * Instruments constructor.
	 *
	 * @param \Illuminate\Container\Container $app
	 */
	public function __construct(Container $app)
	{
		$this->app = $app;
		$this->driver = $app['instruments.driver'];

		$this->driver->tags([
			'app' => config('instruments.application') ?: Str::slug(config('app.name')),
			'env' => App::environment(),
		]);
	}

	/**
	 * @return \Exolnet\Instruments\Drivers\Driver
	 */
	public function getDriver()
	{
		return $this->driver;
	}

	/**
	 * @return void
	 */
	public function boot()
	{
		$listeners = config('instruments.listeners');

		foreach ($listeners as $listener) {
			$listenMethod = 'listen'. Str::studly($listener);
			$this->$listenMethod();
		}
	}

	/**
	 * @return void
	 */
	protected function listenHttp()
	{
		/** @var \Illuminate\Foundation\Http\Kernel $kernel */
		$kernel = app(Kernel::class);

		$kernel->pushMiddleware(InstrumentsMiddleware::class);
	}

	/**
	 * @return void
	 */
	protected function listenDatabase()
	{
		app('events')->listen('illuminate.query', function($query, $bindings, $time, $connection) {
			$query = trim($query);

			if (preg_match('/^(SELECT|UPDATE|DELETE)/i', $query, $match)) {
				$type  = strtolower($match[1]);
				$table = $this->extractTableName($query, 'FROM');
			} elseif (preg_match('/^(INSERT|REPLACE)/i', $query, $match)) {
				$type  = strtolower($match[1]);
				$table = $this->extractTableName($query, 'INTO');
			} else {
				$type  = 'questions';
				$table = 'null';
			}

			$this->driver->flashTags(compact('connection', 'table', 'type'))->timing('app.query_time', $time);
		});
	}

	/**
	 * @param string $query
	 * @param string $afterKeyword
{'fraction_non_alphanumeric': 0.17719336063806854, 'fraction_numerical': 0.0008622547962923044, 'mean_word_length': 4.184357541899441, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2715260', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1536, 'n_words': 285}