<commit_before><?php

namespace App\Api\v1\Endpoints;


use App\Api\v1\Components\Mock;
use Illuminate\Http\Request;
use App\ElasticUtil;

class Terminal extends BaseEndpoint
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {
        $q = $request->input('q');

        // uncomment to revert to mock data
        //$ret = (new Mock())->generateMockData($q);
        //return response()->json($ret);

        $util = new ElasticUtil();
        $match = '{
        "size" : 50,
        "query" : {
        "match" : { REPLACE }
    }
    }';

        $matchAll = '{
        "size" : 50,
        "query" : {
        "match_all" : { }
    }
    }';

        if (empty($q)) {
            $query = $matchAll;
        } else {
            $query = str_replace('REPLACE', '"_all":"'.$q.'"', $match);
        }

        $data = $util->getTerminalData(json_decode($query, true));

        foreach ($data['hits'] as $item) {
            $source = $item['_source'];

            // TODO: write an adapter for different data types
            if (! isset($source['syslog_message'])) {
                // filter out everything non syslog
                // TODO: move pre-filtering to elastic query
                continue;
            }

            $syslogMessage = $source['syslog_message']);
            if ($syslogMessage === null) {
                // TODO: handle decoding failure
                continue;
            }

            $message = [<commit_msg>fix typo<commit_after><?php

namespace App\Api\v1\Endpoints;


use App\Api\v1\Components\Mock;
use Illuminate\Http\Request;
use App\ElasticUtil;

class Terminal extends BaseEndpoint
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {
        $q = $request->input('q');

        // uncomment to revert to mock data
        //$ret = (new Mock())->generateMockData($q);
        //return response()->json($ret);

        $util = new ElasticUtil();
        $match = '{
        "size" : 50,
        "query" : {
        "match" : { REPLACE }
    }
    }';

        $matchAll = '{
        "size" : 50,
        "query" : {
        "match_all" : { }
    }
    }';

        if (empty($q)) {
            $query = $matchAll;
        } else {
            $query = str_replace('REPLACE', '"_all":"'.$q.'"', $match);
        }

        $data = $util->getTerminalData(json_decode($query, true));

        foreach ($data['hits'] as $item) {
            $source = $item['_source'];

            // TODO: write an adapter for different data types
            if (! isset($source['syslog_message'])) {
                // filter out everything non syslog
                // TODO: move pre-filtering to elastic query
                continue;
            }

            $syslogMessage = $source['syslog_message'];
            if ($syslogMessage === null) {
                // TODO: handle decoding failure
                continue;
            }

            $message = [
{'fraction_non_alphanumeric': 0.14892926670992862, 'fraction_numerical': 0.003893575600259572, 'mean_word_length': 1.6215986394557824, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 11, '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': '5992045', 'n_tokens_mistral': 919, 'n_tokens_neox': 885, 'n_words': 201}