Skip to content

Commit c08e777

Browse files
[feature] add pct gradient to referrers table too
1 parent 66770a6 commit c08e777

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

assets/src/css/dashboard-2.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
--bs-warning-text-emphasis: #664d03;
4141
--bs-warning-bg-subtle: #fff3cd;
4242
--bs-warning-border-subtle: #ffe69c;
43+
--koko-analytics-row-gradient-color: rgba(104, 159, 210, 0.06);
4344
}
4445

4546
.koko-analytics {

src/Dashboard.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function show()
7171
$posts_count = $stats->count_posts($date_start_str, $date_end_str);
7272
$referrers = $stats->get_referrers($date_start_str, $date_end_str, $referrers_offset, $referrers_limit);
7373
$referrers_count = $stats->count_referrers($date_start_str, $date_end_str);
74+
$referrers_sum = $stats->sum_referrers($date_start_str, $date_end_str);
7475
$realtime = get_realtime_pageview_count('-1 hour');
7576

7677
if (isset($_GET['group']) && in_array($_GET['group'], ['day', 'week', 'month', 'year'])) {

src/Resources/views/dashboard-page.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
<tbody>
193193
<?php foreach ($posts as $i => $p) { ?>
194194
<?php $pct = $totals->pageviews > 0 && $page === 0 ? round(($p->pageviews / $totals->pageviews) * 100, 0) : 0; ?>
195-
<tr <?php echo $page == $p->path ? 'class="page-filter-active"' : ''; ?> style="background: linear-gradient(to right,rgba(104, 159, 210, 0.05) <?= $pct ?>%, transparent <?= $pct ?>%);">
195+
<tr <?php echo $page == $p->path ? 'class="page-filter-active"' : ''; ?> style="background: linear-gradient(to right, var(--koko-analytics-row-gradient-color) <?= $pct ?>%, transparent <?= $pct ?>%);">
196196
<td class="text-muted"><?php echo $posts_offset + $i + 1; ?></td>
197197
<td class="text-truncate"><a href="<?php echo esc_attr(add_query_arg(['p' => $p->path])); ?>"><?php echo esc_html($p->label); ?></a></td>
198198
<td class="text-end d-none d-lg-table-cell"><?php echo number_format_i18n(max(1, $p->visitors)); ?></td>
@@ -233,7 +233,8 @@
233233
</thead>
234234
<tbody>
235235
<?php foreach ($referrers as $i => $r) { ?>
236-
<tr>
236+
<?php $pct = $referrers_sum > 0 ? round(($r->pageviews / $referrers_sum) * 100, 0) : 0; ?>
237+
<tr style="background: linear-gradient(to right, var(--koko-analytics-row-gradient-color) <?= $pct ?>%, transparent <?= $pct ?>%);">
237238
<td class="text-muted"><?php echo $referrers_offset + $i + 1; ?></td>
238239
<td class="text-truncate"><?php echo Fmt::referrer_url_label(esc_html($r->url)); ?></td>
239240
<td class="text-end d-none d-lg-table-cell"><?php echo number_format_i18n(max(1, $r->visitors)); ?></td>

src/Stats.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,16 @@ public function count_referrers(string $start_date, string $end_date): int
199199
[$start_date, $end_date]
200200
));
201201
}
202+
203+
public function sum_referrers(string $start_date, string $end_date): int
204+
{
205+
/** @var \wpdb $wpdb */
206+
global $wpdb;
207+
return (int) $wpdb->get_var($wpdb->prepare(
208+
"SELECT SUM(s.pageviews)
209+
FROM {$wpdb->prefix}koko_analytics_referrer_stats s
210+
WHERE s.date >= %s AND s.date <= %s",
211+
[$start_date, $end_date]
212+
));
213+
}
202214
}

tests/benchmarks/functions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ function bench(Closure $fn, $iterations = 1000)
1111
}
1212
$time_end = microtime(true);
1313

14-
1514
// return total time taken (in seconds)
16-
return round(($time_end - $time_start) * 1000 * 1000, 2);
15+
return ($time_end - $time_start) * 1000;
1716
}
1817

1918
function bench_assert($expected, $actual)

tests/benchmarks/static-method-vs-instance-method.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
class A
66
{
7-
public function method(): void
8-
{
9-
}
7+
public function method(): void {}
108
}
119

1210
class B
1311
{
14-
public static function method(): void
15-
{
16-
}
12+
public static function method(): void {}
1713
}
1814

1915
$iterations = 100000;
@@ -23,17 +19,17 @@ public static function method(): void
2319
$instance = new A();
2420
$instance->method();
2521
}, $iterations);
26-
printf("new instance method \t%.2f ns\t(%.2f per it)\n", $time, $time / $iterations);
22+
printf("new instance method \t%.5f ms\t(%.5f ms per it)\n", $time, $time / $iterations);
2723

2824
// existing instance
2925
$instance = new A();
3026
$time = bench(function () use ($instance) {
3127
$instance->method();
3228
}, $iterations);
33-
printf("instance method \t%.2f ns\t(%.2f per it)\n", $time, $time / $iterations);
29+
printf("instance method \t%.5f ms\t(%.5f ms per it)\n", $time, $time / $iterations);
3430

3531
// static method
3632
$time = bench(function () {
3733
B::method();
3834
}, $iterations);
39-
printf("static method\t\t%.2f ns\t(%.2f per it)\n", $time, $time / $iterations);
35+
printf("static method\t\t%.5f ms\t(%.5f ms per it)\n", $time, $time / $iterations);

0 commit comments

Comments
 (0)