-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.ps1
More file actions
152 lines (127 loc) · 7.65 KB
/
demo.ps1
File metadata and controls
152 lines (127 loc) · 7.65 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Force UTF-8 encoding for the entire script
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
# Set the console to UTF-8 mode
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$PostgresHost = "flink-postgres"
$PostgresUser = "flink_user"
$PostgresDB = "flink_db"
$FlinkClientContainer = "flink-client-pyflink"
$JobManagerHostPort = "jobmanager:8081" # Adresse pour la soumission des jobs Flink
# --- Fonctions Utilitaires ---
function Pause-For-Execution {
param([string]$message = "...")
Write-Host "`n$message" -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
}
function Run-Command-Interactive {
param([string]$command, [string]$description)
Write-Host "`n--- $description ---" -ForegroundColor Cyan
Write-Host "Commande à exécuter :" -ForegroundColor White
Write-Host "$command" -ForegroundColor Green # Affiche la commande avant exécution
Pause-For-Execution "..."
Write-Host "Exécution en cours..." -ForegroundColor DarkYellow
try {
# Execute command with UTF-8 encoding
$tempOutputFile = Join-Path $env:TEMP "temp_output.txt"
$process = Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -Command `"$command`"" -NoNewWindow -Wait -PassThru -RedirectStandardOutput $tempOutputFile
$output = Get-Content -Path $tempOutputFile -Encoding UTF8
$output | Out-Host
Remove-Item $tempOutputFile -ErrorAction SilentlyContinue
Write-Host "Commande exécutée avec succès." -ForegroundColor Green
} catch {
Write-Host "ERREUR : La commande '$command' a échoué." -ForegroundColor Red
Write-Error $_.Exception.Message
exit 1
}
Pause-For-Execution "Commande '$command' ..."
}
function Run-SqlCommands-Interactive {
param([string]$sqlContent, [string]$description)
Write-Host "`n--- $description ---" -ForegroundColor Cyan
Write-Host "Commandes SQL à exécuter dans PostgreSQL :" -ForegroundColor White
Write-Host "$sqlContent" -ForegroundColor Green # Affiche les commandes SQL avant exécution
Pause-For-Execution "..."
# Créer un fichier SQL temporaire sur l'hôte
$tempSqlFile = Join-Path $env:TEMP "temp_sql_commands.sql"
[System.IO.File]::WriteAllText($tempSqlFile, $sqlContent, [System.Text.Encoding]::UTF8)
Write-Host "Exécution en cours..." -ForegroundColor DarkYellow
try {
# Exécuter les commandes SQL en pipant le contenu du fichier temporaire vers psql
$pipeCommand = "Get-Content -Path `"$tempSqlFile`" -Encoding UTF8 | docker exec -i $PostgresHost psql -U $PostgresUser -d $PostgresDB"
$tempOutputFile = Join-Path $env:TEMP "temp_output.txt"
$process = Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -Command `"$pipeCommand`"" -NoNewWindow -Wait -PassThru -RedirectStandardOutput $tempOutputFile
$output = Get-Content -Path $tempOutputFile -Encoding UTF8
$output | Out-Host
Remove-Item $tempOutputFile -ErrorAction SilentlyContinue
Write-Host "Commandes SQL exécutées avec succès." -ForegroundColor Green
} catch {
Write-Host "ERREUR : Les commandes SQL ont échoué." -ForegroundColor Red
Write-Error $_.Exception.Message
exit 1
} finally {
# Nettoyer le fichier SQL temporaire
Remove-Item $tempSqlFile -ErrorAction SilentlyContinue
}
Pause-For-Execution "..."
}
# --- Début de la Démonstration ---
Pause-For-Execution "..."
# 1. Démarrage du Cluster Docker
Run-Command-Interactive "docker compose up -d --build"
Start-Sleep -Seconds 2 # Attendre que les services soient entièrement démarrés
Run-Command-Interactive "docker ps -a"
# 2. Configuration de la Base de Données PostgreSQL
$sql_create_tables = @"
DROP TABLE IF EXISTS factory_machine_summary_stream;
DROP TABLE IF EXISTS factory_machine_summary_batch;
CREATE TABLE IF NOT EXISTS factory_machine_summary_stream (
machine_type VARCHAR(255) PRIMARY KEY,
avg_temperature_c REAL,
avg_vibration_mms REAL,
avg_sound_db REAL,
avg_power_consumption_kw REAL,
total_machines BIGINT,
rule_based_faulty_machines BIGINT
);
CREATE TABLE IF NOT EXISTS factory_machine_summary_batch (
machine_type VARCHAR(255) PRIMARY KEY,
avg_temperature_c REAL,
avg_vibration_mms REAL,
avg_sound_db REAL,
avg_power_consumption_kw REAL,
total_machines BIGINT,
rule_based_faulty_machines BIGINT
);
"@
Run-SqlCommands-Interactive $sql_create_tables "2. Création ou réinitialisation des tables de résultats dans PostgreSQL"
# 3. Soumission du Job Flink Streaming
Run-Command-Interactive "docker exec -it $FlinkClientContainer flink run --python /flink-job/streaming_sensor_analyzer.py --pyFiles /flink-job/streaming_sensor_analyzer.py --jobmanager $JobManagerHostPort" "3. Soumission du Job Flink Streaming (ce job restera en cours d'exécution et mettra à jour les données en continu)"
Write-Host "Le job de streaming est lancé. Vous pouvez visualiser son statut sur l'interface Flink UI à http://localhost:8081" -ForegroundColor Yellow
Write-Host "Attente de 5 secondes pour que le job de streaming commence à générer des données et à écrire dans PostgreSQL..." -ForegroundColor DarkYellow
Start-Sleep -Seconds 5 # Laisser le temps au job de démarrer et de commencer à écrire
# 4. Soumission du Job Flink Batch
Run-Command-Interactive "docker exec -it $FlinkClientContainer flink run --python /flink-job/batch_sensor_analyzer.py --pyFiles /flink-job/batch_sensor_analyzer.py --jobmanager $JobManagerHostPort" "4. Soumission du Job Flink Batch (ce job s'exécutera une fois et se terminera automatiquement)"
Write-Host "Le job batch va s'exécuter, lire le fichier CSV généré et écrire ses résultats finaux dans PostgreSQL." -ForegroundColor Yellow
Write-Host "Attente de 5 secondes pour que le job batch se termine complètement..." -ForegroundColor DarkYellow
Start-Sleep -Seconds 5 # Laisser le temps au job batch de s'exécuter
# 5. Vérification des Résultats dans PostgreSQL
Write-Host "`n--- 5. Vérification des Résultats dans PostgreSQL ---" -ForegroundColor Cyan
$sql_select_stream = "SELECT * FROM factory_machine_summary_stream;"
Run-SqlCommands-Interactive $sql_select_stream "Vérification des résultats du job de STREAMING (1ère fois - les valeurs peuvent changer lors des prochaines vérifications)"
Write-Host "Attente de 5 secondes pour une nouvelle vérification du streaming (pour observer les mises à jour)..." -ForegroundColor DarkYellow
Start-Sleep -Seconds 5
Run-SqlCommands-Interactive $sql_select_stream "Vérification des résultats du job de STREAMING (2ème fois)"
Write-Host "Attente de 5 secondes pour une dernière vérification du streaming..." -ForegroundColor DarkYellow
Start-Sleep -Seconds 5
Run-SqlCommands-Interactive $sql_select_stream "Vérification des résultats du job de STREAMING (3ème fois)"
$sql_select_batch = "SELECT * FROM factory_machine_summary_batch;"
Run-SqlCommands-Interactive $sql_select_batch "Vérification des résultats du job BATCH (résultats finaux et stables)"
# 6. Arrêt et Nettoyage du Projet
Run-Command-Interactive "docker compose down -v" "6. Arrêt et Nettoyage de tous les conteneurs et volumes Docker (suppression des données PostgreSQL)"
Write-Host "`n*****************************************************" -ForegroundColor Green
Write-Host "* DÉMONSTRATION TERMINÉE AVEC SUCCÈS ! *" -ForegroundColor Green
Write-Host "*****************************************************" -ForegroundColor Green