File size: 5,535 Bytes
bb2d919
68baa23
 
bb2d919
 
 
 
 
 
 
 
 
 
acfb514
 
 
 
 
 
 
68baa23
acfb514
 
bb2d919
 
 
68baa23
bb2d919
 
 
 
68baa23
bb2d919
 
 
68baa23
bb2d919
 
 
 
 
 
68baa23
bb2d919
68baa23
 
 
 
 
 
 
 
 
 
bb2d919
 
 
 
 
 
 
 
 
 
 
68baa23
bb2d919
 
 
 
 
 
 
 
68baa23
bb2d919
68baa23
 
 
 
 
 
 
bb2d919
68baa23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb2d919
 
 
 
 
acfb514
bb2d919
 
 
 
 
 
 
acfb514
bb2d919
 
 
 
 
 
 
 
 
 
 
 
acfb514
bb2d919
 
 
 
 
 
 
acfb514
bb2d919
 
 
 
 
 
 
 
 
 
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
# ─────────────────────────────────────────────────────────────────────────────
# TRINETRA - Database Layer v2
# Queries planet_candidates_v2 β€” two-layer architecture enforced
# ─────────────────────────────────────────────────────────────────────────────

import os
from supabase import create_client, Client
from typing import Optional, List, Dict, Any


class TrinetraDB:

    def __init__(self):
        self._client: Optional[Client] = None

    def _get_client(self) -> Client:
        if self._client is None:
            url = os.environ.get("SUPABASE_URL", "").strip()
            key = os.environ.get("SUPABASE_KEY", "").strip()
            if not url or not key:
                raise ValueError("SUPABASE_URL and SUPABASE_KEY not set.")
            self._client = create_client(url, key)
        return self._client

    async def ping(self) -> bool:
        try:
            self._get_client().table("planet_candidates_v2").select("id").limit(1).execute()
            return True
        except Exception:
            return False

    # ── planet_candidates_v2 ──────────────────────────────────────────────────

    async def get_all_planets(self) -> List[Dict[str, Any]]:
        try:
            result = self._get_client().table("planet_candidates_v2").select("*").execute()
            return result.data or []
        except Exception:
            return []

    async def get_planet_by_id(self, star_id: str) -> Optional[Dict[str, Any]]:
        try:
            # Try planet_name first (Kepler-22b), then star_id (Kepler-22)
            result = (
                self._get_client().table("planet_candidates_v2")
                .select("*")
                .eq("planet_name", star_id)
                .execute()
            )
            if result.data:
                return result.data[0]
            # Fallback to star_id
            result = (
                self._get_client().table("planet_candidates_v2")
                .select("*")
                .eq("star_id", star_id)
                .execute()
            )
            return result.data[0] if result.data else None
        except Exception:
            return None

    async def get_planets_by_tier(self, tier: str) -> List[Dict[str, Any]]:
        try:
            result = (
                self._get_client().table("planet_candidates_v2")
                .select("*")
                .eq("habitability_tier", tier)
                .execute()
            )
            return result.data or []
        except Exception:
            return []

    async def get_planets_by_detection_status(self, status: str) -> List[Dict[str, Any]]:
        try:
            result = (
                self._get_client().table("planet_candidates_v2")
                .select("*")
                .eq("detection_status", status)
                .execute()
            )
            return result.data or []
        except Exception:
            return []

    async def get_validated_planets(self) -> List[Dict[str, Any]]:
        """Returns only planets that passed the detection gate."""
        try:
            result = (
                self._get_client().table("planet_candidates_v2")
                .select("*")
                .neq("detection_status", "FALSE_POSITIVE")
                .neq("habitability_tier", "NOT_EVALUATED")
                .execute()
            )
            return result.data or []
        except Exception:
            return []

    # ── processed_stars ──────────────────────────────────────────────────────

    async def get_all_stars(self) -> List[Dict[str, Any]]:
        try:
            result = self._get_client().table("processed_stars").select("*").execute()
            return result.data or []
        except Exception:
            return []

    async def get_star_by_id(self, star_id: str) -> Optional[Dict[str, Any]]:
        try:
            result = (
                self._get_client().table("processed_stars")
                .select("*")
                .eq("star_id", star_id)
                .execute()
            )
            return result.data[0] if result.data else None
        except Exception:
            return None

    # ── tce_candidates ───────────────────────────────────────────────────────

    async def get_all_tce(self) -> List[Dict[str, Any]]:
        try:
            result = self._get_client().table("tce_candidates").select("*").execute()
            return result.data or []
        except Exception:
            return []

    async def get_tce_by_id(self, star_id: str) -> Optional[Dict[str, Any]]:
        try:
            result = (
                self._get_client().table("tce_candidates")
                .select("*")
                .eq("star_id", star_id)
                .execute()
            )
            return result.data[0] if result.data else None
        except Exception:
            return None


db = TrinetraDB()