k96beni commited on
Commit
f63571f
·
verified ·
1 Parent(s): d0cfb17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # slack_reporter/app.py
2
  import os
3
  import time
4
  import schedule
@@ -26,6 +26,10 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
26
 
27
  # --- Funktion: Skapa grafer ---
28
  def generate_graphs(df):
 
 
 
 
29
  now = datetime.now()
30
  df['timestamp'] = pd.to_datetime(df['timestamp'])
31
 
@@ -40,6 +44,7 @@ def generate_graphs(df):
40
  user_graph_path = "daily_users.png"
41
  plt.savefig(user_graph_path)
42
  plt.close()
 
43
 
44
  # 2. Meddelanden per timme (72h)
45
  cutoff = now - timedelta(hours=72)
@@ -53,10 +58,10 @@ def generate_graphs(df):
53
  hourly_graph_path = "hourly_msgs.png"
54
  plt.savefig(hourly_graph_path)
55
  plt.close()
 
56
 
57
  return user_graph_path, hourly_graph_path
58
 
59
-
60
  # --- Funktion: Ladda upp grafer till Hugging Face Hub ---
61
  def upload_graphs_to_hub(user_graph_path, hourly_graph_path):
62
  api = HfApi()
@@ -67,6 +72,8 @@ def upload_graphs_to_hub(user_graph_path, hourly_graph_path):
67
  repo_type="dataset",
68
  token=HF_TOKEN
69
  )
 
 
70
  api.upload_file(
71
  path_or_fileobj=hourly_graph_path,
72
  path_in_repo="graphs/hourly_msgs.png",
@@ -74,6 +81,8 @@ def upload_graphs_to_hub(user_graph_path, hourly_graph_path):
74
  repo_type="dataset",
75
  token=HF_TOKEN
76
  )
 
 
77
  return (
78
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/graphs/daily_users.png",
79
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/graphs/hourly_msgs.png"
@@ -90,6 +99,8 @@ def send_to_slack(user_url, hourly_url):
90
  }
91
  try:
92
  response = requests.post(WEBHOOK_URL, json=payload)
 
 
93
  response.raise_for_status()
94
  logging.info("Slack-meddelande postat.")
95
  except Exception as e:
@@ -143,6 +154,10 @@ def run_report():
143
  df = parse_legacy_log(log_path)
144
 
145
  user_graph, hourly_graph = generate_graphs(df)
 
 
 
 
146
  user_url, hourly_url = upload_graphs_to_hub(user_graph, hourly_graph)
147
  send_to_slack(user_url, hourly_url)
148
  logging.info("Rapport skickad till Slack.")
@@ -173,3 +188,4 @@ with gr.Blocks() as app:
173
 
174
  if __name__ == "__main__":
175
  app.launch()
 
 
1
+ # slack_reporter/app.py – med förbättrad loggning
2
  import os
3
  import time
4
  import schedule
 
26
 
27
  # --- Funktion: Skapa grafer ---
28
  def generate_graphs(df):
29
+ if df.empty:
30
+ logging.warning("DataFrame är tom – inga loggar att analysera.")
31
+ return None, None
32
+
33
  now = datetime.now()
34
  df['timestamp'] = pd.to_datetime(df['timestamp'])
35
 
 
44
  user_graph_path = "daily_users.png"
45
  plt.savefig(user_graph_path)
46
  plt.close()
47
+ logging.info(f"Graf sparad: {user_graph_path}")
48
 
49
  # 2. Meddelanden per timme (72h)
50
  cutoff = now - timedelta(hours=72)
 
58
  hourly_graph_path = "hourly_msgs.png"
59
  plt.savefig(hourly_graph_path)
60
  plt.close()
61
+ logging.info(f"Graf sparad: {hourly_graph_path}")
62
 
63
  return user_graph_path, hourly_graph_path
64
 
 
65
  # --- Funktion: Ladda upp grafer till Hugging Face Hub ---
66
  def upload_graphs_to_hub(user_graph_path, hourly_graph_path):
67
  api = HfApi()
 
72
  repo_type="dataset",
73
  token=HF_TOKEN
74
  )
75
+ logging.info("Uploader lyckades: daily_users.png")
76
+
77
  api.upload_file(
78
  path_or_fileobj=hourly_graph_path,
79
  path_in_repo="graphs/hourly_msgs.png",
 
81
  repo_type="dataset",
82
  token=HF_TOKEN
83
  )
84
+ logging.info("Uploader lyckades: hourly_msgs.png")
85
+
86
  return (
87
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/graphs/daily_users.png",
88
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/graphs/hourly_msgs.png"
 
99
  }
100
  try:
101
  response = requests.post(WEBHOOK_URL, json=payload)
102
+ logging.info(f"Slack response status: {response.status_code}")
103
+ logging.info(f"Slack response text: {response.text}")
104
  response.raise_for_status()
105
  logging.info("Slack-meddelande postat.")
106
  except Exception as e:
 
154
  df = parse_legacy_log(log_path)
155
 
156
  user_graph, hourly_graph = generate_graphs(df)
157
+ if user_graph is None or hourly_graph is None:
158
+ logging.warning("Hoppar Slack-post – grafer saknas.")
159
+ return "⚠️ Inga data att rapportera."
160
+
161
  user_url, hourly_url = upload_graphs_to_hub(user_graph, hourly_graph)
162
  send_to_slack(user_url, hourly_url)
163
  logging.info("Rapport skickad till Slack.")
 
188
 
189
  if __name__ == "__main__":
190
  app.launch()
191
+