File size: 27,067 Bytes
62e9aac
ffe022c
 
 
62e9aac
ffe022c
 
 
 
 
 
62e9aac
 
 
ffe022c
 
 
 
 
 
 
 
2806d4d
 
62e9aac
2806d4d
62e9aac
2806d4d
62e9aac
 
2806d4d
 
 
 
 
 
 
 
 
 
 
 
ffe022c
2806d4d
 
 
 
 
 
ffe022c
 
2806d4d
 
 
ffe022c
62e9aac
ffe022c
 
 
 
62e9aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2806d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62e9aac
ffe022c
 
 
62e9aac
 
 
 
 
 
 
 
 
ffe022c
62e9aac
 
2806d4d
62e9aac
 
 
 
 
 
 
ffe022c
62e9aac
 
 
 
 
 
974c830
2806d4d
62e9aac
2806d4d
 
 
62e9aac
 
 
 
2806d4d
 
 
974c830
62e9aac
ffe022c
62e9aac
ffe022c
62e9aac
 
ffe022c
62e9aac
ffe022c
62e9aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ef01c
ffe022c
62e9aac
 
 
ffe022c
62e9aac
 
ffe022c
 
 
62e9aac
ffe022c
 
 
 
62e9aac
 
ffe022c
62e9aac
 
ffe022c
 
 
62e9aac
ffe022c
1a492df
ffe022c
 
2806d4d
62e9aac
ffe022c
2806d4d
62e9aac
 
 
 
 
 
 
 
2806d4d
 
62e9aac
 
2806d4d
62e9aac
 
2806d4d
62e9aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ef01c
62e9aac
c4ef01c
62e9aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ef01c
62e9aac
 
 
 
 
 
 
c4ef01c
62e9aac
 
 
 
c4ef01c
 
ffe022c
508b80b
ffe022c
 
 
 
 
 
 
 
 
 
 
62e9aac
 
ffe022c
 
62e9aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe022c
62e9aac
ffe022c
 
 
 
2806d4d
 
 
ffe022c
2806d4d
 
 
 
 
 
ffe022c
2806d4d
 
 
ffe022c
 
2806d4d
ffe022c
 
2806d4d
 
ffe022c
 
 
 
2806d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe022c
2806d4d
ffe022c
2806d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe022c
2806d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
974c830
 
 
2806d4d
 
974c830
2806d4d
974c830
 
ffe022c
2806d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
974c830
2806d4d
 
 
 
 
 
 
 
 
 
974c830
2806d4d
 
 
 
ffe022c
2806d4d
 
ffe022c
2806d4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import gzip
import re
import os
import pickle
from datetime import datetime
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import requests
from dotenv import load_dotenv

load_dotenv()

GOAL_WORDS = 2_200_000
CATEGORY_GOAL = 1_100_000

OUR_TEAM_PROJECT_IDS = {29, 30, 31, 32, 33, 37}
ANNOTATED_STATES = ["Acceptable", "No Rating"]
GOAL_STATES = ["Acceptable", "No Rating", "ReqAttn (entities)"]

# Map project IDs to annotator IDs (for admin-created annotations)
PROJECT_ANNOTATOR_MAP = {
    29: 27,
    30: 28,
    31: 29,
    32: 30,
    33: 31,
    37: 33,
}

ANNOTATOR_NAMES = {
    1: "Admin",
    27: "A.K.",
    28: "Jo.Ε .",
    29: "Ju.Ε .",
    30: "G.Z.",
    31: "L.M.",
    33: "M.M.",
}

TEAM_COLORS = {
    27: "#0066cc",  # A.K.
    28: "#00cccc",  # Jo.Ε .
    29: "#00cc00",  # Ju.Ε .
    30: "#ff9900",  # G.Z.
    31: "#9933ff",  # L.M.
    33: "#cc0000",  # M.M.
}

# Helper: map annotator names to colors (derived from TEAM_COLORS and ANNOTATOR_NAMES)
COLORS_BY_NAME = {ANNOTATOR_NAMES[aid]: color for aid, color in TEAM_COLORS.items() if aid in ANNOTATOR_NAMES}

# Cache file location (persists between runs)
CACHE_FILE = Path(".cache.pkl.gz")

st.set_page_config(page_title="Annotation Progress", page_icon="πŸ“Š", layout="wide")


# ============== Data layer ==============

