Beginnings of Heuristics work
[contractdashboard.git] / heuristics / historyHeuristics.php
blob:a/heuristics/historyHeuristics.php -> blob:b/heuristics/historyHeuristics.php
--- a/heuristics/historyHeuristics.php
+++ b/heuristics/historyHeuristics.php
@@ -1,1 +1,129 @@
+<?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,$conn;
+	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,$conn;
+	$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,$conn;
+	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,$conn;
+	$query = 'select avg(count), stddev(count) from (
+            select (case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) 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];
+}