2020-11-05 11:59:12 -07:00
|
|
|
# Copyright 2020 Dirk Klimpel
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
2021-11-29 15:19:45 -07:00
|
|
|
from http import HTTPStatus
|
2020-11-05 11:59:12 -07:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
|
|
|
|
2023-02-01 14:35:24 -07:00
|
|
|
from synapse.api.constants import Direction
|
2020-11-05 11:59:12 -07:00
|
|
|
from synapse.api.errors import Codes, SynapseError
|
2023-02-01 14:35:24 -07:00
|
|
|
from synapse.http.servlet import RestServlet, parse_enum, parse_integer, parse_string
|
2020-11-05 11:59:12 -07:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
|
|
|
|
from synapse.storage.databases.main.stats import UserSortOrder
|
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class UserMediaStatisticsRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Get statistics about uploaded media by users.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/statistics/users/media$")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2020-11-05 11:59:12 -07:00
|
|
|
|
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
order_by = parse_string(
|
2021-12-08 09:59:40 -07:00
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=UserSortOrder.USER_ID.value,
|
|
|
|
allowed_values=(
|
|
|
|
UserSortOrder.MEDIA_LENGTH.value,
|
|
|
|
UserSortOrder.MEDIA_COUNT.value,
|
|
|
|
UserSortOrder.USER_ID.value,
|
|
|
|
UserSortOrder.DISPLAYNAME.value,
|
|
|
|
),
|
2020-11-05 11:59:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
start = parse_integer(request, "from", default=0)
|
|
|
|
if start < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter from must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
limit = parse_integer(request, "limit", default=100)
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter limit must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
from_ts = parse_integer(request, "from_ts", default=0)
|
|
|
|
if from_ts < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter from_ts must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
until_ts = parse_integer(request, "until_ts")
|
|
|
|
if until_ts is not None:
|
|
|
|
if until_ts < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter until_ts must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
if until_ts <= from_ts:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter until_ts must be greater than from_ts.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
search_term = parse_string(request, "search_term")
|
|
|
|
if search_term == "":
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-11-05 11:59:12 -07:00
|
|
|
"Query parameter search_term cannot be an empty string.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2023-02-01 14:35:24 -07:00
|
|
|
direction = parse_enum(request, "dir", Direction, default=Direction.FORWARDS)
|
2020-11-05 11:59:12 -07:00
|
|
|
|
|
|
|
users_media, total = await self.store.get_users_media_usage_paginate(
|
|
|
|
start, limit, from_ts, until_ts, order_by, direction, search_term
|
|
|
|
)
|
|
|
|
ret = {"users": users_media, "total": total}
|
|
|
|
if (start + limit) < total:
|
|
|
|
ret["next_token"] = start + len(users_media)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, ret
|
2023-04-26 10:00:11 -06:00
|
|
|
|
|
|
|
|
|
|
|
class LargestRoomsStatistics(RestServlet):
|
|
|
|
"""Get the largest rooms by database size.
|
|
|
|
|
|
|
|
Only works when using PostgreSQL.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/statistics/database/rooms$")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.stats_controller = hs.get_storage_controllers().stats
|
|
|
|
|
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
room_sizes = await self.stats_controller.get_room_db_size_estimate()
|
|
|
|
|
|
|
|
return HTTPStatus.OK, {
|
|
|
|
"rooms": [
|
|
|
|
{"room_id": room_id, "estimated_size": size}
|
|
|
|
for room_id, size in room_sizes
|
|
|
|
]
|
|
|
|
}
|