Coverage for app/backend/src/couchers/models/community_checks.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-23 10:14 +0000

1import enum 

2from datetime import datetime 

3 

4from sqlalchemy import BigInteger, DateTime, Enum, Float, ForeignKey, Index, Integer, String, func 

5from sqlalchemy.orm import Mapped, mapped_column 

6 

7from couchers.models.base import Base 

8 

9 

10class CommunityIssuePriority(enum.Enum): 

11 high = enum.auto() 

12 medium = enum.auto() 

13 low = enum.auto() 

14 

15 

16class CommunityIssue(Base, kw_only=True): 

17 """ 

18 A way in which a community breaks one of the rules in couchers.helpers.community_checks 

19 

20 An issue stays open while every run of the checks finds it, and is resolved by the first run that does not. 

21 """ 

22 

23 __tablename__ = "community_issues" 

24 

25 id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) 

26 rule: Mapped[str] = mapped_column(String) 

27 priority: Mapped[CommunityIssuePriority] = mapped_column(Enum(CommunityIssuePriority)) 

28 node_id: Mapped[int] = mapped_column(ForeignKey("nodes.id", ondelete="CASCADE"), index=True) 

29 # the parent, the community it overlaps, or the earlier copy, depending on the rule 

30 other_node_id: Mapped[int | None] = mapped_column(ForeignKey("nodes.id", ondelete="CASCADE"), default=None) 

31 area_km2: Mapped[float | None] = mapped_column(Float, default=None) 

32 share: Mapped[float | None] = mapped_column(Float, default=None) 

33 details: Mapped[str] = mapped_column(String) 

34 # the area outside the parent, or the overlap, for drawing on a map 

35 area_geojson: Mapped[str | None] = mapped_column(String, default=None) 

36 first_seen: Mapped[datetime] = mapped_column(DateTime(timezone=True)) 

37 last_seen: Mapped[datetime] = mapped_column(DateTime(timezone=True)) 

38 resolved: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

39 

40 __table_args__ = ( 

41 Index( 

42 "ix_community_issues_open", 

43 rule, 

44 node_id, 

45 func.coalesce(other_node_id, 0), 

46 unique=True, 

47 postgresql_where=resolved == None, 

48 ), 

49 Index("ix_community_issues_resolved", resolved), 

50 ) 

51 

52 

53class CommunityCheckRun(Base, kw_only=True): 

54 __tablename__ = "community_check_runs" 

55 

56 id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) 

57 time: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) 

58 open_issues: Mapped[int] = mapped_column(Integer) 

59 active_users: Mapped[int] = mapped_column(Integer) 

60 # users active in the last 90 days whose location is outside every locality 

61 active_users_outside_localities: Mapped[int] = mapped_column(Integer)