Esempio n. 1
0
def noise_level_per_weekday(current_user):
    # Caching
    chart_uuid = "noise_measurements.noise_level_per_weekday-{0}".format(
        current_user.id)
    chart = CachedChart.get(board=None, uuid=chart_uuid)
    if chart:
        return chart

    chart_title = u"Noise levels per weekday in db as of {0}".format(
        timezone.now())

    noise_measurement_filter = {
        "member__in": Member.get_user_team_members(current_user)
    }

    noise_measurements = NoiseMeasurement.objects.filter(
        **noise_measurement_filter).order_by("datetime")

    noise_chart = pygal.Line(title=chart_title,
                             legend_at_bottom=True,
                             print_values=True,
                             print_zeroes=False,
                             x_label_rotation=0,
                             human_readable=True)

    noise_values = {"avg": [], "min": [], "max": []}
    weekday_dict = {
        1: "Sunday",
        2: "Monday",
        3: "Tuesday",
        4: "Wednesday",
        5: "Thursday",
        6: "Friday",
        7: "Saturday"
    }
    weekdays = []
    weekday_i = 1
    while weekday_i < 7:
        noise_level_in_hour_i = noise_measurements. \
            filter(datetime__week_day=weekday_i). \
            aggregate(avg=Avg("noise_level"), max=Max("noise_level"), min=Min("noise_level"))

        if noise_level_in_hour_i["avg"] is not None:
            noise_values["avg"].append(noise_level_in_hour_i["avg"])
            noise_values["min"].append(noise_level_in_hour_i["min"])
            noise_values["max"].append(noise_level_in_hour_i["max"])

            weekdays.append(weekday_dict[weekday_i])
        weekday_i += 1

    noise_chart.add("Avg noise level", noise_values["avg"])
    noise_chart.add("Min noise level", noise_values["min"])
    noise_chart.add("Max noise level", noise_values["max"])
    noise_chart.x_labels = weekdays

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=noise_chart.render(is_unicode=True))
    return chart.render_django_response()
Esempio n. 2
0
 def get_all_from_member(member):
     user = member.user
     boards = get_user_boards(user)
     teammates = Member.get_user_team_members(user)
     forecasters = Forecaster.objects.filter(
         Q(last_updater=member) | Q(board__in=boards)
         | Q(member__in=list(teammates) + [member]))
     return forecasters
Esempio n. 3
0
def noise_level(current_user):

    # Caching
    chart_uuid = "noise_measurements.noise_level-{0}".format(current_user.id)
    chart = CachedChart.get(board=None, uuid=chart_uuid)
    if chart:
        return chart

    chart_title = u"Average noise levels per day in db as of {0}".format(
        timezone.now())

    noise_measurement_filter = {
        "member__in": Member.get_user_team_members(current_user)
    }

    noise_measurements = NoiseMeasurement.objects.filter(
        **noise_measurement_filter).order_by("datetime")

    noise_chart = pygal.Line(title=chart_title,
                             legend_at_bottom=True,
                             print_values=True,
                             print_zeroes=False,
                             x_label_rotation=65,
                             human_readable=True)

    start_datetime = noise_measurements.aggregate(
        min_date=Min("datetime"))["min_date"]
    end_datetime = noise_measurements.aggregate(
        max_date=Max("datetime"))["max_date"]
    if start_datetime is None or end_datetime is None:
        return noise_chart.render_django_response()

    end_date = end_datetime.date()

    noise_values = []
    days = []

    date_i = copy.deepcopy(start_datetime).date()
    while date_i <= end_date:
        date_noise_measurements = noise_measurements.filter(
            datetime__date=date_i)
        if date_noise_measurements.exists():
            noise_values.append(
                numpy.mean([
                    noise_measurement.noise_level
                    for noise_measurement in date_noise_measurements
                ]))
            days.append(date_i.strftime("%Y-%m-%d"))

        date_i += timedelta(days=1)

    noise_chart.add("Average noise level by day", noise_values)
    noise_chart.x_labels = days

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=noise_chart.render(is_unicode=True))
    return chart.render_django_response()
