forked from CenterForCollectiveLearning/DIVE-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.py
225 lines (188 loc) · 6.77 KB
/
manager.py
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
214
215
216
217
218
219
220
221
222
223
224
225
import yaml
import shutil
import contextlib
import os
from os import listdir, curdir
from os.path import isfile, join, isdir
import boto3
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager
from sqlalchemy.orm.exc import NoResultFound
from dive.base.core import create_app
from dive.base.db import db_access
from dive.base.db.accounts import register_user
from dive.base.constants import Role
from dive.base.db.models import Project, Dataset, Dataset_Properties, Field_Properties, Spec, Exported_Spec, Team, User
from dive.worker.core import celery, task_app
from dive.worker.pipelines import ingestion_pipeline, viz_spec_pipeline, full_pipeline, relationship_pipeline
from dive.worker.ingestion.upload import get_dialect, get_encoding
import logging
logger = logging.getLogger(__name__)
excluded_filetypes = ['json', 'py', 'yaml']
app = create_app()
app.app_context().push()
mode = os.environ.get('MODE', 'DEVELOPMENT')
if mode == 'DEVELOPMENT': app.config.from_object('config.DevelopmentConfig')
elif mode == 'TESTING': app.config.from_object('config.TestingConfig')
elif mode == 'PRODUCTION': app.config.from_object('config.ProductionConfig')
db = SQLAlchemy(app)
manager = Manager(app)
if app.config['STORAGE_TYPE'] == 's3':
s3_client = boto3.client('s3',
aws_access_key_id=app.config['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=app.config['AWS_SECRET_ACCESS_KEY'],
region_name=app.config['AWS_REGION']
)
from dive.base.db.models import *
migrate = Migrate(app, db, compare_type=True)
manager.add_command('db', MigrateCommand)
@manager.command
def fresh_migrations():
try:
shutil.rmtree('migrations')
except OSError as e:
pass
command = 'DROP TABLE IF EXISTS alembic_version;'
db.engine.execute(command)
@manager.command
def drop():
app.logger.info("Dropping tables")
try:
shutil.rmtree('migrations')
except OSError as e:
pass
db.session.commit()
db.reflect()
db.drop_all()
@manager.command
def create():
app.logger.info("Creating tables")
db.session.commit()
db.create_all()
db.session.commit()
@manager.command
def remove_uploads():
app.logger.info("Removing data directories in upload folder")
if os.path.isdir(app.config['STORAGE_PATH']):
STORAGE_PATH = os.path.join(os.curdir, app.config['STORAGE_PATH'])
shutil.rmtree(STORAGE_PATH)
from dive.base.constants import specific_type_to_scale
@manager.command
def migrate_scale_type():
all_field_properties = Field_Properties.query.filter_by().all()
for fp in all_field_properties:
setattr(fp, 'scale', specific_type_to_scale[fp.type])
db.session.commit()
@manager.command
def recreate():
app.logger.info("Recreating tables")
drop()
create()
@manager.command
def delete_specs():
from dive.base.db.models import Spec
all_specs = Spec.query.all()
map(db.session.delete, all_specs)
db.session.commit()
@manager.command
def users():
user_fixture_file = open('fixtures.yaml', 'rt')
users = yaml.load(user_fixture_file.read())['users']
for user in users:
app.logger.info('Created user: %s', user['username'])
register_user(
user['username'],
user['email'],
user['password'],
admin=user['admin'],
teams=user['teams'],
confirmed=True,
create_teams=True
)
import datetime
@manager.command
def delete_stale_anonymous_users(days=1):
logger.info('Deleting stale anonymous users, with threshold %s days', days)
anonymous_users = User.query.filter_by(anonymous=True).all()
count = 0
for u in anonymous_users:
age = datetime.datetime.utcnow() - u.creation_date
stale = age > datetime.timedelta(days)
logger.info('User %s: %s', u.id, stale)
if stale:
db.session.delete(u)
count += 1
db.session.commit()
logger.info('Deleted %s stale anonymous users', count)
@manager.command
def delete_all_anonymous_users():
logger.info('Deleting anonymous users')
anonymous_users = User.query.filter_by(anonymous=True).all()
for u in anonymous_users:
db.session.delete(u)
db.session.commit()
@manager.command
def delete_preloaded_datasets():
logger.info('Deleting preloaded datasets')
preloaded_datasets = Dataset.query.filter_by(preloaded=True).all()
for pd in preloaded_datasets:
db.session.delete(pd)
db.session.commit()
def ensure_dummy_project():
logger.info('Ensuring dummy project for preloaded datasets')
try:
dummy_project = Project.query.filter_by(id=-1).one()
except NoResultFound as e:
p = Project(
id=-1,
title='Dummy Project',
description='Dummy Description'
)
db.session.add(p)
db.session.commit()
@manager.command
def preload_datasets():
'''
Usage: have preloaded directory on local and mirrored files on remote S3 bucket
'''
preloaded_dir = app.config['PRELOADED_PATH']
top_level_config_file = open(join(preloaded_dir, 'metadata.yaml'), 'rt')
top_level_config = yaml.load(top_level_config_file.read())
datasets = top_level_config['datasets']
ensure_dummy_project()
delete_preloaded_datasets()
# Iterate through project directories
for dataset in datasets:
project_id = -1
file_name = dataset.get('file_name')
app.logger.info('Ingesting preloaded dataset: %s', file_name)
path = join(preloaded_dir, file_name)
file_object = open(path, 'r')
if app.config['STORAGE_TYPE'] == 's3':
remote_path = 'https://s3.amazonaws.com/%s/%s/%s' % (app.config['AWS_DATA_BUCKET'], project_id, file_name)
dataset = db_access.insert_dataset(
project_id,
path = path if (app.config['STORAGE_TYPE'] == 'file') else remote_path,
description = dataset.get('description'),
encoding = get_encoding(file_object),
dialect = get_dialect(file_object),
offset = None,
title = dataset.get('title'),
file_name = file_name,
type = dataset.get('file_type'),
preloaded = True,
storage_type = app.config['STORAGE_TYPE'],
info_url = dataset.get('info_url'),
tags = dataset.get('tags')
)
ingestion_result = ingestion_pipeline.apply(args=[dataset['id'], project_id])
# relationship_result = relationship_pipeline.apply(args=[ project_id ])
# relationship_result.get()
# TODO Visualiation ingest
# TODO Regression ingest
# TODO Aggregation ingest
# TODO Correlation ingest
# TODO Comparison ingest
if __name__ == "__main__":
manager.run()