Beispiel #1
0
def _parse_parameters(valid_fields, request):
    '''
    Parses all the possible parameters for any rest request and returns it as a dictionary with either
    param values or defaults.
    If parsing fails it will raise ValueError (for ex. allowing caller to return BadRequest)
    '''
    req_fields = request.GET.get('fields', ','.join(valid_fields)).split(',')
    for field in req_fields:
        if field not in valid_fields:
            raise ValueError
    from_date = views_utils.str2datetime(request.GET.get('from', datetime.datetime.fromtimestamp(0).isoformat()))
    to_date = views_utils.str2datetime(request.GET.get('to', datetime.datetime.now().isoformat()))

    if 'bins' in request.GET:
        bins = int(request.GET.get('bins'))
    else:
        bins = None

    return {'fields': req_fields, 'from': from_date, 'to': to_date, 'bins': bins}
Beispiel #2
0
def contributors_defects_data(session, request):
    """
    Returns the contributors and defects for all the files between two dates.

    QueryDict params:
        response_type (str)             'application/json'|'text/csv', default='application/json'
        startdate (json date format)    As per ISO 8601, default=datetime.fromtimestamp(0)
        enddate (json date format)      As per ISO 8601, default=datetime.now()

    Args:
        request (django.http.HttpRequest)

    Returns:
        django.http.HttpResponse
    """
    response_type = request.GET.get("response_type", "application/json")
    startdate = views_utils.str2datetime(request.GET.get('startdate', datetime.fromtimestamp(0).isoformat()))
    enddate = views_utils.str2datetime(request.GET.get('enddate', datetime.now().isoformat()))

    # Pylint incorrectly identifies start/enddate as possible tuples (str2datetime makes sure that is not possible)
    # plus this code is specifically tested in a unittest so its safe.
    filename = "contributors_defects_files_" + startdate.strftime("%Y%m%d") + "-" + enddate.strftime("%Y%m%d")  # pylint: disable=E1101, C0301

    cm1 = aliased(ChangeMetric)
    cm2 = aliased(ChangeMetric)
    cm_metrics = session.query(cm1.file_id,
                               MetricsDb_TreemapData.replace_null_0(func.sum(cm1.nloc)).label("nloc"),
                               MetricsDb_TreemapData.replace_null_0(func.sum(cm1.cyclomatic_complexity))\
                               .label("cyclomatic_complexity"))\
                               .outerjoin(cm2, and_(cm1.function_id == cm2.function_id, cm1.date < cm2.date))\
                               .filter(cm2.date.is_(None))\
                               .group_by(cm1.file_id)\
                               .subquery()

    cm_contributors = session.query(ChangeMetric.file_id,
                                    func.count(distinct(ChangeMetric.user_id)).label('contributors'))\
                                    .filter(ChangeMetric.date.between(startdate, enddate))\
                                    .group_by(ChangeMetric.file_id)\
                                    .subquery()

    defect_mods = session.query(DefectModification.file_id,
                                func.count(distinct(DefectModification.user_id)).label('contributors'),
                                func.count(distinct(DefectModification.defect_id)).label('defects'))\
                                .join(DefectMeta, DefectMeta.id == DefectModification.defect_id)\
                                .filter(DefectModification.date.between(startdate, enddate))\
                                .group_by(DefectModification.file_id)

    defects_a = defect_mods.filter(DefectMeta.severity == 'A').subquery()
    defects_b = defect_mods.filter(DefectMeta.severity == 'B').subquery()
    defects_c = defect_mods.filter(DefectMeta.severity == 'C').subquery()
    defects_improvement = defect_mods.filter(DefectMeta.severity == 'Improvement').subquery()
    defect_mods = defect_mods.subquery()

    # Note, we are only retrieveing files with a subsystem with an inner join
    query = session.query(File.subsystem_id,
                          File.id,
                          File.file,
                          Subsystem.subsystem,
                          MetricsDb_TreemapData.replace_null_0(cm_contributors.c.contributors).label("contributors_cm"),
                          MetricsDb_TreemapData.replace_null_0(defect_mods.c.contributors).label("contributors_tr"),
                          MetricsDb_TreemapData.replace_null_0(defect_mods.c.defects).label("defects"),
                          MetricsDb_TreemapData.replace_null_0(defects_a.c.defects).label("defects_a"),
                          MetricsDb_TreemapData.replace_null_0(defects_b.c.defects).label("defects_b"),
                          MetricsDb_TreemapData.replace_null_0(defects_c.c.defects).label("defects_c"),
                          MetricsDb_TreemapData.replace_null_0(defects_improvement.c.defects)\
                          .label("defects_improvement"),
                          MetricsDb_TreemapData.replace_null_0(cm_metrics.c.cyclomatic_complexity)\
                          .label("cyclomatic_complexity"),
                          MetricsDb_TreemapData.replace_null_0(cm_metrics.c.nloc).label("nloc"))\
                                .join(Subsystem, File.subsystem_id == Subsystem.id)\
                                .outerjoin(cm_metrics, cm_metrics.c.file_id == File.id)\
                                .outerjoin(cm_contributors, cm_contributors.c.file_id == File.id)\
                                .outerjoin(defect_mods, defect_mods.c.file_id == File.id)\
                                .outerjoin(defects_a, defects_a.c.file_id == File.id)\
                                .outerjoin(defects_b, defects_b.c.file_id == File.id)\
                                .outerjoin(defects_c, defects_c.c.file_id == File.id)\
                                .outerjoin(defects_improvement, defects_improvement.c.file_id == File.id)\
                                .order_by(File.id)

    data = [views_utils.get_query_columns(query)] + query.all()
    return views_utils.dump_data(data, response_type, filename=filename)
