ai_agents_test-1.0.0-alpha1/src/Entity/AgentTestGroupResult.php

src/Entity/AgentTestGroupResult.php
<?php

declare(strict_types=1);

namespace Drupal\ai_agents_test\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\ai_agents_test\AgentTestGroupResultInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the agent test group result entity class.
 *
 * @ContentEntityType(
 *   id = "ai_agents_test_group_result",
 *   label = @Translation("Agent Test Group Result"),
 *   label_collection = @Translation("Agent Test Group Results"),
 *   label_singular = @Translation("agent test group result"),
 *   label_plural = @Translation("agent test group results"),
 *   label_count = @PluralTranslation(
 *     singular = "@count agent test group results",
 *     plural = "@count agent test group results",
 *   ),
 *   handlers = {
 *     "list_builder" = "Drupal\ai_agents_test\AgentTestGroupResultListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "add" = "Drupal\ai_agents_test\Form\AgentTestGroupResultForm",
 *       "edit" = "Drupal\ai_agents_test\Form\AgentTestGroupResultForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
 *       "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "ai_agents_test_group_result",
 *   admin_permission = "administer ai_agents_test_group_result",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid",
 *     "owner" = "uid",
 *   },
 *   links = {
 *     "collection" = "/admin/content/ai-agents-test/group/result",
 *     "add-form" = "/admin/content/ai-agents-test/group/result/add",
 *     "canonical" = "/admin/content/ai-agents-test/group/result/{ai_agents_test_group_result}",
 *     "edit-form" = "/admin/content/ai-agents-test/group/result/{ai_agents_test_group_result}/edit",
 *     "delete-form" = "/admin/content/ai-agents-test/group/result/{ai_agents_test_group_result}/delete",
 *     "delete-multiple-form" = "/admin/content/ai-agents-test/group/result/delete-multiple",
 *   },
 * )
 */
final class AgentTestGroupResult extends ContentEntityBase implements AgentTestGroupResultInterface {

  use EntityChangedTrait;
  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage): void {
    parent::preSave($storage);
    if (!$this->getOwnerId()) {
      // If no owner has been set explicitly, make the anonymous user the owner.
      $this->setOwnerId(0);
    }

    // Check if the amount of tests is the same as the amount of results.
    $total = count($this->ai_agents_test_group->entity->tests);
    $finished = count($this->ai_agents_test_results);
    if ($total === $finished) {
      // We set it to finished.
      $this->set('process_status', 'finished');
      // We calculate the success percentage.
      $success = 0;
      foreach ($this->ai_agents_test_results as $result) {
        if ($result->entity->result->value === 'success') {
          $success++;
        }
      }
      $success_percentage = ($success / $total) * 100;
      $this->set('success_percentage', $success_percentage);
      // Check if the success percentage is greater than or equal to the value.
      if ($success_percentage >= $this->ai_agents_test_group->entity->approval_percentage->value) {
        $this->set('result', 'success');
      }
      else {
        $this->set('result', 'failure');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {

    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Label'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['description'] = BaseFieldDefinition::create('text_long')
      ->setLabel(t('Description'))
      ->setDescription(t('A description of the test group result.'))
      ->setDisplayOptions('form', [
        'type' => 'text_textarea',
        'weight' => 10,
        'rows' => 4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'type' => 'text_default',
        'label' => 'above',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Author'))
      ->setSetting('target_type', 'user')
      ->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'author',
        'weight' => 15,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the agent test group result was created.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'timestamp',
        'weight' => 20,
      ])
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the agent test group result was last edited.'));

    $fields['ai_agents_test_group'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Test Group'))
      ->setSetting('target_type', 'ai_agents_test_group')
      ->setRequired(TRUE)
      ->setDescription(t('The test group that was run.'))
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => 25,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
        'weight' => 25,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['ai_agents_test_results'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Test Results'))
      ->setSetting('target_type', 'ai_agents_test_result')
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
      ->setDescription(t('The individual test results for this group run.'))
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => 30,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
        'weight' => 30,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['success_percentage'] = BaseFieldDefinition::create('float')
      ->setLabel(t('Success Percentage'))
      ->setDescription(t('The percentage of successful tests in this group run.'))
      ->setDisplayOptions('form', [
        'type' => 'number',
        'weight' => 32,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'number_decimal',
        'weight' => 32,
        'settings' => [
          'thousand_separator' => '',
          'decimal_separator' => '.',
          'scale' => 2,
          'prefix_suffix' => TRUE,
        ],
      ])
      ->setDisplayConfigurable('view', TRUE);

    // So we can add cron jobs or other runners in the future.
    $fields['process_status'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Process Status'))
      ->setDescription(t('The current status of the test group run.'))
      ->setDefaultValue('pending')
      ->setSetting('allowed_values', [
        'pending' => 'Pending',
        'running' => 'Running',
        'completed' => 'Completed',
        'failed' => 'Failed',
      ])
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 35,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'list_default',
        'weight' => 35,
      ])
      ->setDisplayConfigurable('view', TRUE);

    // The chat provider used for the test.
    $fields['chat_provider'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Chat Provider'))
      ->setDescription(t('The chat provider used for running the tests.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 40,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 40,
      ])
      ->setDisplayConfigurable('view', TRUE);

    // The model used for the test.
    $fields['model'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Model'))
      ->setDescription(t('The model used for running the tests.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 45,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 45,
      ])
      ->setDisplayConfigurable('view', TRUE);

    // Result can either be success, failure or inconclusive. Select list.
    $fields['result'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Result'))
      ->setDescription(t('The overall result of the test group run.'))
      ->setSetting('allowed_values', [
        'success' => 'Success',
        'failure' => 'Failure',
      ])
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 34,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'list_default',
        'weight' => 34,
      ])
      ->setDisplayConfigurable('view', TRUE);

    // Add evaluation provider field.
    $fields['eval_provider'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Evaluation Provider'))
      ->setDescription(t('The provider used for evaluating the test results.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 50,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 50,
      ])
      ->setDisplayConfigurable('view', TRUE);

    // Add evaluation model field.
    $fields['eval_model'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Evaluation Model'))
      ->setDescription(t('The model used for evaluating the test results.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 55,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 55,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['config_reset'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Config Reset'))
      ->setDescription(t('Whether configuration was reset for this test group run.'))
      ->setDefaultValue(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'weight' => 60,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'weight' => 60,
        'settings' => [
          'format' => 'yes-no',
        ],
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['summary'] = BaseFieldDefinition::create('text_long')
      ->setLabel(t('Summary'))
      ->setDescription(t('A summary of the test group results.'))
      ->setDisplayOptions('form', [
        'type' => 'text_textarea',
        'weight' => 65,
        'rows' => 6,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'type' => 'text_default',
        'label' => 'above',
        'weight' => 65,
      ])
      ->setDisplayConfigurable('view', TRUE);

    return $fields;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc