# Copyright (C) 2008, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Transport decorator that logs transport operations."""

# see also the transportstats plugin, which gives you some summary information
# in a machine-readable dump

import logging
import time
import types

from dromedary import decorator

logger = logging.getLogger("dromedary.log")


class TransportLogDecorator(decorator.TransportDecorator):
    """Decorator for Transports that logs interesting operations to brz.log.

    In general we want to log things that usually take a network round trip
    and may be slow.

    Not all operations are logged yet.

    See also TransportTraceDecorator, that records a machine-readable log in
    memory for eg testing.
    """

    def __init__(self, *args, **kw):
        """Initialize the TransportLogDecorator.

        Args:
            *args: Arguments passed to parent TransportDecorator.
            **kw: Keyword arguments passed to parent TransportDecorator.
        """
        super().__init__(*args, **kw)

        def _make_hook(hookname):
            def _hook(relpath, *args, **kw):
                return self._log_and_call(hookname, relpath, *args, **kw)

            return _hook

        # GZ 2017-05-21: Not all methods take relpath as first argument, for
        # instance copy_to takes list of relpaths. Also, unclear on url vs
        # filesystem path split. Needs tidying up.
        for methodname in (
            "append_bytes",
            "append_file",
            "copy_to",
            "delete",
            "get",
            "has",
            "open_write_stream",
            "mkdir",
            "move",
            "put_bytes",
            "put_bytes_non_atomic",
            "put_file",
            "put_file_non_atomic",
            "list_dir",
            "lock_read",
            "lock_write",
            "readv",
            "rename",
            "rmdir",
            "stat",
            "ulock",
        ):
            setattr(self, methodname, _make_hook(methodname))

    @classmethod
    def _get_url_prefix(self):
        return "log+"

    def iter_files_recursive(self):
        """Iterate through all files recursively.

        Returns:
            Iterator of file paths beneath this transport.
        """
        # needs special handling because it does not have a relpath parameter
        logger.debug("iter_files_recursive %s", self._decorated.base)
        return self._call_and_log_result("iter_files_recursive", (), {})

    def _log_and_call(self, methodname, relpath, *args, **kwargs):
        kwargs_str = dict(kwargs) if kwargs else ""
        logger.debug(
            "%s %s %s %s",
            methodname,
            relpath,
            self._shorten(self._strip_tuple_parens(args)),
            kwargs_str,
        )
        return self._call_and_log_result(methodname, (relpath,) + args, kwargs)

    def _call_and_log_result(self, methodname, args, kwargs):
        before = time.time()
        try:
            result = getattr(self._decorated, methodname)(*args, **kwargs)
        except Exception as e:
            logger.debug("  --> %s", e)
            logger.debug("      %.03fs", time.time() - before)
            raise
        return self._show_result(before, methodname, result)

    def _show_result(self, before, methodname, result):
        result_len = None
        if (
            isinstance(result, types.GeneratorType)
            or type(result).__name__ == "list_iterator"
        ):
            # We now consume everything from the generator so that we can show
            # the results and the time it took to get them.  However, to keep
            # compatibility with callers that may specifically expect a result
            # (see <https://launchpad.net/bugs/340347>) we also return a new
            # generator, reset to the starting position.
            result = list(result)
            return_result = iter(result)
        else:
            return_result = result
        # Is this an io object with a getvalue() method?
        getvalue = getattr(result, "getvalue", None)
        if getvalue is not None:
            val = repr(getvalue())
            result_len = len(val)
            shown_result = "%s(%s) (%d bytes)" % (
                result.__class__.__name__,
                self._shorten(val),
                result_len,
            )
        elif methodname == "readv":
            num_hunks = len(result)
            total_bytes = sum((len(d) for o, d in result))
            shown_result = "readv response, %d hunks, %d total bytes" % (
                num_hunks,
                total_bytes,
            )
            result_len = total_bytes
        else:
            shown_result = self._shorten(self._strip_tuple_parens(result))
        logger.debug("  --> %s", shown_result)
        # The log decorator no longer shows the elapsed time or transfer rate
        # because they're available in the log prefixes and the transport
        # activity display respectively.
        if False:
            elapsed = time.time() - before
            if result_len and elapsed > 0:
                # this is the rate of higher-level data, not the raw network
                # speed using base-10 units (see HACKING.txt).
                logger.debug(
                    "      %9.03fs %8dkB/s", elapsed, result_len / elapsed / 1000
                )
            else:
                logger.debug("      %9.03fs", elapsed)
        return return_result

    def _shorten(self, x):
        if len(x) > 70:
            x = x[:67] + "..."
        return x

    def _strip_tuple_parens(self, t):
        t = repr(t)
        if t[0] == "(" and t[-1] == ")":
            t = t[1:-1]
        return t


def get_test_permutations():
    """Return the permutations to be used in testing."""
    from dromedary.tests import test_server

    return [(TransportLogDecorator, test_server.LogDecoratorServer)]
