• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Freegle / iznik-server / 9785cc63-4b10-470b-b841-69247e465a29

20 Dec 2024 04:18PM UTC coverage: 92.423% (-0.003%) from 92.426%
9785cc63-4b10-470b-b841-69247e465a29

push

circleci

edwh
Don't use microvolunteering to review autoreposts.

25492 of 27582 relevant lines covered (92.42%)

31.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/http/api/abtest.php
1
<?php
2
namespace Freegle\Iznik;
3

4
function abtest() {
5
    global $dbhr, $dbhm;
1✔
6

7
    $me = Session::whoAmI($dbhr, $dbhm);
1✔
8

9
    $id = (Utils::presint('id', $_REQUEST, NULL));
1✔
10

11
    $p = new Polls($dbhr, $dbhm, $id);
1✔
12
    $ret = [ 'ret' => 100, 'status' => 'Unknown verb' ];
1✔
13

14
    switch ($_REQUEST['type']) {
1✔
15
        case 'GET': {
1✔
16
            $uid = Utils::presdef('uid', $_REQUEST, NULL);
1✔
17
            $variants = $dbhr->preQuery("SELECT * FROM abtest WHERE uid = ? AND suggest = 1 ORDER BY rate DESC, RAND();", [
1✔
18
                $uid
1✔
19
            ]);
1✔
20

21
            # We use a bandit test so that we get the benefit of the best option, while still exploring others.
22
            # See http://stevehanov.ca/blog/index.php?id=132 for an example description.
23
            $r = Utils::randomFloat();
1✔
24

25
            if ($r < 0.1) {
1✔
26
                # The 10% case we choose a random one of the other options.
27
                $s = rand(0, count($variants) - 1);
1✔
28
                $variant = $variants[$s];
1✔
29
            } else {
30
                # Most of the time we choose the currently best-performing option.
31
                $variant = count($variants) > 0 ? $variants[0] : NULL;
×
32
            }
33

34
            $ret = [
1✔
35
                'ret' => 0,
1✔
36
                'status' => 'Success',
1✔
37
                'variant' => $variant
1✔
38
            ];
1✔
39
            break;
1✔
40
        }
41
        case 'POST': {
1✔
42
            $uid = Utils::presdef('uid', $_REQUEST, NULL);
1✔
43
            $variant = Utils::presdef('variant', $_REQUEST, NULL);
1✔
44
            $shown = array_key_exists('shown', $_REQUEST) ? filter_var($_REQUEST['shown'], FILTER_VALIDATE_BOOLEAN) : NULL;
1✔
45
            $action = array_key_exists('action', $_REQUEST) ? filter_var($_REQUEST['action'], FILTER_VALIDATE_BOOLEAN) : NULL;
1✔
46

47
            # The client can decide that an action is more valuable.  In this case we weight it, which will result in
48
            # it getting a higher rate, and therefore being chosen more often.
49
            $score = (Utils::presint('score', $_REQUEST, 1));
1✔
50

51
            # Ignore the app.  This is because we can be tweaking experiments and have results which are contaminated
52
            # by old code on the app which hasn't updated.
53
            if (!Utils::pres('app', $_REQUEST) && $uid && $variant) {
1✔
54
                if (!is_null($shown)) {
1✔
55
                    $sql = "INSERT INTO abtest (uid, variant, shown) VALUES (" . $dbhm->quote($uid) . ", " . $dbhm->quote($variant) . ", 1) ON DUPLICATE KEY UPDATE shown = shown + 1, rate = COALESCE(100 * action / shown, 0);";
1✔
56
                    $dbhm->background($sql);
1✔
57
                }
58

59
                if (!is_null($action)) {
1✔
60
                    $sql = "INSERT INTO abtest (uid, variant, action) VALUES (" . $dbhm->quote($uid) . ", " . $dbhm->quote($variant) . ", $score) ON DUPLICATE KEY UPDATE action = action + $score, rate = COALESCE(100 * action / shown, 0);";
1✔
61
//                    $dbhm->preExec($sql);
62
                    $dbhm->background($sql);
1✔
63
                }
64
            }
65

66
            $ret = [
1✔
67
                'ret' => 0,
1✔
68
                'status' => 'Success'
1✔
69
            ];
1✔
70
            break;
1✔
71
        }
72
    }
73

74
    return($ret);
1✔
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc