This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an admin API for users' media statistics (#8700)
Add `GET /_synapse/admin/v1/statistics/users/media` to get statisics about local media usage by users. Related to #6094 It is the first API for statistics. Goal is to avoid/reduce usage of sql queries like [Wiki analyzing Synapse](https://github.com/matrix-org/synapse/wiki/SQL-for-analyzing-Synapse-PostgreSQL-database-stats) Signed-off-by: Dirk Klimpel dirk@klimpel.org
- Loading branch information
Showing
6 changed files
with
820 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add an admin API for local user media statistics. Contributed by @dklimpel. |
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,83 @@ | ||
# Users' media usage statistics | ||
|
||
Returns information about all local media usage of users. Gives the | ||
possibility to filter them by time and user. | ||
|
||
The API is: | ||
|
||
``` | ||
GET /_synapse/admin/v1/statistics/users/media | ||
``` | ||
|
||
To use it, you will need to authenticate by providing an `access_token` | ||
for a server admin: see [README.rst](README.rst). | ||
|
||
A response body like the following is returned: | ||
|
||
```json | ||
{ | ||
"users": [ | ||
{ | ||
"displayname": "foo_user_0", | ||
"media_count": 2, | ||
"media_length": 134, | ||
"user_id": "@foo_user_0:test" | ||
}, | ||
{ | ||
"displayname": "foo_user_1", | ||
"media_count": 2, | ||
"media_length": 134, | ||
"user_id": "@foo_user_1:test" | ||
} | ||
], | ||
"next_token": 3, | ||
"total": 10 | ||
} | ||
``` | ||
|
||
To paginate, check for `next_token` and if present, call the endpoint | ||
again with `from` set to the value of `next_token`. This will return a new page. | ||
|
||
If the endpoint does not return a `next_token` then there are no more | ||
reports to paginate through. | ||
|
||
**Parameters** | ||
|
||
The following parameters should be set in the URL: | ||
|
||
* `limit`: string representing a positive integer - Is optional but is | ||
used for pagination, denoting the maximum number of items to return | ||
in this call. Defaults to `100`. | ||
* `from`: string representing a positive integer - Is optional but used for pagination, | ||
denoting the offset in the returned results. This should be treated as an opaque value | ||
and not explicitly set to anything other than the return value of `next_token` from a | ||
previous call. Defaults to `0`. | ||
* `order_by` - string - The method in which to sort the returned list of users. Valid values are: | ||
- `user_id` - Users are ordered alphabetically by `user_id`. This is the default. | ||
- `displayname` - Users are ordered alphabetically by `displayname`. | ||
- `media_length` - Users are ordered by the total size of uploaded media in bytes. | ||
Smallest to largest. | ||
- `media_count` - Users are ordered by number of uploaded media. Smallest to largest. | ||
* `from_ts` - string representing a positive integer - Considers only | ||
files created at this timestamp or later. Unix timestamp in ms. | ||
* `until_ts` - string representing a positive integer - Considers only | ||
files created at this timestamp or earlier. Unix timestamp in ms. | ||
* `search_term` - string - Filter users by their user ID localpart **or** displayname. | ||
The search term can be found in any part of the string. | ||
Defaults to no filtering. | ||
* `dir` - string - Direction of order. Either `f` for forwards or `b` for backwards. | ||
Setting this value to `b` will reverse the above sort order. Defaults to `f`. | ||
|
||
|
||
**Response** | ||
|
||
The following fields are returned in the JSON response body: | ||
|
||
* `users` - An array of objects, each containing information | ||
about the user and their local media. Objects contain the following fields: | ||
- `displayname` - string - Displayname of this user. | ||
- `media_count` - integer - Number of uploaded media by this user. | ||
- `media_length` - integer - Size of uploaded media in bytes by this user. | ||
- `user_id` - string - Fully-qualified user ID (ex. `@user:server.com`). | ||
* `next_token` - integer - Opaque value used for pagination. See above. | ||
* `total` - integer - Total number of users after filtering. |
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,122 @@ | ||
# -*- coding: utf-8 -*- | ||
# 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 | ||
from typing import TYPE_CHECKING, Tuple | ||
|
||
from synapse.api.errors import Codes, SynapseError | ||
from synapse.http.servlet import RestServlet, parse_integer, parse_string | ||
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.hs = hs | ||
self.auth = hs.get_auth() | ||
self.store = hs.get_datastore() | ||
|
||
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: | ||
await assert_requester_is_admin(self.auth, request) | ||
|
||
order_by = parse_string( | ||
request, "order_by", default=UserSortOrder.USER_ID.value | ||
) | ||
if order_by not in ( | ||
UserSortOrder.MEDIA_LENGTH.value, | ||
UserSortOrder.MEDIA_COUNT.value, | ||
UserSortOrder.USER_ID.value, | ||
UserSortOrder.DISPLAYNAME.value, | ||
): | ||
raise SynapseError( | ||
400, | ||
"Unknown value for order_by: %s" % (order_by,), | ||
errcode=Codes.INVALID_PARAM, | ||
) | ||
|
||
start = parse_integer(request, "from", default=0) | ||
if start < 0: | ||
raise SynapseError( | ||
400, | ||
"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( | ||
400, | ||
"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( | ||
400, | ||
"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( | ||
400, | ||
"Query parameter until_ts must be a string representing a positive integer.", | ||
errcode=Codes.INVALID_PARAM, | ||
) | ||
if until_ts <= from_ts: | ||
raise SynapseError( | ||
400, | ||
"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( | ||
400, | ||
"Query parameter search_term cannot be an empty string.", | ||
errcode=Codes.INVALID_PARAM, | ||
) | ||
|
||
direction = parse_string(request, "dir", default="f") | ||
if direction not in ("f", "b"): | ||
raise SynapseError( | ||
400, "Unknown direction: %s" % (direction,), errcode=Codes.INVALID_PARAM | ||
) | ||
|
||
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) | ||
|
||
return 200, ret |
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
Oops, something went wrong.