From ba8656eb9fd7b1c36225380f921f7017781ab534 Mon Sep 17 00:00:00 2001 From: db0 Date: Thu, 12 Oct 2023 18:30:14 +0200 Subject: [PATCH] feat: tag count Closes #46 --- fediseer/apis/models/v1.py | 4 ++++ fediseer/apis/v1/tags.py | 18 ++++++++++++++++++ fediseer/database/functions.py | 13 ++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/fediseer/apis/models/v1.py b/fediseer/apis/models/v1.py index 178b4d3..af6e205 100644 --- a/fediseer/apis/models/v1.py +++ b/fediseer/apis/models/v1.py @@ -170,3 +170,7 @@ class Models: 'overwrite': fields.Boolean(required=False, default=False, description="Set to true, to modify all existing entries with new data."), 'hesitations': fields.List(fields.Nested(self.input_batch_entry)), }) + self.response_model_tag_info = api.model('TagsInfo', { + 'tag': fields.String(description="The tag name (lowercased)", example="anarchism"), + 'count': fields.Integer(description="The amount of instances tagged with this tag", example="5"), + }) \ No newline at end of file diff --git a/fediseer/apis/v1/tags.py b/fediseer/apis/v1/tags.py index e3f0d85..5fe7c77 100644 --- a/fediseer/apis/v1/tags.py +++ b/fediseer/apis/v1/tags.py @@ -5,6 +5,24 @@ from fediseer.consts import MAX_TAGS class Tags(Resource): + get_parser = reqparse.RequestParser() + get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") + + @api.expect(get_parser) + @api.marshal_with(models.response_model_tag_info, code=200, description='Tag counts', as_list=True) + @api.response(400, 'Bad Request', models.response_model_error) + @api.response(401, 'Invalid API Key', models.response_model_error) + @api.response(403, 'Access Denied', models.response_model_error) + + def get(self): + '''Display all known tags (converted to lowercase) + And count how many times they've been used + ''' + self.args = self.get_parser.parse_args() + rows_dict = database.get_tag_counts() + tags = [{"tag":tag, "count": rows_dict[tag]} for tag in rows_dict] + return tags,200 + put_parser = reqparse.RequestParser() put_parser.add_argument("apikey", type=str, required=True, help="The sending instance's API key.", location='headers') put_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") diff --git a/fediseer/database/functions.py b/fediseer/database/functions.py index 8314016..fd475d5 100644 --- a/fediseer/database/functions.py +++ b/fediseer/database/functions.py @@ -540,4 +540,15 @@ def count_instance_tags(instance_id): query = InstanceTag.query.filter( InstanceTag.instance_id == instance_id, ) - return query.count() \ No newline at end of file + return query.count() + +def get_tag_counts(): + query = db.session.query( + func.lower(InstanceTag.tag).label('tag'), + func.count().label('tag_count') + ).group_by(InstanceTag.tag) + + result = query.all() + + tag_counts = {row.tag: row.tag_count for row in result} + return tag_counts \ No newline at end of file