This is an automated email from the ASF dual-hosted git repository. humbedooh pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git
commit 775998e7ffaec614323584e815451f1b9c88a104 Author: Daniel Gruno <[email protected]> AuthorDate: Sun Sep 6 19:20:07 2020 +0200 Add stats endpoint As mentioned in the code, this only works with public emails for now. AAA is being worked on. --- server/endpoints/stats.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/server/endpoints/stats.py b/server/endpoints/stats.py new file mode 100644 index 0000000..2f470f6 --- /dev/null +++ b/server/endpoints/stats.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +"""Simple endpoint that returns the server's gathered activity data""" +""" THIS ONLY DEALS WITH PUBLIC EMAILS FOR NOW - AAA IS BEING WORKED ON""" +import plugins.server +import plugins.session +import plugins.mbox +import plugins.defuzzer +import plugins.offloader +import re +import collections +import email.utils +import typing + +PYPONY_RE_PREFIX = re.compile(r"^([a-zA-Z]+:\s*)+") + + +async def process( + server: plugins.server.BaseServer, + session: plugins.session.SessionObject, + indata: dict, +) -> dict: + + query_defuzzed = plugins.defuzzer.defuzz(indata) + query_defuzzed_nodate = plugins.defuzzer.defuzz(indata, nodate=True) + results = await plugins.mbox.query( + session, + query_defuzzed, + query_limit=server.config.database.max_hits, + shorten=True, + ) + + for msg in results: + msg["gravatar"] = plugins.mbox.gravatar(msg) + wordcloud = await plugins.mbox.wordcloud(session, query_defuzzed) + first_year, last_year = await plugins.mbox.get_years(session, query_defuzzed_nodate) + + tstruct, authors = await server.runners.run(plugins.mbox.construct_threads, results) + xlist = indata.get("list", "*") + xdomain = indata.get("domain", "*") + + all_authors = sorted( + [[author, count] for author, count in authors.items()], key=lambda x: x[1] + ) + top10_authors = [] + for x in [x for x in reversed([x for x in all_authors])][:10]: + author, count = x + name, address = email.utils.parseaddr(author) + top10_authors.append( + { + "email": address, + "name": name, + "count": count, + "gravatar": plugins.mbox.gravatar(author), + } + ) + + return { + "firstYear": first_year, + "lastYear": last_year, + "hits": len(results), + "numparts": len(authors), + "no_threads": len(tstruct), + "emails": list(sorted(results, key=lambda x: x['epoch'])), + "cloud": wordcloud, + "participants": top10_authors, + "thread_struct": tstruct, + "search_list": f"<{xlist}.{xdomain}>", + "domain": xdomain, + "list": f"{xlist}@{xdomain}", + } + + +def register(server: plugins.server.BaseServer): + return plugins.server.Endpoint(process)
