-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.php
141 lines (103 loc) · 3.97 KB
/
run.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
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*************************************************
* Set-up
*************************************************/
$config = include('config.php');
$client = new SoapClient($config['wsdl'], array("trace"=>1, "exceptions"=>1));
// generate random seed value
date_default_timezone_set('America/New_York');
$seed=time() . rand();
// make hash value using sha1 function
$clear= $config['sourcekey'] . $seed . $config['pin'];
$hash=sha1($clear);
// assembly ueSecurityToken as an array
// (php5 will correct the type for us)
$tok=array(
'SourceKey'=>$config['sourcekey'],
'PinHash'=>array(
'Type'=>'sha1',
'Seed'=>$seed,
'HashValue'=>$hash
),
'ClientIP'=>getHostByName(getHostName())
);
$now = date("Y-m-d H:i:s",time());
$now = date("Y-m-d H:i:s", date_add(date_create_from_format('Y-m-d H:i:s',$now), date_interval_create_from_date_string($config['timediff'] . ' hour'))->getTimestamp());
$message = "Now (adjusted server time): " . $now . "\n";
$interval = $config['interval'];
$message .= "Interval: " . $interval . " minutes\n------\n";
$firstIntervalStart = date("Y-m-d H:i:s", date_sub(date_create_from_format('Y-m-d H:i:s',$now), date_interval_create_from_date_string($interval*2 . ' minute'))->getTimestamp());
$betweenIntervals = date("Y-m-d H:i:s", date_sub(date_create_from_format('Y-m-d H:i:s',$now), date_interval_create_from_date_string($interval . ' minute'))->getTimestamp());
/*************************************************
* Do search for transactions in first interval.
*************************************************/
$search=array(
array(
'Field'=>'created',
'Type'=>'gt',
'Value'=> $firstIntervalStart),
array(
'Field'=>'created',
'Type'=>'lt',
'Value'=> $betweenIntervals),
array(
'Field'=>'type',
'Type'=>'eq',
'Value'=> 'S'),
);
$start=0;
$limit=1000;
$matchall=true;
$sort='created';
$res=$client->searchTransactions($tok,$search,$matchall,$start,$limit,$sort);
$compareCt = $res->TransactionsMatched;
$message .= "Orders, first interval: " . $compareCt . "\n";
$comparePerMin = round(($compareCt / $interval), 1);
$message .= "per min: " . $comparePerMin . "\n------\n";
/*************************************************
* Do search for transactions in second interval.
*************************************************/
$search=array(
array(
'Field'=>'created',
'Type'=>'gt',
'Value'=> $betweenIntervals),
array(
'Field'=>'created',
'Type'=>'lt',
'Value'=> $now),
array(
'Field'=>'type',
'Type'=>'eq',
'Value'=> 'S'),
);
$res=$client->searchTransactions($tok,$search,$matchall,$start,$limit,$sort);
$nowCt = $res->TransactionsMatched;
$mostRecentOrder= end($res->Transactions);
$message .= "Orders, second interval: " . $nowCt . "\n";
$nowPerMin = round(($nowCt / $interval), 1);
$message .= "per min: " . $nowPerMin . "\n------\n";
/*************************************************
* Check growth rate accross intervals, send email
* if threshold passed.
*************************************************/
$growthRate = round(($nowPerMin / $comparePerMin), 1);
$message .= "Growth Rate: " . $growthRate . "\n------\n";
$minutesSinceMRO = round((strtotime($now) - strtotime($mostRecentOrder->DateTime)) / 60, 1);
if ($mostRecentOrder) {
$message .= "Most Recent Order: " . $mostRecentOrder->DateTime . " (" . $mostRecentOrder->Details->OrderID . ")\n";
$message .= "minutes since: " . $minutesSinceMRO . "\n";
}
echo $message;
if ((($growthRate < $config['threshold']) || ($nowCt < 1)) &&
($minutesSinceMRO > $config['recentThreshold']) &&
($comparePerMin > $config['firstThreshold'])) {
$email = $config['email'];
$subject = 'SLOW ORDERS ALERT';
$message = $message;
if(mail($email, $subject, $message)) {
echo "successfull email\n";
} else {
echo "unsuccessful email\n";
}
}