SPARC64 commited on
Commit
851155e
·
verified ·
1 Parent(s): 2f67df0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -20,30 +20,28 @@ def load_log_file(file):
20
  return None, f"エラー: {str(e)}"
21
 
22
  def search_logs(file, keyword, year, month, day):
23
- # 検索条件を作成
24
- search_condition = f"キーワード: {keyword}, 年: {year}, 月: {month}, 日: {day}"
25
  """ログを検索し、結果を返す"""
26
  log_lines, error = load_log_file(file)
27
  if error:
28
- return "", error, None, "検索条件: なし"
29
 
30
  if not log_lines:
31
- return "", "エラー: ログファイルが空です。", None, "検索条件: なし"
32
 
33
  # 検索条件のチェック
34
- is_word_search = bool(search_word.strip())
35
  is_date_search = year != "----" and month != "----" and day != "----"
36
 
37
  if is_word_search and is_date_search:
38
- return "", "エラー: ワード検索と日付検索は同時に行えません。", None, "検索条件: なし"
39
 
40
  # 検索条件の設定
41
  search_condition = "検索条件: なし"
42
  results = []
43
 
44
  if is_word_search:
45
- search_condition = f"検索条件: ワード[{search_word}]"
46
- results = [f"- {line.strip()}" for line in log_lines if search_word in line]
47
 
48
  elif is_date_search:
49
  try:
@@ -54,11 +52,11 @@ def search_logs(file, keyword, year, month, day):
54
  results = extract_date_range_logs(log_lines, start_date, end_date)
55
  results = [f"- {line.strip()}" for line in results]
56
  except ValueError:
57
- return "", "エラー: 無効な日付形式です。", None, "検索条件: なし"
58
 
59
  formatted_results = "\n".join(results) if results else "一致する結果が見つかりませんでした。"
60
 
61
- return formatted_results, message, search_condition
62
 
63
  def extract_date_range_logs(lines, start_date, end_date):
64
  """指定された日付範囲のログを抽出する"""
@@ -119,22 +117,24 @@ with gr.Blocks() as demo:
119
  result_table = gr.Textbox(label="検索結果", interactive=False, lines=10)
120
  result_message = gr.Textbox(label="ステータス", interactive=False)
121
 
 
 
122
  # エクスポート機能
123
  export_button = gr.Button("TXTをエクスポートする")
124
  export_output = gr.File(label="ダウンロード", interactive=False)
125
  export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
126
 
127
  search_button.click(
128
- search_logs,
129
- inputs=[file_input, search_word, year, month, day],
130
- outputs=[result_table, result_message, gr.State()] # ここに search_condition を格納
131
- )
132
- export_button.click(
133
- export_results,
134
- inputs=[result_table, gr.State()], # ここで search_condition を渡す
135
- outputs=[export_output, export_status]
136
- )
137
 
 
 
 
 
 
138
 
139
  gr.Markdown(f"[📖 ヘルプページ]({SUPPORT_URL})")
140
  gr.Markdown(f"[🛠 サポート]({SUPPORT_URL})")
 
20
  return None, f"エラー: {str(e)}"
21
 
22
  def search_logs(file, keyword, year, month, day):
 
 
23
  """ログを検索し、結果を返す"""
24
  log_lines, error = load_log_file(file)
25
  if error:
26
+ return "", error, None
27
 
28
  if not log_lines:
29
+ return "", "エラー: ログファイルが空です。", None
30
 
31
  # 検索条件のチェック
32
+ is_word_search = bool(keyword.strip())
33
  is_date_search = year != "----" and month != "----" and day != "----"
34
 
35
  if is_word_search and is_date_search:
36
+ return "", "エラー: ワード検索と日付検索は同時に行えません。", None
37
 
38
  # 検索条件の設定
39
  search_condition = "検索条件: なし"
40
  results = []
41
 
42
  if is_word_search:
43
+ search_condition = f"検索条件: ワード[{keyword}]"
44
+ results = [f"- {line.strip()}" for line in log_lines if keyword in line]
45
 
46
  elif is_date_search:
47
  try:
 
52
  results = extract_date_range_logs(log_lines, start_date, end_date)
53
  results = [f"- {line.strip()}" for line in results]
54
  except ValueError:
55
+ return "", "エラー: 無効な日付形式です。", None
56
 
57
  formatted_results = "\n".join(results) if results else "一致する結果が見つかりませんでした。"
58
 
59
+ return formatted_results, f"{len(results)} 件の結果が見つかりました。", search_condition
60
 
61
  def extract_date_range_logs(lines, start_date, end_date):
62
  """指定された日付範囲のログを抽出する"""
 
117
  result_table = gr.Textbox(label="検索結果", interactive=False, lines=10)
118
  result_message = gr.Textbox(label="ステータス", interactive=False)
119
 
120
+ search_condition_state = gr.State()
121
+
122
  # エクスポート機能
123
  export_button = gr.Button("TXTをエクスポートする")
124
  export_output = gr.File(label="ダウンロード", interactive=False)
125
  export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
126
 
127
  search_button.click(
128
+ search_logs,
129
+ inputs=[file_input, search_word, year, month, day],
130
+ outputs=[result_table, result_message, search_condition_state]
131
+ )
 
 
 
 
 
132
 
133
+ export_button.click(
134
+ export_results,
135
+ inputs=[result_table, search_condition_state],
136
+ outputs=[export_output, export_status]
137
+ )
138
 
139
  gr.Markdown(f"[📖 ヘルプページ]({SUPPORT_URL})")
140
  gr.Markdown(f"[🛠 サポート]({SUPPORT_URL})")