Skip to content

Commit dd6a132

Browse files
authored
Merge pull request #29 from fact-project/factdb
Add factdb peewee models
2 parents abc3bab + 6188fb5 commit dd6a132

File tree

6 files changed

+874
-0
lines changed

6 files changed

+874
-0
lines changed

README.rst

+31
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,37 @@ source file:
7575
7676
pixel_x, pixel_y = get_pixel_coords()
7777
78+
factdb
79+
------
80+
81+
This module contains ``peewee`` ``Models`` for our ``factdata`` MySQL database.
82+
These were automatically created by ``peewee`` and provide means to query this database in python without writing raw sql queries.
83+
84+
For example, to get the total number of runs take by FACT you can do:
85+
86+
.. code:: python
87+
88+
from fact.factdb import connect_database, RunInfo
89+
90+
connect_database() # this uses the credentials module if no config is given
91+
92+
num_runs = RunInfo.select().count()
93+
94+
A few convenience functions are already implemented.
95+
To get a ``pandas.DataFrame`` containing the observation time per source and runtype, you can do:
96+
97+
98+
.. code:: python
99+
100+
from fact.factdb import connect_database, get_ontime_per_source_and_runtype
101+
102+
connect_database()
103+
104+
num_runs = RunInfo.select().count()
105+
print(get_ontime_by_source_and_runtype())
106+
107+
108+
78109
auxservices
79110
-----------
80111

fact/factdb/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .database import factdata_db
2+
from .utils import get_ontime_by_source, get_ontime_by_source_and_runtype
3+
from .models import *

fact/factdb/database.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from peewee import MySQLDatabase
2+
import wrapt
3+
from ..credentials import get_credentials
4+
5+
factdata_db = MySQLDatabase(None)
6+
7+
8+
def connect_database(config=None):
9+
if config is None:
10+
config = get_credentials()['database']
11+
factdata_db.init(**config)
12+
factdata_db.connect()

0 commit comments

Comments
 (0)