def _get_credentials():
    """Get Label Studio URL and API key from secrets or environment."""
    try:
        url = st.secrets.get("LABEL_STUDIO_URL", os.getenv("LABEL_STUDIO_URL", "")).rstrip("/")
        key = st.secrets.get("LABEL_STUDIO_API_KEY", os.getenv("LABEL_STUDIO_API_KEY", ""))
    except (KeyError, FileNotFoundError, AttributeError):
        url = os.getenv("LABEL_STUDIO_URL", "").rstrip("/")
        key = os.getenv("LABEL_STUDIO_API_KEY", "")
    return url, key


def _load_cache():
    """Load disk cache (gzip-compressed pickle)."""
    if CACHE_FILE.exists():
        try:
            with gzip.open(CACHE_FILE, "rb") as f:
                return pickle.load(f)
        except Exception:
            pass
    # Try loading old uncompressed cache for migration
    old_cache = Path(".cache.pkl")
    if old_cache.exists():
        try:
            with open(old_cache, "rb") as f:
                cache = pickle.load(f)
            _save_cache(cache)
            old_cache.unlink()
            return cache
        except Exception:
            pass
    return {}


def _save_cache(cache):
    """Save disk cache (gzip-compressed pickle)."""
    try:
        with gzip.open(CACHE_FILE, "wb") as f:
            pickle.dump(cache, f)
    except Exception:
        pass


def _build_df(all_rows):
    """Build a DataFrame from row dicts."""
    if not all_rows:
        return pd.DataFrame(columns=[
            "task_id", "project_id", "project", "project_group",
            "annotator", "annotator_email", "date", "state", "words", "category",
            "is_annotated", "is_goal_state",
        ])
    df = pd.DataFrame(all_rows)
    df["words"] = df["words"].astype(int)
    df["date"] = pd.to_datetime(df["date"], errors="coerce")
    df["is_annotated"] = df["state"].isin(ANNOTATED_STATES)
    df["is_goal_state"] = df["state"].isin(GOAL_STATES)
    return df


def load_df_from_cache():
    """Build DataFrame from disk cache only β€” no API calls, instant."""
    cache = _load_cache()
    if not cache:
        return None, None

    all_rows = []
    last_updated = None
    for key, data in cache.items():
        if key.startswith("project_"):
            all_rows.extend(data.get("rows", []))
            ts = data.get("last_updated")
            if ts and (not last_updated or ts > last_updated):
                last_updated = ts

    if not all_rows:
        return None, None

    return _build_df(all_rows), last_updated


@st.cache_data(ttl=3600)
def fetch_users(url, key):
    """Fetch all users and create a mapping of user_id -> user_name."""
    try:
        headers = {"Authorization": f"Token {key}"}
        resp = requests.get(f"{url}/api/users", headers=headers, timeout=30)
        resp.raise_for_status()
        users = resp.json()

        user_map = {}
        for user in users:
            user_id = user.get("id")
            first_name = user.get("first_name", "")
            email = user.get("email", "")
            name = first_name or email or f"User {user_id}"
            user_map[user_id] = name

        return user_map
    except Exception:
        return {}


