File size: 3,185 Bytes
9d57416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Root/child batches: membership and per-segment rows read from each import summary (review-import.json)."""
from pathlib import PurePosixPath
from .errors import require
from .display_contract import METRIC_UNITS, progress

# Fields every import summary carries (segment_conversion writes them); a batch row shows exactly these.
ROW_FIELDS = ('world_id', 'duration_tier', 'duration_s', 'actor_count', 'actor_counts', 'intent_counts', 'generator_version')


class Batches:
    def __init__(self, registry):
        self.registry, self.members = registry, {}
        self.refresh()

    def refresh(self):
        members = {}
        for entry in self.registry.entries.values():
            alias, relative = entry.id.split('/', 1)
            parts = PurePosixPath(relative).parts
            # Direct segments belong to the root batch; nested repro/other
            # collections remain separate child batches, without duplication.
            batch = alias if len(parts) <= 1 else alias + '/' + parts[0]
            members.setdefault(batch, []).append(entry.id)
        for alias in self.registry.batch_failures:
            members.setdefault(alias, [])
        for batch, ids in getattr(self.registry, 'render_batches', {}).items():
            require(batch not in members, 'MEDIA_INDEX_INVALID', '渲染批次与剧本批次重名')
            members[batch] = list(ids)
        self.members = members

    def entries(self, batch_id):
        require(batch_id in self.members, 'BATCH_NOT_FOUND', '批号未登记')
        return [self.registry.entries[id] for id in self.members[batch_id]]

    def listing(self):
        return [{'id': id, 'segment_count': len(ids) + len(self.registry.batch_failures.get(id, [])),
                 'progress': self.view(id)['progress'],   # Import summaries are read, not computed: same counts as the batch page
                 'generated_count': len(ids), 'not_generated_count': len(self.registry.batch_failures.get(id, [])),
                 'root': id.split('/')[0]} for id, ids in sorted(self.members.items())]

    def view(self, batch_id, filters=None):
        rows = [{'id': entry.id, **{k: entry.summary.get(k) for k in ROW_FIELDS},
                 'summary_status': 'failed' if entry.summary.get('metadata_error') else 'ready',
                 'error': entry.summary.get('metadata_error')} for entry in self.entries(batch_id)]
        rows.extend(self.registry.batch_failures.get(batch_id, []))
        filters = filters or {}
        filtered = [r for r in rows if (not filters.get('world') or r.get('world_id') == filters['world'])
                    and (not filters.get('status') or r['summary_status'] == filters['status'])
                    and (not filters.get('query') or filters['query'] in r['id'])]
        return {'schema': 'batch-view/2', 'id': batch_id, 'rows': filtered, 'metric_units': METRIC_UNITS,
                'filtered_progress': progress(filtered), 'progress': progress(rows),
                'source_index': self.registry.index_sources.get(batch_id), 'source_note': self.registry.root_notes.get(batch_id),
                'worlds': sorted({r.get('world_id') for r in rows if r.get('world_id')})}