Tidy up imports
[contractdashboard.git] / heuristics / historyHeuristics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
$heuristics["HISTORY_LOW_TRANSACTIONS_AGENCY"] = Array(
        "description" => "unusual for agency due to previous low number of transactions "
);
function HISTORY_LOW_TRANSACTIONS_AGENCY($cn)
{
        $thisAgencyTransactions = getAgencyTransactions($cn['agencyName']);
        $averageAgencyTransactions = getAverageAgencyTransactions();
        $stddevAgencyTransactions = getstddevAgencyTransactions();
        $diff = strtotime($cn['contractEnd']) - strtotime($cn['contractStart']);
        $days = intval($diff / (60 * 60 * 24));
        $value = abs($days - $averageAgencyTransactions) / $stddevAgencyTransactions;
        return Array(
                "heuristic_value" => $value,
                "raw_value" => $days,
                "mean" => $averageAgencyTransactions,
                "stddev" => $stddevAgencyTransactions
        );
}
$agencyTransactions = Array();
function getAgencyTransactions($agencyName)
{
        global $agencyTransactions;
        if (!$agencyTransactions[$agencyName]) {
                $query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
    $result = $conn->query($query);
    $r = $result->fetch(PDO::FETCH_BOTH);
                $agencyTransactions[$agencyName] = $r[0];
        }
        return $agencyTransactions[$agencyName];
}
$averageAgencyTransactions;
function getAverageAgencyTransactions()
{
        global $averageAgencyTransactions;
        if (!$averageAgencyTransactions) {
                getStatsAgencyTransactions();
        }
        return $averageAgencyTransactions;
}
$stddevAgencyTransactions;
function getstddevAgencyTransactions()
{
        global $stddevAgencyTransactions;
        if (!$stddevAgencyTransactions) {
                getStatsAgencyTransactions();
        }
        return $stddevAgencyTransactions;
}
function getStatsAgencyTransactions()
{
        global $averageAgencyTransactions, $stddevAgencyTransactions;
        $query = "select avg(count), STDDEV(count) from (select count(*) as count
                from contractnotice group by agencyName) as a;";
    $result = $conn->query($query);
    $r = $result->fetch(PDO::FETCH_BOTH);
        $averageAgencyTransactions = $r[0];
        $stddevAgencyTransactions = $r[1];
}
$heuristics["HISTORY_LOW_TRANSACTIONS_SUPPLIER"] = Array(
        "description" => "unusual for supplier due to previous low number of transactions "
);
function HISTORY_LOW_TRANSACTIONS_SUPPLIER($cn)
{
        $thisSupplierTransactions = getSupplierTransactions($cn['supplierName'], $cn['supplierABN']);
        $averageSupplierTransactions = getAverageSupplierTransactions();
        $stddevSupplierTransactions = getstddevSupplierTransactions();
        $diff = strtotime($cn['contractEnd']) - strtotime($cn['contractStart']);
        $days = intval($diff / (60 * 60 * 24));
        $value = abs($days - $averageSupplierTransactions) / $stddevSupplierTransactions;
        return Array(
                "heuristic_value" => $value,
                "raw_value" => $days,
                "mean" => $averageSupplierTransactions,
                "stddev" => $stddevSupplierTransactions
        );
}
$supplierTransactions = Array();
function getSupplierTransactions($supplierName, $supplierABN)
{
        global $supplierTransactions;
        if ($supplierABN != 0 && $supplierABN != "") {
                if (!$supplierTransactions[$supplierABN]) {
                        $query = 'select count(*) from contractnotice where supplierABN = "' . $supplierABN . '"';
                            $result = $conn->query($query);
    $r = $result->fetch(PDO::FETCH_BOTH);
                        $supplierTransactions[$supplierABN] = $r[0];
                }
                return $supplierTransactions[$supplierABN];
        }
        if (!$supplierTransactions[$supplierName]) {
                $query = 'select count(*) from contractnotice where supplierName = "' . $supplierName . '"';
    $result = $conn->query($query);
    $r = $result->fetch(PDO::FETCH_BOTH);
                $supplierTransactions[$supplierName] = $r[0];
        }
        return $supplierTransactions[$supplierName];
}
$averageSupplierTransactions;
function getAverageSupplierTransactions()
{
        global $averageSupplierTransactions;
        if (!$averageSupplierTransactions) {
                getStatsSupplierTransactions();
        }
        return $averageSupplierTransactions;
}
$stddevSupplierTransactions;
function getstddevSupplierTransactions()
{
        global $stddevSupplierTransactions;
        if (!$stddevSupplierTransactions) {
                getStatsSupplierTransactions();
        }
        return $stddevSupplierTransactions;
}
function getStatsSupplierTransactions()
{
        global $averageSupplierTransactions, $stddevSupplierTransactions;
        $query = 'select avg(count), stddev(count) from (select IF(supplierABN != "",supplierABN,supplierName) as supplierID, count(*) as count from contractnotice group by supplierID) as a;';
    $result = $conn->query($query);
    $r = $result->fetch(PDO::FETCH_BOTH);
        $averageSupplierTransactions = $r[0];
        $stddevSupplierTransactions = $r[1];
}