Skip to content
TKTail

Purpose-built scripts and the TKTail workflow - the source

scripts/scenarios.py

SHA-256 (LF line endings, as in the source ZIP): c4cd2cf3d938e238975eae16e204cddf6acf60fe7a5e76449f95bc4ea64f2c69 · MIT license (the benchmark suite only)

"""competitive-bench-v1 - the scenarios: what each one asks, its ground truth (written by datasets/generate.py), how
TKTail asks it over MCP, and the one normalized form every implementation's answer is compared in.

A row (what is counted) is a log line: for the application log a line containing the text / a record's first line
carrying `elapsed: <number>ms`; for the W3C log a request line (the # header lines are not records).
"""

FIVE_XX = {'field': {'name': 'sc-status', 'op': 'ge', 'value': '500'}}
OPEN = [{'kind': 'open'}]


def definition(*conditions):
    return {'sources': OPEN, 'predicate': {'all': list(conditions)}}


SCENARIOS = {
    'literal-error': {
        'dataset': 'applog', 'expected': 'linesWithERROR', 'shape': 'count',
        'task': 'Count the lines containing the literal text ERROR (case-sensitive).',
        'tktail': {'definition': definition({'text': 'ERROR', 'case': True})},
    },
    'elapsed-ge-1000': {
        'dataset': 'applog', 'expected': 'recordsElapsedGe1000', 'shape': 'count',
        'task': 'Count the records whose "elapsed: <number>ms" is 1000 or more (the number read exactly; 1000.000 counts).',
        'tktail': {'definition': definition({'number': {'input': 'text', 'extract': 'regex', 'pattern': r'elapsed: (?<ms>\d+(?:\.\d+)?)ms',
                                                        'group': 'ms', 'op': 'ge', 'value': 1000}})},
    },
    'count-5xx': {
        'dataset': 'iis', 'expected': 'count5xx', 'shape': 'count',
        'task': 'Count the requests with sc-status 500 or higher.',
        'tktail': {'definition': definition(FIVE_XX)},
    },
    'count-slow': {
        'dataset': 'iis', 'expected': 'countTimeTakenGe1000', 'shape': 'count',
        'task': 'Count the requests with time-taken 1000 (ms) or more.',
        'tktail': {'definition': definition({'field': {'name': 'time-taken', 'op': 'ge', 'value': '1000'}})},
    },
    'top-ip-5xx': {
        'dataset': 'iis', 'expected': 'top10ClientIp5xx', 'shape': 'top',
        'task': 'The 10 client addresses (c-ip) with the most requests with sc-status 500 or higher, with their counts.',
        'tktail': {'definition': definition(FIVE_XX), 'aggregate': {'groupBy': ['c-ip'], 'top': 10}},
    },
    'top-uri-5xx': {
        'dataset': 'iis', 'expected': 'top10UriStem5xx', 'shape': 'top',
        'task': 'The 10 URL paths (cs-uri-stem) with the most requests with sc-status 500 or higher.',
        'tktail': {'definition': definition(FIVE_XX), 'aggregate': {'groupBy': ['cs-uri-stem'], 'top': 10}},
    },
    'per-minute-5xx': {
        'dataset': 'iis', 'expected': 'perMinute5xx', 'shape': 'buckets',
        'task': 'Requests with sc-status 500 or higher per minute (the W3C time is UTC), in time order.',
        'tktail': {'definition': definition(FIVE_XX), 'aggregate': {'bucket': '1m'}},
    },
    'top-ip-503-post': {
        'dataset': 'iis', 'expected': 'top10ClientIp503Post', 'shape': 'top',
        'task': 'The 10 client addresses with the most POST requests answered 503.',
        'tktail': {'definition': definition({'field': {'name': 'sc-status', 'op': 'eq', 'value': '503'}},
                                            {'field': {'name': 'cs-method', 'op': 'eq', 'value': 'POST', 'case': True}}),
                   'aggregate': {'groupBy': ['c-ip'], 'top': 10}},
    },
}

# The investigation session (scenario 6): the questions of one incident, asked one after another.
SESSION = ['count-5xx', 'top-ip-5xx', 'top-uri-5xx', 'per-minute-5xx', 'count-slow']


def normalize_tktail(scenario, answer):
    """TKTail's answer in the compared form."""
    shape = SCENARIOS[scenario]['shape']
    if shape == 'count':
        return {'count': answer['counts']['matches']}
    agg = answer['aggregate']
    if not agg.get('complete'):
        raise RuntimeError(f'the analysis is not complete: {agg.get("caveats")}')
    if shape == 'top':
        return {'top': [[g['key'][0], g['count']] for g in agg['top']]}
    return {'buckets': [[b['start'][:16], b['count']] for b in agg['histogram']['buckets']]}


def normalize_script(scenario, result):
    shape = SCENARIOS[scenario]['shape']
    if shape == 'count':
        return {'count': result['count']}
    if shape == 'top':
        return {'top': [[k, c] for k, c in result['top']]}
    return {'buckets': [[k, c] for k, c in result['buckets']]}


def expected_of(scenario, manifest):
    shape = SCENARIOS[scenario]['shape']
    value = manifest['expected'][SCENARIOS[scenario]['expected']]
    if shape == 'count':
        return {'count': value}
    if shape == 'top':
        return {'top': value}
    return {'buckets': value}