fedspendingtransparency/usaspending-api

View on GitHub

Showing 160 of 222 total issues

File download_column_historical_lookups.py has 3065 lines of code (exceeds 250 allowed). Consider refactoring.
Open

"""
Sets up mappings from column names used in downloads to the query paths used to get the data from django.

Not in use while we pull CSV data from the non-historical tables. Until we switch to pulling CSV downloads from the
historical tables TransactionFPDS and TransactionFABS, import download_column_lookups.py instead.
Severity: Major
Found in usaspending_api/download/v2/download_column_historical_lookups.py - About 1 wk to fix

    Function matview_search_filter has a Cognitive Complexity of 176 (exceeds 15 allowed). Consider refactoring.
    Open

    def matview_search_filter(filters, model, for_downloads=False):
        queryset = model.objects.all()
    
        recipient_scope_q = Q(recipient_location_country_code="USA") | Q(recipient_location_country_name="UNITED STATES")
        pop_scope_q = Q(pop_country_code="USA") | Q(pop_country_name="UNITED STATES")
    Severity: Minor
    Found in usaspending_api/awards/v2/filters/search.py - About 3 days to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    File load_transactions_in_delta.py has 1286 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    import copy
    import logging
    import re
    
    from contextlib import contextmanager
    Severity: Major
    Found in usaspending_api/etl/management/commands/load_transactions_in_delta.py - About 3 days to fix

      File conftest_spark.py has 1236 lines of code (exceeds 250 allowed). Consider refactoring.
      Open

      import json
      import logging
      import uuid
      from datetime import datetime
      from pathlib import Path
      Severity: Major
      Found in usaspending_api/tests/conftest_spark.py - About 3 days to fix

        Function subaward_filter has a Cognitive Complexity of 144 (exceeds 15 allowed). Consider refactoring.
        Open

        def subaward_filter(filters, for_downloads=False):
            queryset = SubawardSearch.objects.all()
        
            recipient_scope_q = Q(sub_legal_entity_country_code="USA") | Q(sub_legal_entity_country_name="UNITED STATES")
            pop_scope_q = Q(sub_place_of_perform_country_co="USA") | Q(sub_place_of_perform_country_name="UNITED STATES")
        Severity: Minor
        Found in usaspending_api/awards/v2/filters/sub_award.py - About 2 days to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        File download_annotation_functions.py has 863 lines of code (exceeds 250 allowed). Consider refactoring.
        Open

        import datetime
        from typing import List, Optional
        
        from django.db.models.functions import Cast, Coalesce
        from django.db.models import (
        Severity: Major
        Found in usaspending_api/download/helpers/download_annotation_functions.py - About 2 days to fix

          Function post has a Cognitive Complexity of 110 (exceeds 15 allowed). Consider refactoring.
          Open

              def post(self, request, pk, format=None):
                  # create response
                  response = {"results": {}}
          
                  # get federal account id from url
          Severity: Minor
          Found in usaspending_api/accounts/views/federal_accounts_v2.py - About 2 days to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          File sqs_work_dispatcher.py has 825 lines of code (exceeds 250 allowed). Consider refactoring.
          Open

          import logging
          import inspect
          import json
          import os
          
          
          Severity: Major
          Found in usaspending_api/common/sqs/sqs_work_dispatcher.py - About 1 day to fix

            Cyclomatic complexity is too high in function get_business_categories_fpds. (104)
            Open

            def get_business_categories_fpds(row):
                # This function is supposed to be invoked as a Spark UDF with a named_struct containing the necessary
                # fields passed to it.
            
                def row_get(row, col_name):

            Cyclomatic Complexity

            Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

            Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

            Construct Effect on CC Reasoning
            if +1 An if statement is a single decision.
            elif +1 The elif statement adds another decision.
            else +0 The else statement does not cause a new decision. The decision is at the if.
            for +1 There is a decision at the start of the loop.
            while +1 There is a decision at the while statement.
            except +1 Each except branch adds a new conditional path of execution.
            finally +0 The finally block is unconditionally executed.
            with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
            assert +1 The assert statement internally roughly equals a conditional statement.
            Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
            Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

            Source: http://radon.readthedocs.org/en/latest/intro.html

            File query_with_filters.py has 784 lines of code (exceeds 250 allowed). Consider refactoring.
            Open

            import copy
            import itertools
            import logging
            import re
            from datetime import datetime
            Severity: Major
            Found in usaspending_api/common/query_with_filters.py - About 1 day to fix

              Function validate_post_request has a Cognitive Complexity of 96 (exceeds 15 allowed). Consider refactoring.
              Open

                  def validate_post_request(self, request):
                      if "filters" in request:
                          for filt in request["filters"]:
                              if "combine_method" in filt:
                                  try:
              Severity: Minor
              Found in usaspending_api/common/api_request_utils.py - About 1 day to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              File request_validations.py has 723 lines of code (exceeds 250 allowed). Consider refactoring.
              Open

              import json
              from copy import deepcopy
              from datetime import datetime, MINYEAR, MAXYEAR
              from django.conf import settings
              from typing import Optional
              Severity: Major
              Found in usaspending_api/download/v2/request_validations.py - About 1 day to fix

                Cyclomatic complexity is too high in function matview_search_filter. (84)
                Open

                def matview_search_filter(filters, model, for_downloads=False):
                    queryset = model.objects.all()
                
                    recipient_scope_q = Q(recipient_location_country_code="USA") | Q(recipient_location_country_name="UNITED STATES")
                    pop_scope_q = Q(pop_country_code="USA") | Q(pop_country_name="UNITED STATES")

                Cyclomatic Complexity

                Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

                Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

                Construct Effect on CC Reasoning
                if +1 An if statement is a single decision.
                elif +1 The elif statement adds another decision.
                else +0 The else statement does not cause a new decision. The decision is at the if.
                for +1 There is a decision at the start of the loop.
                while +1 There is a decision at the while statement.
                except +1 Each except branch adds a new conditional path of execution.
                finally +0 The finally block is unconditionally executed.
                with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
                assert +1 The assert statement internally roughly equals a conditional statement.
                Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
                Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

                Source: http://radon.readthedocs.org/en/latest/intro.html

                File swap_in_new_table.py has 672 lines of code (exceeds 250 allowed). Consider refactoring.
                Open

                import json
                import logging
                import re
                
                from datetime import datetime
                Severity: Major
                Found in usaspending_api/etl/management/commands/swap_in_new_table.py - About 1 day to fix

                  File download_generation.py has 668 lines of code (exceeds 250 allowed). Consider refactoring.
                  Open

                  import json
                  import logging
                  import multiprocessing
                  import os
                  from copy import deepcopy
                  Severity: Major
                  Found in usaspending_api/download/filestreaming/download_generation.py - About 1 day to fix

                    Function get_business_categories_fpds has a Cognitive Complexity of 83 (exceeds 15 allowed). Consider refactoring.
                    Open

                    def get_business_categories_fpds(row):
                        # This function is supposed to be invoked as a Spark UDF with a named_struct containing the necessary
                        # fields passed to it.
                    
                        def row_get(row, col_name):
                    Severity: Minor
                    Found in usaspending_api/broker/helpers/get_business_categories.py - About 1 day to fix

                    Cognitive Complexity

                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                    A method's cognitive complexity is based on a few simple rules:

                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                    • Code is considered more complex for each "break in the linear flow of the code"
                    • Code is considered more complex when "flow breaking structures are nested"

                    Further reading

                    File spark.py has 639 lines of code (exceeds 250 allowed). Consider refactoring.
                    Open

                    """
                    Spark utility functions that could be used as stages or steps of an ETL job (aka "data pipeline")
                    
                    NOTE: This is distinguished from the usaspending_api.common.helpers.spark_helpers module, which holds mostly boilerplate
                    functions for setup and configuration of the spark environment
                    Severity: Major
                    Found in usaspending_api/common/etl/spark.py - About 1 day to fix

                      File award_search.py has 638 lines of code (exceeds 250 allowed). Consider refactoring.
                      Open

                      from usaspending_api.awards.v2.lookups.lookups import award_type_mapping
                      
                      AWARD_SEARCH_COLUMNS = {
                          "treasury_account_identifiers": {"delta": "ARRAY<INTEGER>", "postgres": "INTEGER[]", "gold": False},
                          "award_id": {"delta": "LONG NOT NULL", "postgres": "BIGINT NOT NULL", "gold": False},
                      Severity: Major
                      Found in usaspending_api/search/delta_models/award_search.py - About 1 day to fix

                        Function build_elasticsearch_result has a Cognitive Complexity of 81 (exceeds 15 allowed). Consider refactoring.
                        Open

                            def build_elasticsearch_result(self, response: dict) -> Dict[str, dict]:
                                results = {}
                                geo_info_buckets = response.get("group_by_agg_key", {}).get("buckets", [])
                        
                                for bucket in geo_info_buckets:
                        Severity: Minor
                        Found in usaspending_api/disaster/v2/views/spending_by_geography.py - About 1 day to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        File federal_accounts_v2.py has 622 lines of code (exceeds 250 allowed). Consider refactoring.
                        Open

                        import ast
                        from collections import OrderedDict
                        from typing import List
                        
                        from django.db.models import DecimalField, Exists, F, Func, OuterRef, Q, Subquery, Sum
                        Severity: Major
                        Found in usaspending_api/accounts/views/federal_accounts_v2.py - About 1 day to fix
                          Severity
                          Category
                          Status
                          Source
                          Language