def fetch_project_data(proj, url, headers, user_map, since_date=None):
    """Fetch data from one project using the export API (excludes predictions)."""
    pid, name, task_count = proj["id"], proj.get("title", f"Project {proj['id']}"), proj.get("task_number", 0)
    group = "Our Team" if pid in OUR_TEAM_PROJECT_IDS else "Others"

    resp = requests.get(
        f"{url}/api/projects/{pid}/export",
        headers=headers,
        params={"exportType": "JSON", "download_all_tasks": "true"},
        timeout=60,
    )
    resp.raise_for_status()
    tasks = resp.json()

    rows = []
    submitted_count = 0
    max_updated_at = since_date

    for task in tasks:
        task_updated = task.get("updated_at")
        if task_updated and (not max_updated_at or task_updated > max_updated_at):
            max_updated_at = task_updated

        if since_date and task_updated and task_updated <= since_date:
            continue

        task_data = task.get("data", {})
        words = task_data.get("words") or len(task_data.get("text", "").split())
        category = task_data.get("category")

        annots = [a for a in task.get("annotations", []) if not a.get("was_cancelled")]
        if not annots:
            rows.append(
                {
                    "task_id": task.get("id"),
                    "project_id": pid,
                    "project": name,
                    "project_group": group,
                    "annotator": None,
                    "annotator_email": None,
                    "date": None,
                    "state": "Not Annotated",
                    "words": int(words),
                    "category": category,
                }
            )
            continue

        submitted_count += 1

        ann = annots[0]
        date = ann.get("created_at", "")[:10] or None

        completed_by = ann.get("completed_by")

        if isinstance(completed_by, dict):
            annotator_id = completed_by.get("id")
            annotator_email = completed_by.get("email", "Unknown")
        elif isinstance(completed_by, int):
            annotator_id = completed_by
            annotator_email = f"user_{completed_by}"
        else:
            annotator_id = None
            annotator_email = "unknown"

        if group == "Our Team" and annotator_id == 1 and pid in PROJECT_ANNOTATOR_MAP:
            mapped_id = PROJECT_ANNOTATOR_MAP[pid]
            if mapped_id:
                annotator_id = mapped_id

        if annotator_id in ANNOTATOR_NAMES:
            annotator_name = ANNOTATOR_NAMES[annotator_id]
        elif annotator_id in user_map:
            annotator_name = user_map[annotator_id]
        else:
            annotator_name = f"User {annotator_id}" if annotator_id else "Unknown"

        rating = None
        for item in ann.get("result", []):
            if item.get("type") == "choices" and item.get("from_name") == "text_rating":
                rating = item.get("value", {}).get("choices", [None])[0]
                break

        has_entities = any(i.get("type") == "labels" for i in ann.get("result", []))
        if rating is None:
            state = "No Rating"
        elif rating == "Requires Attention":
            state = f"ReqAttn ({'entities' if has_entities else 'empty'})"
        elif rating == "Unacceptable":
            state = f"Unacceptable ({'entities' if has_entities else 'empty'})"
        else:
            state = "Acceptable"

        rows.append(
            {
                "task_id": task.get("id"),
                "project_id": pid,
                "project": name,
                "project_group": group,
                "annotator": annotator_name,
                "annotator_email": annotator_email,
                "date": date,
                "state": state,
                "words": int(words),
                "category": category,
            }
        )

    return pid, task_count, submitted_count, rows, max_updated_at


