resource-watch/aqueduct-analysis-microservice

View on GitHub
aqueduct/services/food_supply_chain_service.py

Summary

Maintainability
F
3 days
Test Coverage
F
10%

File food_supply_chain_service.py has 693 lines of code (exceeds 250 allowed). Consider refactoring.
Open

#!/usr/local/bin/python
"""
Name: AqFood_Supply_Chain_Analyzer.py
Project: Aqueduct Food Tool Enhancements
Description:
Severity: Major
Found in aqueduct/services/food_supply_chain_service.py - About 1 day to fix

    Function find_locations has 68 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        def find_locations(self, water_unit):
            """
            :param water_unit: Geometry ID associated with the selected indicator. AQID for aquifers or PFAF_ID for watersheds
            :return: dataframe with that matches each supply location to its watersheds and crops (close to final product); dataframe with all errors logged
            """
    Severity: Major
    Found in aqueduct/services/food_supply_chain_service.py - About 2 hrs to fix

      Function prepare_payload has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

          def prepare_payload(self, payload):
              new_payload = {}
      
              for key in payload:
                  new_key = self.output_lookup.get(key)
      Severity: Minor
      Found in aqueduct/services/food_supply_chain_service.py - About 1 hr 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

      Function run has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

          def run(self):
              try:
                  # b = os.path.getsize(self.user_input)
                  # f = open(self.user_input, 'rb')
                  # first_bytes = f.read(50)
      Severity: Minor
      Found in aqueduct/services/food_supply_chain_service.py - About 55 mins 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

      Function fuzzy_merge has 6 arguments (exceeds 4 allowed). Consider refactoring.
      Open

          def fuzzy_merge(self, df_1, df_2, key1, key2, threshold=90, limit=1):
      Severity: Minor
      Found in aqueduct/services/food_supply_chain_service.py - About 45 mins to fix

        Function clean_buffer has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def clean_buffer(self, row):
                """
                :param row: individual row (1 coordinate + material type)
                :return: buffer radius in decimal degrees
                """
        Severity: Minor
        Found in aqueduct/services/food_supply_chain_service.py - About 25 mins 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

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                ad0_ids = df_adm[["row", "GID_0"]][
                    (df_adm["Select_By"] == "country") & (~df_adm["Country_clean"].isna())
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 1 other location - About 2 hrs to fix
        aqueduct/services/food_supply_chain_service.py on lines 786..787

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 58.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                ad1_ids = df_ad1m[["row", "GID_1"]][
                    (df_ad1m["Select_By"] == "state") & (~df_ad1m["State_clean"].isna())
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 1 other location - About 2 hrs to fix
        aqueduct/services/food_supply_chain_service.py on lines 739..740

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 58.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Identical blocks of code found in 3 locations. Consider refactoring.
        Open

                s3 = session.client(
                    service_name="s3",
                    aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
                    aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
                    endpoint_url=os.environ.get("ENDPOINT_URL"),
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 2 other locations - About 1 hr to fix
        aqueduct/services/food_supply_chain_service.py on lines 524..529
        aqueduct/workers/supply-chain-worker.py on lines 13..17

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Identical blocks of code found in 3 locations. Consider refactoring.
        Open

                if os.environ.get("ENDPOINT_URL"):
                    s3 = session.client(
                        service_name="s3",
                        aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
                        aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 2 other locations - About 1 hr to fix
        aqueduct/services/food_supply_chain_service.py on lines 969..973
        aqueduct/workers/supply-chain-worker.py on lines 13..17

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                    self.df_admnames["AQID"] = self.df_admnames["AQID"].apply(
                        lambda x: ast.literal_eval(x)
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 1 other location - About 1 hr to fix
        aqueduct/services/food_supply_chain_service.py on lines 302..303

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 39.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                    self.df_admnames["PFAF_ID"] = self.df_admnames["PFAF_ID"].apply(
                        lambda x: ast.literal_eval(x)
        Severity: Major
        Found in aqueduct/services/food_supply_chain_service.py and 1 other location - About 1 hr to fix
        aqueduct/services/food_supply_chain_service.py on lines 305..306

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 39.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        There are no issues that match your filters.

        Category
        Status