Beispiel #3
0
def contributors_defects_data(session, request):
    """
    Returns the contributors and defects for all the files between two dates.

    QueryDict params:
        response_type (str)             'application/json'|'text/csv', default='application/json'
        startdate (json date format)    As per ISO 8601, default=datetime.fromtimestamp(0)
        enddate (json date format)      As per ISO 8601, default=datetime.now()

    Args:
        request (django.http.HttpRequest)

    Returns:
        django.http.HttpResponse
    """
    response_type = request.GET.get("response_type", "application/json")
    startdate = views_utils.str2datetime(
        request.GET.get('startdate',
                        datetime.fromtimestamp(0).isoformat()))
    enddate = views_utils.str2datetime(
        request.GET.get('enddate',
                        datetime.now().isoformat()))

    # Pylint incorrectly identifies start/enddate as possible tuples (str2datetime makes sure that is not possible)
    # plus this code is specifically tested in a unittest so its safe.
    filename = "contributors_defects_files_" + startdate.strftime(
        "%Y%m%d") + "-" + enddate.strftime("%Y%m%d")  # pylint: disable=E1101, C0301

    cm1 = aliased(ChangeMetric)
    cm2 = aliased(ChangeMetric)
    cm_metrics = session.query(cm1.file_id,
                               MetricsDb_TreemapData.replace_null_0(func.sum(cm1.nloc)).label("nloc"),
                               MetricsDb_TreemapData.replace_null_0(func.sum(cm1.cyclomatic_complexity))\
                               .label("cyclomatic_complexity"))\
                               .outerjoin(cm2, and_(cm1.function_id == cm2.function_id, cm1.date < cm2.date))\
                               .filter(cm2.date.is_(None))\
                               .group_by(cm1.file_id)\
                               .subquery()

    cm_contributors = session.query(ChangeMetric.file_id,
                                    func.count(distinct(ChangeMetric.user_id)).label('contributors'))\
                                    .filter(ChangeMetric.date.between(startdate, enddate))\
                                    .group_by(ChangeMetric.file_id)\
                                    .subquery()

    defect_mods = session.query(DefectModification.file_id,
                                func.count(distinct(DefectModification.user_id)).label('contributors'),
                                func.count(distinct(DefectModification.defect_id)).label('defects'))\
                                .join(DefectMeta, DefectMeta.id == DefectModification.defect_id)\
                                .filter(DefectModification.date.between(startdate, enddate))\
                                .group_by(DefectModification.file_id)

    defects_a = defect_mods.filter(DefectMeta.severity == 'A').subquery()
    defects_b = defect_mods.filter(DefectMeta.severity == 'B').subquery()
    defects_c = defect_mods.filter(DefectMeta.severity == 'C').subquery()
    defects_improvement = defect_mods.filter(
        DefectMeta.severity == 'Improvement').subquery()
    defect_mods = defect_mods.subquery()

    # Note, we are only retrieveing files with a subsystem with an inner join
    query = session.query(File.subsystem_id,
                          File.id,
                          File.file,
                          Subsystem.subsystem,
                          MetricsDb_TreemapData.replace_null_0(cm_contributors.c.contributors).label("contributors_cm"),
                          MetricsDb_TreemapData.replace_null_0(defect_mods.c.contributors).label("contributors_tr"),
                          MetricsDb_TreemapData.replace_null_0(defect_mods.c.defects).label("defects"),
                          MetricsDb_TreemapData.replace_null_0(defects_a.c.defects).label("defects_a"),
                          MetricsDb_TreemapData.replace_null_0(defects_b.c.defects).label("defects_b"),
                          MetricsDb_TreemapData.replace_null_0(defects_c.c.defects).label("defects_c"),
                          MetricsDb_TreemapData.replace_null_0(defects_improvement.c.defects)\
                          .label("defects_improvement"),
                          MetricsDb_TreemapData.replace_null_0(cm_metrics.c.cyclomatic_complexity)\
                          .label("cyclomatic_complexity"),
                          MetricsDb_TreemapData.replace_null_0(cm_metrics.c.nloc).label("nloc"))\
                                .join(Subsystem, File.subsystem_id == Subsystem.id)\
                                .outerjoin(cm_metrics, cm_metrics.c.file_id == File.id)\
                                .outerjoin(cm_contributors, cm_contributors.c.file_id == File.id)\
                                .outerjoin(defect_mods, defect_mods.c.file_id == File.id)\
                                .outerjoin(defects_a, defects_a.c.file_id == File.id)\
                                .outerjoin(defects_b, defects_b.c.file_id == File.id)\
                                .outerjoin(defects_c, defects_c.c.file_id == File.id)\
                                .outerjoin(defects_improvement, defects_improvement.c.file_id == File.id)\
                                .order_by(File.id)

    data = [views_utils.get_query_columns(query)] + query.all()
    return views_utils.dump_data(data, response_type, filename=filename)