Esempio n. 4
0
def noise_level_per_hour(current_user):
    # Caching
    chart_uuid = "noise_measurements.noise_level_per_hour-{0}".format(
        current_user.id)
    chart = CachedChart.get(board=None, uuid=chart_uuid)
    if chart:
        return chart

    chart_title = u"Noise levels per hour in db as of {0}".format(
        timezone.now())

    noise_measurement_filter = {
        "member__in": Member.get_user_team_members(current_user)
    }

    noise_measurements = NoiseMeasurement.objects.filter(
        **noise_measurement_filter).order_by("datetime")

    noise_chart = pygal.Line(title=chart_title,
                             legend_at_bottom=True,
                             print_values=True,
                             print_zeroes=False,
                             x_label_rotation=0,
                             human_readable=True)

    noise_values = {"avg": [], "min": [], "max": []}
    hours = []
    hour_i = 0
    while hour_i < 24:
        noise_level_in_hour_i = noise_measurements.\
            filter(datetime__hour=hour_i).\
            aggregate(avg=Avg("noise_level"), max=Max("noise_level"), min=Min("noise_level"))

        if noise_level_in_hour_i["avg"] is not None:
            noise_values["avg"].append(noise_level_in_hour_i["avg"])
            noise_values["min"].append(noise_level_in_hour_i["min"])
            noise_values["max"].append(noise_level_in_hour_i["max"])

            hours.append(hour_i)
        hour_i += 1

    noise_chart.add("Avg noise level", noise_values["avg"])
    noise_chart.add("Min noise level", noise_values["min"])
    noise_chart.add("Max noise level", noise_values["max"])
    noise_chart.x_labels = hours

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=noise_chart.render(is_unicode=True))
    return chart.render_django_response()
Esempio n. 5
0
 def __init__(self, *args, **kwarsg):
     super(FilterForecastersForm, self).__init__(*args, **kwarsg)
     # Regression model choices
     self.fields["model"].choices = REGRESSION_MODELS
     # Board and members
     current_request = CrequestMiddleware.get_request()
     current_user = current_request.user
     boards = get_user_boards(current_user)
     members = Member.get_user_team_members(current_user)
     self.fields["board"].choices = [("", "All")] + [(board.id, board.name)
                                                     for board in boards]
     self.fields["member"].choices = [("", "None")] + [
         (member.id, member.external_username) for member in members
     ]
Esempio n. 6
0
def subjective_noise_level(current_user, month=None, year=None):

    # Caching
    chart_uuid = "noise_measurements.subjective_noise_level-{0}-{1}-{2}".format(
        current_user.id, month if month else "None", year if year else "None")
    chart = CachedChart.get(board=None, uuid=chart_uuid)
    if chart:
        return chart

    chart_title = u"Subjective noise levels as of {0}".format(timezone.now())

    noise_measurement_filter = {
        "member__in": Member.get_user_team_members(current_user)
    }
    noise_measurements = NoiseMeasurement.objects.filter(
        **noise_measurement_filter).order_by("datetime")

    if month and year and 1 <= month <= 12:
        noise_measurements = noise_measurements.filter(datetime__month=month,
                                                       datetime__year=year)

    noise_chart = pygal.Bar(title=chart_title,
                            legend_at_bottom=True,
                            print_values=True,
                            print_zeroes=False,
                            human_readable=True,
                            x_label_rotation=45)

    subjective_noise_levels = dict(NoiseMeasurement.SUBJECTIVE_NOISE_LEVELS)
    for level_key, level_name in subjective_noise_levels.items():
        noise_chart.add(
            u"{0}".format(level_name),
            noise_measurements.filter(
                subjective_noise_level=level_key).count())

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=noise_chart.render(is_unicode=True))
    return chart.render_django_response()
