Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 0 additions & 159 deletions logic.py

This file was deleted.

129 changes: 129 additions & 0 deletions logic_simplified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import re
from datetime import datetime

TIMESTAMP_PATTERN = r'(\d{4}[-/.]\d{2}[-/.]\d{2}[T ]\d{2}:\d{2}:\d{2})'
DATE_FORMATS = [
'%Y-%m-%dT%H:%M:%S',
'%Y-%m-%d %H:%M:%S',
'%Y/%m/%d %H:%M:%S',
'%Y.%m.%d %H:%M:%S',
]


def parse_any_date(date_str):
for fmt in DATE_FORMATS:
try:
return datetime.strptime(date_str[:19], fmt)
except ValueError:
continue
return None


def extract_timestamp(line: str):
match = re.search(TIMESTAMP_PATTERN, line)
return parse_any_date(match.group(1)) if match else None



def normalize_threshold(value):
try:

return max(1, int(value))
except (ValueError, TypeError):
return 60


def classify_gap(gap: float):

if gap > 3600:
return "CRITICAL", "Extended System Blackout / High Risk Log Erasure"
if gap > 600:
return "CRITICAL", "Significant Data Void / Potential Tampering"
if gap > 300:
return "WARNING", "Service Interruption Detected"
return "WARNING", "Minor Sequential Lag"


def clamp_score(value: float) -> float:
return max(0.0, min(100.0, value))


def analyze_logs(file_path, threshold_seconds=60, use_24_hour=True, progress_callback=None):
if progress_callback:
progress_callback(0, "Starting analysis")

threshold_seconds = normalize_threshold(threshold_seconds)
time_format = '%Y-%m-%d %H:%M:%S' if use_24_hour else '%Y-%m-%d %I:%M:%S %p'
incidents = []
last_time = None

with open(file_path, 'r', encoding='utf-8', errors='ignore') as handle:
for line in handle:
current_time = extract_timestamp(line)
if not current_time:
continue

if last_time is not None:
gap = (current_time - last_time).total_seconds()
if gap > threshold_seconds:
severity, details = classify_gap(gap)
incidents.append(
{
"start": last_time.strftime(time_format),
"end": current_time.strftime(time_format),
"duration": int(gap),
"severity": severity,
"details": details,
}
)

last_time = current_time

score = calculate_integrity_score(incidents)
if progress_callback:
progress_callback(100, "Analysis completed.")

return {
"total_gaps": len(incidents),
"integrity_score": score,
"incidents": incidents,
}


def calculate_integrity_score(incidents):
if not incidents:
return 100.0

durations = [inc["duration"] for inc in incidents]
total_gap_time = sum(durations)
longest_gap = max(durations)
average_gap = total_gap_time / len(durations)

if average_gap < 120:
per_gap_penalty = 1.0
elif average_gap < 600:
per_gap_penalty = 3.0
elif average_gap < 3600:
per_gap_penalty = 6.0
else:
per_gap_penalty = 10.0

frequency_penalty = min(40.0, len(durations) * per_gap_penalty)
duration_penalty = min(35.0, total_gap_time / 3600 * 10.0)

if longest_gap >= 86400:
longest_penalty = 25.0
elif longest_gap >= 3600:
longest_penalty = 15.0
elif longest_gap >= 600:
longest_penalty = 8.0
elif longest_gap >= 300:
longest_penalty = 4.0
else:
longest_penalty = 1.0

clustering_penalty = 0.0
if len(durations) > 10 and average_gap < 300:
clustering_penalty = min(10.0, (len(durations) - 10) * 0.5)

return clamp_score(100.0 - (frequency_penalty + duration_penalty + longest_penalty + clustering_penalty))
Loading