-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat-summary.php
More file actions
137 lines (124 loc) · 4.63 KB
/
Copy pathstat-summary.php
File metadata and controls
137 lines (124 loc) · 4.63 KB
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
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/recipientList.php';
// Debug override
// $recipientList = $errorRecipientList;
$tz = 'US/Pacific';
$dt = new DateTime('now', new DateTimeZone($tz));
$time = $dt->format('m/d/Y');
$spreadsheet_id = '1WyK1B1ik9t5y2dMcNbO8pj8D8Na7khpeazertAGq1c0';
$sheet_name = "'Scalars'";
$range = "A1:B18"; // summary totals
$get_range = $sheet_name . "!" . $range;
$client = new \Google_Client();
$client->setApplicationName('WA Notify Summary');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/enxsheets-credentials.json');
$service;
try {
$service = new Google_Service_Sheets($client);
} catch (Exception $e) {
// echo "Error connecting to service:\n";
// echo $e."\n";
// echo "Retrying...\n";
// sleep(30);
try {
$service = new Google_Service_Sheets($client);
} catch (Exception $e) {
echo "Failed to data.\n$e\n";
exit;
}
}
$sheet_values;
try {
$response = $service->spreadsheets_values->get($spreadsheet_id, $get_range);
$sheet_values = $response->getValues();
} catch (Exception $e) {
// echo "Error connecting to service:\n";
// echo $e."\n";
// echo "Retrying...\n";
// sleep(30);
try {
$response = $service->spreadsheets_values->get($spreadsheet_id, $get_range);
$sheet_values = $response->getValues();
} catch (Exception $e) {
echo "Failed to get current data.\n$e\n";
exit;
}
}
$values = array();
foreach ($sheet_values as $row) {
if (sizeof($row) == 2) {
// is data row
if ($row[1] && $row[1] != "#VALUE!") {
// all data rows should have a value
$values[$row[0]] = $row[1];
} else {
// bad sheet data fetch, cancel, notify and run again manually
$subject = "WA Notify Report - Error";
$message = "There was a problem fetching summary values:\n\n" . json_encode($sheet_values);
$message .= "<p>Detailed Statistics:<br>https://docs.google.com/spreadsheets/d/$spreadsheet_id</p>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "From: <wanotify@cirg.washington.edu>\r\n";
mail($errorRecipientList, $subject, $message, $headers);
exit;
}
}
}
if (isset($values["Total iOS Activations"], $values["Total Android Activations"], $values["Total Activations"], $values["Rate in smartphone owners"])) {
$ios = $values["Total iOS Activations"];
$android = $values["Total Android Activations"];
$total = $values["Total Activations"];
$smartphone_rate = $values["Rate in smartphone owners"];
} else {
$subject = "WA Notify Report - Error";
$message = "There was a problem fetching summary values:\n\n" . json_encode($sheet_values);
$message .= "<p>Detailed Statistics:<br>https://docs.google.com/spreadsheets/d/$spreadsheet_id</p>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "From: <wanotify@cirg.washington.edu>\r\n";
mail($errorRecipientList, $subject, $message, $headers);
exit;
}
$precision = 10000;
$lt_threshold = 4500;
$est = $total - ($total % $precision);
$diff = $total - $est;
$sign;
if ($diff < ($precision - $lt_threshold)) {
$sign = "More than ";
} else {
$sign = "Nearly ";
$est = $est+$precision;
}
$est = $est/(10**6);
$est = $sign . $est;
$subject = "WA Notify Report - $time";
$txt = '';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "From: <wanotify@cirg.washington.edu>\r\n";
$message = "
<html>
<head>
<title>WA Notify Summary, $time</title>
</head>
<body>
<p>WA Notify Activation Counts:</p>
<table>
<tr><th style='text-align:left'>Date/Time</th><td style='text-align:right'>{$dt->format('m/d/Y H:i')}</td></tr>
<tr><th style='text-align:left'>Estimate</th><td style='text-align:right'>$est million</td></tr>
<tr><th style='text-align:left'>App Activations</th><td style='text-align:right'>".number_format($total)."</td></tr>
<tr><th style='text-align:left'>iOS Activations</th><td style='text-align:right'>".number_format($ios)."</td></tr>
<tr><th style='text-align:left'>Android Activations</th><td style='text-align:right'>".number_format($android)."</td></tr>
<tr><th style='text-align:left'>Activation Rate in smartphone owners</th><td style='text-align:right'>".$smartphone_rate."</td></tr>
</table>
<br>
<p>Detailed Statistics:<br>https://docs.google.com/spreadsheets/d/$spreadsheet_id</p>
</body>
</html>
";
mail($recipientList, $subject, $message, $headers);
?>