Organization module
Queries
Set of Organization queries
Source code in kili/queries/organization/__init__.py
class QueriesOrganization:
"""
Set of Organization queries
"""
# pylint: disable=too-many-arguments,too-many-locals
def __init__(self, auth):
"""Initialize the subclass.
Args:
auth: KiliAuth object
"""
self.auth = auth
# pylint: disable=dangerous-default-value
@Compatible(['v1', 'v2'])
@typechecked
def organizations(
self,
email: Optional[str] = None,
organization_id: Optional[str] = None,
fields: List[str] = ['id', 'name'],
first: int = 100,
skip: int = 0,
disable_tqdm: bool = False,
as_generator: bool = False) -> Union[List[dict], Generator[dict, None, None]]:
# pylint: disable=line-too-long
"""Get a generator or a list of organizations that match a set of criteria.
Args:
email : Email of a user of the organization
organization_id : Identifier of the organization
fields: All the fields to request among the possible fields for the organizations.
See [the documentation](https://cloud.kili-technology.com/docs/python-graphql-api/graphql-api/#organization)
for all possible fields.
first: Maximum number of organizations to return.
Skip: Number of skipped organizations (they are ordered by creation date)
disable_tqdm: If `True`, the progress bar will be disabled
as_generator: If `True`, a generator on the organizations is returned.
Returns:
A result object which contains the query if it was successful,
or an error message.
Examples:
>>> kili.organizations(organization_id=organization_id, fields=['users.email'])
[{'users': [{'email': 'john@doe.com'}]}]
"""
count_args = {"email": email, "organization_id": organization_id}
disable_tqdm = disable_tqdm or as_generator
payload_query = {
'where': {
'id': organization_id,
'user': {
'email': email,
}
}
}
organizations_generator = row_generator_from_paginated_calls(
skip,
first,
self.count_organizations,
count_args,
self._query_organizations,
payload_query,
fields,
disable_tqdm
)
if as_generator:
return organizations_generator
return list(organizations_generator)
def _query_organizations(self,
skip: int,
first: int,
payload: dict,
fields: List[str]):
payload.update({'skip': skip, 'first': first})
_gql_organizations = gql_organizations(
fragment_builder(fields, Organization))
result = self.auth.client.execute(_gql_organizations, payload)
return format_result('data', result)
@Compatible(['v2'])
@typechecked
def count_organizations(
self,
email: Optional[str] = None,
organization_id: Optional[str] = None) -> int:
"""Count organizations that match a set of criteria.
Args:
email: Email of a user of the organization
organization_id: Identifier of the organization
Returns:
A result object which contains the query if it was successful,
or an error message.
"""
variables = {
'where': {
'id': organization_id,
'user': {
'email': email,
}
}
}
result = self.auth.client.execute(GQL_ORGANIZATIONS_COUNT, variables)
return format_result('data', result)
@Compatible(['v2'])
@typechecked
def organization_metrics(self, organization_id: str = None,
start_date: datetime = datetime.now(),
end_date: datetime = datetime.now()):
"""Get organization metrics.
Args:
organization_id: Identifier of the organization
start_date: Start date of the metrics computation
end_date: End date of the metrics computation
Returns:
A result object which contains the query if it was successful,
or an error message.
"""
variables = {
'where': {
'organizationId': organization_id,
'startDate': start_date.isoformat(sep='T', timespec='milliseconds') + 'Z',
'endDate': end_date.isoformat(sep='T', timespec='milliseconds') + 'Z',
}
}
result = self.auth.client.execute(GQL_ORGANIZATION_METRICS, variables)
return format_result('data', result)
count_organizations(self, email=None, organization_id=None)
Count organizations that match a set of criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
email |
Optional[str] |
Email of a user of the organization |
None |
organization_id |
Optional[str] |
Identifier of the organization |
None |
Returns:
Type | Description |
---|---|
int |
A result object which contains the query if it was successful, or an error message. |
Source code in kili/queries/organization/__init__.py
@Compatible(['v2'])
@typechecked
def count_organizations(
self,
email: Optional[str] = None,
organization_id: Optional[str] = None) -> int:
"""Count organizations that match a set of criteria.
Args:
email: Email of a user of the organization
organization_id: Identifier of the organization
Returns:
A result object which contains the query if it was successful,
or an error message.
"""
variables = {
'where': {
'id': organization_id,
'user': {
'email': email,
}
}
}
result = self.auth.client.execute(GQL_ORGANIZATIONS_COUNT, variables)
return format_result('data', result)
organization_metrics(self, organization_id=None, start_date=datetime.datetime(2022, 6, 28, 16, 25, 16, 967351), end_date=datetime.datetime(2022, 6, 28, 16, 25, 16, 967353))
Get organization metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
organization_id |
str |
Identifier of the organization |
None |
start_date |
datetime |
Start date of the metrics computation |
datetime.datetime(2022, 6, 28, 16, 25, 16, 967351) |
end_date |
datetime |
End date of the metrics computation |
datetime.datetime(2022, 6, 28, 16, 25, 16, 967353) |
Returns:
Type | Description |
---|---|
A result object which contains the query if it was successful, or an error message. |
Source code in kili/queries/organization/__init__.py
@Compatible(['v2'])
@typechecked
def organization_metrics(self, organization_id: str = None,
start_date: datetime = datetime.now(),
end_date: datetime = datetime.now()):
"""Get organization metrics.
Args:
organization_id: Identifier of the organization
start_date: Start date of the metrics computation
end_date: End date of the metrics computation
Returns:
A result object which contains the query if it was successful,
or an error message.
"""
variables = {
'where': {
'organizationId': organization_id,
'startDate': start_date.isoformat(sep='T', timespec='milliseconds') + 'Z',
'endDate': end_date.isoformat(sep='T', timespec='milliseconds') + 'Z',
}
}
result = self.auth.client.execute(GQL_ORGANIZATION_METRICS, variables)
return format_result('data', result)
organizations(self, email=None, organization_id=None, fields=['id', 'name'], first=100, skip=0, disable_tqdm=False, as_generator=False)
Get a generator or a list of organizations that match a set of criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
email |
Email of a user of the organization |
None |
|
organization_id |
Identifier of the organization |
None |
|
fields |
List[str] |
All the fields to request among the possible fields for the organizations. See the documentation for all possible fields. |
['id', 'name'] |
first |
int |
Maximum number of organizations to return. |
100 |
Skip |
Number of skipped organizations (they are ordered by creation date) |
required | |
disable_tqdm |
bool |
If |
False |
as_generator |
bool |
If |
False |
Returns:
Type | Description |
---|---|
Union[List[dict], Generator[dict, NoneType]] |
A result object which contains the query if it was successful, or an error message. |
Examples:
>>> kili.organizations(organization_id=organization_id, fields=['users.email'])
[{'users': [{'email': 'john@doe.com'}]}]
Source code in kili/queries/organization/__init__.py
@Compatible(['v1', 'v2'])
@typechecked
def organizations(
self,
email: Optional[str] = None,
organization_id: Optional[str] = None,
fields: List[str] = ['id', 'name'],
first: int = 100,
skip: int = 0,
disable_tqdm: bool = False,
as_generator: bool = False) -> Union[List[dict], Generator[dict, None, None]]:
# pylint: disable=line-too-long
"""Get a generator or a list of organizations that match a set of criteria.
Args:
email : Email of a user of the organization
organization_id : Identifier of the organization
fields: All the fields to request among the possible fields for the organizations.
See [the documentation](https://cloud.kili-technology.com/docs/python-graphql-api/graphql-api/#organization)
for all possible fields.
first: Maximum number of organizations to return.
Skip: Number of skipped organizations (they are ordered by creation date)
disable_tqdm: If `True`, the progress bar will be disabled
as_generator: If `True`, a generator on the organizations is returned.
Returns:
A result object which contains the query if it was successful,
or an error message.
Examples:
>>> kili.organizations(organization_id=organization_id, fields=['users.email'])
[{'users': [{'email': 'john@doe.com'}]}]
"""
count_args = {"email": email, "organization_id": organization_id}
disable_tqdm = disable_tqdm or as_generator
payload_query = {
'where': {
'id': organization_id,
'user': {
'email': email,
}
}
}
organizations_generator = row_generator_from_paginated_calls(
skip,
first,
self.count_organizations,
count_args,
self._query_organizations,
payload_query,
fields,
disable_tqdm
)
if as_generator:
return organizations_generator
return list(organizations_generator)