<commit_before># the following try/except block will make the custom check compatible with any Agent version
try:
    # first, try to import the base class from old versions of the Agent...
    from checks import AgentCheck
except ImportError:
    # ...if the above failed, the check is running in Agent version 6 or later
    from datadog_checks.checks import AgentCheck
    from datadog_checks.errors import CheckException

from datadog_checks.utils.subprocess_output import get_subprocess_output
import json

# content of the special variable __version__ will be shown in the Agent status page
__version__ = "1.0.0"

EXPECTED_RESPONSE_CODE = "NO_ERROR"

class LighthouseCheck(AgentCheck):
    def check(self, instance):
        lighthouse_url = instance.get('url')
        lighthouse_name = instance.get('name')

        if not lighthouse_url or not lighthouse_name:
            self.log.error("missing instance url or name")
            raise CheckException("missing lighthouse instance url or name, please fix yaml")

        cmd = ["/usr/local/bin/lighthouse",
               lighthouse_url,
               "--output",
               "json",
               "--quiet",
               "--chrome-flags='--headless'"]

        json_string, error_message, exit_code = self._get_lighthouse_report(cmd, self.log, False)

        # check for error since we have raise_on_empty_output set to False
        if exit_code > 0:
            self.log.error("lighthouse subprocess error {0} exit code {1} for url: {2}"
                           .format(error_message, exit_code, lighthouse_url)
                           )
            raise CheckException(json_string, error_message, exit_code)

        try:
            data = json.loads(json_string)
        except Exception as e:
            self.log.warn("lighthouse response JSON structure different than expected for url: {0}".format(lighthouse_url))
            raise CheckException(error_message, exit_code, e)

        if data["runtimeError"]["code"] == EXPECTED_RESPONSE_CODE:
            score_accessibility = round(data["categories"]["accessibility"]["score"] * 100)
            score_best_practices = round(data["categories"]["best-practices"]["score"] * 100)
            score_performance = round(data["categories"]["performance"]["score"] * 100)
            score_pwa = round(data["categories"]["pwa"]["score"] * 100)<commit_msg>formatting fixes<commit_after># the following try/except block will make the custom check compatible with any Agent version
try:
    # first, try to import the base class from old versions of the Agent...
    from checks import AgentCheck
except ImportError:
    # ...if the above failed, the check is running in Agent version 6 or later
    from datadog_checks.checks import AgentCheck
    from datadog_checks.errors import CheckException

from datadog_checks.utils.subprocess_output import get_subprocess_output
import json

# content of the special variable __version__ will be shown in the Agent status page
__version__ = "1.0.0"

EXPECTED_RESPONSE_CODE = "NO_ERROR"


class LighthouseCheck(AgentCheck):
    def check(self, instance):
        lighthouse_url = instance.get('url')
        lighthouse_name = instance.get('name')

        if not lighthouse_url or not lighthouse_name:
            self.log.error("missing instance url or name")
            raise CheckException("missing lighthouse instance url or name, please fix yaml")

        cmd = ["/usr/local/bin/lighthouse",
               lighthouse_url,
               "--output",
               "json",
               "--quiet",
               "--chrome-flags='--headless'"]

        json_string, error_message, exit_code = self._get_lighthouse_report(cmd, self.log, False)

        # check for error since we have raise_on_empty_output set to False
        if exit_code > 0:
            self.log.error("lighthouse subprocess error {0} exit code {1} for url: {2}"
                           .format(error_message, exit_code, lighthouse_url)
                           )
            raise CheckException(json_string, error_message, exit_code)

        try:
            data = json.loads(json_string)
        except Exception as e:
            self.log.warn("lighthouse response JSON different than expected for url: {0}".format(lighthouse_url))
            raise CheckException(error_message, exit_code, e)

        if data["runtimeError"]["code"] == EXPECTED_RESPONSE_CODE:
            score_accessibility = round(data["categories"]["accessibility"]["score"] * 100)
            score_best_practices = round(data["categories"]["best-practices"]["score"] * 100)
            score_performance = round(data["categories"]["performance"]["score"] * 100)
            score_pwa = round(data["categories"]["pwa"]["score"] * 100)
{'fraction_non_alphanumeric': 0.09525807805287453, 'fraction_numerical': 0.008812421317666807, 'mean_word_length': 2.8014354066985647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4037109', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1284, 'n_words': 379}