def check_and_update(status_container):
    """Check for updates and fetch if needed. Returns True if cache was updated."""
    url, key = _get_credentials()
    if not url or not key:
        status_container.error("Missing credentials. Set LABEL_STUDIO_URL and LABEL_STUDIO_API_KEY.")
        return False

    headers = {"Authorization": f"Token {key}"}

    # Fetch project list to detect changes
    resp = requests.get(f"{url}/api/projects", headers=headers, timeout=30)
    resp.raise_for_status()
    projects = resp.json().get("results", [])

    cache = _load_cache()
    user_map = fetch_users(url, key)

    projects_to_fetch = []  # (proj, since_date, cached_rows_or_None, reason)
    unchanged = 0

    for proj in projects:
        pid = proj["id"]
        proj_name = proj.get("title", f"Project {pid}")
        task_count = proj.get("task_number", 0)
        api_submitted_count = proj.get("num_tasks_with_annotations", 0)
        cache_key = f"project_{pid}"

        if cache_key not in cache:
            projects_to_fetch.append((proj, None, None, "new project"))
        else:
            cached = cache[cache_key]
            old_tasks = cached.get("task_count", 0)
            old_submitted = cached.get("submitted_count", 0)
            if old_tasks != task_count:
                diff = task_count - old_tasks
                projects_to_fetch.append((proj, None, None,
                    f"{'+' if diff > 0 else ''}{diff} tasks"))
            elif old_submitted != api_submitted_count:
                diff = api_submitted_count - old_submitted
                last_updated = cached.get("last_updated")
                if last_updated:
                    projects_to_fetch.append((proj, last_updated, cached["rows"],
                        f"{'+' if diff > 0 else ''}{diff} annotations"))
                else:
                    projects_to_fetch.append((proj, None, None,
                        f"{'+' if diff > 0 else ''}{diff} annotations"))
            else:
                unchanged += 1

    total_fetches = len(projects_to_fetch)

    if total_fetches == 0:
        return False  # Nothing changed

    # Build a summary of what's updating
    update_names = []
    for p in projects_to_fetch:
        proj_id = p[0]['id']
        # Show annotator name for team projects, project ID for others
        if proj_id in PROJECT_ANNOTATOR_MAP:
            annotator_id = PROJECT_ANNOTATOR_MAP[proj_id]
            name = ANNOTATOR_NAMES.get(annotator_id, f"#{proj_id}")
            update_names.append(f"{name} {p[3]}")
        else:
            update_names.append(f"#{proj_id} ({p[3]})")
    status_container.info(f"Updating {total_fetches} project(s): {', '.join(update_names)}")

    with ThreadPoolExecutor(max_workers=10) as executor:
        futures = []

        for proj, since_date, cached_rows, reason in projects_to_fetch:
            proj_name = proj.get("title", f"Project {proj['id']}")
            api_sub = proj.get("num_tasks_with_annotations", 0)
            is_incremental = since_date is not None and cached_rows is not None
            futures.append((
                "incremental" if is_incremental else "full",
                executor.submit(fetch_project_data, proj, url, headers, user_map, since_date),
                cached_rows,
                proj_name,
                reason,
                api_sub,
            ))

        progress = status_container.progress(0, text=f"Updating {total_fetches} projects...")
        for i, (mode, future, cached_rows, proj_name, reason, api_sub) in enumerate(futures):
            pid = future.result()[0]  # Get project ID early

            # Show annotator name for team projects
            if pid in PROJECT_ANNOTATOR_MAP:
                annotator_id = PROJECT_ANNOTATOR_MAP[pid]
                display_name = ANNOTATOR_NAMES.get(annotator_id, f"#{pid}")
                progress.progress(i / total_fetches, text=f"Fetching: {display_name} {reason}...")
            else:
                progress.progress(i / total_fetches, text=f"Fetching: #{pid} ({reason})...")

            pid, task_count, _, rows, max_updated_at = future.result()

            if mode == "incremental" and cached_rows is not None:
                prev_timestamp = cache.get(f"project_{pid}", {}).get("last_updated")
                if rows:
                    cached_by_id = {row["task_id"]: row for row in cached_rows}
                    for row in rows:
                        cached_by_id[row["task_id"]] = row
                    rows = list(cached_by_id.values())
                else:
                    rows = cached_rows
                max_updated_at = max_updated_at or prev_timestamp

            cache[f"project_{pid}"] = {
                "task_count": task_count,
                "submitted_count": api_sub,
                "last_updated": max_updated_at,
                "rows": rows
            }

            # Show annotator name for team projects
            if pid in PROJECT_ANNOTATOR_MAP:
                annotator_id = PROJECT_ANNOTATOR_MAP[pid]
                display_name = ANNOTATOR_NAMES.get(annotator_id, f"#{pid}")
                progress.progress((i + 1) / total_fetches, text=f"Done: {display_name} {reason}")
            else:
                progress.progress((i + 1) / total_fetches, text=f"Done: #{pid} ({reason})")

    # Save updated timestamp
    cache["_last_checked"] = datetime.now().isoformat()
    _save_cache(cache)
    return True


def anonymize(name):
    """Convert '26 [Name Lastname]' to 'N.L. (26)'"""
    if name == "Others":
        return "Others"
    match = re.match(r"(\d+)\s+\[(.+?)\]", name)
    if match:
        num, full = match.groups()
        parts = full.split()
        if len(parts) >= 2:
            return f"{parts[0][0]}.{parts[-1][0]}. ({num})"
    return name


# ============== Page layout ==============

st.title("πŸ“Š Annotation Progress Dashboard")

# Status bar placeholder at the very top (before any data)
status_bar = st.empty()

# Phase 1: Load cached data instantly (no API calls)
df, cache_timestamp = load_df_from_cache()

if df is None:
    # No cache at all β€” must do a full fetch before we can show anything
    status_bar.info("First load β€” fetching data from Label Studio...")
    updated = check_and_update(status_bar)
    df, cache_timestamp = load_df_from_cache()
    if df is None:
        st.error("Could not load any data. Check your Label Studio credentials.")
        st.stop()

# Phase 2: Show "last updated" and check for updates in the background
# Use session_state to throttle update checks (every 5 minutes)
now = datetime.now()
last_check = st.session_state.get("_last_update_check")
needs_check = last_check is None or (now - last_check).total_seconds() > 300

if needs_check:
    updated = check_and_update(status_bar)
    st.session_state["_last_update_check"] = now
    if updated:
        status_bar.empty()
        st.rerun()  # Rerun to display fresh data

