-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
213 lines (178 loc) · 8.37 KB
/
Copy pathmodels.py
File metadata and controls
213 lines (178 loc) · 8.37 KB
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
"""SQLAlchemy 2.0 ORM models for the Parsify database schema.
Defines the four core tables: users, api_keys, jobs, usage_records.
All timestamps are timezone-aware. JSON columns use PostgreSQL JSONB.
"""
from __future__ import annotations
import logging
from datetime import datetime
from typing import Any, Optional
from sqlalchemy import (
Boolean,
DateTime,
Float,
Index,
Integer,
String,
Text,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
logger = logging.getLogger(__name__)
class Base(DeclarativeBase):
"""Declarative base for all ORM models."""
class User(Base):
"""Registered Parsify user.
Attributes:
id: Auto-incrementing surrogate primary key.
user_id: Public stable identifier (UUID string).
email: User's email address — unique.
clerk_user_id: Clerk authentication user ID — unique when set.
name: Optional display name.
tier: Billing tier — "free", "pro", or "enterprise".
stripe_customer_id: Stripe customer identifier.
stripe_subscription_id: Active Stripe subscription identifier.
created_at: UTC timestamp when the record was inserted.
"""
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
email: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
clerk_user_id: Mapped[Optional[str]] = mapped_column(
String(64), nullable=True, unique=True
)
name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
tier: Mapped[str] = mapped_column(String(32), nullable=False, default="free")
stripe_customer_id: Mapped[Optional[str]] = mapped_column(
String(64), nullable=True
)
stripe_subscription_id: Mapped[Optional[str]] = mapped_column(
String(64), nullable=True
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
__table_args__ = (
Index("ix_users_user_id", "user_id"),
Index("ix_users_email", "email"),
Index("ix_users_clerk_user_id", "clerk_user_id"),
)
def __repr__(self) -> str:
return f"<User user_id={self.user_id!r} email={self.email!r} tier={self.tier!r}>"
class APIKey(Base):
"""Hashed API key associated with a user.
Attributes:
id: Auto-incrementing surrogate primary key.
key_id: Public stable identifier for the key.
user_id: Foreign-key reference to users.user_id.
name: Human-readable label for the key.
prefix: Visible prefix shown in listings (e.g. "pk_xxxxxxxx").
key_hash: bcrypt/SHA-256 hash of the full key — never the raw value.
is_active: Whether the key can authenticate requests.
created_at: UTC timestamp when the record was inserted.
"""
__tablename__ = "api_keys"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
key_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
user_id: Mapped[str] = mapped_column(String(64), nullable=False)
name: Mapped[str] = mapped_column(String(100), nullable=False)
prefix: Mapped[str] = mapped_column(String(32), nullable=False)
key_hash: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
__table_args__ = (
Index("ix_api_keys_key_id", "key_id"),
Index("ix_api_keys_user_id", "user_id"),
Index("ix_api_keys_key_hash", "key_hash"),
)
def __repr__(self) -> str:
return f"<APIKey key_id={self.key_id!r} name={self.name!r} active={self.is_active!r}>"
class Job(Base):
"""Scrape or process job submitted through the API.
Attributes:
id: Auto-incrementing surrogate primary key.
job_id: Public stable identifier (UUID string).
status: Current state — "pending", "running", "completed", "failed", "cancelled".
url: Target URL for scrape jobs.
job_type: "scrape" or "process".
max_pages: Page limit configured at submission time.
output_format: Requested output format.
webhook_url: Optional callback URL for completion events.
created_at: UTC timestamp when the job was submitted.
started_at: UTC timestamp when execution began.
completed_at: UTC timestamp when execution finished.
pages_scraped: Count of successfully scraped pages.
pages_failed: Count of pages that encountered errors.
progress: Completion percentage (0.0–100.0).
error_message: Human-readable error if the job failed.
output_files: JSONB list of output file paths/URLs.
summary: JSONB arbitrary result metadata.
owner_key_id: API key that submitted the job.
"""
__tablename__ = "jobs"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
job_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending")
url: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
job_type: Mapped[str] = mapped_column(String(32), nullable=False)
max_pages: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
output_format: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
webhook_url: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
started_at: Mapped[Optional[datetime]] = mapped_column(
DateTime(timezone=True), nullable=True
)
completed_at: Mapped[Optional[datetime]] = mapped_column(
DateTime(timezone=True), nullable=True
)
pages_scraped: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
pages_failed: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
progress: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
output_files: Mapped[Optional[Any]] = mapped_column(JSONB, nullable=True)
summary: Mapped[Optional[Any]] = mapped_column(JSONB, nullable=True)
owner_key_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
__table_args__ = (
Index("ix_jobs_job_id", "job_id"),
Index("ix_jobs_owner_key_id", "owner_key_id"),
Index("ix_jobs_owner_key_id_status", "owner_key_id", "status"),
)
def __repr__(self) -> str:
return (
f"<Job job_id={self.job_id!r} status={self.status!r} type={self.job_type!r}>"
)
class UsageRecord(Base):
"""Single API usage event for billing and analytics.
Attributes:
id: Auto-incrementing surrogate primary key.
timestamp: UTC time when the request was received.
endpoint: API path that was called.
method: HTTP method (GET, POST, etc.).
status_code: HTTP response status code.
response_time_ms: Request processing time in milliseconds.
key_id: API key that made the request.
pages_count: Pages consumed by this request (0 for non-scrape calls).
"""
__tablename__ = "usage_records"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
endpoint: Mapped[str] = mapped_column(String(255), nullable=False)
method: Mapped[str] = mapped_column(String(10), nullable=False)
status_code: Mapped[int] = mapped_column(Integer, nullable=False)
response_time_ms: Mapped[float] = mapped_column(Float, nullable=False)
key_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
pages_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
__table_args__ = (
Index("ix_usage_records_key_id", "key_id"),
Index("ix_usage_records_key_id_timestamp", "key_id", "timestamp"),
)
def __repr__(self) -> str:
return (
f"<UsageRecord key_id={self.key_id!r} endpoint={self.endpoint!r} "
f"status={self.status_code!r}>"
)