from graph import Graph
import matplotlib.pyplot as plt
import os

class Cumulative(Graph):
    def __init__(self):
        super().__init__('cumulative')
        self.data = {
            'date': [], # should directly correlate to count and size
            'count': [],
            'size': []
        }

    def process(self, ip, time, request, size, location, log):
        if len(self.data['date']) == 0:
            self.data['date'].append(time)
            self.data['count'].append(0)
            self.data['size'].append(0)

        if self.data['date'][-1] != time:
            self.data['date'].append(time)
            self.data['count'].append(self.data['count'][-1])
            self.data['size'].append(self.data['size'][-1])

        self.data['count'][-1] += 1
        self.data['size'][-1] += size
        
        return []

    def draw(self, path):
        print('Generating cumulative graphs...')
        old_size = plt.rcParams['font.size']
        plt.rcParams.update({'font.size': 12.5})

        self.cm(path, self.data['count'], 'Cumulative Downloads by Count Over Time', 'Number of Downloads', 'cumulative_count.png')
        self.cm(path, self.data['size'], 'Cumulative Downloads by File Size Over Time', 'Download Size (GiB)', 'cumulative_size.png')

        plt.rcParams.update({'font.size': old_size})
    
    def cm(self, path, series, title, yaxis, filename):
        fig, ax = plt.subplots(figsize=(12.8, 6))
        ax.stackplot(self.data['date'], series, alpha=0.75)
        ax.set_title(title)
        ax.set_ylabel(yaxis)
        ax.set_xlabel('Date of Month')

        plt.savefig(os.path.join(path, filename), bbox_inches='tight', dpi=100)
    