Skip to content

Instantly share code, notes, and snippets.

@omkar0001
Last active September 22, 2020 12:02
Show Gist options
  • Save omkar0001/f97259308847826649155ec9700c89ca to your computer and use it in GitHub Desktop.
Save omkar0001/f97259308847826649155ec9700c89ca to your computer and use it in GitHub Desktop.

Requirements:

  • Ability to change the settings from the backend for different states, without changing the backend.
    • name:
    • show_completions: Should we show the completions?
    • popup_on_every_session: Should we show the popup on every session?
    • status_code: Override the default status code.
    • message:
  • Initially should be able to support the below states.
    • ADMIN_NOT_AUTHORIZED
    • NOT_AUTHENTICATED
    • MODEL_DEPLOYED
    • READY_TO_DEPLOY
    • TRAINING
    • SERVICE_DOWN
    • SUBSCRIPTION_EXPIRED
  • Ability to change the state settings at company level. Also, at group of agents level.

Psuedocode:

  • The code will be in Authentication service.
# States Config yaml. It should be set in authentication service. 
 states:
     default:
       ADMIN_NOT_AUTHORIZED:
         show_completions: false
         popup_on_every_session: true
       ...
     override:
       - subdomain: washposthelp:
         agents:[]
         states:
           ADMIN_NOT_AUTHORIZED:
             show_completions: false
             popup_on_every_session: true
     ...

class States(Enum):
    ADMIN_NOT_AUTHORIZED
    NOT_AUTHENTICATED
    MODEL_DEPLOYED
    READY_TO_DEPLOY
    TRAINING
    SERVICE_DOWN
    SUBSCRIPTION_EXPIRED

class MicroStates(Enum):
    ADMIN_NOT_AUTHORIZED
    NOT_AUTHENTICATED 
    MODEL_DEPLOYED
    SERVICE_DOWN
    SUBSCRIPTION_EXPIRED

class MicroStateMachine:
    def __init__():
        pass    
   
    def next(micro_state_name:MicroStates, response:requests.Response):
        # Gets the value of micro state based on the response.
        return micro_state_value
   
def getStateFromMicroStates(microstates: dict):
     # Gets the state.
     return state:States 
 
def trasformResponse(response, state, stateConfig):
    # Transform the response.
    return response_json, status_code
 
def __main__():
    # This is the main function. It runs on every request.
     
    response = request(routing_service)
    state_config = loadStateConfig(configYaml)
    initial_microstate_dict = {key : None for key in MicorStates}
    state_machine = StateMachine()
    next_microstate_dict = {}
    for key, value in initial_micorstate_dict.keys():
        next_value = state_machine.next(key, response)
        next_microstate_dict[key] = next_value
     
    state = getStateFromMicroStates(next_microstate_dict)
    response_json, status_code = trasformResponse(response, state, stateConfig)
    return response_json, status_code 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment