Skip to content

Commit

Permalink
Modify the mode functions to encompass all types of labels (purpose, …
Browse files Browse the repository at this point in the history
…replaced_mode...)
  • Loading branch information
jiji14 committed Jan 5, 2024
1 parent 5c8f20c commit 5308550
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 65 deletions.
80 changes: 43 additions & 37 deletions emission/core/wrapper/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ def createProfile(uuid, ts):
'source':'Shankari',
'update_ts': ts,
'mpg_array': [defaultMpg],
'modes': {}
'mode': {},
'purpose': {},
'replaced_mode' : {}
}
writeResultProfile = get_profile_db().update_one(
{'user_id': uuid},
Expand Down Expand Up @@ -229,64 +231,68 @@ def unregister(userEmail):
get_profile_db().delete_one({'user_id': uuid})
return uuid

def getUserCustomModes(self):
def getUserCustomLabel(self, key):
user = get_profile_db().find_one({'user_id': self.uuid})
if 'modes' in user:
modes = user['modes']
filteredModes = {key: value for key, value in modes.items() if value.get('isActive', False)}
sortedModes = dict(sorted(filteredModes.items(), key=lambda x: (x[1]["frequency"]), reverse=True))
return list(sortedModes)
if key in user:
labels = user[key]
filteredLabels = {key: value for key, value in labels.items() if value.get('isActive', False)}
sortedLabels = dict(sorted(filteredLabels.items(), key=lambda x: (x[1]["frequency"]), reverse=True))
return list(sortedLabels)
else:
return []

def insertUserCustomMode(self, new_mode):
def insertUserCustomLabel(self, inserted_label):
from datetime import datetime
user = get_profile_db().find_one({'user_id': self.uuid})
modes = user['modes'] if 'modes' in user else {}
modes[new_mode] = {
key = inserted_label['key']
label = inserted_label['label']
items = user[key] if key in user else {}
items[label] = {
'createdAt': datetime.now(),
'frequency': 0,
'isActive': True,
}
get_profile_db().update_one({'user_id': self.uuid}, {'$set': {'modes': modes}})
return self.getUserCustomModes()
get_profile_db().update_one({'user_id': self.uuid}, {'$set': {key: items}})
return self.getUserCustomLabel(key)

def updateUserCustomMode(self, updated_mode):
def updateUserCustomLabel(self, updated_label):
from datetime import datetime
user = get_profile_db().find_one({'user_id': self.uuid})
modes = user['modes'] if 'modes' in user else {}
old_mode = updated_mode['old_mode']
new_mode = updated_mode['new_mode']
is_new_mode_must_added = updated_mode['is_new_mode_must_added']

# when a user changed a mode to an exsiting customized mode
if new_mode in modes:
updated_frequency = modes[new_mode]['frequency'] + 1
modes[new_mode]['frequency'] = updated_frequency
key = updated_label['key']
items = user[key] if key in user else {}
old_label = updated_label['old_label']
new_label = updated_label['new_label']
is_new_label_must_added = updated_label['is_new_label_must_added']
# when a user changed a label to an exsiting customized label
if new_label in items:
updated_frequency = items[new_label]['frequency'] + 1
items[new_label]['frequency'] = updated_frequency

# when a user added a new customized mode
if is_new_mode_must_added and not new_mode in modes:
modes[new_mode] = {
# when a user added a new customized label
if is_new_label_must_added and not new_label in items:
items[new_label] = {
'createdAt': datetime.now(),
'frequency': 1,
'isActive': True,
}

# when a user chaged a mode from an exsiting customized mode
if old_mode in modes:
updated_frequency = modes[old_mode]['frequency'] - 1
modes[old_mode]['frequency'] = updated_frequency
# when a user chaged a label from an exsiting customized label
if old_label in items:
updated_frequency = items[old_label]['frequency'] - 1
items[old_label]['frequency'] = updated_frequency

get_profile_db().update_one({'user_id': self.uuid}, {'$set': {'modes': modes}})
return self.getUserCustomModes()
get_profile_db().update_one({'user_id': self.uuid}, {'$set': {key: items}})
return self.getUserCustomLabel(key)

def deleteUserCustomMode(self, deleted_mode):
def deleteUserCustomLabel(self, deleted_label):
user = get_profile_db().find_one({'user_id': self.uuid})
modes = user['modes'] if 'modes' in user else {}
key = deleted_label['key']
label = deleted_label['label']
items = user[key] if key in user else {}

if deleted_mode in modes:
modes[deleted_mode]['isActive'] = False
if label in items:
items[label]['isActive'] = False

get_profile_db().update_one({'user_id': self.uuid}, {'$set': {'modes': modes}})
return self.getUserCustomModes()
get_profile_db().update_one({'user_id': self.uuid}, {'$set': {key: items}})
return self.getUserCustomLabel(key)

60 changes: 32 additions & 28 deletions emission/net/api/cfc_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,42 +306,46 @@ def getUserProfile():
user = User.fromUUID(user_uuid)
return user.getProfile()

@post('/mode/get')
def getUserCustomModes():
logging.debug("Called getUserCustomModes")
@post('/customlabel/get')
def getUserCustomLabels():
logging.debug("Called getUserCustomLabels")
keys = request.json['keys']
user_uuid = getUUID(request)
user = User.fromUUID(user_uuid)
return { 'modes' : user.getUserCustomModes() }

@post('/mode/insert')
def insertUserCustomModes():
logging.debug("Called insertUserMode")
new_mode = request.json['new_mode']
to_return = {}
for key in keys:
to_return[key] = user.getUserCustomLabel(key)
return to_return

@post('/customlabel/insert')
def insertUserCustomLabel():
logging.debug("Called insertUserCustomLabel")
inserted_label = request.json['inserted_label']
user_uuid = getUUID(request)
user = User.fromUUID(user_uuid)
to_return = user.insertUserCustomMode(new_mode)
logging.debug("Successfully inserted mode for user %s" % user_uuid)
return { 'modes' : to_return }

@post('/mode/update')
def updateUserCustomMode():
logging.debug("Called updateUserMode")
updated_mode = request.json['updated_mode']
to_return = user.insertUserCustomLabel(inserted_label)
logging.debug("Successfully inserted label for user %s" % user_uuid)
return { 'label' : to_return }

@post('/customlabel/update')
def updateUserCustomLabel():
logging.debug("Called updateUserCustomLabel")
updated_label = request.json['updated_label']
user_uuid = getUUID(request)
user = User.fromUUID(user_uuid)
to_return = user.updateUserCustomMode(updated_mode)
logging.debug("Successfully updated mode for user %s" % user_uuid)
return { 'modes' : to_return }

@post('/mode/delete')
def deleteUserCustomMode():
logging.debug("Called deleteUserCustomMode")
deleted_mode = request.json['deleted_mode']
to_return = user.updateUserCustomLabel(updated_label)
logging.debug("Successfully updated label label for user %s" % user_uuid)
return { 'label' : to_return }

@post('/customlabel/delete')
def deleteUserCustomLabel():
logging.debug("Called deleteUserCustomLabel")
deleted_label = request.json['deleted_label']
user_uuid = getUUID(request)
user = User.fromUUID(user_uuid)
to_return = user.deleteUserCustomMode(deleted_mode)
logging.debug("Successfully deleted mode for user %s" % user_uuid)
return { 'modes' : to_return }
to_return = user.deleteUserCustomLabel(deleted_label)
logging.debug("Successfully deleted label for user %s" % user_uuid)
return { 'label' : to_return }

@post('/result/metrics/<time_type>')
def summarize_metrics(time_type):
Expand Down

0 comments on commit 5308550

Please sign in to comment.