# Show the "last updated" timestamp
if cache_timestamp:
    try:
        ts = pd.Timestamp(cache_timestamp)
        if ts.tzinfo:
            ts = ts.tz_convert("Europe/Vilnius")
        else:
            ts = ts.tz_localize("UTC").tz_convert("Europe/Vilnius")
        updated_str = ts.strftime("%Y-%m-%d %H:%M")
    except Exception:
        updated_str = str(cache_timestamp)[:16]
    status_bar.caption(f"Last updated: {updated_str} | Auto-refresh: 5 min | Press 'R' to refresh")
else:
    status_bar.caption(f"Updated: {pd.Timestamp.now(tz='Europe/Vilnius').strftime('%Y-%m-%d %H:%M')} | Auto-refresh: 5 min | Press 'R' to refresh")

st.markdown("---")

# ============== Overview metrics ==============
total = df[df["is_goal_state"]]["words"].sum()
remaining = GOAL_WORDS - total
progress = total / GOAL_WORDS * 100

# Calculate category breakdowns for overview
df_ready = df[df["is_annotated"]]  # Acceptable + No Rating
df_needs_fixing = df[df["state"] == "ReqAttn (entities)"]

# Calculate pacing estimates
df_pace = df[df["is_goal_state"] & df["date"].notna()].copy()
daily_totals = df_pace.groupby("date")["words"].sum().reset_index()
daily_totals = daily_totals.set_index("date").sort_index()
cumulative_total = daily_totals.cumsum()
cumulative_total = cumulative_total[cumulative_total.index >= pd.Timestamp("2025-12-18")]

if len(cumulative_total) > 0:
    last_date = cumulative_total.index[-1]
    current_total = cumulative_total.iloc[-1]["words"]

    # Calculate rate from last 14 days
    lookback = cumulative_total[cumulative_total.index >= last_date - pd.Timedelta(days=14)]
    if len(lookback) >= 2:
        days = (last_date - lookback.index[0]).days or 1
        rate = (current_total - lookback.iloc[0]["words"]) / days
        days_left = (GOAL_WORDS - current_total) / rate if rate > 0 else 0
        completion = last_date + pd.Timedelta(days=days_left)
        weekly_rate = rate * 7
    else:
        rate = completion = weekly_rate = None
else:
    rate = completion = weekly_rate = None

# Calculate category breakdowns for display
mok_ready = df_ready[df_ready["category"] == "mokslinis"]["words"].sum()
mok_fixing = df_needs_fixing[df_needs_fixing["category"] == "mokslinis"]["words"].sum()
mok_total = mok_ready + mok_fixing
mok_remaining = CATEGORY_GOAL - mok_total

zin_ready = df_ready[df_ready["category"] == "ziniasklaida"]["words"].sum()
zin_fixing = df_needs_fixing[df_needs_fixing["category"] == "ziniasklaida"]["words"].sum()
zin_total = zin_ready + zin_fixing
zin_remaining = CATEGORY_GOAL - zin_total

# Display metrics
col1, col2, col3 = st.columns(3)

# mokslinis category
mok_progress = mok_total / CATEGORY_GOAL * 100
col1.metric("mokslinis", f"{mok_total:,}")
if mok_remaining > 0:
    col1.markdown(f"<small>{mok_progress:.1f}% of 1.1M β€’ {mok_remaining:,} remaining</small>", unsafe_allow_html=True)
else:
    col1.markdown(f"<small>{mok_progress:.1f}% of 1.1M β€’ βœ“ Complete</small>", unsafe_allow_html=True)

# ziniasklaida category
zin_progress = zin_total / CATEGORY_GOAL * 100
col2.metric("ziniasklaida", f"{zin_total:,}")
if zin_remaining > 0:
    col2.markdown(f"<small>{zin_progress:.1f}% of 1.1M β€’ {zin_remaining:,} remaining</small>", unsafe_allow_html=True)
else:
    col2.markdown(f"<small>{zin_progress:.1f}% of 1.1M β€’ βœ“ Complete</small>", unsafe_allow_html=True)

# Completion estimate
if weekly_rate:
    col3.metric("Est. Completion", completion.strftime("%Y-%m-%d"))
    col3.markdown(f"<small>πŸ“Š {int(weekly_rate):,} words/week β€’ {days_left / 7:.1f} weeks left</small>", unsafe_allow_html=True)
else:
    col3.metric("Est. Completion", "N/A")

st.markdown("---")

# ============== Weekly Stats ==============
st.subheader("πŸ“Š Weekly Stats")
st.caption("Goal states (Acceptable + No Rating + ReqAttn with entities)")

