feat: tag count

Closes #46
pull/53/head
db0 2023-10-12 18:30:14 +02:00
parent 97ff20c35a
commit ba8656eb9f
3 changed files with 34 additions and 1 deletions

View File

@ -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."), '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)), '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"),
})

View File

@ -5,6 +5,24 @@ from fediseer.consts import MAX_TAGS
class Tags(Resource): 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 = reqparse.RequestParser()
put_parser.add_argument("apikey", type=str, required=True, help="The sending instance's API key.", location='headers') 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") put_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers")

View File

@ -541,3 +541,14 @@ def count_instance_tags(instance_id):
InstanceTag.instance_id == instance_id, InstanceTag.instance_id == instance_id,
) )
return query.count() 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