# Path: archive_chan/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=255, primary_key = True)
#     active = models.BooleanField(
#         default=True,
#         help_text='Should this board be updated with new posts?'
#     )
#     store_threads_for = models.IntegerField(
#         default=48,
#         help_text='[hours] After that much time passes from the last reply in a NOT SAVED thread it will be deleted. Set to 0 to preserve threads forever.'
#     )
#     replies_threshold = models.IntegerField(
#         default=20,
#         help_text='Store threads after they reach that many replies.'
#     )
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return format("/%s/" % self.name)
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:board', args=[self.name])
# 
# class Thread(models.Model):
#     board = models.ForeignKey('Board')
#     number = models.IntegerField()
#     saved = models.BooleanField(default=False) # Threads which are not saved will get deleted after some time.
#     auto_saved = models.BooleanField(default=False) # Was this thread saved automatically by a trigger?
#     tags = models.ManyToManyField('Tag', through='TagToThread')
# 
#     replies = models.IntegerField(default=0)
#     images = models.IntegerField(default=0)
#     first_reply = models.DateTimeField(null=True, default=None)
#     last_reply = models.DateTimeField(null=True, default=None)
# 
#     # Used by scraper.
#     def last_reply_time(self):
#         last = self.post_set.last()
#         if last is None:
#             return None
#         else:
#             return last.time
# 
#     # Used by scraper.
#     def count_replies(self):
#         return self.post_set.count() - 1
# 
#     # Used by board template. Can't figure out a query which wouldd fetch everything in one go.
#     def first_post(self):
#         return self.post_set.select_related('image').first()
# 
#     class Meta:
#         unique_together = ('board', 'number')
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:thread', args=[self.board.name, self.number])
# 
# class Post(models.Model):
#     thread = models.ForeignKey('Thread')
# 
#     number = models.IntegerField()
#     time = models.DateTimeField()
# 
#     name = models.CharField(max_length=255, blank=True)
#     trip = models.CharField(max_length=255, blank=True)
#     email = models.CharField(max_length=255, blank=True)
#     country = models.CharField(max_length=2, blank=True)
# 
#     subject = models.TextField(blank=True)
#     comment = models.TextField(blank=True)
# 
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def is_main(self):
#         return (self.number == self.thread.number)
# 
#     class Meta:
#         ordering = ['number']
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return '%s#post-%s' % (self.thread.get_absolute_url(), self.number)
# 
# Path: archive_chan/settings.py
# class AppSettings:
#     app_settings = {
#         'API_WAIT': 1, # [seconds] Delay between two API calls (catalog/list of posts). This should follow the API rules.
#         'FILE_WAIT': 0, # [seconds] Delay between two file downloads (images/thumbnails). This should follow the API rules (no limit at this point).
#         'CONNECTION_TIMEOUT': 10, # [seconds] Code downloading the data will stop waiting for a response after that time.
#         'RECENT_POSTS_AGE': 48, # [hours] Used for selecting statistics when the board stores posts forever without deleting them. Read more in views.ajax_board_stats
#         'SCRAPER_THREADS_NUMBER': 4, # Number of additional program threads running at the same time. In other words that many 4chan threads will be updated at the same time.
#         'VIEW_CACHE_AGE': 60 * 5, # [seconds] max age of the dynamic pages eg. board
#         'VIEW_CACHE_AGE_STATIC': 60 * 60 * 24, # [seconds] max age of the static pages eg. stats
#         'MEDIA_URL': settings.MEDIA_URL, # You can override the URL from which the downloaded photos are served.
#     }
# 
#     @classmethod
#     def get(self, name):
#         return getattr(settings, 'ARCHIVE_CHAN_' + name, self.app_settings[name])


import datetime
from django.utils.timezone import utc
from django.db.models import Count, Min, Max
from archive_chan.models import Board, Thread, Post
from archive_chan.settings import AppSettings




def get_stats(**kwargs):
    board_name = kwargs.get('board', None)
    thread_number = kwargs.get('thread', None)

    context = {}

    # This time is used when selecting data for a chart and recent posts.
    # It is supposed to prevent drawing to much data on the chart and ensures correct results when calculating posts per hour
    # (saved threads which do not get deleted would alter the results).
    timespan = AppSettings.get('RECENT_POSTS_AGE')

    queryset_posts = Post.objects