cutoff_date = pd.Timestamp("2025-12-22")

# Filter data - use GOAL_STATES to match progress metrics
# Show annotators for our team's projects, "Others" for everything else
df_week = df[df["is_goal_state"] & df["date"].notna()].copy()
df_week["week_start"] = df_week["date"] - pd.to_timedelta(df_week["date"].dt.dayofweek, unit="d")
df_week["member"] = df_week.apply(
    lambda r: (r["annotator"] if r["annotator"] else "Unknown") if r["project_group"] == "Our Team" else "Others",
    axis=1
)

# Weekly pivot (all data)
weekly_all = df_week.pivot_table(index="week_start", columns="member", values="words", aggfunc="sum", fill_value=0).astype(int)

# Split into before and after cutoff
weekly_before = weekly_all[weekly_all.index < cutoff_date]
weekly_after = weekly_all[weekly_all.index >= cutoff_date]

# Ensure consistent columns
all_members = set(weekly_all.columns)
if "Others" not in all_members:
    all_members.add("Others")

for member in all_members:
    if member not in weekly_after.columns:
        weekly_after[member] = 0
    if member not in weekly_before.columns:
        weekly_before[member] = 0

# Sort columns by total contribution
totals = weekly_all.sum().sort_values(ascending=False)
weekly_after = weekly_after[totals.index]
weekly_after["Total"] = weekly_after.sum(axis=1)

# Calculate "Before" summary row
before_totals = weekly_before[totals.index].sum()
before_totals["Total"] = before_totals.sum()

# Format weekly data for display
display = weekly_after.reset_index()
display["Week"] = display["week_start"].dt.strftime("%Y-%m-%d") + " - " + (display["week_start"] + pd.Timedelta(days=6)).dt.strftime("%Y-%m-%d")
display = display.drop("week_start", axis=1)
display = display[["Week"] + list(totals.index) + ["Total"]]

# Add "Before" row at the beginning
before_row = pd.DataFrame([{"Week": f"Before {cutoff_date.strftime('%Y-%m-%d')}", **before_totals}])
display = pd.concat([before_row, display], ignore_index=True)

# Add TOTAL row at the end
all_totals = weekly_all[totals.index].sum()
all_totals["Total"] = all_totals.sum()
total_row = pd.DataFrame([{"Week": "TOTAL", **all_totals}])
display = pd.concat([display, total_row], ignore_index=True)

# Format numbers
for col in display.columns:
    if col != "Week":
        display[col] = display[col].apply(lambda x: f"{int(x):,}" if pd.notna(x) else "")

# Style and show
def style_row(row):
    if row["Week"] == "TOTAL":
        return ["font-weight: bold; background-color: #f0f0f0;"] * len(row)
    elif row["Week"].startswith("Before"):
        return ["font-style: italic; background-color: #f9f9f9;"] * len(row)
    return [""] * len(row)

styled = display.style.apply(style_row, axis=1).set_properties(subset=["Total"], **{"font-weight": "bold"})
st.dataframe(styled, hide_index=True, use_container_width=True, height='content')

st.markdown("---")

# ============== Category Breakdown ==============
st.subheader("πŸ“ˆ Category Breakdown")
st.caption("Requirement: 1.1M words from each category")

# df_ready and df_needs_fixing already defined in overview section
df_total = df[df["is_goal_state"]]

# Calculate by category
mok_ready = df_ready[df_ready["category"] == "mokslinis"]["words"].sum()
mok_fixing = df_needs_fixing[df_needs_fixing["category"] == "mokslinis"]["words"].sum()
mok_total = mok_ready + mok_fixing

zin_ready = df_ready[df_ready["category"] == "ziniasklaida"]["words"].sum()
zin_fixing = df_needs_fixing[df_needs_fixing["category"] == "ziniasklaida"]["words"].sum()
zin_total = zin_ready + zin_fixing

total_ready = mok_ready + zin_ready
total_fixing = mok_fixing + zin_fixing
total_all = total_ready + total_fixing

cat_df = pd.DataFrame(
    {
        "Category": ["mokslinis", "ziniasklaida"],
        "Ready": [f"{mok_ready:,}", f"{zin_ready:,}"],
        "Needs Fixing": [f"{mok_fixing:,}", f"{zin_fixing:,}"],
        "Total": [f"{mok_total:,}", f"{zin_total:,}"],
        "Goal": [f"{CATEGORY_GOAL:,}", f"{CATEGORY_GOAL:,}"],
        "Progress": [
            f"{mok_total / CATEGORY_GOAL * 100:.1f}%",
            f"{zin_total / CATEGORY_GOAL * 100:.1f}%",
        ],
    }
)
st.dataframe(cat_df, hide_index=True, use_container_width=True, height='content')

