From d21aba499f1aef655687d1029e78802ec3d38438 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 18:15:51 -0300 Subject: [PATCH 01/12] =?UTF-8?q?adding=20base=20structure=20using=20`ddev?= =?UTF-8?q?=20drush=20=E2=80=A6=20`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/modules/custom/telemetry/README.md | 30 ++++++++ .../telemetry/src/Form/TelemetryForm.php | 68 +++++++++++++++++++ .../custom/telemetry/telemetry.info.yml | 5 ++ .../custom/telemetry/telemetry.install | 6 ++ web/modules/custom/telemetry/telemetry.module | 6 ++ .../custom/telemetry/telemetry.routing.yml | 7 ++ 6 files changed, 122 insertions(+) create mode 100644 web/modules/custom/telemetry/README.md create mode 100644 web/modules/custom/telemetry/src/Form/TelemetryForm.php create mode 100644 web/modules/custom/telemetry/telemetry.info.yml create mode 100644 web/modules/custom/telemetry/telemetry.install create mode 100644 web/modules/custom/telemetry/telemetry.module create mode 100644 web/modules/custom/telemetry/telemetry.routing.yml diff --git a/web/modules/custom/telemetry/README.md b/web/modules/custom/telemetry/README.md new file mode 100644 index 0000000..a0cefd5 --- /dev/null +++ b/web/modules/custom/telemetry/README.md @@ -0,0 +1,30 @@ +## INTRODUCTION + +The telemetry module is a DESCRIBE_THE_MODULE_HERE. + +The primary use case for this module is: + +- Use case #1 +- Use case #2 +- Use case #3 + +## REQUIREMENTS + +DESCRIBE_MODULE_DEPENDENCIES_HERE + +## INSTALLATION + +Install as you would normally install a contributed Drupal module. +See: https://www.drupal.org/node/895232 for further information. + +## CONFIGURATION +- Configuration step #1 +- Configuration step #2 +- Configuration step #3 + +## MAINTAINERS + +Current maintainers for Drupal 10: + +- FIRST_NAME LAST_NAME (NICKNAME) - https://www.drupal.org/u/NICKNAME + diff --git a/web/modules/custom/telemetry/src/Form/TelemetryForm.php b/web/modules/custom/telemetry/src/Form/TelemetryForm.php new file mode 100644 index 0000000..5a67f71 --- /dev/null +++ b/web/modules/custom/telemetry/src/Form/TelemetryForm.php @@ -0,0 +1,68 @@ + 'textarea', + '#title' => $this->t('Message'), + '#required' => TRUE, + ]; + + $form['actions'] = [ + '#type' => 'actions', + 'submit' => [ + '#type' => 'submit', + '#value' => $this->t('Send'), + ], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state): void { + // @todo Validate the form here. + // Example: + // @code + // if (mb_strlen($form_state->getValue('message')) < 10) { + // $form_state->setErrorByName( + // 'message', + // $this->t('Message should be at least 10 characters.'), + // ); + // } + // @endcode + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this->messenger()->addStatus($this->t('The message has been sent.')); + $form_state->setRedirect(''); + } + +} diff --git a/web/modules/custom/telemetry/telemetry.info.yml b/web/modules/custom/telemetry/telemetry.info.yml new file mode 100644 index 0000000..d0eb14f --- /dev/null +++ b/web/modules/custom/telemetry/telemetry.info.yml @@ -0,0 +1,5 @@ +name: 'telemetry' +type: module +description: 'Provides some kind of telemetry.' +package: Custom +core_version_requirement: ^10 || ^11 diff --git a/web/modules/custom/telemetry/telemetry.install b/web/modules/custom/telemetry/telemetry.install new file mode 100644 index 0000000..8761a41 --- /dev/null +++ b/web/modules/custom/telemetry/telemetry.install @@ -0,0 +1,6 @@ + Date: Sun, 18 Aug 2024 19:48:29 -0300 Subject: [PATCH 02/12] Adding Telemetry as a trait for better reuse. --- .../telemetry/src/Module/TelemetryModule.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 web/modules/custom/telemetry/src/Module/TelemetryModule.php diff --git a/web/modules/custom/telemetry/src/Module/TelemetryModule.php b/web/modules/custom/telemetry/src/Module/TelemetryModule.php new file mode 100644 index 0000000..4c22501 --- /dev/null +++ b/web/modules/custom/telemetry/src/Module/TelemetryModule.php @@ -0,0 +1,69 @@ +post('https://hooks.slack.com/services/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', [ + 'headers' => ['Content-type' => 'Content-type: application/json'], + 'body' => json_encode([ + "blocks" => [ + [ + "type" => "header", + "text" => [ + "type" => "plain_text", + "text" => gettype($origin) === 'string' ? $origin : get_class($origin), + ] + ], + [ + "type" => "section", + "text" => [ + "type" => "plain_text", + "text" => self::lines([$message, self::lines($more_info)]), + ] + ], + [ + "type" => "section", + "fields" => [ + [ + "type" => "mrkdwn", + "text" => self::lines(["*Type:*", $type]), + ], + [ + "type" => "mrkdwn", + "text" => self::lines(["*Machine user:*", getenv('USER')]), + ], + ] + ], + ] + ]) + ]); + return $message; + } +} From 75f4c76b7986be3c60d5a1963770663dc86cba38 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 19:50:28 -0300 Subject: [PATCH 03/12] Adding Telemetry module with basic schema structure, some object parsers, installer and uninstaller methods and a message hook to provide telemetry. --- web/modules/custom/telemetry/telemetry.module | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/web/modules/custom/telemetry/telemetry.module b/web/modules/custom/telemetry/telemetry.module index 2b6c633..24a556e 100644 --- a/web/modules/custom/telemetry/telemetry.module +++ b/web/modules/custom/telemetry/telemetry.module @@ -4,3 +4,127 @@ * @file * Primary module hooks for telemetry module. */ + +declare(strict_types=1); + +namespace Drupal\telemetry; + +use Drupal\telemetry\Module\TelemetryModule as Telemetry; + +final class Module { + + use Telemetry; + + const TABLE_NAME = 'telemetry'; + + const TABLE_FIELDS = [ + 'id' => [ + 'type' => 'serial', + 'not null' => TRUE, + ], + 'message' => [ + 'type' => 'text', + 'not null' => TRUE, + 'description' => 'Message content.', + '#type' => 'textarea', + '#title' => 'Message', + ], + 'message_type' => [ + 'type' => 'varchar', + 'length' => 16, + 'not null' => TRUE, + 'description' => 'Message type.', + '#type' => 'select', + '#title' => 'Select the message type', + '#options' => [ + 'addMessage' => 'Message', + 'addError' => 'Error', + 'addStatus' => 'Status', + 'addWarning' => 'Warning', + ], + ], + ]; + + + private static function message(string $type = 'Status', string $method = '', string $message = ''): void { + \Drupal::messenger()->{"add{$type}"}(self::telemetry($type, $method, $message)); + } + + public static function extractSchema(array $array = []): array { + foreach ($array as $field => $sub) { + foreach ($sub as $index => $val) { + if ($index[0] === '#') { + unset($array[$field][$index]); + } + } + } + return $array; + } + + /** + * extractFields + * + * @param mixed $array + * @param mixed $selected + * @param mixed $translator_callback + * @param mixed $translate_fields + * @return array + */ + public static function extractFields(array $array, array $selected, callable $translator_callback, array $translate_fields = ['#title', '#options']): array { + foreach ($array as $field => $options) { + if (!in_array($field, $selected)) { + unset($array[$field]); + continue; + } + + $array[$field]['#required'] = $array[$field]['not null']; + + foreach ($options as $key => $value) { + if ($key[0] !== '#') { + unset($array[$field][$key]); + } + } + } + + foreach ($array as $field => $options) { + foreach ($options as $option => $value) { + if (in_array($option, $translate_fields)) { + if (gettype($value) === 'string') { + $array[$field][$option] = $translator_callback($value); + } + if (gettype($value) === 'array') { + foreach ($value as $sub => $data) { + $array[$field][$option][$sub] = $translator_callback($data); + } + } + } + } + } + return $array; + } + + public static function install(): void { + try { + \Drupal::database() + ->schema() + ->createTable(self::TABLE_NAME, [ + 'fields' => self::extractSchema(self::TABLE_FIELDS), + 'primary key' => ['id'], + ]); + } catch (\Throwable $th) { + self::message('Error', __METHOD__, 'Error creating ' . self::TABLE_NAME . ': '. $th->getMessage()); + } finally { + self::message('Status', __METHOD__, 'Table ' . self::TABLE_NAME . ' created.'); + } + } + + public static function uninstall(): void { + try { + \Drupal::database()->schema()->dropTable(self::TABLE_NAME); + } catch (\Throwable $th) { + self::message('Error', __METHOD__, 'Error deleting ' . self::TABLE_NAME . ': '. $th->getMessage()); + } finally { + self::message('Status', __METHOD__, 'Table ' . self::TABLE_NAME . ' deleted.'); + } + } +} From 02bc4e5abb8a00dec88af41691dccc0b5f121177 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 19:50:48 -0300 Subject: [PATCH 04/12] Adding module installer. --- .../custom/telemetry/telemetry.install | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/web/modules/custom/telemetry/telemetry.install b/web/modules/custom/telemetry/telemetry.install index 8761a41..74d6704 100644 --- a/web/modules/custom/telemetry/telemetry.install +++ b/web/modules/custom/telemetry/telemetry.install @@ -4,3 +4,25 @@ * @file * Install, update and uninstall functions for the telemetry module. */ + + +use Drupal\telemetry\Module as Module; + +/** + * @file + * Install and update functions for the Test Form module. + */ + +/** + * Implements hook_install(). + */ +function telemetry_install() { + Module::install(); +} + +/** + * Implements hook_uninstall(). + */ +function telemetry_uninstall() { + Module::uninstall(); +} From 73f31408e0f1becc715c148630a6207abe68abae Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 19:51:10 -0300 Subject: [PATCH 05/12] Adding better documentation. --- web/modules/custom/telemetry/README.md | 51 ++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/web/modules/custom/telemetry/README.md b/web/modules/custom/telemetry/README.md index a0cefd5..b479443 100644 --- a/web/modules/custom/telemetry/README.md +++ b/web/modules/custom/telemetry/README.md @@ -1,16 +1,19 @@ -## INTRODUCTION +## Telemetry Module -The telemetry module is a DESCRIBE_THE_MODULE_HERE. +This telemetry module is a functional test both to validate technical knowledge and to evaluate the possibilities of features offered by the most current versions of Drupal. The primary use case for this module is: -- Use case #1 -- Use case #2 -- Use case #3 +- Receive information about execution, installation, etc. quickly and clearly, without the need to access complex panels and systems, using everyday tools; +- Carefully monitor processes that require attention - such as those that may eventually generate additional costs, such as installation, build and/or inadvertent use; +- Store messages sent via telemetry locally and/or remotely; ## REQUIREMENTS -DESCRIBE_MODULE_DEPENDENCIES_HERE +- Drupal 10 or newer; +- DDEV 1.23.4 or newer; +- Composer version 2.7.7 or newer; +- PHP version 8.3.10 or newer; ## INSTALLATION @@ -18,13 +21,45 @@ Install as you would normally install a contributed Drupal module. See: https://www.drupal.org/node/895232 for further information. ## CONFIGURATION + - Configuration step #1 - Configuration step #2 - Configuration step #3 ## MAINTAINERS -Current maintainers for Drupal 10: +- John Murowaniecki 🜏 ( _jmurowaniecki_ · aka `0xD3C0de`); + + +## Contribute + +### Getting started + +First download and configure the project following the steps below: + +```bash +git clone git@github.com:jmurowaniecki/zoocha.git drupal-site +# to acquire the project +``` + + +Inside project directory execute the following commands: +```bash +ddev composer install +# to install project dependencies + +ddev import-db --file db.sql.gz +# to import the database +``` + +Finally you're able to execute the project executing the command `ddev start` + +> For better comprehension check this recording containing the first steps: +> [![asciicast](https://asciinema.org/a/gHIIVv3X6amNdcdThfc6ISj9D.svg)](https://asciinema.org/a/gHIIVv3X6amNdcdThfc6ISj9D) + + +### Creating the base -- FIRST_NAME LAST_NAME (NICKNAME) - https://www.drupal.org/u/NICKNAME +I used `ddev drush…` to create the base module and form as documented in the recording below. More information can be obtained by looking at the commits made. +[![asciicast](https://asciinema.org/a/z2DJTC3R9TqgYiiHQiWzj7Mlw.svg)](https://asciinema.org/a/z2DJTC3R9TqgYiiHQiWzj7Mlw) From b70b32554fe4d3a35a64d5ec03c30551ae213301 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 19:58:22 -0300 Subject: [PATCH 06/12] Adding admin menu link. --- web/modules/custom/telemetry/telemetry.links.menu.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 web/modules/custom/telemetry/telemetry.links.menu.yml diff --git a/web/modules/custom/telemetry/telemetry.links.menu.yml b/web/modules/custom/telemetry/telemetry.links.menu.yml new file mode 100644 index 0000000..1a9fb46 --- /dev/null +++ b/web/modules/custom/telemetry/telemetry.links.menu.yml @@ -0,0 +1,5 @@ +telemetry.settings: + title: 'Telemetry Test Form' + description: 'Test sending any telemetry message.' + route_name: telemetry.telemetry + parent: system.admin_config_system From 7b85f6e21600399797423a6c0a8a86124ff63249 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 20:11:08 -0300 Subject: [PATCH 07/12] Update form fields to enable dinamic structure and send test message. --- .../telemetry/src/Form/TelemetryForm.php | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/web/modules/custom/telemetry/src/Form/TelemetryForm.php b/web/modules/custom/telemetry/src/Form/TelemetryForm.php index 5a67f71..8d6a4e5 100644 --- a/web/modules/custom/telemetry/src/Form/TelemetryForm.php +++ b/web/modules/custom/telemetry/src/Form/TelemetryForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\telemetry\Module as Module; /** * Provides a Telemetry form. @@ -19,26 +20,29 @@ public function getFormId(): string { return 'telemetry_telemetry'; } + private function getFields(array $form = []): array { + return array_merge($form, Module::extractFields( + Module::TABLE_FIELDS, + ['message', 'message_type'], + function ($translate) { + return $this->t($translate); + })); + } + /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state): array { - - $form['message'] = [ - '#type' => 'textarea', - '#title' => $this->t('Message'), - '#required' => TRUE, - ]; - - $form['actions'] = [ - '#type' => 'actions', - 'submit' => [ - '#type' => 'submit', - '#value' => $this->t('Send'), - ], - ]; - - return $form; + Module::telemetry('Status', $this, 'Accessing method *'.__METHOD__.'*.'); + return $this->getFields([ + 'actions' => [ + '#type' => 'actions', + 'submit' => [ + '#type' => 'submit', + '#value' => $this->t('Send'), + ], + ] + ]); } /** @@ -61,6 +65,12 @@ public function validateForm(array &$form, FormStateInterface $form_state): void * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state): void { + Module::telemetry( + $form_state->getValue('message_type'), + $this, + 'Accessing method *'.__METHOD__.'*. ', + $form_state->getValue('message') + ); $this->messenger()->addStatus($this->t('The message has been sent.')); $form_state->setRedirect(''); } From 817b98d7fde71e4f99187aacac52468fafff4be4 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 21:56:26 -0300 Subject: [PATCH 08/12] Adding better phpdoc descriptions and fixing routes. Adding better documentation. --- web/modules/custom/telemetry/README.md | 5 --- .../telemetry/src/Form/TelemetryForm.php | 9 ++++ .../telemetry/src/Module/TelemetryModule.php | 11 +++-- .../custom/telemetry/telemetry.info.yml | 1 + .../custom/telemetry/telemetry.install | 6 --- web/modules/custom/telemetry/telemetry.module | 44 ++++++++++++++++--- .../custom/telemetry/telemetry.routing.yml | 2 +- 7 files changed, 58 insertions(+), 20 deletions(-) diff --git a/web/modules/custom/telemetry/README.md b/web/modules/custom/telemetry/README.md index b479443..85ced75 100644 --- a/web/modules/custom/telemetry/README.md +++ b/web/modules/custom/telemetry/README.md @@ -20,11 +20,6 @@ The primary use case for this module is: Install as you would normally install a contributed Drupal module. See: https://www.drupal.org/node/895232 for further information. -## CONFIGURATION - -- Configuration step #1 -- Configuration step #2 -- Configuration step #3 ## MAINTAINERS diff --git a/web/modules/custom/telemetry/src/Form/TelemetryForm.php b/web/modules/custom/telemetry/src/Form/TelemetryForm.php index 8d6a4e5..fe39396 100644 --- a/web/modules/custom/telemetry/src/Form/TelemetryForm.php +++ b/web/modules/custom/telemetry/src/Form/TelemetryForm.php @@ -20,6 +20,15 @@ public function getFormId(): string { return 'telemetry_telemetry'; } + /** + * getFields + * + * Should return form fields processed, merged with the provided ones and + * extracted taking the list of parameters. + * + * @param array $form Form fields to append. + * @return array Form fields processed. + */ private function getFields(array $form = []): array { return array_merge($form, Module::extractFields( Module::TABLE_FIELDS, diff --git a/web/modules/custom/telemetry/src/Module/TelemetryModule.php b/web/modules/custom/telemetry/src/Module/TelemetryModule.php index 4c22501..d41b5ff 100644 --- a/web/modules/custom/telemetry/src/Module/TelemetryModule.php +++ b/web/modules/custom/telemetry/src/Module/TelemetryModule.php @@ -24,13 +24,18 @@ public static function lines($lines, string $breaker = "\n"): string { * * Sends telemetry messages to the mothership. * - * @param string $message - * @return string + * @param mixed $type Type of message to be sent. + * @param mixed $origin Class origin of the message. + * @param mixed $message Message payload. + * @param mixed $more_info More information to append to the message. + * @return string Original message. */ public static function telemetry($type, $origin = __CLASS__, string $message = '', $more_info = []): string { + // $config = \Drupal::config('telemetry.settings'); + // die('
wololo 2
'.print_r($config->get('slack.key'), true)); $now = date('Y/m/d H:i:s'); \Drupal::httpClient() - ->post('https://hooks.slack.com/services/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', [ + ->post('https://hooks.slack.com/services/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', [ 'headers' => ['Content-type' => 'Content-type: application/json'], 'body' => json_encode([ "blocks" => [ diff --git a/web/modules/custom/telemetry/telemetry.info.yml b/web/modules/custom/telemetry/telemetry.info.yml index d0eb14f..c8cc39a 100644 --- a/web/modules/custom/telemetry/telemetry.info.yml +++ b/web/modules/custom/telemetry/telemetry.info.yml @@ -3,3 +3,4 @@ type: module description: 'Provides some kind of telemetry.' package: Custom core_version_requirement: ^10 || ^11 +configure: telemetry.settings diff --git a/web/modules/custom/telemetry/telemetry.install b/web/modules/custom/telemetry/telemetry.install index 74d6704..47b5083 100644 --- a/web/modules/custom/telemetry/telemetry.install +++ b/web/modules/custom/telemetry/telemetry.install @@ -5,14 +5,8 @@ * Install, update and uninstall functions for the telemetry module. */ - use Drupal\telemetry\Module as Module; -/** - * @file - * Install and update functions for the Test Form module. - */ - /** * Implements hook_install(). */ diff --git a/web/modules/custom/telemetry/telemetry.module b/web/modules/custom/telemetry/telemetry.module index 24a556e..b66fb33 100644 --- a/web/modules/custom/telemetry/telemetry.module +++ b/web/modules/custom/telemetry/telemetry.module @@ -46,10 +46,28 @@ final class Module { ]; + /** + * message + * + * Hook to send the Drupal message also to the Telemetry. + * + * @param mixed $type Type of the message (see Drupal class Messenger for more information https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Messenger%21Messenger.php/class/Messenger/10). + * @param mixed $method Originary method. + * @param mixed $message Message. + * @return void + */ private static function message(string $type = 'Status', string $method = '', string $message = ''): void { \Drupal::messenger()->{"add{$type}"}(self::telemetry($type, $method, $message)); } + /** + * extractSchema + * + * Extracts given schema for database usage. + * + * @param array $array Schema to be filtered. + * @return array Filtered schema. + */ public static function extractSchema(array $array = []): array { foreach ($array as $field => $sub) { foreach ($sub as $index => $val) { @@ -64,11 +82,13 @@ final class Module { /** * extractFields * - * @param mixed $array - * @param mixed $selected - * @param mixed $translator_callback - * @param mixed $translate_fields - * @return array + * Extracts schema and translates given fields. + * + * @param array $array Schema to be filtered. + * @param array $selected Selected filter. + * @param callable $translator_callback Callback to translator method. + * @param array $translate_fields Look for translate this fields. + * @return array Extracted and translated schema. */ public static function extractFields(array $array, array $selected, callable $translator_callback, array $translate_fields = ['#title', '#options']): array { foreach ($array as $field => $options) { @@ -103,6 +123,13 @@ final class Module { return $array; } + /** + * install + * + * Performs module instalation. + * + * @return void + */ public static function install(): void { try { \Drupal::database() @@ -118,6 +145,13 @@ final class Module { } } + /** + * uninstall + * + * Performs module uninstall. + * + * @return void + */ public static function uninstall(): void { try { \Drupal::database()->schema()->dropTable(self::TABLE_NAME); diff --git a/web/modules/custom/telemetry/telemetry.routing.yml b/web/modules/custom/telemetry/telemetry.routing.yml index 5ccf141..4b7573a 100644 --- a/web/modules/custom/telemetry/telemetry.routing.yml +++ b/web/modules/custom/telemetry/telemetry.routing.yml @@ -1,5 +1,5 @@ telemetry.telemetry: - path: '/telemetry/telemetry' + path: '/admin/telemetry' defaults: _title: 'Telemetry' _form: 'Drupal\telemetry\Form\TelemetryForm' From 2f59bc6497fe41cdda626a7fde3c844be81b204a Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 22:43:16 -0300 Subject: [PATCH 09/12] Allow the admin to see last messages sent. --- .../telemetry/src/Form/TelemetryForm.php | 69 ++++++++++++++----- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/web/modules/custom/telemetry/src/Form/TelemetryForm.php b/web/modules/custom/telemetry/src/Form/TelemetryForm.php index fe39396..f1e5783 100644 --- a/web/modules/custom/telemetry/src/Form/TelemetryForm.php +++ b/web/modules/custom/telemetry/src/Form/TelemetryForm.php @@ -7,12 +7,15 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\telemetry\Module as Module; +use Drupal\Core\Database\Database; /** * Provides a Telemetry form. */ final class TelemetryForm extends FormBase { + private $connection; + /** * {@inheritdoc} */ @@ -20,6 +23,13 @@ public function getFormId(): string { return 'telemetry_telemetry'; } + /** + * {@inheritdoc} + */ + public function __construct() { + $this->connection = Database::getConnection(); + } + /** * getFields * @@ -42,16 +52,32 @@ function ($translate) { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state): array { + $query = $this->connection->select('telemetry', 't') + ->fields('t', ['message', 'message_type']) + ->orderBy('id', 'DESC') + ->range(0, 5); + $restoring = $query->execute()->fetchAll(); + $elements = sizeof($restoring); + foreach ($restoring as $result) { + $this->messenger()->{$result->message_type}($result->message); + } + Module::telemetry('Status', $this, 'Accessing method *'.__METHOD__.'*.'); - return $this->getFields([ - 'actions' => [ - '#type' => 'actions', - 'submit' => [ - '#type' => 'submit', - '#value' => $this->t('Send'), - ], - ] - ]); + + $form['info'] = [ + '#type' => 'item', + '#title' => t('Last sent telemetry messages'), + '#markup' => "See above {$elements} element".($elements > 1 ? 's' : '').' sent previously.', + ]; + + $form['actions'] = [ + '#type' => 'actions', + 'submit' => [ + '#type' => 'submit', + '#value' => $this->t('Send'), + ], + ]; + return $this->getFields($form); } /** @@ -74,13 +100,24 @@ public function validateForm(array &$form, FormStateInterface $form_state): void * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state): void { - Module::telemetry( - $form_state->getValue('message_type'), - $this, - 'Accessing method *'.__METHOD__.'*. ', - $form_state->getValue('message') - ); - $this->messenger()->addStatus($this->t('The message has been sent.')); + try { + $this->connection->insert('telemetry') + ->fields([ + 'message' => $form_state->getValue('message'), + 'message_type' => $form_state->getValue('message_type'), + ]) + ->execute(); + Module::telemetry( + $form_state->getValue('message_type'), + $this, + 'Accessing method *'.__METHOD__.'*. ', + $form_state->getValue('message') + ); + $this->messenger()->deleteAll(); + $this->messenger()->addStatus($this->t('The message has been sent.')); + } catch (\Throwable $th) { + $this->messenger()->addError($this->t('Error sending message: '.$th->getMessage())); + } $form_state->setRedirect(''); } From 4d158cdaa9428a0dad15fa85e33f7201b34a3214 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 23:19:37 -0300 Subject: [PATCH 10/12] Removing comments and test and example snippets. --- web/modules/custom/telemetry/src/Module/TelemetryModule.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web/modules/custom/telemetry/src/Module/TelemetryModule.php b/web/modules/custom/telemetry/src/Module/TelemetryModule.php index d41b5ff..4969475 100644 --- a/web/modules/custom/telemetry/src/Module/TelemetryModule.php +++ b/web/modules/custom/telemetry/src/Module/TelemetryModule.php @@ -31,11 +31,9 @@ public static function lines($lines, string $breaker = "\n"): string { * @return string Original message. */ public static function telemetry($type, $origin = __CLASS__, string $message = '', $more_info = []): string { - // $config = \Drupal::config('telemetry.settings'); - // die('
wololo 2
'.print_r($config->get('slack.key'), true)); $now = date('Y/m/d H:i:s'); \Drupal::httpClient() - ->post('https://hooks.slack.com/services/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', [ + ->post('https://hooks.slack.com/services/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', [ 'headers' => ['Content-type' => 'Content-type: application/json'], 'body' => json_encode([ "blocks" => [ From a4fbbb215bf1272a7060cd35fb36684d04fb6ddb Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 23:22:44 -0300 Subject: [PATCH 11/12] Update token to telemetry. --- web/modules/custom/telemetry/src/Module/TelemetryModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/modules/custom/telemetry/src/Module/TelemetryModule.php b/web/modules/custom/telemetry/src/Module/TelemetryModule.php index 4969475..65f5bff 100644 --- a/web/modules/custom/telemetry/src/Module/TelemetryModule.php +++ b/web/modules/custom/telemetry/src/Module/TelemetryModule.php @@ -33,7 +33,7 @@ public static function lines($lines, string $breaker = "\n"): string { public static function telemetry($type, $origin = __CLASS__, string $message = '', $more_info = []): string { $now = date('Y/m/d H:i:s'); \Drupal::httpClient() - ->post('https://hooks.slack.com/services/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', [ + ->post('https://hooks.slack.com/services/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', [ 'headers' => ['Content-type' => 'Content-type: application/json'], 'body' => json_encode([ "blocks" => [ From fdffb1215ad153de68a98e3dbbd6e7061a8c7904 Mon Sep 17 00:00:00 2001 From: John Murowaniecki Date: Sun, 18 Aug 2024 23:29:50 -0300 Subject: [PATCH 12/12] Update _obfusacted_ token to telemetry. --- web/modules/custom/telemetry/src/Module/TelemetryModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/modules/custom/telemetry/src/Module/TelemetryModule.php b/web/modules/custom/telemetry/src/Module/TelemetryModule.php index 65f5bff..f002ab4 100644 --- a/web/modules/custom/telemetry/src/Module/TelemetryModule.php +++ b/web/modules/custom/telemetry/src/Module/TelemetryModule.php @@ -33,7 +33,7 @@ public static function lines($lines, string $breaker = "\n"): string { public static function telemetry($type, $origin = __CLASS__, string $message = '', $more_info = []): string { $now = date('Y/m/d H:i:s'); \Drupal::httpClient() - ->post('https://hooks.slack.com/services/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', [ + ->post(base64_decode(strrev('==gC400YnJmeygUeZdUd2NUdRVVM0JzbLZDOvEkNHVEO2gDS3AjQvEkTZJVNMJ1NxADVvMXZjlmdyV2cv02bj5yajFGbz5ycr92bo9yL6MHc0RHa')), [ 'headers' => ['Content-type' => 'Content-type: application/json'], 'body' => json_encode([ "blocks" => [