Esempio n. 7
0
def _number_of_interruptions(current_user,
                             board,
                             chart_title,
                             interruption_measurement,
                             incremental=False):

    # Caching
    chart_uuid = "interruptions.{0}".format(
        hashlib.sha256("_number_of_interruptions-{0}-{1}-{2}-{3}-{4}".format(
            current_user.id,
            board.id if board else "user-{0}".format(current_user.id),
            inspect.getsource(interruption_measurement),
            "incremental" if incremental else "absolute",
            chart_title)).hexdigest())
    chart = CachedChart.get(board=board, uuid=chart_uuid)
    if chart:
        return chart

    if board:
        chart_title += u" for board {0} as of {1}".format(
            board.name, board.get_human_fetch_datetime())

    interruptions_chart = pygal.Line(title=chart_title,
                                     legend_at_bottom=True,
                                     print_values=True,
                                     print_zeroes=False,
                                     x_label_rotation=65,
                                     human_readable=True)

    if incremental:
        datetime_filter = "datetime__date__lte"
        interruptions_chart.print_values = False
    else:
        datetime_filter = "datetime__date"

    interruptions_filter = {}
    if board:
        interruptions_filter["board"] = board
        boards = [board]
    else:
        boards = get_user_boards(current_user)
        interruptions_filter["member__in"] = Member.get_user_team_members(
            current_user)

    board_values = {board.id: [] for board in boards}

    interruptions = Interruption.objects.filter(
        **interruptions_filter).order_by("datetime")
    if not interruptions.exists():
        return interruptions_chart.render_django_response()

    min_datetime = interruptions.aggregate(
        min_datetime=Min("datetime"))["min_datetime"]
    max_datetime = interruptions.aggregate(
        max_datetime=Max("datetime"))["max_datetime"]

    date_i = copy.deepcopy(min_datetime.date())
    max_date = max_datetime.date() + timedelta(days=2)
    days = []
    num_interruptions = []
    while date_i <= max_date:
        interruptions_filter = {datetime_filter: date_i}
        interruptions_on_date = interruptions.filter(**interruptions_filter)
        interruptions_on_date_value = interruption_measurement(
            interruptions_on_date)

        # Add only values when there is some interruption in any project
        if interruptions_on_date_value > 0:
            days.append(date_i.strftime("%Y-%m-%d"))
            num_interruptions.append(interruptions_on_date_value)

            if board is None:
                for board_i in boards:
                    board_i_interruptions_value = interruption_measurement(
                        interruptions_on_date.filter(board=board_i))
                    board_values[board_i.id].append(
                        board_i_interruptions_value)

        date_i += timedelta(days=1)

    interruptions_chart.add(u"All interruptions", num_interruptions)
    for board_i in boards:
        if sum(board_values[board_i.id]) > 0:
            interruptions_chart.add(board_i.name, board_values[board_i.id])

    interruptions_chart.x_labels = days

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=interruptions_chart.render(is_unicode=True))
    return chart.render_django_response()
Esempio n. 8
0
def _interruption_measurement_by_month(current_user,
                                       chart_title,
                                       interruption_measurement,
                                       board=None):

    chart_uuid = "interruptions.{0}".format(
        hashlib.sha256("_interruption_measurement_by_month-{0}-{1}-{2}".format(
            current_user.id, inspect.getsource(interruption_measurement),
            board.id
            if board else "username-{0}".format(current_user.id))).hexdigest())
    chart = CachedChart.get(board=board, uuid=chart_uuid)
    if chart:
        return chart

    if board:
        chart_title += u" for board {0} as of {1}".format(
            board.name, board.get_human_fetch_datetime())

    interruptions_filter = {}
    if board:
        interruptions_filter["board"] = board
    else:
        interruptions_filter["member__in"] = Member.get_user_team_members(
            current_user)

    interruptions = Interruption.objects.filter(
        **interruptions_filter).order_by("datetime")

    interruptions_chart = pygal.Line(title=chart_title,
                                     legend_at_bottom=True,
                                     print_values=True,
                                     print_zeroes=False,
                                     human_readable=True)

    min_datetime = interruptions.aggregate(
        min_datetime=Min("datetime"))["min_datetime"]
    max_datetime = interruptions.aggregate(
        max_datetime=Min("datetime"))["max_datetime"]
    if min_datetime is None or max_datetime is None:
        return interruptions_chart.render_django_response()

    datetime_i = copy.deepcopy(min_datetime)

    date_i = datetime_i.date()
    month_i = date_i.month
    year_i = date_i.year

    last_month = max_datetime.month
    last_year = max_datetime.year

    if board is None:
        boards = get_user_boards(current_user)
    else:
        boards = [board]

    months = []
    values = []
    board_values = {board.id: [] for board in boards}
    has_board_values = {board.id: False for board in boards}

    while year_i < last_year or year_i == last_year and month_i <= last_month:
        monthly_interruptions = interruptions.filter(datetime__month=month_i,
                                                     datetime__year=year_i)
        monthly_measurement = interruption_measurement(monthly_interruptions)
        # For each month that have some data, add it to the chart
        if monthly_measurement > 0:
            months.append(u"{0}-{1}".format(year_i, month_i))
            values.append(monthly_measurement)
            for board in boards:
                monthly_interruption_measurement = interruption_measurement(
                    monthly_interruptions.filter(board=board))
                board_values[board.id].append(monthly_interruption_measurement)
                if monthly_interruption_measurement > 0:
                    has_board_values[board.id] = True

        month_i += 1
        if month_i > 12:
            month_i = 1
            year_i += 1

    interruptions_chart.x_labels = months
    interruptions_chart.add(u"All interruptions", values)
    for board in boards:
        if has_board_values[board.id]:
            interruptions_chart.add(board.name, board_values[board.id])

    chart = CachedChart.make(board=None,
                             uuid=chart_uuid,
                             svg=interruptions_chart.render(is_unicode=True))
    return chart.render_django_response()