Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
import os
|
| 4 |
-
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
SUPPORT_URL = "https://chat.openai.com"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def load_log_file(file):
|
| 9 |
"""アップロードされたログファイルを開いて内容を取得"""
|
| 10 |
if file is None:
|
|
@@ -31,10 +34,12 @@ def search_logs(file, search_word, year, month, day):
|
|
| 31 |
return [], "エラー: ユーザー検索と日付検索を同時に行うことはできません。", None
|
| 32 |
|
| 33 |
results = []
|
|
|
|
| 34 |
|
| 35 |
# ユーザー検索
|
| 36 |
if search_word.strip():
|
| 37 |
results = [line.strip() for line in log_lines if search_word in line]
|
|
|
|
| 38 |
|
| 39 |
# 日付検索
|
| 40 |
elif year != "----" and month != "----" and day != "----":
|
|
@@ -43,13 +48,14 @@ def search_logs(file, search_word, year, month, day):
|
|
| 43 |
start_date = datetime.strptime(search_date, "%Y/%m/%d")
|
| 44 |
end_date = start_date + timedelta(days=1)
|
| 45 |
results = extract_date_range_logs(log_lines, start_date, end_date)
|
|
|
|
| 46 |
except ValueError:
|
| 47 |
return [], "無効な日付形式です。", None
|
| 48 |
|
| 49 |
if results:
|
| 50 |
-
return results, f"{len(results)} 件の結果が見つかりました。", results
|
| 51 |
else:
|
| 52 |
-
return [], "一致する結果が見つかりませんでした。", None
|
| 53 |
|
| 54 |
def extract_date_range_logs(lines, start_date, end_date):
|
| 55 |
"""指定された日付範囲のログを抽出する"""
|
|
@@ -69,16 +75,22 @@ def extract_date_range_logs(lines, start_date, end_date):
|
|
| 69 |
|
| 70 |
return result
|
| 71 |
|
| 72 |
-
def export_results(results):
|
| 73 |
"""検索結果をTXTファイルとしてエクスポート"""
|
| 74 |
if not results:
|
| 75 |
return None, "エラー: エクスポートするデータがありません。"
|
| 76 |
|
| 77 |
-
filename = f"search_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 78 |
file_path = os.path.join("/tmp", filename) # 一時ディレクトリに保存
|
| 79 |
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
with open(file_path, "w", encoding="utf-8") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
f.write("\n".join(results))
|
| 83 |
|
| 84 |
return file_path, "TXTファイルのダウンロードが可能になりました。"
|
|
@@ -111,12 +123,12 @@ with gr.Blocks() as demo:
|
|
| 111 |
search_button.click(
|
| 112 |
search_logs,
|
| 113 |
inputs=[file_input, search_word, year, month, day],
|
| 114 |
-
outputs=[result_table, result_message, export_button]
|
| 115 |
)
|
| 116 |
|
| 117 |
export_button.click(
|
| 118 |
export_results,
|
| 119 |
-
inputs=[result_table],
|
| 120 |
outputs=[export_output, export_status]
|
| 121 |
)
|
| 122 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
import os
|
| 4 |
+
from datetime import datetime, timedelta, timezone
|
| 5 |
|
| 6 |
SUPPORT_URL = "https://chat.openai.com"
|
| 7 |
|
| 8 |
+
# 日本時間の設定
|
| 9 |
+
JST = timezone(timedelta(hours=9))
|
| 10 |
+
|
| 11 |
def load_log_file(file):
|
| 12 |
"""アップロードされたログファイルを開いて内容を取得"""
|
| 13 |
if file is None:
|
|
|
|
| 34 |
return [], "エラー: ユーザー検索と日付検索を同時に行うことはできません。", None
|
| 35 |
|
| 36 |
results = []
|
| 37 |
+
search_condition = ""
|
| 38 |
|
| 39 |
# ユーザー検索
|
| 40 |
if search_word.strip():
|
| 41 |
results = [line.strip() for line in log_lines if search_word in line]
|
| 42 |
+
search_condition = f"ユーザー検索: {search_word}"
|
| 43 |
|
| 44 |
# 日付検索
|
| 45 |
elif year != "----" and month != "----" and day != "----":
|
|
|
|
| 48 |
start_date = datetime.strptime(search_date, "%Y/%m/%d")
|
| 49 |
end_date = start_date + timedelta(days=1)
|
| 50 |
results = extract_date_range_logs(log_lines, start_date, end_date)
|
| 51 |
+
search_condition = f"日付検索: {year}/{month}/{day}"
|
| 52 |
except ValueError:
|
| 53 |
return [], "無効な日付形式です。", None
|
| 54 |
|
| 55 |
if results:
|
| 56 |
+
return results, f"{len(results)} 件の結果が見つかりました。", results, search_condition
|
| 57 |
else:
|
| 58 |
+
return [], "一致する結果が見つかりませんでした。", None, search_condition
|
| 59 |
|
| 60 |
def extract_date_range_logs(lines, start_date, end_date):
|
| 61 |
"""指定された日付範囲のログを抽出する"""
|
|
|
|
| 75 |
|
| 76 |
return result
|
| 77 |
|
| 78 |
+
def export_results(results, search_condition):
|
| 79 |
"""検索結果をTXTファイルとしてエクスポート"""
|
| 80 |
if not results:
|
| 81 |
return None, "エラー: エクスポートするデータがありません。"
|
| 82 |
|
| 83 |
+
filename = f"search_results_{datetime.now(JST).strftime('%Y%m%d_%H%M%S')}.txt"
|
| 84 |
file_path = os.path.join("/tmp", filename) # 一時ディレクトリに保存
|
| 85 |
|
| 86 |
+
execution_date = datetime.now(JST).strftime("%Y/%m/%d %H:%M:%S")
|
| 87 |
+
|
| 88 |
try:
|
| 89 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 90 |
+
f.write("プログラム名: LINEログマネージャー\n")
|
| 91 |
+
f.write(f"検索実行日(日本時間): {execution_date}\n")
|
| 92 |
+
f.write(f"検索条件: {search_condition}\n")
|
| 93 |
+
f.write(f"--- 検索結果({len(results)} 件ヒット) ---\n")
|
| 94 |
f.write("\n".join(results))
|
| 95 |
|
| 96 |
return file_path, "TXTファイルのダウンロードが可能になりました。"
|
|
|
|
| 123 |
search_button.click(
|
| 124 |
search_logs,
|
| 125 |
inputs=[file_input, search_word, year, month, day],
|
| 126 |
+
outputs=[result_table, result_message, export_button, gr.State()]
|
| 127 |
)
|
| 128 |
|
| 129 |
export_button.click(
|
| 130 |
export_results,
|
| 131 |
+
inputs=[result_table, gr.State()],
|
| 132 |
outputs=[export_output, export_status]
|
| 133 |
)
|
| 134 |
|