You are here

public function AppAnalyticsFormBase::buildForm in Apigee Edge 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

1 call to AppAnalyticsFormBase::buildForm()
DeveloperAppAnalyticsForm::buildForm in src/Form/DeveloperAppAnalyticsForm.php
Form constructor.
1 method overrides AppAnalyticsFormBase::buildForm()
DeveloperAppAnalyticsForm::buildForm in src/Form/DeveloperAppAnalyticsForm.php
Form constructor.

File

src/Form/AppAnalyticsFormBase.php, line 106

Class

AppAnalyticsFormBase
App analytics form builder for developer- and team apps.

Namespace

Drupal\apigee_edge\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, ?AppInterface $app = NULL) {

  // Little sanity check, child classes must set this parameter from route
  // before they call parent.
  if ($app === NULL) {
    $this
      ->messenger()
      ->addError($this
      ->t('Something went wrong.'));
    $this
      ->logger('apigee_edge')
      ->critical('App parameter was missing when the app analytics form got built.');
    return $form;
  }
  $config = $this
    ->config('apigee_edge.common_app_settings');
  $analytics_environment = $config
    ->get('analytics_environment');
  $analytics_available_environments = $config
    ->get('analytics_available_environments');
  $form_state
    ->disableRedirect();
  $form['#attached']['library'][] = 'apigee_edge/apigee_edge.analytics';
  $form['#attributes']['class'][] = 'apigee-edge-app-analytics';
  $form['controls'] = [
    '#type' => 'container',
  ];
  $form['controls']['label_container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'controls-label',
      ],
    ],
  ];
  $form['controls']['label_container']['label'] = [
    '#markup' => $this
      ->t('Filter:'),
  ];
  $form['controls']['environment'] = [
    '#type' => 'value',
    '#value' => $analytics_environment,
  ];
  if (count($analytics_available_environments) > 1) {
    $form['controls']['environment'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#title' => t('Environment'),
      '#title_display' => 'invisible',
      '#default_value' => $analytics_environment,
      '#options' => array_combine($analytics_available_environments, $analytics_available_environments),
    ];
  }
  $form['controls']['metrics'] = [
    '#type' => 'select',
    '#options' => [
      'avg(total_response_time)' => $this
        ->t('Average response time'),
      'max(total_response_time)' => $this
        ->t('Max response time'),
      'min(total_response_time)' => $this
        ->t('Min response time'),
      'sum(message_count)' => $this
        ->t('Message count'),
      'sum(is_error)' => $this
        ->t('Error count'),
    ],
    '#default_value' => 'avg(total_response_time)',
    '#title' => t('Metrics'),
    '#title_display' => 'invisible',
  ];
  $form['controls']['since'] = [
    '#type' => 'datetime',
  ];
  $form['controls']['date_separator_container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'date-separator',
      ],
    ],
  ];
  $form['controls']['date_separator_container']['date_separator'] = [
    '#markup' => '-',
  ];
  $form['controls']['until'] = [
    '#type' => 'datetime',
  ];
  $form['controls']['quick_date_picker'] = [
    '#type' => 'select',
    '#options' => [
      '1d' => $this
        ->t('Last Day'),
      '1w' => $this
        ->t('Last 7 Days'),
      '2w' => $this
        ->t('Last 2 Weeks'),
      'custom' => $this
        ->t('Custom range'),
    ],
    '#title' => t('Date range'),
    '#title_display' => 'invisible',
  ];
  $form['controls']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Apply'),
  ];
  $offset = date('Z') / 3600;
  if ($offset > 0) {
    $offset = "+{$offset}";
  }
  elseif ($offset === 0) {
    $offset = "±{$offset}";
  }
  $form['timezone'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->t('Your timezone: @timezone (UTC@offset)', [
      '@timezone' => date_default_timezone_get(),
      '@offset' => $offset,
    ]),
  ];
  $form['export_csv'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Export CSV'),
    '#attributes' => [
      'role' => 'button',
    ],
  ];
  $form['chart'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'chart_container',
    ],
  ];
  $metric = $this
    ->getRequest()->query
    ->get('metric');
  $since = $this
    ->getRequest()->query
    ->get('since');
  $until = $this
    ->getRequest()->query
    ->get('until');
  $environment = $this
    ->getRequest()->query
    ->get('environment');
  if ($this
    ->validateQueryString($form, $metric, $since, $until, $environment)) {
    $form['controls']['metrics']['#default_value'] = $metric;
    $since_datetime = DrupalDatetime::createFromTimestamp($since);
    $since_datetime
      ->setTimezone(new \Datetimezone(date_default_timezone_get()));
    $until_datetime = DrupalDatetime::createFromTimestamp($until);
    $until_datetime
      ->setTimezone(new \Datetimezone(date_default_timezone_get()));
    $form['controls']['since']['#default_value'] = $since_datetime;
    $form['controls']['until']['#default_value'] = $until_datetime;
    $form['controls']['quick_date_picker']['#default_value'] = 'custom';
    $form['controls']['environment']['#default_value'] = $environment;
  }
  else {
    $default_since_value = new DrupalDateTime();
    $default_since_value
      ->sub(new \DateInterval('P1D'));
    $default_until_value = new DrupalDateTime();
    $form['controls']['since']['#default_value'] = $default_since_value;
    $form['controls']['until']['#default_value'] = $default_until_value;
    $metric = $form['controls']['metrics']['#default_value'];
    $since = $default_since_value
      ->getTimestamp();
    $until = $default_until_value
      ->getTimestamp();
    $environment = $analytics_environment;
  }
  if (empty($form_state
    ->getUserInput())) {

    // The last parameter allows to expose and make analytics environment
    // configurable later on the form.
    $this
      ->generateResponse($form, $app, $metric, $since, $until, $environment);
  }
  return $form;
}