cryogenic22 commited on
Commit
d419ea2
·
verified ·
1 Parent(s): 58d467a

Create data/schema.py

Browse files
Files changed (1) hide show
  1. data/schema.py +259 -0
data/schema.py ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Pharmaceutical Analytics Database Schema
3
+
4
+ This module defines the SQLite database schema for the pharmaceutical analytics demo.
5
+ It includes tables for sales data, products, regions, territories, prescribers,
6
+ pharmacies, marketing campaigns, and other relevant entities.
7
+ """
8
+
9
+ import sqlite3
10
+ from typing import Dict, List, Any
11
+
12
+ # SQLite database schema definitions
13
+ SCHEMA_DEFINITIONS = {
14
+ "regions": """
15
+ CREATE TABLE IF NOT EXISTS regions (
16
+ region_id TEXT PRIMARY KEY,
17
+ region_name TEXT NOT NULL,
18
+ country TEXT NOT NULL,
19
+ division TEXT NOT NULL,
20
+ population INTEGER NOT NULL
21
+ )
22
+ """,
23
+
24
+ "territories": """
25
+ CREATE TABLE IF NOT EXISTS territories (
26
+ territory_id TEXT PRIMARY KEY,
27
+ territory_name TEXT NOT NULL,
28
+ region_id TEXT NOT NULL,
29
+ sales_rep_id TEXT NOT NULL,
30
+ FOREIGN KEY (region_id) REFERENCES regions (region_id)
31
+ )
32
+ """,
33
+
34
+ "products": """
35
+ CREATE TABLE IF NOT EXISTS products (
36
+ product_id TEXT PRIMARY KEY,
37
+ product_name TEXT NOT NULL,
38
+ therapeutic_area TEXT NOT NULL,
39
+ molecule TEXT NOT NULL,
40
+ launch_date DATE NOT NULL,
41
+ status TEXT NOT NULL,
42
+ list_price REAL NOT NULL
43
+ )
44
+ """,
45
+
46
+ "competitor_products": """
47
+ CREATE TABLE IF NOT EXISTS competitor_products (
48
+ competitor_product_id TEXT PRIMARY KEY,
49
+ product_name TEXT NOT NULL,
50
+ manufacturer TEXT NOT NULL,
51
+ therapeutic_area TEXT NOT NULL,
52
+ molecule TEXT NOT NULL,
53
+ launch_date DATE NOT NULL,
54
+ list_price REAL NOT NULL,
55
+ competing_with_product_id TEXT NOT NULL,
56
+ FOREIGN KEY (competing_with_product_id) REFERENCES products (product_id)
57
+ )
58
+ """,
59
+
60
+ "prescribers": """
61
+ CREATE TABLE IF NOT EXISTS prescribers (
62
+ prescriber_id TEXT PRIMARY KEY,
63
+ name TEXT NOT NULL,
64
+ specialty TEXT NOT NULL,
65
+ practice_type TEXT NOT NULL,
66
+ territory_id TEXT NOT NULL,
67
+ decile INTEGER NOT NULL,
68
+ FOREIGN KEY (territory_id) REFERENCES territories (territory_id)
69
+ )
70
+ """,
71
+
72
+ "pharmacies": """
73
+ CREATE TABLE IF NOT EXISTS pharmacies (
74
+ pharmacy_id TEXT PRIMARY KEY,
75
+ name TEXT NOT NULL,
76
+ address TEXT NOT NULL,
77
+ territory_id TEXT NOT NULL,
78
+ pharmacy_type TEXT NOT NULL,
79
+ monthly_rx_volume INTEGER NOT NULL,
80
+ FOREIGN KEY (territory_id) REFERENCES territories (territory_id)
81
+ )
82
+ """,
83
+
84
+ "distribution_centers": """
85
+ CREATE TABLE IF NOT EXISTS distribution_centers (
86
+ dc_id TEXT PRIMARY KEY,
87
+ dc_name TEXT NOT NULL,
88
+ region_id TEXT NOT NULL,
89
+ inventory_capacity INTEGER NOT NULL,
90
+ FOREIGN KEY (region_id) REFERENCES regions (region_id)
91
+ )
92
+ """,
93
+
94
+ "sales": """
95
+ CREATE TABLE IF NOT EXISTS sales (
96
+ sale_id INTEGER PRIMARY KEY,
97
+ sale_date DATE NOT NULL,
98
+ product_id TEXT NOT NULL,
99
+ region_id TEXT NOT NULL,
100
+ territory_id TEXT NOT NULL,
101
+ prescriber_id TEXT NOT NULL,
102
+ pharmacy_id TEXT NOT NULL,
103
+ units_sold INTEGER NOT NULL,
104
+ revenue REAL NOT NULL,
105
+ cost REAL NOT NULL,
106
+ margin REAL NOT NULL,
107
+ FOREIGN KEY (product_id) REFERENCES products (product_id),
108
+ FOREIGN KEY (region_id) REFERENCES regions (region_id),
109
+ FOREIGN KEY (territory_id) REFERENCES territories (territory_id),
110
+ FOREIGN KEY (prescriber_id) REFERENCES prescribers (prescriber_id),
111
+ FOREIGN KEY (pharmacy_id) REFERENCES pharmacies (pharmacy_id)
112
+ )
113
+ """,
114
+
115
+ "marketing_campaigns": """
116
+ CREATE TABLE IF NOT EXISTS marketing_campaigns (
117
+ campaign_id INTEGER PRIMARY KEY,
118
+ campaign_name TEXT NOT NULL,
119
+ start_date DATE NOT NULL,
120
+ end_date DATE NOT NULL,
121
+ product_id TEXT NOT NULL,
122
+ campaign_type TEXT NOT NULL,
123
+ target_audience TEXT NOT NULL,
124
+ channels TEXT NOT NULL,
125
+ budget REAL NOT NULL,
126
+ spend REAL NOT NULL,
127
+ FOREIGN KEY (product_id) REFERENCES products (product_id)
128
+ )
129
+ """,
130
+
131
+ "market_events": """
132
+ CREATE TABLE IF NOT EXISTS market_events (
133
+ event_id INTEGER PRIMARY KEY,
134
+ event_date DATE NOT NULL,
135
+ event_type TEXT NOT NULL,
136
+ description TEXT NOT NULL,
137
+ affected_products TEXT NOT NULL,
138
+ affected_regions TEXT NOT NULL,
139
+ impact_score REAL NOT NULL
140
+ )
141
+ """,
142
+
143
+ "sales_targets": """
144
+ CREATE TABLE IF NOT EXISTS sales_targets (
145
+ target_id INTEGER PRIMARY KEY,
146
+ product_id TEXT NOT NULL,
147
+ region_id TEXT NOT NULL,
148
+ period TEXT NOT NULL,
149
+ target_units REAL NOT NULL,
150
+ target_revenue REAL NOT NULL,
151
+ FOREIGN KEY (product_id) REFERENCES products (product_id),
152
+ FOREIGN KEY (region_id) REFERENCES regions (region_id)
153
+ )
154
+ """,
155
+
156
+ "inventory": """
157
+ CREATE TABLE IF NOT EXISTS inventory (
158
+ inventory_id INTEGER PRIMARY KEY,
159
+ product_id TEXT NOT NULL,
160
+ dc_id TEXT NOT NULL,
161
+ date DATE NOT NULL,
162
+ units_available INTEGER NOT NULL,
163
+ units_allocated INTEGER NOT NULL,
164
+ units_in_transit INTEGER NOT NULL,
165
+ days_of_supply REAL NOT NULL,
166
+ FOREIGN KEY (product_id) REFERENCES products (product_id),
167
+ FOREIGN KEY (dc_id) REFERENCES distribution_centers (dc_id)
168
+ )
169
+ """,
170
+
171
+ "external_factors": """
172
+ CREATE TABLE IF NOT EXISTS external_factors (
173
+ factor_id INTEGER PRIMARY KEY,
174
+ date DATE NOT NULL,
175
+ region_id TEXT NOT NULL,
176
+ factor_type TEXT NOT NULL,
177
+ factor_value REAL NOT NULL,
178
+ description TEXT NOT NULL,
179
+ FOREIGN KEY (region_id) REFERENCES regions (region_id)
180
+ )
181
+ """
182
+ }
183
+
184
+ # Indexes for optimizing queries
185
+ INDEX_DEFINITIONS = [
186
+ "CREATE INDEX IF NOT EXISTS idx_sales_date ON sales (sale_date)",
187
+ "CREATE INDEX IF NOT EXISTS idx_sales_product ON sales (product_id)",
188
+ "CREATE INDEX IF NOT EXISTS idx_sales_region ON sales (region_id)",
189
+ "CREATE INDEX IF NOT EXISTS idx_sales_territory ON sales (territory_id)",
190
+ "CREATE INDEX IF NOT EXISTS idx_inventory_date ON inventory (date)",
191
+ "CREATE INDEX IF NOT EXISTS idx_inventory_product ON inventory (product_id)",
192
+ "CREATE INDEX IF NOT EXISTS idx_marketing_dates ON marketing_campaigns (start_date, end_date)",
193
+ "CREATE INDEX IF NOT EXISTS idx_external_factors_date ON external_factors (date)",
194
+ "CREATE INDEX IF NOT EXISTS idx_external_factors_region ON external_factors (region_id)"
195
+ ]
196
+
197
+ def create_schema(db_path: str = "data/pharma_db.sqlite") -> None:
198
+ """Create database schema in the SQLite database"""
199
+ conn = sqlite3.connect(db_path)
200
+ cursor = conn.cursor()
201
+
202
+ # Create tables
203
+ for table_name, schema_sql in SCHEMA_DEFINITIONS.items():
204
+ cursor.execute(schema_sql)
205
+
206
+ # Create indexes
207
+ for index_sql in INDEX_DEFINITIONS:
208
+ cursor.execute(index_sql)
209
+
210
+ conn.commit()
211
+ conn.close()
212
+
213
+ print(f"Database schema created in {db_path}")
214
+
215
+ def get_table_info(db_path: str = "data/pharma_db.sqlite") -> Dict[str, List[Dict[str, Any]]]:
216
+ """Get information about all tables in the database"""
217
+ conn = sqlite3.connect(db_path)
218
+ cursor = conn.cursor()
219
+
220
+ # Get list of tables
221
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
222
+ tables = cursor.fetchall()
223
+
224
+ table_info = {}
225
+
226
+ for table in tables:
227
+ table_name = table[0]
228
+
229
+ # Get column information
230
+ cursor.execute(f"PRAGMA table_info({table_name})")
231
+ columns = cursor.fetchall()
232
+
233
+ # Format column information
234
+ column_info = []
235
+ for col in columns:
236
+ column_info.append({
237
+ "name": col[1],
238
+ "type": col[2],
239
+ "notnull": col[3],
240
+ "default": col[4],
241
+ "primary_key": col[5]
242
+ })
243
+
244
+ table_info[table_name] = column_info
245
+
246
+ conn.close()
247
+ return table_info
248
+
249
+ if __name__ == "__main__":
250
+ # Create the database schema when run directly
251
+ create_schema()
252
+
253
+ # Print table information
254
+ table_info = get_table_info()
255
+ for table_name, columns in table_info.items():
256
+ print(f"Table: {table_name}")
257
+ for col in columns:
258
+ print(f" - {col['name']} ({col['type']})")
259
+ print()