Skip to content

Commit

Permalink
feat: add the activity insight (#703)
Browse files Browse the repository at this point in the history
* chore: fix ci

* feat: add the activity insight

* chore: add the test for the activity

* chore: fix ci
  • Loading branch information
xingwanying authored Jan 21, 2025
1 parent bee2c37 commit 2c53b08
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
18 changes: 16 additions & 2 deletions server/insight/router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
from typing import Optional
from fastapi import APIRouter, Depends
from fastapi import APIRouter
from insight.service.activity import get_activity_data
from insight.service.issue import get_issue_data
from insight.service.pr import get_code_changes, get_pr_data


# ref: https://open-digger.cn/en/docs/user_docs/metrics/metrics_usage_guide
router = APIRouter(
prefix="/api/insight",
tags=["insight"],
Expand Down Expand Up @@ -49,3 +50,16 @@ def get_code_change_insight(repo_name: str):

except Exception as e:
return json.dumps({"success": False, "message": str(e)})


@router.get("/activity")
def get_activity_insight(repo_name: str):
try:
result = get_activity_data(repo_name)
return {
"success": True,
"data": result,
}

except Exception as e:
return json.dumps({"success": False, "message": str(e)})
28 changes: 28 additions & 0 deletions server/insight/service/activity.py
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 []
3 changes: 0 additions & 3 deletions server/insight/service/issue.py
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


Expand Down
3 changes: 0 additions & 3 deletions server/insight/service/pr.py
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


Expand Down
45 changes: 45 additions & 0 deletions server/tests/insight/test_activity.py
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)
5 changes: 5 additions & 0 deletions server/utils/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def get_data(repo_name, metrics_mapping):
print(
f"Error fetching data from {url} (status code: {response.status_code})"
)
return {
"year": [],
"quarter": [],
"month": [],
}

def format_result(data):
result = []
Expand Down

0 comments on commit 2c53b08

Please sign in to comment.