Skip to content

Commit

Permalink
Feat: enable user see a summary of user data
Browse files Browse the repository at this point in the history
  • Loading branch information
khalifan-kfan committed Jul 3, 2024
1 parent 060a447 commit 6f6ceee
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
27 changes: 27 additions & 0 deletions api_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,33 @@ paths:
500:
description: "Internal Server Error"

"/users/profile_summary/{user_id}":
get:
tags:
- users
consumes:
- application/json
produces:
- application/json
parameters:
- in: header
name: Authorization
required: true
description: "Bearer [token]"
- in: path
name: user_id
required: true
type: string
responses:
200:
description: "Success"
404:
description: "User not found"
400:
description: "Bad request"
500:
description: "Internal Server Error"

"/activity_feed":
get:
tags:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .users import (
UsersView, UserLoginView, UserEmailVerificationView, UserFollowersView, UserFollowView,
EmailVerificationRequest, ForgotPasswordView, ResetPasswordView, UserDisableView, UserEnableView,
UserDetailView, AdminLoginView, OAuthView, UserDataSummaryView, UserAdminUpdateView, InActiveUsersView)
UserDetailView, AdminLoginView, OAuthView, UserDataSummaryView, UserAdminUpdateView, InActiveUsersView, UserProfileSummaryView)
from .deployments import DeploymentsView
from .clusters import (
ClustersView, ClusterDetailView, ClusterNamespacesView,
Expand All @@ -20,7 +20,7 @@
from .user_role import UserRolesView
from .transactions import TransactionRecordView, TransactionRecordDetailView, CreditTransactionRecordView, CreditPurchaseTransactionRecordView
from .project import (
ProjectsView, ProjectDetailView, UserProjectsView, ProjectGetCostsView, ClusterProjectsView,ProjectPinView,
ProjectsView, ProjectDetailView, UserProjectsView, ProjectGetCostsView, ClusterProjectsView, ProjectPinView,
ProjectDisableView, ProjectEnableView)
from .app import (AppsView, ProjectAppsView, AppDetailView, AppLogsView,
AppRevertView, AppReviseView, AppRedeployView, AppDisableView, AppEnableView, AppDockerWebhookListenerView)
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask_bcrypt import Bcrypt
from app.schemas import UserSchema, UserGraphSchema, ActivityLogSchema
from app.models.user import User
from app.models.project_users import ProjectFollowers
from app.models.role import Role
from app.helpers.confirmation import send_verification
from app.helpers.email import send_email
Expand All @@ -31,6 +32,7 @@
from bson.json_util import dumps
from app.models.app import App
from app.helpers.crane_app_logger import logger
from collections import Counter


class UsersView(Resource):
Expand Down Expand Up @@ -1264,3 +1266,47 @@ def get(self, user_id):
status='success',
data=dict(followers=json.loads(users_data))
), 200


class UserProfileSummaryView(Resource):

@ jwt_required
def get(self, user_id):
"""
Returns metrics about a user's following information and their resource counts.
"""
try:
user = User.get_by_id(user_id)

if not user:
return dict(status='fail', message='User not found'), 404

print('hdf')

project_app_counts = Counter(len(project.apps)
for project in user.projects)
# A count of the project records that they own in the ProjectFollowers' table is the count of users that follow their projects
count_of_projects_followers = (
db.session.query(func.count(ProjectFollowers.user_id))
.join(Project, ProjectFollowers.project_id == Project.id)
.filter(Project.owner_id == user_id)
.scalar()
)

user_data = {
'projects_count': len(user.projects),
'following_count': user.followed.count(),
'follower_count': user.followers.count(),
'followed_projects_count': len(user.followed_projects),
'projects_followers_count': count_of_projects_followers,
'apps_count': project_app_counts.total()
}
print(user_data)

return dict(
status='success',
data=user_data
), 200

except Exception as e:
return dict(status='fail', message=str(e)), 500
6 changes: 4 additions & 2 deletions app/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
UserAdminUpdateView, AppRevertView, ProjectGetCostsView, TransactionRecordView, CreditTransactionRecordView, CreditPurchaseTransactionRecordView,
BillingInvoiceView, BillingInvoiceNotificationView, SystemSummaryView, CreditDetailView, ProjectUsersView, ProjectUsersTransferView, AppReviseView,
ProjectUsersHandleInviteView, ClusterProjectsView, ProjectDisableView, ProjectEnableView, AppRedeployView, AppDisableView, AppEnableView,
ProjectTagsView,ProjectTagsDetailView,
UserDisableView, UserEnableView, AppDockerWebhookListenerView, UserFollowersView, UserFollowView, ProjectFollowingView, ActivityFeedView)
ProjectTagsView, ProjectTagsDetailView,
UserDisableView, UserEnableView, AppDockerWebhookListenerView, UserFollowersView, UserFollowView, ProjectFollowingView, ActivityFeedView, UserProfileSummaryView)
from app.controllers.app import AppRevisionsView
from app.controllers.billing_invoice import BillingInvoiceDetailView
from app.controllers.receipts import BillingReceiptsDetailView, BillingReceiptsView
Expand Down Expand Up @@ -46,6 +46,8 @@

api.add_resource(UserFollowView, '/users/<string:user_id>/following')
api.add_resource(UserFollowersView, '/users/<string:user_id>/followers')
api.add_resource(UserProfileSummaryView,
'/users/profile_summary/<string:user_id>')

# Activity Feed
api.add_resource(ActivityFeedView, '/activity_feed')
Expand Down

0 comments on commit 6f6ceee

Please sign in to comment.