Coverage for app / services / knowledge / web_as_of.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1""" 

2A website source as one of its crawls left it, compared with now. 

3 

4A crawl writes a page version only when the page's text changed, so the 

5pages list (each page's newest version) cannot say what the source held at 

6an earlier crawl. A site whose crawls keep finding new URLs (a news section, 

7a listing) mostly shows pages at version 1. This module answers the question 

8from the versions themselves: each one names the crawl that wrote it, so the 

9version *in force after* a crawl is the newest one that crawl or an earlier 

10one wrote. 

11 

12For every stored page this gives: 

13 

14* ``then``: the version in force after the crawl, or none if the page was 

15 first stored later; 

16* ``now``: the newest version; 

17* ``written``: what the crawl itself did to the page, ``new`` (its first 

18 version) or ``changed``, or nothing; 

19* ``since``: how ``now`` differs from ``then``: ``added``, ``changed`` or 

20 ``unchanged``. 

21 

22Pages a source's ``max_age_days`` window removed since are gone with their 

23versions. They can be counted (each crawl records ``pages_expired``) but not 

24shown. Reads stored rows only; fetches nothing. 

25""" 

26 

27from __future__ import annotations 

28 

29from collections import defaultdict 

30from collections.abc import Iterable 

31from dataclasses import dataclass 

32from enum import Enum 

33from typing import Any 

34 

35from app.db.postgres.models import ( 

36 KnowledgeCrawl, 

37 KnowledgeDocument, 

38 KnowledgePageVersion, 

39) 

40from app.util.time import as_utc 

41 

42 

43class Written(str, Enum): 

44 """What a crawl did to a page.""" 

45 

46 new = "new" 

47 changed = "changed" 

48 

49 

50class Since(str, Enum): 

51 """How a page's newest version differs from its version after a crawl.""" 

52 

53 added = "added" 

54 changed = "changed" 

55 unchanged = "unchanged" 

56 

57 

58@dataclass 

59class PageAsOf: 

60 doc: KnowledgeDocument 

61 then: KnowledgePageVersion | None 

62 now: KnowledgePageVersion 

63 written: Written | None 

64 since: Since 

65 

66 

67def _in_force_after(version: KnowledgePageVersion, crawl: KnowledgeCrawl) -> bool: 

68 """Whether ``crawl`` or an earlier one wrote ``version``. 

69 

70 Only one crawl of a source runs at a time, so another crawl's version 

71 fetched before this one started was an earlier crawl's. A version that 

72 names no crawl (its row deleted) is placed by time alone: fetched before 

73 this crawl finished, or at all while it is still running. 

74 """ 

75 fetched = as_utc(version.fetched_at) 

76 if version.crawl_id is not None: 

77 return version.crawl_id == crawl.id or fetched < as_utc(crawl.started_at) 

78 return crawl.finished_at is None or fetched <= as_utc(crawl.finished_at) 

79 

80 

81def pages_as_of( 

82 crawl: KnowledgeCrawl, 

83 docs: Iterable[KnowledgeDocument], 

84 versions: Iterable[KnowledgePageVersion], 

85) -> list[PageAsOf]: 

86 """Every stored page with a version, as ``crawl`` left it and as it is 

87 now, in URL order. A page with no version at all (only ever failed) has 

88 nothing to show and is left out.""" 

89 by_doc: dict[Any, list[KnowledgePageVersion]] = defaultdict(list) 

90 for version in versions: 

91 by_doc[version.document_id].append(version) 

92 

93 pages: list[PageAsOf] = [] 

94 for doc in sorted(docs, key=lambda d: d.source_id or ""): 

95 mine = by_doc.get(doc.id) 

96 if not mine: 

97 continue 

98 now = max(mine, key=lambda v: v.version_number) 

99 earlier = [v for v in mine if _in_force_after(v, crawl)] 

100 then = max(earlier, key=lambda v: v.version_number) if earlier else None 

101 wrote = next( 

102 (v for v in mine if v.crawl_id is not None and v.crawl_id == crawl.id), 

103 None, 

104 ) 

105 written: Written | None = None 

106 if wrote is not None: 

107 written = Written.new if wrote.version_number == 1 else Written.changed 

108 if then is None: 

109 since = Since.added 

110 elif then.id == now.id: 

111 since = Since.unchanged 

112 else: 

113 since = Since.changed 

114 pages.append(PageAsOf(doc, then, now, written, since)) 

115 return pages