Implement initial project structure and setup
Browse files- .gitignore +3 -0
- app.py +115 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv/
|
| 2 |
+
*.un~
|
| 3 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SUPABASE_URL = userdata.get('SUPABASE_URL')
|
| 2 |
+
|
| 3 |
+
SUPABASE_KEY = userdata.get('SUPABASE_KEY')
|
| 4 |
+
|
| 5 |
+
import supabase
|
| 6 |
+
table_threshold = "Threshold_data"
|
| 7 |
+
table_sensor = "Sensor_data"
|
| 8 |
+
table_troubleshooting = "Troubleshooting_collection"
|
| 9 |
+
|
| 10 |
+
# クライアントの初期化
|
| 11 |
+
supabase_client = supabase.create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# データ取得
|
| 15 |
+
threshold_data = supabase_client.table(table_threshold).select("*").execute()
|
| 16 |
+
sensor_data = supabase_client.table(table_sensor).select("*").execute()
|
| 17 |
+
troubleshooting_data = supabase_client.table(table_troubleshooting).select("*").execute()
|
| 18 |
+
|
| 19 |
+
import pandas as pd
|
| 20 |
+
threshold_df = pd.DataFrame(threshold_data.data)
|
| 21 |
+
sensor_df = pd.DataFrame(sensor_data.data)
|
| 22 |
+
troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
|
| 23 |
+
|
| 24 |
+
def check_thresholds(sensor_df, threshold_df):
|
| 25 |
+
alerts = []
|
| 26 |
+
|
| 27 |
+
# '下限'と'上限'カラムを数値型に変換。変換できない値はNaNとする。
|
| 28 |
+
threshold_df['下限'] = pd.to_numeric(threshold_df['下限'], errors='coerce')
|
| 29 |
+
threshold_df['上限'] = pd.to_numeric(threshold_df['上限'], errors='coerce')
|
| 30 |
+
|
| 31 |
+
for _, row in threshold_df.iterrows():
|
| 32 |
+
metric = row["指標名"]
|
| 33 |
+
min_val = row["下限"]
|
| 34 |
+
max_val = row["上限"]
|
| 35 |
+
data_no = row["No."] # Get the 'No.' from threshold_df
|
| 36 |
+
|
| 37 |
+
# センサーデータに指標が存在しない場合はスキップ
|
| 38 |
+
if metric not in sensor_df.columns:
|
| 39 |
+
continue
|
| 40 |
+
|
| 41 |
+
# センサーデータの該当カラムを数値型に変換。変換できない値はNaNとする。
|
| 42 |
+
sensor_metric_data = pd.to_numeric(sensor_df[metric], errors='coerce')
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
for i, value in enumerate(sensor_metric_data):
|
| 46 |
+
# Assuming 'datetime' is the timestamp column in sensor_df
|
| 47 |
+
timestamp = sensor_df.loc[i, "datetime"] if "datetime" in sensor_df.columns else i
|
| 48 |
+
|
| 49 |
+
# 下限チェック
|
| 50 |
+
if pd.notna(min_val) and pd.notna(value) and value < min_val:
|
| 51 |
+
alerts.append({
|
| 52 |
+
"timestamp": timestamp,
|
| 53 |
+
"metric": metric,
|
| 54 |
+
"value": value,
|
| 55 |
+
"status": f"下限値 {min_val} 未満",
|
| 56 |
+
"data no.": data_no # Add the 'data no.'
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
# 上限チェック
|
| 60 |
+
if pd.notna(max_val) and pd.notna(value) and value > max_val:
|
| 61 |
+
alerts.append({
|
| 62 |
+
"timestamp": timestamp,
|
| 63 |
+
"metric": metric,
|
| 64 |
+
"value": value,
|
| 65 |
+
"status": f"上限値 {max_val} 超過",
|
| 66 |
+
"data no.": data_no # Add the 'data no.'
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
return pd.DataFrame(alerts)
|
| 70 |
+
|
| 71 |
+
# 実行
|
| 72 |
+
alerts_df = check_thresholds(sensor_df, threshold_df)
|
| 73 |
+
|
| 74 |
+
# 同じタイムスタンプでdata no.が二つ以上あるデータを抽出
|
| 75 |
+
grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
|
| 76 |
+
multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
|
| 77 |
+
|
| 78 |
+
# alerts_df を複数データ番号を持つタイムスタンプでフィルタリング
|
| 79 |
+
filtered_alerts_df = alerts_df[alerts_df['timestamp'].isin(multiple_data_nos_timestamps)]
|
| 80 |
+
|
| 81 |
+
# タイムスタンプでグループ化し、各タイムスタンプのユニークなデータ番号をリストとして取得
|
| 82 |
+
data_nos_by_timestamp = filtered_alerts_df.groupby('timestamp')['data no.'].unique().apply(list)
|
| 83 |
+
|
| 84 |
+
# 結果を辞書のリストに変換(オプション。分かりやすさのため)
|
| 85 |
+
result_list = []
|
| 86 |
+
for timestamp, data_nos in data_nos_by_timestamp.items():
|
| 87 |
+
# 数値のリストをカンマ区切りの文字列に変換
|
| 88 |
+
data_nos_str = ', '.join(map(str, data_nos))
|
| 89 |
+
result_list.append({"timestamp": timestamp, "data_nos": data_nos_str})
|
| 90 |
+
|
| 91 |
+
# 辞書のリストをpandas DataFrameに変換
|
| 92 |
+
result_df = pd.DataFrame(result_list)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# troubleshooting_dfの指標No.列をリストのリストに変換
|
| 96 |
+
# 例: '1,2' -> [1, 2]
|
| 97 |
+
troubleshooting_indicator_lists = troubleshooting_df['指標No.'].str.split(',').apply(lambda x: [int(i) for i in x])
|
| 98 |
+
|
| 99 |
+
# result_dfのdata_nos列をリストのリストに変換
|
| 100 |
+
# 例: '5, 25' -> [5, 25]
|
| 101 |
+
result_data_nos_lists = result_df['data_nos'].str.split(', ').apply(lambda x: [int(i) for i in x])
|
| 102 |
+
|
| 103 |
+
# 組み合わせを持つかどうか判定し、該当する情報を出力
|
| 104 |
+
for i, result_nos in enumerate(result_data_nos_lists):
|
| 105 |
+
result_timestamp = result_df.loc[i, 'timestamp']
|
| 106 |
+
for j, troubleshooting_nos in enumerate(troubleshooting_indicator_lists):
|
| 107 |
+
# troubleshooting_nos の組み合わせが result_nos の組み合わせに含まれるか判定
|
| 108 |
+
if set(troubleshooting_nos).issubset(set(result_nos)):
|
| 109 |
+
troubleshooting_situation = troubleshooting_df.loc[j, 'シチュエーション\n(対応が必要な状況)']
|
| 110 |
+
troubleshooting_action = troubleshooting_df.loc[j, 'sub goal到達のために必要な行動\n(解決策)']
|
| 111 |
+
|
| 112 |
+
print(f"Timestamp: {result_timestamp}")
|
| 113 |
+
print(f"Situation: {troubleshooting_situation}")
|
| 114 |
+
print(f"Action: {troubleshooting_action}")
|
| 115 |
+
print("-" * 20) # 区切り線
|