-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/petercat-ai/petercat into f…
…ixlogin
- Loading branch information
Showing
17 changed files
with
334 additions
and
19 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
plugins: [ | ||
require('tailwindcss'), | ||
require('autoprefixer') | ||
require('autoprefixer'), | ||
require("postcss-nested"), | ||
] | ||
}; |
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
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
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
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,65 @@ | ||
import json | ||
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"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.get("/issue") | ||
def get_issue_insight(repo_name: str): | ||
try: | ||
result = get_issue_data(repo_name) | ||
return { | ||
"success": True, | ||
"data": result, | ||
} | ||
|
||
except Exception as e: | ||
return json.dumps({"success": False, "message": str(e)}) | ||
|
||
|
||
@router.get("/pr") | ||
def get_pr_insight(repo_name: str): | ||
try: | ||
result = get_pr_data(repo_name) | ||
return { | ||
"success": True, | ||
"data": result, | ||
} | ||
|
||
except Exception as e: | ||
return json.dumps({"success": False, "message": str(e)}) | ||
|
||
|
||
@router.get("/code_change") | ||
def get_code_change_insight(repo_name: str): | ||
try: | ||
result = get_code_changes(repo_name) | ||
return { | ||
"success": True, | ||
"data": result, | ||
} | ||
|
||
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)}) |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from utils.insight import get_data | ||
|
||
|
||
def get_issue_data(repo_name): | ||
metrics_mapping = { | ||
"issues_new": "open", | ||
"issues_closed": "close", | ||
"issue_comments": "comment", | ||
} | ||
issue_data = get_data(repo_name, metrics_mapping) | ||
return issue_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,18 @@ | ||
from utils.insight import get_data | ||
|
||
|
||
def get_pr_data(repo_name): | ||
metrics_mapping = { | ||
"change_requests": "open", | ||
"change_requests_accepted": "merge", | ||
"change_requests_reviews": "reviews", | ||
} | ||
return get_data(repo_name, metrics_mapping) | ||
|
||
|
||
def get_code_changes(repo_name): | ||
metrics_mapping = { | ||
"code_change_lines_add": "add", | ||
"code_change_lines_remove": "remove", | ||
} | ||
return get_data(repo_name, metrics_mapping) |
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,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) |
Oops, something went wrong.