<commit_before><?php

namespace Zephia\ZLeader\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use DataGrid;
use Zephia\ZLeader\Model\Lead;
use Zephia\ZLeader\Model\Field;
 
class LeadController extends Controller
{
    public function index()
    {
        $columnables = Field::columnables()->get();
        $filtrables = Field::filtrables()->get();
        return view('ZLeader::lead.index', ['columnables' => $columnables, 'filtrables' => $filtrables]);
    }

    public function show(Request $request, $lead_id)
    {
        $lead = Lead::with(['values', 'values.field'])->find($lead_id);

        $response_data = $lead;

        $response_data['form_name'] = $lead->form->name;
        $response_data['area_name'] = $lead->form->area->name;
        $response_data['company_name'] = $lead->form->area->company->name;

        unset(
            $response_data['updated_at'], 
            $response_data['notify'], 
            $response_data['form_id'], 
            $response_data['fb_leadgen_id'], 
            $response_data['user_agent'],
            $response_data['form']
        );

        return Response::json($response_data);
    }

    public function datagrid()
    {
        $lead_model = Lead::query();

        if (app('user') !== false) {
            $company_id = app('user')->company_id;
            if (app('user')->inRole(app('users_role'))) {
                $lead_model->whereHas('form', function($query) use ($company_id) {
                    return $query->whereHas('area', function($query) use ($company_id) {
                        return $query
                            ->where('company_id', '=', $company_id);
                    });
                });
            }
        }

        $leads = $lead_model->get();

        $data = [];
        $keys = [];

        foreach ($leads as $lead) {
            $row = [];
            $row['id'] = $lead->id;
            $row['date'] = $lead->created_at->format('d/m/Y H:i:s');
            $row['created_at'] = $lead->created_at;
            $row['company_name'] = $lead->form->area->company->name;
            $row['area_name'] = $lead->form->area->name;
            $row['form_name'] = $lead->form->name;
            $row['utm_source'] = $lead->utm_source;
            $row['utm_medium'] = $lead->utm_medium;
            $row['utm_campaign'] = $lead->utm_campaign;
            $row['utm_content'] = $lead->utm_content;
            $row['utm_term'] = $lead->utm_term;
            $row['referer'] = $lead->referer;

            $keys[] = 'id';
            $keys[] = 'date';
            $keys[] = 'created_at';
            $keys[] = 'company_name';
            $keys[] = 'area_name';
            $keys[] = 'form_name';
            $keys[] = 'utm_source';
            $keys[] = 'utm_medium';
            $keys[] = 'utm_campaign';
            $keys[] = 'utm_content';
            $keys[] = 'utm_term';
            $keys[] = 'referer';

            // inject values
            foreach ($lead->values as $lead_value) {
                $row[$lead_value->key] = $lead_value->value;

                if(!in_array($lead_value->key, $keys)) {
                    $keys[] = $lead_value->key;
                }
            }
            
            $data[] = $row;
        }

        //dd($data);

        return DataGrid::make($data, $keys, [
            'sort' => 'id',
            'direction' => 'desc',
            'csv_delimiter' => ';',
        ]);
    }
}
<commit_msg>Limit datagrid to 10000 records<commit_after><?php

namespace Zephia\ZLeader\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use DataGrid;
use Zephia\ZLeader\Model\Lead;
use Zephia\ZLeader\Model\Field;
 
class LeadController extends Controller
{
    public function index()
    {
        $columnables = Field::columnables()->get();
        $filtrables = Field::filtrables()->get();
        return view('ZLeader::lead.index', ['columnables' => $columnables, 'filtrables' => $filtrables]);
    }

    public function show(Request $request, $lead_id)
    {
        $lead = Lead::with(['values', 'values.field'])->find($lead_id);

        $response_data = $lead;

        $response_data['form_name'] = $lead->form->name;
        $response_data['area_name'] = $lead->form->area->name;
        $response_data['company_name'] = $lead->form->area->company->name;

        unset(
            $response_data['updated_at'], 
            $response_data['notify'], 
            $response_data['form_id'], 
            $response_data['fb_leadgen_id'], 
            $response_data['user_agent'],
            $response_data['form']
        );

        return Response::json($response_data);
    }

    public function datagrid()
    {
        $lead_model = Lead::query();

        if (app('user') !== false) {
            $company_id = app('user')->company_id;
            if (app('user')->inRole(app('users_role'))) {
                $lead_model->whereHas('form', function($query) use ($company_id) {
                    return $query->whereHas('area', function($query) use ($company_id) {
                        return $query
                            ->where('company_id', '=', $company_id);
                    });
                });
            }
        }

        $leads = $lead_model->orderBy('id', 'desc')->take(10000)->get();

        $data = [];
        $keys = [];

        foreach ($leads as $lead) {
            $row = [];
            $row['id'] = $lead->id;
            $row['date'] = $lead->created_at->format('d/m/Y H:i:s');
            $row['created_at'] = $lead->created_at;
            $row['company_name'] = $lead->form->area->company->name;
            $row['area_name'] = $lead->form->area->name;
            $row['form_name'] = $lead->form->name;
            $row['utm_source'] = $lead->utm_source;
            $row['utm_medium'] = $lead->utm_medium;
            $row['utm_campaign'] = $lead->utm_campaign;
            $row['utm_content'] = $lead->utm_content;
            $row['utm_term'] = $lead->utm_term;
            $row['referer'] = $lead->referer;

            $keys[] = 'id';
            $keys[] = 'date';
            $keys[] = 'created_at';
            $keys[] = 'company_name';
            $keys[] = 'area_name';
            $keys[] = 'form_name';
            $keys[] = 'utm_source';
            $keys[] = 'utm_medium';
            $keys[] = 'utm_campaign';
            $keys[] = 'utm_content';
            $keys[] = 'utm_term';
            $keys[] = 'referer';

            // inject values
            foreach ($lead->values as $lead_value) {
                $row[$lead_value->key] = $lead_value->value;

                if(!in_array($lead_value->key, $keys)) {
                    $keys[] = $lead_value->key;
                }
            }
            
            $data[] = $row;
        }

        //dd($data);

        return DataGrid::make($data, $keys, [
            'sort' => 'id',
            'direction' => 'desc',
            'csv_delimiter' => ';',
        ]);
    }
}

{'fraction_non_alphanumeric': 0.17937535171637592, 'fraction_numerical': 0.0014068655036578502, 'mean_word_length': 1.8619162640901772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '1996967', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2166, 'n_words': 331}