2020-06-21 20:54:05 +00:00
|
|
|
from collections import defaultdict
|
2021-01-05 16:09:34 +00:00
|
|
|
from operator import itemgetter
|
2022-07-08 14:32:56 +00:00
|
|
|
from typing import Mapping, Type
|
2020-06-21 20:54:05 +00:00
|
|
|
|
2022-07-08 14:32:56 +00:00
|
|
|
from mongoengine import Q
|
|
|
|
|
|
|
|
from apiserver.apimodels.organization import TagsRequest, EntitiesCountRequest
|
2021-01-05 14:28:49 +00:00
|
|
|
from apiserver.bll.organization import OrgBLL, Tags
|
2022-11-29 15:44:19 +00:00
|
|
|
from apiserver.bll.project import ProjectBLL
|
2022-07-08 14:32:56 +00:00
|
|
|
from apiserver.database.model import User, AttributedDocument, EntityVisibility
|
|
|
|
from apiserver.database.model.model import Model
|
|
|
|
from apiserver.database.model.project import Project
|
|
|
|
from apiserver.database.model.task.task import Task
|
2021-01-05 14:28:49 +00:00
|
|
|
from apiserver.service_repo import endpoint, APICall
|
2022-03-15 14:28:59 +00:00
|
|
|
from apiserver.services.utils import get_tags_filter_dictionary, sort_tags_response
|
2020-06-01 10:00:35 +00:00
|
|
|
|
|
|
|
org_bll = OrgBLL()
|
2022-11-29 15:44:19 +00:00
|
|
|
project_bll = ProjectBLL()
|
2020-06-01 10:00:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@endpoint("organization.get_tags", request_data_model=TagsRequest)
|
|
|
|
def get_tags(call: APICall, company, request: TagsRequest):
|
2020-06-21 20:54:05 +00:00
|
|
|
filter_dict = get_tags_filter_dictionary(request.filter)
|
|
|
|
ret = defaultdict(set)
|
|
|
|
for entity in Tags.Model, Tags.Task:
|
|
|
|
tags = org_bll.get_tags(
|
|
|
|
company, entity, include_system=request.include_system, filter_=filter_dict,
|
|
|
|
)
|
|
|
|
for field, vals in tags.items():
|
|
|
|
ret[field] |= vals
|
|
|
|
|
2022-03-15 14:28:59 +00:00
|
|
|
call.result.data = sort_tags_response(ret)
|
2021-01-05 16:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@endpoint("organization.get_user_companies")
|
|
|
|
def get_user_companies(call: APICall, company_id: str, _):
|
|
|
|
users = [
|
2022-03-15 14:28:59 +00:00
|
|
|
{"id": u.id, "name": u.name, "avatar": u.avatar}
|
2021-01-05 16:09:34 +00:00
|
|
|
for u in User.objects(company=company_id).only("avatar", "name", "company")
|
|
|
|
]
|
|
|
|
|
|
|
|
call.result.data = {
|
|
|
|
"companies": [
|
|
|
|
{
|
|
|
|
"id": company_id,
|
|
|
|
"name": call.identity.company_name,
|
|
|
|
"allocated": len(users),
|
|
|
|
"owners": sorted(users, key=itemgetter("name")),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2022-07-08 14:32:56 +00:00
|
|
|
|
|
|
|
|
2022-11-29 15:44:19 +00:00
|
|
|
@endpoint("organization.get_entities_count")
|
|
|
|
def get_entities_count(call: APICall, company, request: EntitiesCountRequest):
|
2022-07-08 14:32:56 +00:00
|
|
|
entity_classes: Mapping[str, Type[AttributedDocument]] = {
|
|
|
|
"projects": Project,
|
|
|
|
"tasks": Task,
|
|
|
|
"models": Model,
|
|
|
|
"pipelines": Project,
|
2022-07-08 14:45:03 +00:00
|
|
|
"datasets": Project,
|
2022-07-08 14:32:56 +00:00
|
|
|
}
|
|
|
|
ret = {}
|
|
|
|
for field, entity_cls in entity_classes.items():
|
|
|
|
data = call.data.get(field)
|
|
|
|
if data is None:
|
|
|
|
continue
|
|
|
|
|
2022-11-29 15:44:19 +00:00
|
|
|
if request.active_users:
|
|
|
|
if entity_cls is Project:
|
|
|
|
requested_ids = data.get("id")
|
|
|
|
if isinstance(requested_ids, str):
|
|
|
|
requested_ids = [requested_ids]
|
|
|
|
ids, _ = project_bll.get_projects_with_active_user(
|
|
|
|
company=company,
|
|
|
|
users=request.active_users,
|
|
|
|
project_ids=requested_ids,
|
|
|
|
allow_public=True,
|
|
|
|
)
|
|
|
|
if not ids:
|
|
|
|
ret[field] = 0
|
|
|
|
continue
|
|
|
|
data["id"] = ids
|
|
|
|
elif not data.get("user"):
|
|
|
|
data["user"] = request.active_users
|
|
|
|
|
2022-07-08 14:32:56 +00:00
|
|
|
query = Q()
|
2022-11-29 15:44:19 +00:00
|
|
|
if entity_cls in (Project, Task) and not request.search_hidden:
|
2022-07-08 14:32:56 +00:00
|
|
|
query &= Q(system_tags__ne=EntityVisibility.hidden.value)
|
|
|
|
|
|
|
|
ret[field] = entity_cls.get_count(
|
|
|
|
company=company, query_dict=data, query=query, allow_public=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
call.result.data = ret
|