docs: def getMonthlyPerformance():
      '''
      This function does the work of compiling monthly performance data
      that can either be rendered as CSV or as JSON
      '''
      when_all = {
          'eventregistration__dropIn': False,
          'eventregistration__cancelled': False,
      }
  
      # Get objects at the Series level so that we can calculate StudentHours
      series_counts = list(Series.objects.annotate(
          eventregistrations=Sum(Case(When(Q(**when_all),then=1),output_field=IntegerField())),)
          .values('year','month','eventregistrations','duration'))
  
      for series in series_counts:
          series['studenthours'] = (series.get('eventregistrations') or 0) * (series.get('duration') or 0)
  
      all_years = set([x['year'] for x in series_counts])
  
      dataseries_list = ['EventRegistrations', 'Registrations','Hours','StudentHours','AvgStudents']
  
      yearTotals = {}
  
      # Initialize dictionaries
      for dataseries in dataseries_list:
          yearTotals[dataseries] = {'MonthlyAverage': {}}
          for year in all_years:
              yearTotals[dataseries][year] = {}
  
      # Fill in by year and month for a cleaner looping process
      for year in all_years:
  
          # Monthly Totals
          for month in range(1,13):
              # Total EventRegistrations per month is retrieved by the query above.
              yearTotals['EventRegistrations'][year][month] = sum([x['eventregistrations'] or 0 for x in series_counts if x['month'] == month and x['year'] == year])
  
              # Total Registrations per month and hours per month require a separate query for each month
              yearTotals['Registrations'][year][month] = len(Registration.objects.filter(eventregistration__dropIn=False, eventregistration__cancelled=False,eventregistration__event__year=year,eventregistration__event__month=month).distinct())
              yearTotals['Hours'][year][month] = sum([x['duration'] or 0 for x in series_counts if x['month'] == month and x['year'] == year])
              yearTotals['StudentHours'][year][month] = sum([x['studenthours'] or 0 for x in series_counts if x['month'] == month and x['year'] == year])
  
              if yearTotals['Hours'][year][month] > 0:
                  yearTotals['AvgStudents'][year][month] = yearTotals['StudentHours'][year][month] / float(yearTotals['Hours'][year][month])
              else:
                  yearTotals['AvgStudents'][year][month] = 0
  
          # Annual Totals
          for sub_series in ['EventRegistrations','Registrations','Hours','StudentHours']:
              yearTotals[sub_series][year]['Total'] = sum([x for x in yearTotals[sub_series][year].values()])
  
          # Annual (Monthly) Averages
          month_count = len([x for k,x in yearTotals['Hours'][year].items() if k in range(1,13) and x > 0])
          if month_count > 0:
              for sub_series in ['EventRegistrations','Registrations','Hours','StudentHours']:
                  yearTotals[sub_series][year]['Average'] = yearTotals[sub_series][year]['Total'] / float(month_count)
              yearTotals['AvgStudents'][year]['Average'] = yearTotals['StudentHours'][year]['Total'] / float(yearTotals['Hours'][year]['Total'])
  
      # Monthly Averages
      for month in range(1,13):
          yearly_hours_data = [x[month] for k,x in yearTotals['Hours'].items() if k in all_years and x[month] > 0]
          yearly_studenthours_data = [x[month] for k,x in yearTotals['StudentHours'].items() if k in all_years and x[month] > 0]
          yearly_eventregistrations_data = [x[month] for k,x in yearTotals['EventRegistrations'].items() if k in all_years and yearTotals['Hours'][k][month] > 0]
          yearly_registrations_data = [x[month] for k,x in yearTotals['Registrations'].items() if k in all_years and yearTotals['Hours'][k][month] > 0]
  
          year_count = len(yearly_hours_data)
  
          if year_count > 0:
              yearTotals['EventRegistrations']['MonthlyAverage'][month] = sum([x for x in yearly_eventregistrations_data]) / year_count
              yearTotals['Registrations']['MonthlyAverage'][month] = sum([x for x in yearly_registrations_data]) / year_count
              yearTotals['Hours']['MonthlyAverage'][month] = sum([x for x in yearly_hours_data]) / year_count
              yearTotals['StudentHours']['MonthlyAverage'][month] = sum([x for x in yearly_studenthours_data]) / year_count
              yearTotals['AvgStudents']['MonthlyAverage'][month] = yearTotals['StudentHours']['MonthlyAverage'][month] / float(yearTotals['Hours']['MonthlyAverage'][month])
  
      return yearTotals