Closed abkrim closed 2 days ago
Hey @abkrim , it's hard to know how to isolate the issue. Can you give me a simple way to recreate this with:
Thanks
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use PDPhilip\Elasticsearch\Eloquent\Model;
class WorkAnalyzer extends Model
{
const CREATED_AT = null;
const UPDATED_AT = null;
const MAX_SIZE = 2000;
protected $connection = 'elasticsearch';
/**
* Check __construct method
*
* @var string
*/
protected $index = 'work-analyzers';
protected $casts = [
'datetime' => 'datetime',
];
protected $guarded = [];
public function getConnectionName(): string
{
return $this->connection;
}
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$mapping = require base_path('elk/mappings/work-analyzers.php');
$this->fillable = $this->processMapping($mapping['properties']);
if (app()->environment('testing')) {
$this->index = 'test-analyzers';
}
}
private function processMapping(array $properties): array
{
foreach ($properties as $key => $value) {
if (isset($value['type']) && $value['type'] === 'runtime') {
unset($properties[$key]);
} elseif (isset($value['script'])) {
unset($properties[$key]);
}
}
return array_keys($properties);
}
public function commandCenter(): BelongsTo|\PDPhilip\Elasticsearch\Relations\BelongsTo
{
return $this->belongsTo(CommandCenter::class, 'modem_id', 'id');
}
}
<?php
return [
'runtime' => [
'total_consumption_ok' => [
'type' => 'double',
'script' => [
'source' => "boolean isNumber(def input) {
if (input instanceof Number) {
return true;
}
if (input instanceof String) {
try {
Double.parseDouble(input);
return true;
} catch (NumberFormatException e) {
return false;
}
}
return false;
}
double sum = 0;
if (doc['pa1_w'].size() != 0 && isNumber(doc['pa1_w'].value) && doc['pa1_w'].value > 0) {
sum += doc['pa1_w'].value;
}
if (doc['pa2_w'].size() != 0 && isNumber(doc['pa2_w'].value) && doc['pa2_w'].value > 0) {
sum += doc['pa2_w'].value;
}
if (doc['pa3_w'].size() != 0 && isNumber(doc['pa3_w'].value) && doc['pa3_w'].value > 0) {
sum += doc['pa3_w'].value;
}
emit(sum);",
'lang' => 'painless',
],
],
],
'properties' => [
// Internal
'log_id' => [
'type' => 'keyword', // int to keyword
],
// Status
'status_code' => [
'type' => 'keyword',
],
'message' => [
'type' => 'keyword',
'index' => false,
'doc_values' => false,
],
'mod_bus_error' => [
'type' => 'keyword',
'index' => false,
'doc_values' => false,
],
//
'modem_id' => [
'type' => 'keyword',
],
'datetime' => [
'type' => 'date',
'format' => 'epoch_second||yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis',
'ignore_malformed' => true,
],
'provider' => [
'type' => 'keyword',
],
'response_time' => [
'type' => 'float',
'index' => false,
'doc_values' => false,
],
// End internal
// Hay muchos sin date al principio
'ip' => [
'type' => 'ip',
'doc_values' => false,
'index' => false,
],
'eneact_kwh' => [
'type' => 'float',
],
'enerea_kvarh' => [
'type' => 'float',
],
'eneapa_kvah' => [
'type' => 'float',
],
'eneact1_kwh' => [
'type' => 'float',
],
'eneact2_kwh' => [
'type' => 'float',
],
'eneact3_kwh' => [
'type' => 'float',
],
'enerea1_kvarh' => [
'type' => 'float',
],
'enerea2_kvarh' => [
'type' => 'float',
],
'enerea3_kvarh' => [
'type' => 'float',
],
'eneapa1_kvah' => [
'type' => 'float',
],
'eneapa2_kvah' => [
'type' => 'float',
],
'eneapa3_kvah' => [
'type' => 'float',
],
'powact_w' => [
'type' => 'float',
],
'powrea_var' => [
'type' => 'float',
],
'powapa_va' => [
'type' => 'float',
],
'freq_hz' => [
'type' => 'float',
],
'pa1_w' => [
'type' => 'float',
],
'pa2_w' => [
'type' => 'float',
],
'pa3_w' => [
'type' => 'float',
],
'pr1_var' => [
'type' => 'float',
],
'pr2_var' => [
'type' => 'float',
],
'pr3_var' => [
'type' => 'float',
],
'pp1_va' => [
'type' => 'float',
],
'pp2_va' => [
'type' => 'float',
],
'pp3_va' => [
'type' => 'float',
],
'iac1_a' => [
'type' => 'float',
],
'iac2_a' => [
'type' => 'float',
],
'iac3_a' => [
'type' => 'float',
],
'vac1_v' => [
'type' => 'float',
],
'vac2_v' => [
'type' => 'float',
],
'vac3_v' => [
'type' => 'float',
],
'lvac1_v' => [
'type' => 'float',
],
'lvac2_v' => [
'type' => 'float',
],
'lvac3_v' => [
'type' => 'float',
],
'pf1' => [
'type' => 'float',
],
'pf2' => [
'type' => 'float',
],
'pf3' => [
'type' => 'float',
],
'v_event' => [
'type' => 'byte',
],
'pf' => [
'type' => 'float',
],
'pa1_m_w' => [
'type' => 'float',
],
'pa1_x_w' => [
'type' => 'float',
],
'pa1_a_w' => [
'type' => 'float',
],
'pa2_m_w' => [
'type' => 'float',
],
'pa2_x_w' => [
'type' => 'float',
],
'pa2_a_w' => [
'type' => 'float',
],
'pa3_m_w' => [
'type' => 'float',
],
'pa3_x_w' => [
'type' => 'float',
],
'pa3_a_w' => [
'type' => 'float',
],
'pr1_m_var' => [
'type' => 'float',
],
'pr1_x_var' => [
'type' => 'float',
],
'pr1_a_var' => [
'type' => 'float',
],
'pr2_m_var' => [
'type' => 'float',
],
'pr2_x_var' => [
'type' => 'float',
],
'pr2_a_var' => [
'type' => 'float',
],
'pr3_m_var' => [
'type' => 'float',
],
'pr3_x_var' => [
'type' => 'float',
],
'pr3_a_var' => [
'type' => 'float',
],
'vac1_m_v' => [
'type' => 'float',
],
'vac1_x_v' => [
'type' => 'float',
],
'vac1_a_v' => [
'type' => 'float',
],
'vac2_m_v' => [
'type' => 'float',
],
'vac2_x_v' => [
'type' => 'float',
],
'vac2_a_v' => [
'type' => 'float',
],
'vac3_m_v' => [
'type' => 'float',
],
'vac3_x_v' => [
'type' => 'float',
],
'vac3_a_v' => [
'type' => 'float',
],
'iac1_m_a' => [
'type' => 'float',
],
'iac1_m_x' => [
'type' => 'float',
],
'iac1_x_a' => [
'type' => 'float',
],
'iac1_a_a' => [
'type' => 'float',
],
'iac2_m_a' => [
'type' => 'float',
],
'iac2_m_x' => [
'type' => 'float',
],
'iac2_x_a' => [
'type' => 'float',
],
'iac2_a_a' => [
'type' => 'float',
],
'iac3_m_a' => [
'type' => 'float',
],
'iac3_m_x' => [
'type' => 'float',
],
'iac3_x_a' => [
'type' => 'float',
],
'iac3_a_a' => [
'type' => 'float',
],
'cos1_m' => [
'type' => 'float',
],
'cos1_a' => [
'type' => 'float',
],
'cos1_x' => [
'type' => 'float',
],
'cos2_m' => [
'type' => 'float',
],
'cos2_a' => [
'type' => 'float',
],
'cos2_x' => [
'type' => 'float',
],
'cos3_m' => [
'type' => 'float',
],
'cos3_x' => [
'type' => 'float',
],
'cos3_a' => [
'type' => 'float',
],
'errors' => [
'type' => 'object',
'enabled' => false,
],
'timestamps' => [
'type' => 'object',
'enabled' => false,
],
'slim_data' => [
'type' => 'object',
'enabled' => false,
],
],
];
$workAnalyzer = WorkAnalyzer::create([
'datetime' => now(),
'modem_id' => 1
'provider' => 'provider'
'log_id' => '01JB8XY3SEXKT73GFJTPM0G5BE',
'status_code' => 500,
'timestamp' => [
'job' => 1730204454,
'delayed_at' => 1730204455,
],
'errors' => [
'message' => 'Error....."
'error' => null,
],
]);
But with all step I have added a debug for check add at same time taht your reply, I think is another problem for get correct connection.
a simple
test('response fails with failed via server error', function () {
Bus::fake();
$indice = 'analyzers';
$this->refreshTestingIndex($indice);
$wa = new \App\Models\WorkAnalyzer;
ray($wa->getConnectionName(), config('database.connections.elasticsearch'));
ray(\App\Models\WorkAnalyzer::first()); // This fails and connection name and confi is correct.
Thanks you.
Sorry, sorry.
I'm embarrassed. The mistake was mine,
I had forgotten because I didn't use in my current code that to use your library, I had more configurations in the .env.testing
and not see this details in debug.
ELK_DOCKER_HOST=https://localhost:9200
ELK_DOCKER_CA_BUNDLE=http_ca_local.crt
ELK_DOCKER_USER=elastic
ELK_DOCKER_PASSWORD='FORGET_UPDATE_THIS_PASSWORD_2'
# pdphilip/Elastic ES_AUTH_TYPE=http ES_HOSTS="https://localhost:9200" ES_USERNAME=elastic ES_PASSWORD='FORGET_UPDATE_THIS_PASSWORD_LAST_2' ES_CLOUD_ID= ES_API_ID= ES_API_KEY= ES_SSL_CERT=ssl/http_ca_local.crt ES_INDEX_PREFIX=
Sorry to have bothered you.
A cordial greeting.
Package version
v4.5.0
Describe the bug
I'm very happy with the package, but I'm refactoring some things I had using the original Elasticsearch library, and when refactoring to use model X, the problem comes from the tests.
When I try to create a doc I get an authentication failure on my local test node, telling me that the user does not exist
Errors with create or createWithoutRefresh
To Reproduce
Steps to reproduce the behavior:
Test
Code below is question of test. Call to external API get a falied() response and must be save in Elasticsearch
Error throw
Expected behavior
Well, I tested it in Tinker and it works in production and locally too. Simple, I expected it to save the document and return the data something like in Tinker.
Screenshots: If applicable, add screenshots to help explain your problem.
Component Versions (Paste in the
require
section from your composer.json file):I have reviewed both the .env and the .env.testing and laso debug wuth show me a correct configuration:
Apreciate some help or ideas for debug
More debug
I have added a debug for check
test-analyzers
. Not problem with other index for work with fake data and seeding, namedwork-analyzers
Connjection setup is correct.