# Path: archive_chan/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=255, primary_key = True)
#     active = models.BooleanField(
#         default=True,
#         help_text='Should this board be updated with new posts?'
#     )
#     store_threads_for = models.IntegerField(
#         default=48,
#         help_text='[hours] After that much time passes from the last reply in a NOT SAVED thread it will be deleted. Set to 0 to preserve threads forever.'
#     )
#     replies_threshold = models.IntegerField(
#         default=20,
#         help_text='Store threads after they reach that many replies.'
#     )
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return format("/%s/" % self.name)
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:board', args=[self.name])
# 
# class Thread(models.Model):
#     board = models.ForeignKey('Board')
#     number = models.IntegerField()
#     saved = models.BooleanField(default=False) # Threads which are not saved will get deleted after some time.
#     auto_saved = models.BooleanField(default=False) # Was this thread saved automatically by a trigger?
#     tags = models.ManyToManyField('Tag', through='TagToThread')
# 
#     replies = models.IntegerField(default=0)
#     images = models.IntegerField(default=0)
#     first_reply = models.DateTimeField(null=True, default=None)
#     last_reply = models.DateTimeField(null=True, default=None)
# 
#     # Used by scraper.
#     def last_reply_time(self):
#         last = self.post_set.last()
#         if last is None:
#             return None
#         else:
#             return last.time
# 
#     # Used by scraper.
#     def count_replies(self):
#         return self.post_set.count() - 1
# 
#     # Used by board template. Can't figure out a query which wouldd fetch everything in one go.
#     def first_post(self):
#         return self.post_set.select_related('image').first()
# 
#     class Meta:
#         unique_together = ('board', 'number')
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:thread', args=[self.board.name, self.number])
# 
# class Post(models.Model):
#     thread = models.ForeignKey('Thread')
# 
#     number = models.IntegerField()
#     time = models.DateTimeField()
# 
#     name = models.CharField(max_length=255, blank=True)
#     trip = models.CharField(max_length=255, blank=True)
#     email = models.CharField(max_length=255, blank=True)
#     country = models.CharField(max_length=2, blank=True)
# 
#     subject = models.TextField(blank=True)
#     comment = models.TextField(blank=True)
# 
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def is_main(self):
#         return (self.number == self.thread.number)
# 
#     class Meta:
#         ordering = ['number']
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return '%s#post-%s' % (self.thread.get_absolute_url(), self.number)
# 
# Path: archive_chan/settings.py
# class AppSettings:
#     app_settings = {
#         'API_WAIT': 1, # [seconds] Delay between two API calls (catalog/list of posts). This should follow the API rules.
#         'FILE_WAIT': 0, # [seconds] Delay between two file downloads (images/thumbnails). This should follow the API rules (no limit at this point).
#         'CONNECTION_TIMEOUT': 10, # [seconds] Code downloading the data will stop waiting for a response after that time.
#         'RECENT_POSTS_AGE': 48, # [hours] Used for selecting statistics when the board stores posts forever without deleting them. Read more in views.ajax_board_stats
#         'SCRAPER_THREADS_NUMBER': 4, # Number of additional program threads running at the same time. In other words that many 4chan threads will be updated at the same time.
#         'VIEW_CACHE_AGE': 60 * 5, # [seconds] max age of the dynamic pages eg. board
#         'VIEW_CACHE_AGE_STATIC': 60 * 60 * 24, # [seconds] max age of the static pages eg. stats
#         'MEDIA_URL': settings.MEDIA_URL, # You can override the URL from which the downloaded photos are served.
#     }
# 
#     @classmethod
#     def get(self, name):
#         return getattr(settings, 'ARCHIVE_CHAN_' + name, self.app_settings[name])


# Path: archive_chan/lib/stats.py
import datetime
from django.utils.timezone import utc
from django.db.models import Count, Min, Max
from archive_chan.models import Board, Thread, Post
from archive_chan.settings import AppSettings





def get_stats(**kwargs):
    board_name = kwargs.get('board', None)
    thread_number = kwargs.get('thread', None)

    context = {}

    # This time is used when selecting data for a chart and recent posts.
    # It is supposed to prevent drawing to much data on the chart and ensures correct results when calculating posts per hour
    # (saved threads which do not get deleted would alter the results).
    timespan = AppSettings.get('RECENT_POSTS_AGE')

    queryset_posts = Post.objects

    queryset_threads = Thread.objects