Skip to content

Commit e29a579

Browse files
authored
feat(server): add the active_dates_and_times insight (#724)
* feat(server): add the active_dates_and_times insight * chore: add the test for the activity * chore: fix ci
1 parent 61fc5c3 commit e29a579

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

server/agent/llm/clients/deepseek.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
from typing import Any, List, Optional
42
from langchain_openai import ChatOpenAI
53
from langchain_core.utils.function_calling import convert_to_openai_tool
@@ -35,7 +33,7 @@ def __init__(
3533
max_tokens=max_tokens,
3634
openai_api_key=api_key,
3735
stream_usage=True,
38-
openai_api_base="https://api.deepseek.com"
36+
openai_api_base="https://api.deepseek.com",
3937
)
4038

4139
def get_client(self):
@@ -45,4 +43,4 @@ def get_tools(self, tools: List[Any]):
4543
return [convert_to_openai_tool(tool) for tool in tools]
4644

4745
def parse_content(self, content: List[MessageContent]):
48-
return [c.model_dump() for c in content]
46+
return [c.model_dump() for c in content]

server/insight/router.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from fastapi import APIRouter
3-
from insight.service.activity import get_activity_data
3+
from insight.service.activity import get_active_dates_and_times, get_activity_data
44
from insight.service.issue import get_issue_data
55
from insight.service.pr import get_code_frequency, get_pr_data
66

@@ -63,3 +63,16 @@ def get_activity_insight(repo_name: str):
6363

6464
except Exception as e:
6565
return json.dumps({"success": False, "message": str(e)})
66+
67+
68+
@router.get("/active_dates_and_times")
69+
def get_active_dates_and_times_insight(repo_name: str):
70+
try:
71+
result = get_active_dates_and_times(repo_name)
72+
return {
73+
"success": True,
74+
"data": result,
75+
}
76+
77+
except Exception as e:
78+
return json.dumps({"success": False, "message": str(e)})

server/insight/service/activity.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import requests
2+
import re
23
from typing import List, Dict
34

45

@@ -25,3 +26,69 @@ def get_activity_data(repo_name: str) -> List[Dict[str, int]]:
2526
except Exception as e:
2627
print(e)
2728
return []
29+
30+
31+
def get_active_dates_and_times(repo_name: str):
32+
url = f"https://oss.open-digger.cn/github/{repo_name}/active_dates_and_times.json"
33+
try:
34+
resp = requests.get(url)
35+
resp.raise_for_status()
36+
data = resp.json()
37+
38+
pattern_year = re.compile(r"^\d{4}$") # e.g. "2024"
39+
pattern_quarter = re.compile(r"^\d{4}Q[1-4]$") # e.g. "2024Q3"
40+
pattern_month = re.compile(r"^\d{4}-\d{2}$") # e.g. "2024-08"
41+
42+
years = []
43+
quarters = []
44+
months = []
45+
46+
for k in data.keys():
47+
if pattern_year.match(k):
48+
years.append(k)
49+
elif pattern_quarter.match(k):
50+
quarters.append(k)
51+
elif pattern_month.match(k):
52+
months.append(k)
53+
54+
def safe_get_latest(lst):
55+
return sorted(lst)[-1] if lst else None
56+
57+
latest_year_key = safe_get_latest(years)
58+
latest_quarter_key = safe_get_latest(quarters)
59+
latest_month_key = safe_get_latest(months)
60+
61+
def convert_168_to_day_hour_value(arr_168):
62+
if not arr_168:
63+
return []
64+
65+
day_map = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
66+
result = []
67+
for i, value in enumerate(arr_168):
68+
day_index = i // 24
69+
hour = i % 24
70+
result.append({"day": day_map[day_index], "hour": hour, "value": value})
71+
return result
72+
73+
year_data = (
74+
convert_168_to_day_hour_value(data[latest_year_key])
75+
if latest_year_key
76+
else []
77+
)
78+
quarter_data = (
79+
convert_168_to_day_hour_value(data[latest_quarter_key])
80+
if latest_quarter_key
81+
else []
82+
)
83+
month_data = (
84+
convert_168_to_day_hour_value(data[latest_month_key])
85+
if latest_month_key
86+
else []
87+
)
88+
89+
result = {"year": year_data, "quarter": quarter_data, "month": month_data}
90+
91+
return result
92+
except Exception as e:
93+
print(e)
94+
return []

server/tests/insight/test_activity.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
2-
from unittest.mock import patch, MagicMock
3-
from insight.service.activity import get_activity_data
2+
from unittest.mock import Mock, patch, MagicMock
3+
from insight.service.activity import get_activity_data, get_active_dates_and_times
44

55

66
class TestGetActivityData(unittest.TestCase):
@@ -42,3 +42,39 @@ def test_get_activity_data_invalid_json(self, mock_get):
4242
repo_name = "petercat-ai/petercat"
4343
result = get_activity_data(repo_name)
4444
self.assertEqual(result, [])
45+
46+
47+
class TestGetActiveDatesAndTimes(unittest.TestCase):
48+
49+
@patch("insight.service.activity.requests.get")
50+
def test_get_active_dates_and_times(self, mock_get):
51+
fake_json = {
52+
"2024": [0] * 168,
53+
"2025": [1] * 168,
54+
"2025Q1": [2] * 168,
55+
"2025-01": [3] * 168,
56+
}
57+
58+
mock_resp = Mock()
59+
mock_resp.json.return_value = fake_json
60+
mock_resp.status_code = 200
61+
mock_get.return_value = mock_resp
62+
63+
result = get_active_dates_and_times("petercat-ai/petercat")
64+
65+
self.assertIn("year", result)
66+
self.assertIn("quarter", result)
67+
self.assertIn("month", result)
68+
69+
self.assertEqual(len(result["year"]), 168)
70+
self.assertEqual(len(result["quarter"]), 168)
71+
self.assertEqual(len(result["month"]), 168)
72+
73+
self.assertEqual(result["year"][0], {"day": "Mon", "hour": 0, "value": 1})
74+
self.assertEqual(result["year"][167], {"day": "Sun", "hour": 23, "value": 1})
75+
76+
self.assertEqual(result["quarter"][0], {"day": "Mon", "hour": 0, "value": 2})
77+
self.assertEqual(result["quarter"][167], {"day": "Sun", "hour": 23, "value": 2})
78+
79+
self.assertEqual(result["month"][0], {"day": "Mon", "hour": 0, "value": 3})
80+
self.assertEqual(result["month"][167], {"day": "Sun", "hour": 23, "value": 3})

0 commit comments

Comments
 (0)