Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the activity insight #703

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check failure on line 2 in server/insight/router.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/router.py:2:20: F401 `typing.Optional` imported but unused
from insight.service.activity import get_activity_data

Check failure on line 3 in server/insight/router.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/router.py:3:32: F401 `fastapi.Depends` imported but unused
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 @@

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)})
24 changes: 24 additions & 0 deletions server/insight/service/activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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"

response = requests.get(url)
data = response.json()
xingwanying marked this conversation as resolved.
Show resolved Hide resolved
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]
]
3 changes: 0 additions & 3 deletions server/insight/service/issue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import requests
from collections import defaultdict

from utils.insight import get_data

Check failure on line 1 in server/insight/service/issue.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/service/issue.py:1:8: F401 `requests` imported but unused

Check failure on line 2 in server/insight/service/issue.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/service/issue.py:2:25: F401 `collections.defaultdict` imported but unused

def get_issue_data(repo_name):
metrics_mapping = {
Expand Down
3 changes: 0 additions & 3 deletions server/insight/service/pr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import requests
from collections import defaultdict

from utils.insight import get_data

Check failure on line 1 in server/insight/service/pr.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/service/pr.py:1:8: F401 `requests` imported but unused

Check failure on line 2 in server/insight/service/pr.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/service/pr.py:2:25: F401 `collections.defaultdict` imported but unused

def get_pr_data(repo_name):
metrics_mapping = {
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)
Loading