Beispiel #1
0
	def generate_span_trends(self, from_date, to_date):
		"""Takes logs from self.data and changes them to activity trends for 
		since the previous day based on a given date tuple"""
		activity_list = []
		# Iterate through the log data.
		for log in self.data:
			# Get and format the information we need from the log.
			activity = log[0]
			log_start = unformat_time(tuple_time(log[1]))
			log_end = unformat_time(tuple_time(log[2]))
			minimum = unformat_time((0, 0, 0, from_date[0], from_date[1],
										from_date[2]))
			maximum = unformat_time((59, 59, 23, to_date[0], to_date[1],
										to_date[2]))
			log_time = time_in_span(log_start, log_end, minimum, maximum)
			# If the log exists in the span
			if log_time > 0:
				in_activity_list = False
				# Iterate through the activity list to check for duplicates
				for entry in activity_list:
					if entry[0] == activity:
						# Then the entry is in the activity list
						in_activity_list = True
						# Set the maximum of the span to check as the end of the
						# from_date
						maximum = unformat_time((59, 59, 23, from_date[0],
													from_date[1], from_date[2]))
						# If the log did not happen on the from date 
						if not (time_in_span(log_start, log_end, 
												minimum, maximum) == 0):
							# add the log's time in the span to the activity's
							# start trend time.
							entry[1] += time_in_span(log_start, log_end, 
														minimum, maximum)
						else:
							# Else add it the activity's end trend time
							entry[2] += log_time
				# If the log is not already in the activity list, add it.
				if not in_activity_list:
					activity_list.append([activity, log_time, 0])
		# Make a list of the activities and their respective trends
		trends_list = []
		# Iterate through the activity list
		for entry in activity_list:
			# If the end trend time of the activity exists (I.E. not zero)
			if not entry[2] == 0:
				# Get the trend and convert it to a string with a percent symbol
				percent_change = (((entry[2] - entry[1])*1.00)/(entry[1]*1.00))
				percent_change = str((ceil(percent_change*10000.00))/100.00)
				if percent_change[-2] == '.':
					percent_change += '0'
				percent_change += '%'
				trends_list.append([entry[0], str(percent_change)])
		# Set data as trends
		self.data = trends_list
Beispiel #2
0
	def create_pie_chart(self, data=None, span='all', no=None):
		"""Creates a pie chart from the the data"""
		# Create the list of objects to be added to the chart
		chart_list = []
		# If the span has been specified, then get the logs only for that time
		if not span == None and not span == 'all':
			# Iterate through the log data.
			for log in self.data:
				# Get and format the information we need from the log.
				activity = log[0]
				log_start = unformat_time(tuple_time(log[1]))
				log_end = unformat_time(tuple_time(log[2]))
				color = log[3]
				minimum = unformat_time(span[1])
				maximum = unformat_time(span[2])
				# Add the time and activity to the chart_list.
				log_time = time_in_span(log_start, log_end, minimum, maximum)
				# Check if the activity has already been added to chart_list.
				in_chart_list = False
				for entry in chart_list:
					# If the activity is in the chart_list, make a note and add.
					# its time to the existing list item.
					if entry[0] == activity:
						entry[1] += log_time
						in_chart_list = True
				# If the log is not in the chart_list and it is in the span, add
				# it to the chart_list.
				if not in_chart_list and log_time > 0:
					chart_list.append([activity, log_time, color])
		else:
			# If span is not specified then the data are totals.
			# Set the chart_list equal to the total data.
			for total in data:
				chart_list.append((total[0], total[2], total[3]))
		# Add each entry is the chart_list to the chart	
		self.sort(chart_list)
		# Data must be organized for day, month, etc. before using
		# If size has been specified
		if not self.size == (None, None):
			self.chart = Pie(style=self.style,
								print_values=False,
								fill=True,
								human_readable=True,
								include_x_axis=True,
								width=self.size[0], 
								height=self.size[1])
		# If size has not already been specified
		else:
			# Let the graph dynamically resize within webview
			self.chart = Pie(style=self.style, print_values=False, fill=True,
								human_readable=True, include_x_axis=True)
		if not chart_list == []:
			for entry in chart_list:
				self.chart.add(entry[0], entry[1])
