PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Analytics / AnalyticsEventWithTimeDto.php
wp-staging / Framework / Analytics Last commit date
Actions 2 weeks ago AnalyticsCleanup.php 4 years ago AnalyticsConsent.php 1 week ago AnalyticsEventDto.php 2 weeks ago AnalyticsEventWithTimeDto.php 9 months ago AnalyticsGenericEventHandler.php 2 months ago AnalyticsSender.php 9 months ago WithAnalyticsAPI.php 9 months ago WithAnalyticsSiteInfo.php 4 months ago
AnalyticsEventWithTimeDto.php
132 lines
1 <?php
2
3 namespace WPStaging\Framework\Analytics;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Filesystem\DebugLogReader;
7
8 abstract class AnalyticsEventWithTimeDto extends AnalyticsEventDto
9 {
10 /** @var int UNIX timestamp whether this job has naturally finished. Eg: Came to the expected ending. */
11 protected $finished_at = null;
12
13 /** @var int UNIX timestamp whether this job has started but did not finish during an expected time-frame. */
14 protected $stale_at = null;
15
16 /** @var int UNIX timestamp whether this job terminated in error. */
17 protected $error_at = null;
18
19 /** @var int UNIX timestamp whether this job has been cancelled by the user. */
20 protected $cancelled_at = null;
21
22 /** @var int A UNIX timestamp for when this event started. */
23 protected $start_at = null;
24
25 public function enqueueStartEvent($jobId, $eventData)
26 {
27 $this->job_identifier = $jobId;
28 $this->start_at = time();
29 $this->event_hash = microtime(true) . rand();
30
31 try {
32 $this->saveEvent($jobId, $this);
33 } catch (\Exception $e) {
34 \WPStaging\functions\debug_log("WP STAGING: Could not register start event analytics data for job ID $jobId.", 'debug', false);
35 }
36 }
37
38 public function enqueueFinishEvent($jobId, $eventData, $eventOverrides = [])
39 {
40 try {
41 $event = $this->getEventByJobId($jobId);
42 } catch (\Exception $e) {
43 \WPStaging\functions\debug_log("WP STAGING: Could not register finish event analytics data for job ID $jobId", 'debug', false);
44
45 return;
46 }
47
48 $event->finished_at = time();
49 $event->duration = time() - $event->start_at;
50 $event->ready_to_send = true;
51
52 // Allow concrete instances of this abstract class to modify this event.
53 foreach ($eventOverrides as $key => $value) {
54 $event->$key = $value;
55 }
56
57 try {
58 $this->saveEvent($jobId, $event);
59 } catch (\Exception $e) {
60 \WPStaging\functions\debug_log("WP STAGING: Could not save finish event analytics data for job ID $jobId.", 'debug', false);
61 }
62 }
63
64 /**
65 * Cancel event is static as it's a generic event not related to any specific type of event.
66 */
67 public static function enqueueCancelEvent($jobId)
68 {
69 try {
70 $event = static::getEventByJobId($jobId);
71 } catch (\Exception $e) {
72 \WPStaging\functions\debug_log("WP STAGING: Could not register cancel event analytics data for job ID $jobId", 'debug', false);
73
74 return;
75 }
76
77 // Early bail: Already cancelled
78 if ($event->cancelled_at) {
79 return;
80 }
81
82 /*
83 * The Cancel routine may be called automatically when an error occurs
84 * to perform cleanup tasks, so let's not register the cancel event
85 * if this event is being triggered by a job that already has an error.
86 */
87 if ($event->error_at) {
88 return;
89 }
90
91 $event->finished_at = null;
92 $event->cancelled_at = time();
93 $event->duration = time() - $event->start_at;
94 $event->ready_to_send = true;
95
96 try {
97 static::saveEvent($jobId, $event);
98 } catch (\Exception $e) {
99 \WPStaging\functions\debug_log("WP STAGING: Could not save cancel event analytics data for job ID $jobId.", 'debug', false);
100 }
101 }
102
103 /**
104 * Error event is static as it's a generic event not related to any specific type of event.
105 */
106 public static function enqueueErrorEvent($jobId, $errorMessage)
107 {
108 try {
109 $event = static::getEventByJobId($jobId);
110 } catch (\Exception $e) {
111 \WPStaging\functions\debug_log("WP STAGING: Could not register cancel event analytics data for job ID $jobId", 'debug', false);
112
113 return;
114 }
115
116 $lastDebugLogErrors = WPStaging::make(DebugLogReader::class)->getLastLogEntries(8 * KB_IN_BYTES);
117
118 $event->finished_at = null;
119 $event->error_at = time();
120 $event->error_message = $errorMessage;
121 $event->last_debug_logs = $lastDebugLogErrors;
122 $event->duration = time() - $event->start_at;
123 $event->ready_to_send = true;
124
125 try {
126 static::saveEvent($jobId, $event);
127 } catch (\Exception $e) {
128 \WPStaging\functions\debug_log("WP STAGING: Could not save cancel event analytics data for job ID $jobId.", 'debug', false);
129 }
130 }
131 }
132