-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the activity insight (#703)
* chore: fix ci * feat: add the activity insight * chore: add the test for the activity * chore: fix ci
- Loading branch information
1 parent
bee2c37
commit 2c53b08
Showing
6 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import datetime | ||
import requests | ||
from typing import List, Dict | ||
|
||
|
||
def get_activity_data(repo_name: str) -> List[Dict[str, int]]: | ||
url = f"https://oss.open-digger.cn/github/{repo_name}/activity_details.json" | ||
|
||
try: | ||
response = requests.get(url) | ||
data = response.json() | ||
if not data: | ||
return [] | ||
|
||
# Filter out only the monthly data (excluding quarters) | ||
monthly_data = {k: v for k, v in data.items() if "-" in k} | ||
|
||
# Get the most recent month | ||
most_recent_month_key = max(monthly_data.keys()) | ||
|
||
# Return the data for the most recent month | ||
return [ | ||
{"user": user, "value": value} | ||
for user, value in monthly_data[most_recent_month_key] | ||
] | ||
except Exception as e: | ||
print(e) | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
import requests | ||
from collections import defaultdict | ||
|
||
from utils.insight import get_data | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
import requests | ||
from collections import defaultdict | ||
|
||
from utils.insight import get_data | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from insight.service.activity import get_activity_data | ||
|
||
|
||
class TestGetActivityData(unittest.TestCase): | ||
|
||
@patch("insight.service.activity.requests.get") | ||
def test_get_activity_data(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = { | ||
"2023-12": [("user1", 10), ("user2", 5)], | ||
"2024-01": [("user3", 20)], | ||
"2024-02": [("user4", 25)], | ||
"2024-03": [("user5", 30)], | ||
} | ||
mock_get.return_value = mock_response | ||
repo_name = "petercat-ai/petercat" | ||
expected_result = [{"user": "user5", "value": 30}] | ||
|
||
result = get_activity_data(repo_name) | ||
|
||
self.assertIsInstance(result, list) | ||
self.assertEqual(result, expected_result) | ||
|
||
@patch("insight.service.activity.requests.get") | ||
def test_get_activity_data_empty(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = {} | ||
mock_get.return_value = mock_response | ||
repo_name = "petercat-ai/petercat" | ||
result = get_activity_data(repo_name) | ||
|
||
self.assertEqual(result, []) | ||
|
||
@patch("insight.service.activity.requests.get") | ||
def test_get_activity_data_invalid_json(self, mock_get): | ||
|
||
mock_response = MagicMock() | ||
mock_response.json.side_effect = ValueError("Invalid JSON") | ||
mock_get.return_value = mock_response | ||
|
||
repo_name = "petercat-ai/petercat" | ||
with self.assertRaises(ValueError): | ||
get_activity_data(repo_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters