|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\IntegrationMetrics; |
| 4 | + |
| 5 | +/** |
| 6 | + * Integration Metrics Data Transfer Object |
| 7 | + * |
| 8 | + * This class captures performance metrics for integration processing, including execution time, |
| 9 | + * memory usage, and custom performance data. It acts as a container for performance telemetry |
| 10 | + * that can be serialized to JSON for transmission to monitoring/analytics systems. |
| 11 | + * |
| 12 | + * The class is designed to work with IMetricService which handles metric collection and |
| 13 | + * span lifecycle management (creation, measurement, finalization). |
| 14 | + * |
| 15 | + * Usage Example: |
| 16 | + * <code> |
| 17 | + * $dto = new IMetricDTO(); |
| 18 | + * $dto->integration_name = 'WooCommerce'; |
| 19 | + * $dto->custom_fields['order_count'] = 5; |
| 20 | + * |
| 21 | + * $json = $dto->getJSON(); |
| 22 | + * // Send $json to analytics backend |
| 23 | + * </code> |
| 24 | + * |
| 25 | + * @see IMetricService for metric lifecycle management |
| 26 | + * @see IMetricDTOTrait for integration with IntegrationBase and IntegrationByClassBase |
| 27 | + */ |
| 28 | +class IMetricDTO |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Name of the integration being measured (e.g., 'WooCommerce', 'NinjaForms'). |
| 32 | + * Set by IMetricService::getDTO() when creating a new DTO instance. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + * @psalm-suppress PossiblyUnusedProperty |
| 36 | + */ |
| 37 | + public $integration_name = 'unset'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Version of the metrics schema/format. |
| 41 | + * Can be overridden via IMetricDTOTrait::$imetric_dto_version on the integration class. |
| 42 | + * |
| 43 | + * @var string |
| 44 | + * @psalm-suppress PossiblyUnusedProperty |
| 45 | + */ |
| 46 | + public $dto_version = '1.0.0'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Peak memory usage difference in kilobytes (KB) during integration processing. |
| 50 | + * Calculated as: peak_memory_at_end - peak_memory_on_start |
| 51 | + * Set during IMetricService::finalizeDTO() |
| 52 | + * |
| 53 | + * @var int |
| 54 | + * @psalm-suppress PossiblyUnusedProperty |
| 55 | + */ |
| 56 | + public $peak_memory_diff_kb = 0; |
| 57 | + |
| 58 | + /** |
| 59 | + * Total execution time in milliseconds (ms) for the entire integration processing. |
| 60 | + * Calculated as: end_time_ms - timer_on_start_msec |
| 61 | + * Set during IMetricService::finalizeDTO() |
| 62 | + * |
| 63 | + * @var int |
| 64 | + * @psalm-suppress PossiblyUnusedProperty |
| 65 | + */ |
| 66 | + public $total_exec_time_ms = 0; |
| 67 | + |
| 68 | + /** |
| 69 | + * Custom performance fields added by the integration. |
| 70 | + * Format: field_name => field_value |
| 71 | + * Example: ['form_fields_count' => 15, 'validation_passed' => true] |
| 72 | + * Populated via IMetricService::setCustomField() |
| 73 | + * |
| 74 | + * @var array |
| 75 | + * @psalm-suppress PossiblyUnusedProperty |
| 76 | + */ |
| 77 | + public $custom_fields = array(); |
| 78 | + |
| 79 | + /** |
| 80 | + * Named time spans tracking specific operations within the integration. |
| 81 | + * Format: span_name => ['time_msec' => float, 'memory_kb' => float, 'memory_peak_kb' => float, 'released' => bool] |
| 82 | + * |
| 83 | + * Each span captures: |
| 84 | + * - time_msec: Duration in milliseconds (calculated by IMetricService::lease()) |
| 85 | + * - memory_kb: Memory usage in KB (calculated by IMetricService::lease()) |
| 86 | + * - memory_peak_kb: Peak memory in KB (calculated by IMetricService::lease()) |
| 87 | + * - released: Whether the span has been finalized (true = measurements complete) |
| 88 | + * |
| 89 | + * Spans are created via IMetricService::seek() and finalized via IMetricService::lease() |
| 90 | + * |
| 91 | + * @var array |
| 92 | + */ |
| 93 | + public $spans = array(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Peak memory usage in KB for specific variable groups, tracked by span name. |
| 97 | + * Format: span_name => peak_kb_value |
| 98 | + * Example: ['form_data_vars' => 42.5, 'post_data_vars' => 128.3] |
| 99 | + * Populated via IMetricService::dumpVarsSize() |
| 100 | + * |
| 101 | + * @var array |
| 102 | + * @psalm-suppress PossiblyUnusedProperty |
| 103 | + */ |
| 104 | + public $variable_peak_kb = array(); |
| 105 | + |
| 106 | + /** |
| 107 | + * Internal: Timer value (in ms) captured at metric start. |
| 108 | + * Used to calculate total_exec_time_ms = current_time - timer_on_start_msec |
| 109 | + * Set by IMetricService::startGlobalSeeking() and should not be modified directly. |
| 110 | + * Excluded from JSON output via getArray() |
| 111 | + * |
| 112 | + * @var int |
| 113 | + */ |
| 114 | + public $timer_on_start_msec = 0; |
| 115 | + |
| 116 | + /** |
| 117 | + * Internal: Memory usage (in KB) captured at metric start. |
| 118 | + * Used to track relative memory usage throughout integration processing. |
| 119 | + * Set by IMetricService::startGlobalSeeking() and should not be modified directly. |
| 120 | + * Excluded from JSON output via getArray() |
| 121 | + * |
| 122 | + * @var int |
| 123 | + * @psalm-suppress PossiblyUnusedProperty |
| 124 | + */ |
| 125 | + public $memory_usage_on_start_kb = 0; |
| 126 | + |
| 127 | + /** |
| 128 | + * Internal: Peak memory usage (in KB) captured at metric start. |
| 129 | + * Used to calculate peak_memory_diff_kb = peak_memory_at_end - peak_memory_on_start_kb |
| 130 | + * Set by IMetricService::startGlobalSeeking() and should not be modified directly. |
| 131 | + * Excluded from JSON output via getArray() |
| 132 | + * |
| 133 | + * @var int |
| 134 | + */ |
| 135 | + public $peak_memory_on_start_kb = 0; |
| 136 | + |
| 137 | + /** |
| 138 | + * Internal: Flag indicating whether metric finalization is complete. |
| 139 | + * When true, no further span creation or field updates are allowed. |
| 140 | + * Set by IMetricService::finalizeDTO() |
| 141 | + * Excluded from JSON output via getArray() |
| 142 | + * |
| 143 | + * @var bool |
| 144 | + */ |
| 145 | + public $is_released = false; |
| 146 | + |
| 147 | + /** |
| 148 | + * JSON key name used when embedding this DTO in sender info. |
| 149 | + * Used to identify metric data in analytics payloads. |
| 150 | + * |
| 151 | + * @var string |
| 152 | + */ |
| 153 | + public static $SENDER_INFO_KEY = 'imetric'; |
| 154 | + |
| 155 | + /** |
| 156 | + * Serializes the DTO to JSON string. |
| 157 | + * |
| 158 | + * Converts the DTO object to a JSON-encoded string suitable for transmission to analytics systems. |
| 159 | + * Internally calls getArray() to filter out internal properties before encoding. |
| 160 | + * |
| 161 | + * @return false|string JSON string representation of the DTO, or false if JSON encoding fails |
| 162 | + * |
| 163 | + * @see getArray() for the exact properties included in the output |
| 164 | + */ |
| 165 | + public function getJSON() |
| 166 | + { |
| 167 | + return @json_encode($this->getArray()); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns the DTO as an associative array, excluding internal properties. |
| 172 | + * |
| 173 | + * This method filters out internal tracking properties that are not meant to be transmitted: |
| 174 | + * - is_released: tracks finalization state |
| 175 | + * - peak_memory_on_start_kb: baseline for calculations |
| 176 | + * - memory_usage_on_start_kb: baseline for calculations |
| 177 | + * - timer_on_start_msec: baseline for calculations |
| 178 | + * - SENDER_INFO_KEY: static metadata key |
| 179 | + * |
| 180 | + * All other public properties are included in the output array. |
| 181 | + * |
| 182 | + * Usage: |
| 183 | + * <code> |
| 184 | + * $dto = new IMetricDTO(); |
| 185 | + * $dto->integration_name = 'MyForm'; |
| 186 | + * $dto->custom_fields['status'] = 'success'; |
| 187 | + * |
| 188 | + * $array = $dto->getArray(); |
| 189 | + * // $array now contains integration_name, dto_version, spans, custom_fields, etc. |
| 190 | + * // but not is_released or *_on_start_* properties |
| 191 | + * </code> |
| 192 | + * |
| 193 | + * @return array Associative array of DTO properties ready for JSON serialization |
| 194 | + * |
| 195 | + * @see getJSON() for JSON-encoded output |
| 196 | + */ |
| 197 | + public function getArray() |
| 198 | + { |
| 199 | + $skip_properties = array( |
| 200 | + 'is_released', |
| 201 | + 'peak_memory_on_start_kb', |
| 202 | + 'memory_usage_on_start_kb', |
| 203 | + 'timer_on_start_msec', |
| 204 | + 'SENDER_INFO_KEY' |
| 205 | + ); |
| 206 | + return array_map(function ($value) { |
| 207 | + return $value; |
| 208 | + }, array_diff_key(get_object_vars($this), array_flip($skip_properties))); |
| 209 | + } |
| 210 | +} |
0 commit comments