Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #73

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/test-run-alerts/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
padding: 0 20px;
position: sticky;
top: -0.1px;
z-index: 1;
z-index: 3;
}

&-link {
Expand Down
2 changes: 1 addition & 1 deletion includes/core/utilities/class-image-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function alert_image_aspect_ratio( $alert ) {
* @return string
*/
public static function get_screenshot_url( $object, $type, $size = 'full' ) {
$property = "${type}_screenshot_url";
$property = "{$type}_screenshot_url";

if ( ! property_exists( $object, $property ) ) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions includes/features/class-onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function get_onboardings() {
'description' => wp_kses_post( __( 'Starting from tomorrow, your Test will <strong>run daily</strong>, ensuring consistent monitoring of your page.', 'visual-regression-tests' ) ),
],
[
'element' => '.vrts_navigation_item a[href$="admin.php?page=vrts-settings"]',
'element' => '.vrts-admin-header__navigation-link[href$="admin.php?page=vrts-settings"]',
'title' => wp_kses_post( __( '🛠️ Fine-tune your setup', 'visual-regression-tests' ) ),
'description' => wp_kses_post( __( 'Further customize your Test configuration and plugin settings for an optimized experience.', 'visual-regression-tests' ) ),
],
Expand All @@ -137,7 +137,7 @@ public function get_onboardings() {
[
'side' => 'bottom',
'align' => 'center',
'element' => '.vrts_navigation_item a[href$="admin.php?page=vrts-runs"]',
'element' => '.vrts-admin-header__navigation-link[href$="admin.php?page=vrts-runs"]',
'title' => wp_kses_post( __( '🚀 Meet the new Runs!', 'visual-regression-tests' ) ),
'description' => wp_kses_post( __( 'Alerts are now bundled into Runs. Get a single report for each daily test, manual test, API trigger, or new: <strong>WordPress & plugin update (Pro)</strong>!', 'visual-regression-tests' ) ),
],
Expand Down
15 changes: 3 additions & 12 deletions includes/list-tables/class-test-runs-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ public function prepare_items() {
];

$this->items = Test_Run::get_items( $args );
$test_run_ids = wp_list_pluck( $this->items, 'id' );
$alert_counts = [];
foreach ( Alert::get_unread_count_by_test_run_ids( $test_run_ids ) as $alert_count ) {
$alert_counts[ $alert_count->test_run_id ] = $alert_count->count;
}
foreach ( $this->items as $item ) {
$item->alerts_count = $alert_counts[ $item->id ] ?? 0;
}

$total_items = 0;
if ( null !== $args['filter_status'] ) {
Expand Down Expand Up @@ -198,7 +190,7 @@ public function single_row( $item ) {
class="<?php echo esc_attr( $classes ); ?>"
data-test-run-id="<?php echo esc_attr( $item->id ); ?>"
data-test-run-new="<?php echo esc_attr( $is_new_run ? 'true' : 'false' ); ?>"
<?php echo $item->alerts_count > 0 ? 'data-has-alerts' : ''; ?>
<?php echo $item->unread_alerts_count > 0 ? 'data-has-alerts' : ''; ?>
>
<?php $this->single_row_columns( $item ); ?>
</tr>
Expand Down Expand Up @@ -324,11 +316,10 @@ public function column_trigger( $item ) {
* @return string
*/
public function column_status( $item ) {
$alerts_count = count( maybe_unserialize( $item->alerts ) ?? [] );
$tests_count = count( maybe_unserialize( $item->tests ) ?? [] );
if ( $alerts_count > 0 ) {
if ( $item->alerts_count > 0 ) {
$status_class = 'paused';
$status_text = esc_html__( 'Changes detected ', 'visual-regression-tests' ) . sprintf( '(%s)', $alerts_count );
$status_text = esc_html__( 'Changes detected ', 'visual-regression-tests' ) . sprintf( '(%s)', $item->alerts_count );

} else {
$status_class = 'running';
Expand Down
12 changes: 8 additions & 4 deletions includes/models/class-test-run.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function get_items( $args = [], $return_count = false ) {

if ( isset( $args['filter_status'] ) && null !== $args['filter_status'] ) {
if ( 'changes-detected' === $args['filter_status'] ) {
$where .= ' AND alerts IS NOT NULL';
$where .= ' AND alerts_count > 0';
}
}

Expand Down Expand Up @@ -82,14 +82,18 @@ public static function get_items( $args = [], $return_count = false ) {
runs.id,
$run_title,
runs.tests,
runs.alerts,
SUM( IF( alerts.id IS NOT NULL, 1, 0 ) ) as alerts_count,
SUM( IF( alerts.id IS NOT NULL and alerts.alert_state = 0, 1, 0 ) ) as unread_alerts_count,
runs.trigger,
runs.trigger_notes,
runs.trigger_meta,
runs.started_at,
runs.scheduled_at,
runs.finished_at
FROM $test_runs_table as runs
LEFT JOIN $alerts_table as alerts
ON runs.id = alerts.test_run_id
GROUP BY runs.id
) runs
$where
$orderby
Expand Down Expand Up @@ -372,7 +376,7 @@ public static function get_calculated_status( $test_run ) {
$test_run = self::get_item( $test_run );
}

$has_alerts = ! empty( maybe_unserialize( $test_run->alerts ) );
$has_alerts = ( $test_run->alerts_count ?? 0 ) > 0;

if ( $has_alerts ) {
return 'has-alerts';
Expand Down Expand Up @@ -440,7 +444,7 @@ public static function get_status_data( $test_run ) {

switch ( $test_run_status ) {
case 'has-alerts':
$alerts_count = count( maybe_unserialize( $test_run->alerts ) );
$alerts_count = $test_run->alerts_count;
$class = 'paused';
$text = esc_html__( 'Changes detected', 'visual-regression-tests' );
$instructions = Date_Time_Helpers::get_formatted_relative_date_time( $test_run->finished_at );
Expand Down
3 changes: 1 addition & 2 deletions includes/services/class-test-run-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Vrts\Core\Utilities\Url_Helpers;
use Vrts\Features\Service;
use Vrts\Features\Subscription;
use Vrts\Models\Alert;
use Vrts\Models\Test;
use Vrts\Models\Test_Run;
use Vrts\Services\Email_Service;
Expand All @@ -23,7 +24,6 @@ public function update_run_from_api_data( $data ) {
$test_run = Test_Run::get_by_service_test_run_id( $run_id );

$test_run_just_finished = false;
$alert_ids = [];

if ( $test_run && empty( $test_run->finished_at ) && ! empty( $data['finished_at'] ) ) {
$test_run_just_finished = true;
Expand All @@ -41,7 +41,6 @@ public function update_run_from_api_data( $data ) {

$test_run_id = $this->create_test_run( $data['run_id'], [
'tests' => maybe_serialize( $test_ids ),
'alerts' => ! empty( $alert_ids ) ? maybe_serialize( $alert_ids ) : null,
'started_at' => $data['started_at'],
'finished_at' => $data['finished_at'],
'scheduled_at' => $data['scheduled_at'],
Expand Down
19 changes: 13 additions & 6 deletions includes/tables/class-test-runs-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Test_Runs_Table {

const DB_VERSION = '1.0';
const DB_VERSION = '1.1';
const TABLE_NAME = 'vrts_test_runs';

/**
Expand Down Expand Up @@ -32,7 +32,6 @@ public static function install_table() {
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
service_test_run_id varchar(40),
tests text,
alerts text default NULL,
`trigger` varchar(20),
trigger_notes text,
trigger_meta text default NULL,
Expand Down Expand Up @@ -106,7 +105,7 @@ public static function create_runs_from_alerts() {
'permalink' => get_permalink( $alert->post_id ),
],
] ),
'alerts' => maybe_serialize( [ $alert->id ] ),
'alert_id' => $alert->id,
'trigger' => 'legacy',
'started_at' => $alert->finished_at,
'finished_at' => $alert->finished_at,
Expand All @@ -117,13 +116,21 @@ public static function create_runs_from_alerts() {
return "('" . implode( "','", array_map( 'esc_sql', $run ) ) . "')";
}, $test_runs));

// add alert_id column to test_runs table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "ALTER TABLE {$runs_table} ADD COLUMN alert_id bigint(20) unsigned;" );

// insert all test runs with single query.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "INSERT INTO {$runs_table} (tests, alerts, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );
$wpdb->query( "INSERT INTO {$runs_table} (tests, alert_id, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );

// update test_run_id in alerts table from newly created test runs based on alerts column.
// update test_run_id in alerts table from newly created test runs based on alert_id column.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alerts LIKE CONCAT('%\"', a.id, '\"%') SET a.test_run_id = r.id;" );
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alert_id = a.id SET a.test_run_id = r.id;" );

// remove alert_id column from test_runs table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "ALTER TABLE {$runs_table} DROP COLUMN alert_id;" );

return true;
}
Expand Down
Loading