-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_traffic_stats.ps1
More file actions
63 lines (54 loc) · 2.19 KB
/
run_traffic_stats.ps1
File metadata and controls
63 lines (54 loc) · 2.19 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
# GitHub Traffic Stats Scheduled Task Script
# Runs the Python traffic analytics script and logs output
param(
[string]$LogPath = "$HOME/Code/asciimath/energy/traffic_stats_log.txt"
)
# Set working directory (use forward slashes for Linux)
Set-Location "$HOME/Code/asciimath/energy"
# Use venv Python
$venvPython = "$HOME/Code/asciimath/energy/venv/bin/python"
# Get timestamp
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Log start
"[$timestamp] Starting GitHub traffic stats collection..." | Add-Content $LogPath
try {
# Run the Python script and capture output and exit code
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
$processInfo.FileName = $venvPython
$processInfo.Arguments = "tools/check_traffic_stats.py"
$processInfo.RedirectStandardOutput = $true
$processInfo.RedirectStandardError = $true
$processInfo.UseShellExecute = $false
$processInfo.CreateNoWindow = $true
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $processInfo
$process.Start() | Out-Null
$stdout = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
$process.WaitForExit()
$exitCode = $process.ExitCode
# Log the output
"[$timestamp] Script output:" | Add-Content $LogPath
$stdout | Add-Content $LogPath
if ($stderr) {
"[$timestamp] Script stderr:" | Add-Content $LogPath
$stderr | Add-Content $LogPath
}
if ($exitCode -eq 0) {
# Log completion
$endTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"[$endTimestamp] GitHub traffic stats collection completed successfully." | Add-Content $LogPath
} else {
$errorTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"[$errorTimestamp] ERROR: Python script exited with code $exitCode" | Add-Content $LogPath
}
} catch {
# Log any errors
$errorTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"[$errorTimestamp] EXCEPTION: $($_.Exception.Message)" | Add-Content $LogPath
if ($_.Exception.StackTrace) {
"StackTrace: $($_.Exception.StackTrace)" | Add-Content $LogPath
}
}
# Add separator
"----------------------------------------" | Add-Content $LogPath