Esempio n. 1
0
def showStatistics(null):
    repoCaller = Repo()
    listOfRentedBooks = repoCaller.getListOfRentals()
    mostRentedBooks = repoCaller.getListOfMostRentedBooks()
    mostRentedAuthors = repoCaller.getListOfMostRentedAuthors()
    mostActiveClients = repoCaller.getListOfMostActiveClients()
    listOfBooks = repoCaller.getListOfBooks()
    listOfClients = repoCaller.getListOfClients()
    booksStatistics = "Books statistic:\n"
    authorStatistics = "Author statistics:\n"
    clientsStatistics = "Clients statistics:\n"
    for rentedBooks in listOfRentedBooks:
        try:
            mostRentedAuthors[Rental.getBookId(rentedBooks)] += 1
        except KeyError:
            mostRentedAuthors[Rental.getBookId(rentedBooks)] = 1

    for rentedBooks in listOfRentedBooks:
        try:
            mostRentedBooks[Rental.getBookId(rentedBooks)] += 1
        except KeyError:
            mostRentedBooks[Rental.getBookId(rentedBooks)] = 1

    mostRentedAuthorsList = sorted(mostRentedAuthors.items(),
                                   key=lambda x: x[1],
                                   reverse=True)
    mostRentedBooksList = sorted(mostRentedBooks.items(),
                                 key=lambda x: x[1],
                                 reverse=True)

    for rentedBooks in mostRentedBooksList:
        booksStatistics += "Id of book: " + str(
            rentedBooks[0]) + " Rented: " + str(
                rentedBooks[1]) + " times" + "\n"

    for rentedBooks in mostRentedAuthorsList:
        for books in listOfBooks:
            if (Book.getBookId(books) == rentedBooks[0]):
                authorStatistics += "Author: " + str(
                    Book.getBookAuthor(books)) + " Rented: " + str(
                        rentedBooks[1]) + " times" + "\n"
    for client in listOfClients:
        total = 0
        for rent in listOfRentedBooks:
            if Client.getClientId(client) == Rental.getClientId(
                    rent) and rent.getReturnDate() != "notReturned":
                difference = Rental.getReturnDate(rent) - Rental.getRentDate(
                    rent)
                total += difference.total_seconds() / 86400
        try:
            mostActiveClients[Client.getClientName(client)] += total
        except KeyError:
            repoCaller.addToMostActiveClients(client, total)
    mostActiveClientsList = sorted(mostActiveClients.items(),
                                   key=lambda x: x[1],
                                   reverse=True)

    for client in mostActiveClientsList:
        clientsStatistics += "Name: " + str(
            client[0]) + " Total rental days: " + str(client[1]) + "\n"
    return [booksStatistics, authorStatistics, clientsStatistics]