<commit_before>import datetime
import decimal
import time
from contextlib import closing
from enum import Enum
from typing import Dict, List

import pymysql

from datadog_checks.base import is_affirmative, to_native_string
from datadog_checks.base.utils.db.sql import compute_sql_signature
from datadog_checks.base.utils.db.utils import DBMAsyncJob, obfuscate_sql_with_metadata
from datadog_checks.base.utils.serialization import json
from datadog_checks.base.utils.tracking import tracked_method

from .util import DatabaseConfigurationError, get_truncation_state, warning_with_tags

try:
    import datadog_agent
except ImportError:
    from ..stubs import datadog_agent


ACTIVITY_QUERY = """\
SELECT
    thread_a.thread_id,
    thread_a.processlist_id,
    thread_a.processlist_user,
    thread_a.processlist_host,
    thread_a.processlist_db,
    thread_a.processlist_command,
    thread_a.processlist_state,
    COALESCE(statement.sql_text, thread_a.PROCESSLIST_info) AS sql_text,
    statement.timer_start AS event_timer_start,
    statement.timer_end AS event_timer_end,
    statement.lock_time,
    statement.current_schema,
    waits_a.event_id AS event_id,
    waits_a.end_event_id,
    IF(waits_a.thread_id IS NULL,
        'other',
        COALESCE(
            IF(thread_a.processlist_state = 'User sleep', 'User sleep',
            IF(waits_a.event_id = waits_a.end_event_id, 'CPU', waits_a.event_name)), 'CPU'
        )
    ) AS wait_event,
    waits_a.operation,
    waits_a.timer_start AS wait_timer_start,
    waits_a.timer_end AS wait_timer_end,
    waits_a.object_schema,
    waits_a.object_name,
    waits_a.index_name,
    waits_a.object_type,
    waits_a.source,
    socket.ip,
    socket.port,
    socket.event_name AS socket_event_name
FROM
    performance_schema.threads AS thread_a
    LEFT JOIN performance_schema.events_waits_current AS waits_a ON waits_a.thread_id = thread_a.thread_id
    LEFT JOIN performance_schema.events_statements_current AS statement ON statement.thread_id = thread_a.thread_id
    LEFT JOIN performance_schema.socket_instances AS socket ON socket.thread_id = thread_a.thread_id
WHERE
    thread_a.processlist_state IS NOT NULL
    AND thread_a.processlist_command != 'Sleep'
    AND thread_a.processlist_id != CONNECTION_ID()
    AND thread_a.PROCESSLIST_COMMAND != 'Daemon'
    AND (waits_a.EVENT_NAME != 'idle' OR waits_a.EVENT_NAME IS NULL)
    AND (waits_a.operation != 'idle' OR waits_a.operation IS NULL)
    -- events_waits_current can have multiple rows per thread, thus we use EVENT_ID to identify the row we want to use.
    -- Additionally, we want the row with the highest EVENT_ID which reflects the most recent and current wait.
    AND (
        waits_a.event_id = (
           SELECT
              MAX(waits_b.EVENT_ID)
          FROM  performance_schema.events_waits_current AS waits_b
          Where waits_b.thread_id = thread_a.thread_id
    ) OR waits_a.event_id is NULL)
    -- We ignore rows without SQL text because there will be rows for background operations that do not have
    -- SQL text associated with it.
    AND COALESCE(statement.sql_text, thread_a.PROCESSLIST_info) != "";
"""


class MySQLVersion(Enum):
    # 8.0
    VERSION_80 = 80
    # 5.7
    VERSION_57 = 57
    # 5.6
    VERSION_56 = 56

<commit_msg>remove expensive socket information from the activity query (#13196)<commit_after>import datetime
import decimal
import time
from contextlib import closing
from enum import Enum
from typing import Dict, List

import pymysql

from datadog_checks.base import is_affirmative, to_native_string
from datadog_checks.base.utils.db.sql import compute_sql_signature
from datadog_checks.base.utils.db.utils import DBMAsyncJob, obfuscate_sql_with_metadata
from datadog_checks.base.utils.serialization import json
from datadog_checks.base.utils.tracking import tracked_method

from .util import DatabaseConfigurationError, get_truncation_state, warning_with_tags

try:
    import datadog_agent
except ImportError:
    from ..stubs import datadog_agent


ACTIVITY_QUERY = """\
SELECT
    thread_a.thread_id,
    thread_a.processlist_id,
    thread_a.processlist_user,
    thread_a.processlist_host,
    thread_a.processlist_db,
    thread_a.processlist_command,
    thread_a.processlist_state,
    COALESCE(statement.sql_text, thread_a.PROCESSLIST_info) AS sql_text,
    statement.timer_start AS event_timer_start,
    statement.timer_end AS event_timer_end,
    statement.lock_time,
    statement.current_schema,
    waits_a.event_id AS event_id,
    waits_a.end_event_id,
    IF(waits_a.thread_id IS NULL,
        'other',
        COALESCE(
            IF(thread_a.processlist_state = 'User sleep', 'User sleep',
            IF(waits_a.event_id = waits_a.end_event_id, 'CPU', waits_a.event_name)), 'CPU'
        )
    ) AS wait_event,
    waits_a.operation,
    waits_a.timer_start AS wait_timer_start,
    waits_a.timer_end AS wait_timer_end,
    waits_a.object_schema,
    waits_a.object_name,
    waits_a.index_name,
    waits_a.object_type,
    waits_a.source
FROM
    performance_schema.threads AS thread_a
    LEFT JOIN performance_schema.events_waits_current AS waits_a ON waits_a.thread_id = thread_a.thread_id
    LEFT JOIN performance_schema.events_statements_current AS statement ON statement.thread_id = thread_a.thread_id
WHERE
    thread_a.processlist_state IS NOT NULL
    AND thread_a.processlist_command != 'Sleep'
    AND thread_a.processlist_id != CONNECTION_ID()
    AND thread_a.PROCESSLIST_COMMAND != 'Daemon'
    AND (waits_a.EVENT_NAME != 'idle' OR waits_a.EVENT_NAME IS NULL)
    AND (waits_a.operation != 'idle' OR waits_a.operation IS NULL)
    -- events_waits_current can have multiple rows per thread, thus we use EVENT_ID to identify the row we want to use.
    -- Additionally, we want the row with the highest EVENT_ID which reflects the most recent and current wait.
    AND (
        waits_a.event_id = (
           SELECT
              MAX(waits_b.EVENT_ID)
          FROM  performance_schema.events_waits_current AS waits_b
          Where waits_b.thread_id = thread_a.thread_id
    ) OR waits_a.event_id is NULL)
    -- We ignore rows without SQL text because there will be rows for background operations that do not have
    -- SQL text associated with it.
    AND COALESCE(statement.sql_text, thread_a.PROCESSLIST_info) != "";
"""


class MySQLVersion(Enum):
    # 8.0
    VERSION_80 = 80
    # 5.7
    VERSION_57 = 57
    # 5.6
    VERSION_56 = 56


{'fraction_non_alphanumeric': 0.0656066687249151, 'fraction_numerical': 0.006329113924050633, 'mean_word_length': 3.7051561365286854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6945212', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2095, 'n_words': 517}