st.markdown("---")

# ============== Cumulative Progress ==============
st.subheader("πŸ“Š Cumulative Progress & Projection")

# Cumulative data - show by annotator for our team, "Others" for rest
df_cum = df[df["is_goal_state"] & df["date"].notna()].copy()
df_cum["member"] = df_cum.apply(
    lambda r: (r["annotator"] if r["annotator"] else "Unknown") if r["project_group"] == "Our Team" else "Others",
    axis=1
)

daily = df_cum.groupby(["date", "member"])["words"].sum().reset_index()
pivot = daily.pivot_table(index="date", columns="member", values="words", fill_value=0)
cumulative = pivot.sort_index().cumsum()
cumulative["Total"] = cumulative.sum(axis=1)
cumulative = cumulative[cumulative.index >= pd.Timestamp("2025-12-18")]

# Projection calculation
last_date = cumulative.index[-1]
current = cumulative["Total"].iloc[-1]

# Calculate rate from last 14 days
lookback = cumulative[cumulative.index >= last_date - pd.Timedelta(days=14)]
if len(lookback) >= 2:
    days = (last_date - lookback.index[0]).days or 1
    rate = (current - lookback["Total"].iloc[0]) / days
    days_left = (GOAL_WORDS - current) / rate if rate > 0 else 0
    completion = last_date + pd.Timedelta(days=days_left)
    weekly_rate = rate * 7
else:
    rate = completion = weekly_rate = None

# Chart
fig = go.Figure()

# Goal lines
fig.add_hline(y=1_100_000, line_dash="dot", line_color="orange", annotation_text="Midpoint: 1.1M", annotation_position="top left")
fig.add_hline(y=GOAL_WORDS, line_dash="dot", line_color="red", annotation_text="Goal: 2.2M", annotation_position="top left")

# Members
members = [c for c in cumulative.columns if c not in ["Total", "Others"]]
members = sorted(members, key=lambda x: cumulative[x].iloc[-1], reverse=True)

if "Others" in cumulative.columns:
    fig.add_trace(
        go.Scatter(
            x=cumulative.index,
            y=cumulative["Others"],
            name=f"Others: {cumulative['Others'].iloc[-1]:,.0f}",
            mode="lines",
            line=dict(width=2, color="#7f8c8d"),
        )
    )

for m in members:
    color = COLORS_BY_NAME.get(m, "#34495e")
    fig.add_trace(
        go.Scatter(x=cumulative.index, y=cumulative[m], name=f"{m}: {cumulative[m].iloc[-1]:,.0f}", mode="lines", line=dict(width=2, color=color))
    )

# Total
fig.add_trace(
    go.Scatter(
        x=cumulative.index,
        y=cumulative["Total"],
        name=f"Total: {cumulative['Total'].iloc[-1]:,.0f}",
        mode="lines",
        line=dict(width=3, color="#d4af37"),
        fill="tozeroy",
        fillcolor="rgba(212, 175, 55, 0.1)",
    )
)

# Projection
if completion:
    proj_dates = pd.date_range(last_date, completion, freq="D")
    proj_vals = current + rate * (proj_dates - last_date).days
    fig.add_trace(
        go.Scatter(
            x=proj_dates, y=proj_vals, name=f"Projection ({int(weekly_rate):,}/wk)", mode="lines", line=dict(width=3, color="#d4af37", dash="dot")
        )
    )
    fig.add_trace(
        go.Scatter(
            x=[completion],
            y=[GOAL_WORDS],
            mode="markers+text",
            marker=dict(size=14, color="#d4af37", symbol="diamond"),
            text=[completion.strftime("%b %d")],
            textposition="top center",
            showlegend=False,
        )
    )
    title = f"Cumulative Progress β†’ Est. {completion.strftime('%B %d, %Y')}"
else:
    title = "Cumulative Progress"

fig.update_layout(title=title, xaxis_title="Date", yaxis_title="Cumulative Words", height=600, hovermode="x unified", template="plotly_white")
fig.update_yaxes(tickformat=".2s")

st.plotly_chart(fig, use_container_width=True)