Beispiel #3
0
	def create_bar_chart(self, data, span):
		"""Creates a bar chart from the the data"""
		# Initialize the chart_list
		chart_list = []
		for log in data:
			activity_time = 0
			activity = log[0]
			log_start = unformat_time(tuple_time(log[1]))
			log_end = unformat_time(tuple_time(log[2]))
			color = log[3]
			minimum = span[1]
			maximum = span[2]	
			minimum = unformat_time(minimum)
			maximum = unformat_time(maximum)
			activity_time += time_in_span(log_start, log_end, minimum, maximum)
			in_chart_list = False
			for entry in chart_list:
				if entry[0] == activity:
					entry[1] += activity_time
					in_chart_list = True
			if not in_chart_list and activity_time > 0:
				chart_list.append([activity, activity_time, color])
		self.sort(chart_list)
		# Data must be organized for day, month, etc. before using
		# If size has been specified
		if not self.size == (None, None):
			self.chart = Bar(style=self.style, y_scale=60.0,
								print_values=False, include_x_axis=True,
								width=self.size[0], height=self.size[1])
		# If size has not already been specified
		else:
			# Let the graph dynamically resize within webview
			self.chart = Bar(style=self.style, print_values=False,
								include_x_axis=True, y_scale=60.0)
		self.set_y_labels(chart_list)
		## Add each entry is the chart_list to the chart	
		if not chart_list == []:
			for entry in chart_list:
				time = str(timedelta(seconds=entry[1]))
				if time[1] == ':':
					time = '0' + time
				self.chart.add(entry[0], [{'value':entry[1], 'label':time}])
		else:
			self.chart = Pie(style=self.style, width=self.size[0],
								height=self.size[1])
Beispiel #4
0
 def read_total(self, current_date, span=None):
     """Extrapolates a set of totals based on information in the logs and 
      returns as a list of tuples"""
     totals_list = []
     # Iterate through the logs
     for entry in self.read_log("*"):
         # Get the activity from the entry
         activity = entry[0] 
         # Get the entry's start time
         start_time = tuple_time(entry[1])
         # Get the entry's stop time
         stop_time = tuple_time(entry[2])
         # Get the entry's color
         color = entry[3]
         # Get the time elapsed by the entry and round to the lowest nearby
         # whole integer and subtract 1
         day = current_date[0]
         month = current_date[1]
         year = current_date[2]
         stop_time = unformat_time(stop_time) 
         start_time = unformat_time(start_time)
         minimum = unformat_time((0, 0, 0, 0, 0, 0))
         maximum = unformat_time((59, 59, 23, day, month, year))
         total_time = time_in_span(start_time, stop_time, minimum, maximum)
         if not total_time == 0:
             minimum = unformat_time((0, 0, 0, day, month, year))
             time_today = time_in_span(start_time, stop_time, minimum, maximum)
             is_new_activity = True
             for total in totals_list:
                 if total[0] == entry[0]:
                     updated_total = (entry[0], total[1]+time_today, total[2]+total_time, color)
                     totals_list[totals_list.index(total)] = updated_total
                     is_new_activity = False
             # If the entry is a new activity
             if is_new_activity:
                 # Append it to the day_totals
                 totals_list.append((activity, time_today, total_time, color))
     # Return totals_list
     return totals_list
