protected function AppAnalyticsFormBase::generateResponse in Apigee Edge 8
Requests analytics data and pass to the JavaScript chart drawing function.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\apigee_edge\Entity\AppInterface $app: The app entity that analytics data gets displayed.
string $metric: The filter parameter.
string $since: The start date parameter.
string $until: The end date parameter.
string $environment: The analytics environment to query.
See also
apigee_edge.analytics.js
1 call to AppAnalyticsFormBase::generateResponse()
- AppAnalyticsFormBase::buildForm in src/
Form/ AppAnalyticsFormBase.php - Form constructor.
File
- src/
Form/ AppAnalyticsFormBase.php, line 371
Class
- AppAnalyticsFormBase
- App analytics form builder for developer- and team apps.
Namespace
Drupal\apigee_edge\FormCode
protected function generateResponse(array &$form, AppInterface $app, string $metric, string $since, string $until, string $environment) : void {
$analytics = [];
try {
$analytics = $this
->getAnalytics($app, $metric, $since, $until, $environment);
} catch (MomentException $e) {
$this
->messenger()
->addError($this
->t('Invalid date parameters.'));
} catch (\Exception $e) {
$this
->messenger()
->addError($this
->t('Unable to retrieve analytics data. Please try again.'));
watchdog_exception('apigee_edge', $e);
}
$date_time_zone = new \DateTimeZone(date_default_timezone_get());
$timezone_offset = $date_time_zone
->getOffset(new \DateTime());
$form['#attached']['drupalSettings']['analytics']['timezone_offset'] = $timezone_offset / 60;
// Pass every necessary data to JavaScript.
// Possible parameters:
// - metric: name of the requested metric,
// - timestamps: all time units in the given time interval,
// - values: returned optimized metrics data.
// - skip_zero_values: skip the zero analytics values or not,
// - visualization_options: options for Google Charts draw() function,
// - visualization_options_to_date: which property values should be
// converted to JavaScript Date object on the client-side (timestamps),
// - version: Google Charts library version (default is the current stable),
// - language: to load a chart formatted for a specific locale,
// - chart_container: ID attribute of the chart's HTML container element.
if (isset($analytics['stats']['data'][0]['metric'][0]['values'])) {
// Store analytics data in private temp storage.
$analytics['metric'] = $form['controls']['metrics']['#options'][$metric];
$this->store
->set($data_id = Crypt::randomBytesBase64(), $analytics);
$form['export_csv']['#url'] = Url::fromRoute('apigee_edge.export_analytics.csv', [
'data_id' => $data_id,
]);
$form['#attached']['drupalSettings']['analytics']['metric'] = $form['controls']['metrics']['#options'][$metric];
$form['#attached']['drupalSettings']['analytics']['timestamps'] = $analytics['TimeUnit'];
$form['#attached']['drupalSettings']['analytics']['values'] = $analytics['stats']['data'][0]['metric'][0]['values'];
$form['#attached']['drupalSettings']['analytics']['skip_zero_values'] = FALSE;
$form['#attached']['drupalSettings']['analytics']['language'] = $this
->currentUser()
->getPreferredLangcode();
$form['#attached']['drupalSettings']['analytics']['chart_container'] = $form['chart']['#attributes']['id'];
// Visualization options for Google Charts draw() function,
// must be JSON encoded before passing.
// @see: https://developers.google.com/chart/interactive/docs/gallery/linechart#configuration-options
$visualization_options = [
'width' => '100%',
'legend' => 'none',
'interpolateNulls' => 'true',
'hAxis' => [
'viewWindow' => [
'min' => !empty($analytics['TimeUnit']) ? min($analytics['TimeUnit']) : 0,
'max' => !empty($analytics['TimeUnit']) ? max($analytics['TimeUnit']) : 0,
],
'gridlines' => [
'count' => -1,
'units' => [
'days' => [
'format' => [
'MMM dd',
],
],
'hours' => [
'format' => [
'HH:mm',
'ha',
],
],
],
],
'minorGridlines' => [
'units' => [
'hours' => [
'format' => [
'hh:mm:ss a',
'ha',
],
],
],
],
],
];
$form['#attached']['drupalSettings']['analytics']['visualization_options'] = json_encode($visualization_options);
$visualization_options_to_date = [
'hAxis.viewWindow.min',
'hAxis.viewWindow.max',
];
$form['#attached']['drupalSettings']['analytics']['visualization_options_to_date'] = $visualization_options_to_date;
}
else {
$form['chart']['#attributes']['class'][] = 'chart-container-no-data';
$form['chart']['no_data_text'] = [
'#markup' => $this
->t('No performance data is available for the criteria you supplied.'),
];
}
}