Repository navigation
Expand file tree
/
Copy pathlogging_functions.php
More file actions
115 lines (97 loc) · 3.53 KB
/
Copy pathlogging_functions.php
File metadata and controls
115 lines (97 loc) · 3.53 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
<?php
// logging_functions.php
/**
* Path to logs folder (adjust if needed)
*/
define('LOG_DIR', __DIR__ . '/logs');
define('DAILY_TRACKING_DIR', LOG_DIR . '/daily_tracking');
define('WEEKLY_TRACKING_DIR', LOG_DIR . '/weekly_tracking');
define('DAILY_SUMMARY_DIR', LOG_DIR . '/daily');
define('WEEKLY_SUMMARY_DIR', LOG_DIR . '/weekly');
// Ensure directories exist
foreach ([DAILY_TRACKING_DIR, WEEKLY_TRACKING_DIR, DAILY_SUMMARY_DIR, WEEKLY_SUMMARY_DIR] as $dir) {
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
/**
* Update the daily summary log for a station
*
* @param string $station Station name
* @param array $transmissions Array of ['timestamp' => status] for this run
*/
function logDailySummary($station, $transmissions) {
$date = date("Y-m-d");
$trackingFile = DAILY_TRACKING_DIR . "/{$station}_{$date}.json";
$summaryFile = DAILY_SUMMARY_DIR . "/summary_{$date}.txt";
$tracking = file_exists($trackingFile) ? json_decode(file_get_contents($trackingFile), true) : [];
foreach ($transmissions as $ts => $status) {
if (!isset($tracking[$ts]) || $tracking[$ts] !== $status) {
$tracking[$ts] = $status;
}
}
file_put_contents($trackingFile, json_encode($tracking, JSON_PRETTY_PRINT));
$counts = ['success' => 0, 'skipped' => 0, 'failed' => 0];
foreach ($tracking as $s) {
if (isset($counts[$s])) $counts[$s]++;
}
$line = sprintf("[%s] %d transmissions attempted: %d successful, %d skipped, %d failed\n",
$station,
array_sum($counts),
$counts['success'],
$counts['skipped'],
$counts['failed']
);
$allLines = file_exists($summaryFile) ? file($summaryFile, FILE_IGNORE_NEW_LINES) : [];
$found = false;
foreach ($allLines as &$l) {
if (strpos($l, "[$station]") === 0) {
$l = rtrim($line);
$found = true;
break;
}
}
if (!$found) $allLines[] = rtrim($line);
file_put_contents($summaryFile, implode("\n", $allLines) . "\n");
}
/**
* Update the weekly summary log for a station
*
* @param string $station Station name
* @param array $transmissions Array of ['timestamp' => status] for this run
*/
function logWeeklySummary($station, $transmissions) {
$weekNum = date("oW");
$trackingFile = WEEKLY_TRACKING_DIR . "/{$station}_week{$weekNum}.json";
$summaryFile = WEEKLY_SUMMARY_DIR . "/summary_{$weekNum}.txt";
$tracking = file_exists($trackingFile) ? json_decode(file_get_contents($trackingFile), true) : [];
foreach ($transmissions as $ts => $status) {
if (!isset($tracking[$ts]) || $tracking[$ts] !== $status) {
$tracking[$ts] = $status;
}
}
file_put_contents($trackingFile, json_encode($tracking, JSON_PRETTY_PRINT));
$counts = ['success' => 0, 'skipped' => 0, 'failed' => 0];
foreach ($tracking as $s) {
if (isset($counts[$s])) $counts[$s]++;
}
$line = sprintf("[%s] %d transmissions attempted: %d successful, %d skipped, %d failed\n",
$station,
array_sum($counts),
$counts['success'],
$counts['skipped'],
$counts['failed']
);
$allLines = file_exists($summaryFile) ? file($summaryFile, FILE_IGNORE_NEW_LINES) : [];
$found = false;
foreach ($allLines as &$l) {
if (strpos($l, "[$station]") === 0) {
$l = rtrim($line);
$found = true;
break;
}
}
if (!$found) $allLines[] = rtrim($line);
file_put_contents($summaryFile, implode("\n", $allLines) . "\n");
}
?>