Beispiel #5
0
	def generate_day_trends(self, date):
		"""Takes logs from self.data and changes them to activity trends for 
		since the previous day based on a given date tuple"""
		day_activity_list = []
		previous_day_activity_list = []
		trends = []
		# Iterate through the log data.
		for log in self.data:
			# Get and format the information we need from the log.
			activity = log[0]
			log_start = unformat_time(tuple_time(log[1]))
			log_end = unformat_time(tuple_time(log[2]))
			minimum = unformat_time((0,0,0,date[0],date[1],date[2]))
			maximum = unformat_time((59,59,23,date[0],date[1],date[2]))
			# Add the time and activity to the chart_list.
			log_time = time_in_span(log_start, log_end, minimum, maximum)
			# Check if the activity has already been added to chart_list.
			in_day_activity_list = False
			for entry in day_activity_list:
				# If the activity is in the chart_list, make a note and add.
				# its time to the existing list item.
				if entry[0] == activity:
					entry[1] += log_time
					in_day_activity_list = True
			# If the log is not in the chart_list and it is in the span, add
			# it to the chart_list.
			if not in_day_activity_list and log_time > 0:
				day_activity_list.append([activity, log_time])
		# Iterate through the log data.
		for log in self.data:
			# Get and format the information we need from the log.
			activity = log[0]
			log_start = unformat_time(tuple_time(log[1]))
			log_end = unformat_time(tuple_time(log[2]))
			minimum = unformat_time((0,0,0,date[0],date[1],date[2]))
			minimum -= unformat_time((0,0,0,1,0,0))
			maximum = unformat_time((59,59,23,date[0],date[1],date[2]))
			maximum -= unformat_time((0,0,0,1,0,0))
			# Add the time and activity to the chart_list.
			log_time = time_in_span(log_start, log_end, minimum, maximum)
			# Check if the activity has already been added to chart_list.
			in_previous_day_activity_list = False
			for entry in previous_day_activity_list:
				# If the activity is in the chart_list, make a note and add.
				# its time to the existing list item.
				if entry[0] == activity:
					entry[1] += log_time
					in_previous_day_activity_list = True
			# If the log is not in the chart_list and it is in the span, add
			# it to the chart_list.
			if not in_previous_day_activity_list and log_time > 0:
				previous_day_activity_list.append([activity, log_time])
		# Search through the day_activity_list and previous_day_activity_list 
		# for matching activities
		for activity in day_activity_list:
			for previous_activity in previous_day_activity_list:
				if activity[0] == previous_activity[0]:
					# Get the percent change between the two activities' times
					# as a string in the form of 100.00%
					percent_change = (((activity[1]-previous_activity[1])*1.00)
										/(previous_activity[1]*1.00))
					percent_change = str((ceil(percent_change*10000.00))/100.00)
					if percent_change[-2] == '.':
						percent_change += '0'
					percent_change += '%'
					# Add this activity and it's percent change to trends list 
					trends.append([activity[0], percent_change])
		# Set data as trends
		self.data = trends
def unformat_entry(current_date, entry, days_shown=True):
    """Unformats an entry containing a day formatted as either MM/DD/YYYY
    	or MM/YYYY to an integer describing days or months"""
    # Get the current date tuple
    current_day = current_date[0]
    current_month = current_date[1]
    current_year = current_date[2]
    # If days are set to be shown and date is formatted as MM/DD/YYYY
    if days_shown:
        # See if the date is full of valid integers for day, month, and year
        try:
            # Get the day, month, and year from the entry
            # If the month is only one digit long (month is 9 or less)
            if not entry.get_text()[1] == '/':
                month = int(entry.get_text()[0:2])
                # If the day is only one digit long (day is 9 or less)
                if not entry.get_text()[4] == '/':
                    day = int(entry.get_text()[3:5])
                # Else the day is longer than one digit (day is 10 or higher) 
                else:
                    day = int(entry.get_text()[3:4]) 
            # Else the month is longer than one digit (month is 10 or higher)
            else:
                month = int(entry.get_text()[0:1])
                # If the day is only one digit long (day is 9 or less)
                if not entry.get_text()[3] == '/':
                    day = int(entry.get_text()[2:4])
                # Else the day is longer than one digit (day is 10 or higher) 
                else:
                    day = int(entry.get_text()[2:3])
            # Make sure month is sane
            if month == 0:
                month = 1
            # Make sure day is sane
            if day == 0:
                day = 1
            # Get the year from the last 4 digits in the date
            year = int(entry.get_text()[-4:])
            # Convert the date in the entry to seconds
            date = unformat_time((0, 0, 0, day, month, year))
            # Convert the current date to seconds
            current_date = unformat_time((0, 0, 0, current_day, current_month, 
                                            current_year))
            # Set date to the difference of the entry date and the current 
            # date
            date = date - current_date
            # Return the difference of days in the entry and current day
            return int(date / 86400)
        # If the date is invalid
        except ValueError:
            return None
    # Else days are not set to be shown and date is formatted as MM/YYYY    
    else:
        # See if the date is full of valid integers for month and year
        try:
            # Get the month and year from the entry
            # If the month is only one digit long (month is 9 or less)
            if not entry.get_text()[1] == '/':
                month = int(entry.get_text()[0:2])
            # Else the month is longer than one digit (month is 10 or higher)
            else:
                month = int(entry.get_text()[0:1])
            year = int(entry.get_text()[-4:])
            date = month + year*12
            current_date = current_month + current_year*12
            date = date - current_date
            # Return the number of months in the entry 
            return int(date) 
        # If the date is invalid
        except ValueError:
            return None