# Path: apps/base/views.py
# class BaseView(View):
#     """docstring for BaseView"""
#     @method_decorator(gzip_page)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
#     def get_context_data(self, **kwargs):
#         context = super().get_context_data(**kwargs)
#         return context
# 
# class LoginRequiredMixin(View):
#     """docstring for LoginRequiredMixin"""
#     @method_decorator(login_required)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
# Path: apps/ip/models.py
# class IP(Timestampable):
#     address = models.URLField(max_length=255)
#     vhost = models.CharField(blank=True, max_length=255, default='default')
#     user_profile = models.ForeignKey(UserProfile)
# 
#     class Meta:
#         db_table = 'ips'
#         unique_together = ('user_profile', 'address', 'vhost',)
# 
#     def save(self, *args, **kwargs):
#         if self.address and 'http' in self.address:
#             self.address = self.address.split('://')[1].lower()
#             if '/' in self.address:
#                 self.address = self.address.split('/')[0]
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.address
# 
# Path: apps/transaction/models.py
# class Transaction(Timestampable):
#     address = models.CharField(max_length=255)
#     vhost = models.CharField(max_length=255, default='default')
#     agent_ping_time = models.DateTimeField(blank=True)
#     time_avg = models.FloatField(default=999, blank=True)
# 
#     class Meta:
#         db_table = 'transactions'
# 
#     def __str__(self):
#         return self.address


from django.shortcuts import render
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.db.models import Avg, Count, Max, ExpressionWrapper, F, CharField
from apps.base.views import (BaseView, LoginRequiredMixin)
from apps.ip.models import IP
from .models import Transaction
import json
import datetime
        return super().dispatch(request, *args, **kwargs)

    def get(self, *args, **kwargs):
        result = {
            "data": [ 
            ]
        }

        try:
            your_ip = IP.objects.values_list('address', flat=True).filter(user_profile=self.request.user.profile)
            your_id_transaction = Transaction.objects.filter(address__in=set(your_ip)).values('address').annotate(id=Max('id')).values_list("id", flat=True)
            your_transaction = Transaction.objects.filter(id__in=set(your_id_transaction))
            
            #print("your_transaction: ", your_transaction)
            for transaction in your_transaction:
                arr_data = []
                arr_data.append(transaction.address)
                arr_data.append(transaction.agent_ping_time.strftime('%m/%d/%Y %H:%M:%S'))
                if transaction.time_avg == 999:
                    arr_data.append('Offline')
                else:
                    arr_data.append(transaction.time_avg)

                result['data'].append(arr_data)
        except Exception as error:
            print("Something error: ", error)

        return HttpResponse(json.dumps(result))



# Path: apps/base/views.py
# class BaseView(View):
#     """docstring for BaseView"""
#     @method_decorator(gzip_page)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
#     def get_context_data(self, **kwargs):
#         context = super().get_context_data(**kwargs)
#         return context
# 
# class LoginRequiredMixin(View):
#     """docstring for LoginRequiredMixin"""
#     @method_decorator(login_required)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
# Path: apps/ip/models.py
# class IP(Timestampable):
#     address = models.URLField(max_length=255)
#     vhost = models.CharField(blank=True, max_length=255, default='default')
#     user_profile = models.ForeignKey(UserProfile)
# 
#     class Meta:
#         db_table = 'ips'
#         unique_together = ('user_profile', 'address', 'vhost',)
# 
#     def save(self, *args, **kwargs):
#         if self.address and 'http' in self.address:
#             self.address = self.address.split('://')[1].lower()
#             if '/' in self.address:
#                 self.address = self.address.split('/')[0]
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.address
# 
# Path: apps/transaction/models.py
# class Transaction(Timestampable):
#     address = models.CharField(max_length=255)
#     vhost = models.CharField(max_length=255, default='default')
#     agent_ping_time = models.DateTimeField(blank=True)
#     time_avg = models.FloatField(default=999, blank=True)
# 
#     class Meta:
#         db_table = 'transactions'
# 
#     def __str__(self):
#         return self.address


# Path: apps/transaction/views.py
from django.shortcuts import render
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.db.models import Avg, Count, Max, ExpressionWrapper, F, CharField
from apps.base.views import (BaseView, LoginRequiredMixin)
from apps.ip.models import IP
from .models import Transaction
import json
import datetime

        return super().dispatch(request, *args, **kwargs)

    def get(self, *args, **kwargs):
        result = {
            "data": [ 
            ]
        }

        try:
            your_ip = IP.objects.values_list('address', flat=True).filter(user_profile=self.request.user.profile)
            your_id_transaction = Transaction.objects.filter(address__in=set(your_ip)).values('address').annotate(id=Max('id')).values_list("id", flat=True)
            your_transaction = Transaction.objects.filter(id__in=set(your_id_transaction))
            
            #print("your_transaction: ", your_transaction)
            for transaction in your_transaction:
                arr_data = []
                arr_data.append(transaction.address)
                arr_data.append(transaction.agent_ping_time.strftime('%m/%d/%Y %H:%M:%S'))
                if transaction.time_avg == 999:
                    arr_data.append('Offline')
                else:
                    arr_data.append(transaction.time_avg)

                result['data'].append(arr_data)
        except Exception as error:
            print("Something error: ", error)

        return HttpResponse(json.dumps(result))



class TransactionAPI(BaseView):