class DailyReminderConfig(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) reminder_time = db.Column(db.String(10)) def __init__(self, info): self.reminder_time = info.get('reminder_time') self.code = info.get('code') def __repr__(self): result = {'id': self.id, 'reminder_time': self.reminder_time, 'code': self.code, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): if not info.get('reminder_time'): return (-1, 'No reminder_time params', -1) existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) new_reminder = DailyReminderConfig(info) db.session.add(new_reminder) db.session.commit() return (200, 'Successfully added daily reminder.', new_reminder)
class ImageTextUpload(db.Model): id = db.Column(db.Integer, primary_key=True) image_url = db.Column(db.String(100)) image_name = db.Column(db.String(100)) text = db.Column(db.String(1500)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) def __init__(self, info): self.image_url = info['image_url'] self.image_name = info['image_name'] self.text = info['text'] self.code = info['code'] def __repr__(self): result = {'id': self.id, 'image_url': self.image_url, 'image_name': self.image_name, 'text': self.text, 'code': self.code, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): if ImageTextUpload.query.filter_by(image_url=info['image_url'], text=info['text']).first(): return new_image = ImageTextUpload(info) db.session.add(new_image) db.session.commit()
class MobileSurvey(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50), db.ForeignKey('participant.email')) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) header = db.Column(db.String(200)) response = db.Column(db.String(500)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.email = info['email'] self.code = info['code'] self.header = info['header'] self.response = info['response'] def __repr__(self): result = { 'email': self.email, 'code': self.code, 'header': self.header, 'response': self.response, 'created_at': str(self.created_at) } return json.dumps(result) @staticmethod def add_stats(info): rows = info['logs'].split('\n') if rows == "": print print '***************No survey data.***************' print header = rows[0] response = ';'.join(rows[1:]) if len(rows) > 0 and header != "" and response != "": entry = { 'email': info['email'].strip('#'), 'code': info['code'].strip(), 'header': header, 'response': response } new_stat = MobileSurvey(entry) db.session.add(new_stat) db.session.commit() return 200, 'Successfully added survey log!', len(rows)
class Comment(db.Model): ''' model to store associated comments ''' id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(1000)) image_id = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False) posted_at = db.Column(db.DateTime)
class InAppAnalytics(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50), db.ForeignKey('participant.email')) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) event_time_millis = db.Column(db.BigInteger) event_desc = db.Column(db.String(50)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.email = info['email'] self.code = info['code'] self.event_time_millis = info['event_time_millis'] self.event_desc = info['event_desc'] def __repr__(self): result = { 'id': self.id, 'email': self.email, 'code': self.code, 'event_time_millis': str(self.event_time_millis), 'event_desc': self.event_desc, 'created_at': str(self.created_at) } return json.dumps(result) @staticmethod def add_stats(info): rows = info['logs'].split('\n') for row in rows: if row == "" or info['email'] == "": continue event_time_millis, event_desc = row.split(",") entry = { 'email': info['email'].strip('#'), 'code': info['code'].strip(), 'event_time_millis': event_time_millis, 'event_desc': event_desc } new_stat = InAppAnalytics(entry) db.session.add(new_stat) db.session.commit() return 200, 'Successfully added InAppAnalytics Event!', ""
class Student(db.Model): student_id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(32), unique=False, nullable=False) last_name = db.Column(db.String(32), unique=False, nullable=False) grade = db.Column(db.Integer, nullable=False) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(120), unique=False, nullable=False) school_id = db.Column(db.Integer, db.ForeignKey('school.school_id'), nullable=False) def __repr__(self): return f"Student'({self.username}','{self.image_file}')"
class TP_FgAppLog(db.Model): id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) worker_id = db.Column(db.String(50)) app_id = db.Column(db.String(30)) time_seconds = db.Column(db.String(20)) time_millis = db.Column(db.BigInteger) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) def __init__(self, info): self.worker_id = info['worker_id'] self.code = info['code'] self.app_id = info['app_id'][:30] self.time_seconds = info['time_seconds'] self.time_millis = info['time_millis'] def __repr__(self): result = { 'id': str(self.id), 'created_at': str(self.created_at), 'worker_id': self.worker_id, 'code': self.code, 'app_id': self.app_id, 'time_seconds': self.time_seconds, 'time_millis': self.time_millis } return json.dumps(result) @staticmethod def add_stats(info): worker_id = info['worker_id'] code = info['code'] logs = info['logs'] rows = logs.split(';') for row in rows: if row == "": continue app_id, time_seconds, time_millis = row.split(",") entry = { 'worker_id': worker_id, 'code': code, 'app_id': app_id, 'time_seconds': time_seconds, 'time_millis': time_millis } new_stats = TP_FgAppLog(entry) db.session.add(new_stats) db.session.commit() return (200, 'Successfully added fgAppLog stats!', "")
class MobileNotifLogs(db.Model): id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) worker_id = db.Column(db.String(50)) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) time_millis = db.Column(db.BigInteger) app_id = db.Column(db.String(30)) posted_millis = db.Column(db.BigInteger) action = db.Column(db.String(10)) def __init__(self, info): self.worker_id = info['worker_id'] self.code = info['code'] self.time_millis = info['time_millis'] self.app_id = info['app_id'][:30] self.posted_millis = info['posted_millis'] self.action = info['action'] def __repr__(self): result = { 'id': str(self.id), 'created_at': str(self.created_at), 'worker_id': self.worker_id, 'code': self.code, 'time_millis': self.time_millis, 'posted_millis': self.posted_millis, 'action': self.action } return json.dumps(result) @staticmethod def add_stats(info): rows = info['logs'].split(';') for row in rows: if row != "": time_millis, app_id, posted_millis, action = row.split(",") new_stats = MobileNotifLogs({ 'worker_id': info['worker_id'], 'code': info['code'], 'time_millis': time_millis, 'app_id': app_id, 'posted_millis': posted_millis, 'action': action }) db.session.add(new_stats) db.session.commit() return 200, 'Successfully added phone notif logs!', ""
class ScreenUnlockConfig(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) time_limit = db.Column(db.Integer) unlocked_limit = db.Column(db.Integer) vibration_strength = db.Column(db.String(10)) show_stats = db.Column(db.Boolean, default=False) enable_user_pref = db.Column(db.Boolean, default=False) start_time = db.Column(db.String(50)) end_time = db.Column(db.String(50)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.code = info.get('code') self.time_limit = info.get('time_limit') self.unlocked_limit = info.get('unlocked_limit') self.vibration_strength = info.get('vibration_strength') self.show_stats = info.get('show_stats') self.enable_user_pref = info.get('enable_user_pref') self.start_time = info.get('start_time') self.end_time = info.get('end_time') def __repr__(self): result = {'id': self.id, 'code': self.code, 'time_limit': self.time_limit, 'unlocked_limit': self.unlocked_limit, 'vibration_strength': self.vibration_strength, 'show_stats': self.show_stats, 'enable_user_pref': self.enable_user_pref, 'start_time': self.start_time, 'end_time': self.end_time, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) unlock_setting = ScreenUnlockConfig(info) db.session.add(unlock_setting) db.session.commit() return (200, 'Successfully added screen unlock setting.', unlock_setting)
class RescuetimeData(db.Model): # Store RescueTime data id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50)) created_date = db.Column(db.DateTime) date = db.Column(db.DateTime) time_spent = db.Column(db.Integer) num_people = db.Column(db.Integer) activity = db.Column(db.String(120)) category = db.Column(db.String(120)) productivity = db.Column(db.Integer) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) def __init__(self, profile): self.email = profile.get('email') self.created_date = profile.get('created_date') self.date = profile.get('date') self.time_spent = profile.get('time_spent') self.num_people = profile.get('num_people') self.activity = profile.get('activity') self.category = profile.get('category') self.productivity = profile.get('productivity') def __repr__(self): result = {'id': self.id, 'email': self.email, 'created_date': str(self.created_date), 'date': str(self.date), 'time_spent': self.time_spent, 'num_people': self.num_people, 'activity': self.activity, 'category': self.category, 'productivity': self.productivity} return json.dumps(result) @staticmethod def add(data): new_row = RescuetimeData(data) db.session.add(new_row) db.session.commit() return (200, 'Successfully added data.', new_row)
class RescuetimeConfig(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) productive_duration = db.Column(db.String(50)) distracted_duration = db.Column(db.String(50)) productive_msg = db.Column(db.String(50)) distracted_msg = db.Column(db.String(50)) show_stats = db.Column(db.Boolean, default=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.code = info.get('code') self.productive_duration = info.get('productive_duration') self.distracted_duration = info.get('distracted_duration') self.productive_msg = info.get('productive_msg') self.distracted_msg = info.get('distracted_msg') self.show_stats = info.get('show_stats') def __repr__(self): result = {'id': self.id, 'code': self.code, 'productive_duration': self.productive_duration, 'distracted_duration': self.distracted_duration, 'productive_msg': self.productive_msg, 'distracted_msg': self.distracted_msg, 'show_stats': self.show_stats, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) rt_config = RescuetimeConfig(info) db.session.add(rt_config) db.session.commit() return (200, 'Successfully added rescuetime config.', rt_config)
class VibrationConfig(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) app_id = db.Column(db.String(50)) time_limit = db.Column(db.Integer) open_limit = db.Column(db.Integer) vibration_strength = db.Column(db.String(10)) show_stats = db.Column(db.Boolean, default=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.code = info.get('code') self.app_id = info.get('app_id') self.time_limit = info.get('time_limit') self.open_limit = info.get('open_limit') self.vibration_strength = info.get('vibration_strength') self.show_stats = info.get('show_stats') def __repr__(self): result = {'id': self.id, 'code': self.code, 'app_id': self.app_id, 'time_limit': self.time_limit, 'open_limit': self.open_limit, 'vibration_strength': self.vibration_strength, 'show_stats': self.show_stats, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) new_vibr_setting = VibrationConfig(info) db.session.add(new_vibr_setting) db.session.commit() return (200, 'Successfully added vibration setting.', new_vibr_setting)
class CalendarConfig(db.Model): id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) event_num_limit = db.Column(db.String(5)) event_time_limit = db.Column(db.String(10)) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) def __init__(self, info): self.event_num_limit = info.get('event_num_limit') self.event_time_limit = info.get('event_time_limit') self.code = info.get('code') def __repr__(self): result = {'id': self.id, 'event_time_limit': self.event_time_limit, 'event_num_limit': self.event_num_limit, 'code': self.code, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): if not info.get('event_num_limit') and not info.get('event_time_limit'): print '**********************' print 'calendar params: {}'.format(info) print '**********************' return (-1, 'No calendar params', -1) existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) new_setting = CalendarConfig(info) db.session.add(new_setting) db.session.commit() return (200, 'Successfully added cal setting.', new_setting)
class GeneralNotificationConfig(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) intv_id = db.relationship('Intervention', backref='general_notification_config', lazy='select') title = db.Column(db.String(50)) content = db.Column(db.String(50)) app_id = db.Column(db.String(50)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.title = info.get('title') self.content = info.get('content') self.app_id = info.get('app_id') self.code = info.get('code') def __repr__(self): result = {'id': self.id, 'title': self.title, 'content': self.content, 'app_id': self.app_id, 'code': self.code, 'created_at': str(self.created_at)} return json.dumps(result) @staticmethod def add(info): existing_experiment = Experiment.query.filter_by(code=info['code']).first() if not existing_experiment: invalid_response = 'Invalid experiment code({})'.format(info['code']) return (-1, invalid_response, -1) new_notif = GeneralNotificationConfig(info) db.session.add(new_notif) db.session.commit() return (200, 'Successfully added general notification setting.', new_notif)
class Enrollment(db.Model): id = db.Column(db.Integer, primary_key=True) participant_id = db.Column(db.String(120), db.ForeignKey('participant.email')) exp_code = db.Column(db.String(10), db.ForeignKey('experiment.code')) enrolled_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, data): self.participant_id = data.get('participant_id') self.exp_code = data.get('exp_code') def __repr__(self): result = { 'participant_id': self.participant_id, 'exp_code': self.exp_code, 'enrolled_date': str(self.enrolled_date) } return json.dumps(result) @staticmethod def enroll(data): # Check if participant already registered # if Participant.query.filter_by(email=data['email']).first() == None: # abort(400, "Participant not registered") # Check if already enrolled to prevent duplicate enrollment result = Enrollment.query.filter_by( exp_code=data['exp_code'], participant_id=data['participant_id']).count() if result > 0: response_message = { 'message': 'Participant already enrolled in ' + data['exp_code'] } http_response_code = 200 resp = {} experiment = Experiment.query.filter_by( code=data['exp_code']).first() resp['experiment'] = json.loads(str(experiment)) protocols = Protocol.query.filter_by( exp_code=data['exp_code']).all() resp['protocols'] = json.loads(str(protocols)) return (http_response_code, resp, result) new_enrollment = Enrollment(data) db.session.add(new_enrollment) db.session.commit() result = Enrollment.query.filter_by( exp_code=data['exp_code'], participant_id=data['participant_id']) if Enrollment.query.filter_by( exp_code=data['exp_code'], participant_id=data['participant_id']) is not None: http_response_code = 200 resp = {} experiment = Experiment.query.filter_by( code=data['exp_code']).first() resp['experiment'] = json.loads(str(experiment)) protocols = Protocol.query.filter_by( exp_code=data['exp_code']).all() resp['protocols'] = json.loads(str(protocols)) return (http_response_code, resp, result) else: response_message = {'error': 'Participant enrollment failed'} http_response_code = 500 return (http_response_code, response_message, result)
class Intervention(db.Model): id = db.Column(db.Integer, primary_key=True) notif_id = db.Column(db.Integer, db.ForeignKey('general_notification_config.id')) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) treatment = db.Column(db.String(2000)) start = db.Column(db.DateTime, default=datetime.datetime.utcnow) end = db.Column(db.DateTime, default=datetime.datetime.utcnow) every = db.Column(db.String(30)) when = db.Column(db.String(30)) repeat = db.Column(db.String(30)) intv_type = db.Column(db.String(20)) user_window_hours = db.Column(db.Integer) free_hours_before_sleep = db.Column(db.Integer) def __init__(self, info): self.code = info['code'] self.treatment = info['treatment'] self.start = info['start'] self.end = info['end'] self.every = info['every'] self.when = info['when'] self.repeat = info['repeat'] self.intv_type = info['intv_type'] self.notif_id = info['notif_id'] self.user_window_hours = info['user_window_hours'] self.free_hours_before_sleep = info['free_hours_before_sleep'] def __repr__(self): treatment_image = [] treatment_text = [] if self.treatment: json_treatment = json.loads(self.treatment) for treat_id in json_treatment.values(): txt_img = ImageTextUpload.query.get(treat_id) treatment_image.append(txt_img.image_url) treatment_text.append(txt_img.text) notif = GeneralNotificationConfig.query.filter_by(id=self.notif_id).first() if not notif: notif = '{}' else: notif = json.dumps({'title': notif.title, 'content': notif.content, 'app_id': notif.app_id}) result = { 'created_at': str(self.created_at), 'code': self.code, 'treatment_image': treatment_image, 'treatment_text': treatment_text, 'start': str(self.start), 'end': str(self.end), 'every': self.every, 'when': self.when, 'repeat': self.repeat, 'user_window_hours': self.user_window_hours, 'free_hours_before_sleep': self.free_hours_before_sleep, 'notif_id': self.notif_id, 'notif': notif, 'intv_type': self.intv_type } return json.dumps(result) @staticmethod def add_intervention(info): new_intervention = Intervention(info) db.session.add(new_intervention) db.session.commit() latest_intervention = Intervention.query.order_by('created_at desc').first() return (200, 'Successfully added intervention', latest_intervention) @staticmethod def delete_intervention(created_at): Intervention.query.filter_by(created_at=created_at).delete() db.session.commit()
class RescuetimeUser(db.Model): # google login info and credentials for accessing google calendar email = db.Column(db.String(50), primary_key=True, unique=True) firstname = db.Column(db.String(120)) lastname = db.Column(db.String(120)) gender = db.Column(db.String(10)) picture = db.Column(db.String(120)) google_credentials = db.Column(db.String(2800), unique=True) rescuetime_access_token = db.Column(db.String(120)) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) def __init__(self, profile): self.email = profile.get('email', '') self.firstname = profile.get('given_name', '') self.lastname = profile.get('family_name', '') self.gender = profile.get('gender', '') self.picture = profile.get('picture', '') def __repr__(self): return self.email def is_active(self): return True def is_authenticated(self): """ Returns `True`. NewParticipant is always authenticated. """ return True def is_anonymous(self): """ Returns `False`. There are no Anonymous here. """ return False def get_id(self): """ Take `id` attribute and convert it to `unicode`. """ return unicode(self.email) def update_field(self, key, value): """ Set user field with give value and save to database. """ user = RescuetimeUser.query.get(self.email) setattr(user, key, value) # same: user.key = value db.session.commit() def update_moves_id(self, moves_id): """ Set moves_id field """ if RescuetimeUser.query.filter_by(moves_id=moves_id).first(): return 'Already Exists' self.update_field('moves_id', moves_id) def is_authorized(self, label): """ Return True if datastream label has been authenticated else False. """ if label == 'moves': return self.moves_id and self.moves_access_token and self.moves_refresh_token elif label == 'gcal': return self.google_credentials elif label == 'pam': return self.pam_access_token and self.pam_refresh_token else: raise NotImplementedError("Auth failed for label: %s." % label) @classmethod def get_user(cls, email): """ Return user object from database using primary_key(email). """ return cls.query.get(email) @classmethod def get_all_users(cls): """ Return list of all users from database. """ return cls.query.all() @classmethod def get_all_users_data(cls): """ Return list of all users data from database. """ users = cls.query.all() all_users = [] for user in users: #user_data = [] user_data = collections.OrderedDict() user_data['email'] = user.email user_data['firstname'] = user.firstname user_data['lastname'] = user.lastname user_data['access_token'] = user.rescuetime_access_token all_users.append(user_data) return all_users @classmethod def from_profile(cls, profile): """ Return new user or existing user from database using their profile information. """ email = profile.get('email') if not cls.query.get(email): new_user = cls(profile) db.session.add(new_user) db.session.commit() user = cls.query.get(email) return user @classmethod def update_code(cls, profile): response = 'Code successfully submitted.' is_valid = Experiment.query.filter_by(code=profile['code']).first() is not None if not is_valid: response = 'Invalid code. Try again.' user = cls.query.get(profile['email']) if not user: response = 'RescueTime user does not exist. Sign out and try again.' if is_valid and user: user.code = profile['code'] db.session.commit() return response
class PAM(db.Model): __tablename__ = "pam" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50), db.ForeignKey('participant.email')) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) timestamp_z = db.Column(db.String) affect_arousal = db.Column(db.String) affect_valence = db.Column(db.String) positive_affect = db.Column(db.String) mood = db.Column(db.String) negative_affect = db.Column(db.String) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.email = info['email'] self.code = info['code'] self.timestamp_z = info['timestamp_z'] self.affect_arousal = info['affect_arousal'] self.affect_valence = info['affect_valence'] self.positive_affect = info['positive_affect'] self.mood = info['mood'] self.negative_affect = info['negative_affect'] def __repr__(self): result = { 'email': self.email, 'code': self.code, 'timestamp_z': self.timestamp_z, 'affect_arousal': self.affect_arousal, 'affect_valence': self.affect_valence, 'positive_affect': self.positive_affect, 'mood': self.mood, 'negative_affect': self.negative_affect, 'created_at': str(self.created_at) } return json.dumps(result) @staticmethod def add_stats(info): rows = info['logs'].split('\n') if len(rows) > 0: headers = rows[0].split(',') for row in rows[1:]: if row == "": print "No data in row" continue print print print '**************************' print headers print row print '**************************' values = row.split(',') entry = { 'email': info['email'].strip('#'), 'code': info['code'].strip(), 'timestamp_z': '' if 'timestamp' not in headers else values[headers.index('timestamp')], 'affect_arousal': '' if 'affect_arousal' not in headers else values[headers.index('affect_arousal')], 'affect_valence': '' if 'affect_valence' not in headers else values[headers.index('affect_valence')], 'positive_affect': '' if 'positive_affect' not in headers else values[headers.index('positive_affect')], 'mood': '' if 'mood' not in headers else values[headers.index('mood')], 'negative_affect': '' if 'negative_affect' not in headers else values[headers.index('negative_affect')] } new_stat = PAM(entry) db.session.add(new_stat) db.session.commit() return 200, 'Successfully added PAM log!', ""
class MobileUser(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) email = db.Column(db.String(50), db.ForeignKey('participant.email')) last_installed_ms = db.Column(db.String(30)) pretty_last_installed = db.Column(db.String(30)) app_version_name = db.Column(db.String(10)) app_version_code = db.Column(db.String(10)) phone_model = db.Column(db.String(30)) android_version = db.Column(db.String(10)) device_country = db.Column(db.String(10)) device_id = db.Column(db.String(30)) def __init__(self, info): self.code = info['code'] self.email = info['email'] self.last_installed_ms = info['last_installed_ms'] self.pretty_last_installed = info['pretty_last_installed'] self.app_version_name = info['app_version_name'] self.app_version_code = info['app_version_code'] self.phone_model = info['phone_model'] self.android_version = info['android_version'] self.device_country = info['device_country'] self.device_id = info['device_id'] def __repr__(self): result = { 'email': self.email, 'code': self.code, 'last_installed_ms': self.last_installed_ms, 'pretty_last_installed': self.pretty_last_installed, 'app_version_name': self.app_version_name, 'app_version_code': self.app_version_code, 'phone_model': self.phone_model, 'device_id': self.device_id, 'device_country': self.device_country, 'android_version': self.android_version } return json.dumps(result) @classmethod def register(cls, data): code = data['code'] experiment = Experiment.query.filter_by(code=code).first() if not experiment: return 400, "Experiment doesn't exist" # already in experiment so no need to change anything user_in_experiment = MobileUser.query.filter_by(email=data['email'], code=code).first() if user_in_experiment: return 200, "Already registered. Welcome back!" # enrolling as first timer in an experiment new_user = MobileUser(data) db.session.add(new_user) db.session.commit() return 200, "Successfully registered!" @staticmethod def add_user(info): existing_user = MobileUser.query.filter_by(email=info['email']).first() if existing_user: return (-1, 'Welcome back ' + existing_user.email, existing_user) new_user = MobileUser(info) db.session.add(new_user) db.session.commit() return (200, 'Successfully enrolled in experiment.', new_user)
class NotifEvent(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50), db.ForeignKey('participant.email')) code = db.Column(db.String(10), db.ForeignKey('experiment.code')) alarm_millis = db.Column(db.BigInteger) ringer_mode = db.Column(db.String(10)) method = db.Column(db.String(20)) title = db.Column(db.String(120)) content = db.Column(db.String(50)) app_id = db.Column(db.String(30)) was_dismissed = db.Column(db.Boolean) event_time_millis = db.Column(db.BigInteger) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, info): self.email = info['email'] self.code = info['code'] self.alarm_millis = info['alarm_millis'] self.ringer_mode = info['ringer_mode'] self.method = info['method'] self.title = info['title'] self.content = info['content'] self.app_id = info['app_id'] self.was_dismissed = info['was_dismissed'] self.event_time_millis = info['event_time_millis'] def __repr__(self): result = { 'id': self.id, 'email': self.email, 'code': self.code, 'alarm_millis': self.alarm_millis, 'ringer_mode': self.ringer_mode, 'method': self.method, 'title': self.title, 'content': self.content, 'app_id': self.app_id, 'was_dismissed': self.was_dismissed, 'event_time_millis': str(self.event_time_millis), 'created_at': str(self.created_at) } return json.dumps(result) @staticmethod def add_stats(info): rows = info['logs'].split('\n') for row in rows: if row == "" or info['email'] == "": continue email, code, alarm_millis, ringer_mode, method, title, content, app_id, was_dismissed, event_time_millis = \ row.split(",") entry = { 'email': email.strip( '#'), # temp fix because for some reason username has '# 'code': code.strip(), 'alarm_millis': alarm_millis, 'ringer_mode': ringer_mode, 'method': method, 'title': title, 'content': content, 'app_id': app_id, 'was_dismissed': was_dismissed, 'event_time_millis': event_time_millis } new_stat = NotifEvent(entry) db.session.add(new_stat) db.session.commit() return 200, 'Successfully added Notif Event!', ""
class Protocol(db.Model): id = db.Column(db.Integer, primary_key=True) label = db.Column(db.String(120)) exp_code = db.Column(db.String(10), db.ForeignKey('experiment.code')) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) start_date = db.Column(db.Date) end_date = db.Column(db.Date) frequency = db.Column(db.String(10)) method = db.Column(db.String(20)) notif_details = db.Column(db.String(1600)) notif_appid = db.Column(db.String(300)) notif_type = db.Column(db.String(20)) notif_time = db.Column(db.String(20)) probable_half_notify = db.Column(db.Boolean, default=False) def __init__(self, data): self.label = data.get('label') self.exp_code = data.get('exp_code') self.start_date = data.get('start_date') self.end_date = data.get('end_date') self.frequency = data.get('frequency') self.method = data.get('method') self.notif_details = data.get('notif_details') self.notif_appid = data.get('notif_appid') self.notif_type = data.get('notif_type') self.notif_time = data.get('notif_time') self.probable_half_notify = data.get('probable_half_notify') def __repr__(self): result = { 'id': self.id, 'label': self.label, 'exp_code': self.exp_code, 'created_at': str(self.created_at), 'start_date': str(self.start_date), 'end_date': str(self.end_date), 'frequency': str(self.frequency), 'method': str(self.method), 'notif_details': self.notif_details.encode('utf-8') if self.notif_details else None, 'notif_appid': str(self.notif_appid), 'notif_type': str(self.notif_type), 'notif_time': str(self.notif_time), 'probable_half_notify': str(self.probable_half_notify) } return json.dumps(result) @staticmethod def add_protocol(data): new_protocol = Protocol(data) db.session.add(new_protocol) db.session.commit() latest_protocol = Protocol.query.order_by('created_at desc').first() return 200, 'Successfully added intervention', latest_protocol @staticmethod def delete_protocol(pid): p = db.session.query(Protocol).filter(Protocol.id == pid).first() db.session.delete(p) db.session.commit() return 200, 'Successfully deleted protocol.', p
class Experiment(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(10), unique=True) label = db.Column(db.String(120)) title = db.Column(db.String(120)) description = db.Column(db.String(250)) start_date = db.Column(db.Date) end_date = db.Column(db.Date) rescuetime = db.Column(db.Boolean, default=False) calendar = db.Column(db.Boolean, default=False) phone_notif = db.Column(db.Boolean, default=False) screen_events = db.Column(db.Boolean, default=False) app_usage = db.Column(db.Boolean, default=False) protocols = db.relationship('Protocol', backref='experiment', lazy='select') created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) owner = db.Column(db.String(120), db.ForeignKey('researcher.email')) def __init__(self, info): self.code = info.get('code') if info.get( 'code') else Experiment.generate_unique_id() self.label = info.get('label') self.title = info.get('title') self.description = info.get('description') self.start_date = info['start_date'] self.end_date = info['end_date'] self.rescuetime = info.get('rescuetime') self.calendar = info.get('calendar') self.phone_notif = info.get('phone_notif') self.screen_events = info.get('screen_events') self.app_usage = info.get('app_usage') self.owner = info['owner'] def __repr__(self): result = { 'owner': self.owner, 'code': self.code, 'label': self.label, 'title': self.title, 'description': self.description, 'start_date': str(self.start_date), 'end_date': str(self.end_date), 'rescuetime': self.rescuetime, 'calendar': self.calendar, 'phone_notif': self.phone_notif, 'screen_events': self.screen_events, 'app_usage': self.app_usage, 'created_date': str(self.created_date) } return json.dumps(result) @staticmethod def generate_unique_id(): code = str(uuid.uuid4())[:6] while Experiment.query.filter_by(code=code).first(): code = str(uuid.uuid4())[:6] return code @staticmethod def add_experiment(info): msg = "New experiment was successfully created." existing_experiment = Experiment.query.filter_by( owner=info['owner'], label=info['label']).first() if existing_experiment: # Experiment.delete_experiment(existing_experiment.code) info['code'] = existing_experiment.code Experiment.update_experiment(info) return 200, "Experiment was successfully updated.", info new_experiment = Experiment(info) db.session.add(new_experiment) db.session.commit() # new_experiment = Experiment.query.filter_by(owner=exp['owner'], label=exp['label']).first() # Add protocols to protocols table protocols = json.loads(info['protocols']) for p in protocols: p['exp_code'] = new_experiment.code Protocol.add_protocol(p) return 200, msg, new_experiment @staticmethod def delete_experiment(code): e = db.session.query(Experiment).filter( Experiment.code == code).first() db.session.delete(e) db.session.commit() # Experiment.query.filter_by(code=code).delete() # Protocol.query.filter_by(code=code).delete() # db.session.commit() @staticmethod def update_experiment(info): experiment = Experiment.query.filter_by(code=info['code']).first() experiment.label = info.get('label') experiment.title = info.get('title') experiment.description = info.get('description') experiment.start_date = info.get('start_date') experiment.end_date = info.get('end_date') experiment.rescuetime = info.get('rescuetime') experiment.calendar = info.get('calendar') experiment.phone_notif = info.get('phone_notif') experiment.screen_events = info.get('screen_events') experiment.app_usage = info.get('app_usage') engine = create_engine(SQLALCHEMY_DATABASE_URI) stmt = Protocol.__table__.delete().where( Protocol.exp_code == info['code']) engine.execute(stmt) protocols = json.loads(info['protocols']) for p in protocols: p['exp_code'] = info['code'] Protocol.add_protocol(p) db.session.commit() return experiment @staticmethod def update_group(update): experiment = Experiment.query.filter_by(code=update['code']).first() return experiment