def _tellObserver(self, tuple_, tuples, session, payloadFilt): tupleSelector = TupleSelector(EventDBPropertyTable.tupleName(), {}) self._tupleDataObserver.notifyOfTupleUpdate(tupleSelector) tupleSelector = TupleSelector(EventDBPropertyTuple.tupleName(), {}) self._tupleDataObserver.notifyOfTupleUpdate(tupleSelector) return True
def importInternalGroups(self, importHash: str, groupsVortexMsg: bytes): #: result: InternalGroupImportResultTuple result = yield importInternalGroups.delay(importHash, groupsVortexMsg) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(InternalGroupTuple.tupleName(), {})) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(GroupDetailTuple.tupleName(), {})) return result
def importInternalUsers(self, importHash: str, usersVortexMsg: bytes): #: result: InternalUserImportResultTuple result = yield importInternalUsers.delay(importHash, usersVortexMsg) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(InternalUserTuple.tupleName(), {})) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(UserDetailTuple.tupleName(), {})) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(UserListItemTuple.tupleName(), {})) return result
def replaceProperties(self, modelSetKey: str, propertiesEncodedPayload: str) -> Deferred: yield runPyInPg(logger, self._dbSessionCreator, EventDBImportPropertiesInPgTask.replaceProperties, None, modelSetKey, propertiesEncodedPayload) tupleSelector = TupleSelector(EventDBPropertyTable.tupleName(), {}) self._tupleObservable.notifyOfTupleUpdate(tupleSelector) tupleSelector = TupleSelector(EventDBPropertyTuple.tupleName(), {}) self._tupleObservable.notifyOfTupleUpdate(tupleSelector)
def _processNewTuples(self, lookupTuples): if not lookupTuples: return firstTupleType = lookupTuples[0].tupleType() if DispLevel.tupleType() == firstTupleType: self._levelLookups = lookupTuples elif DispLayer.tupleType() == firstTupleType: self._layerLookups = lookupTuples elif DispColor.tupleType() == firstTupleType: self._colorLookups = lookupTuples elif DispLineStyle.tupleType() == firstTupleType: self._lineStyleLookups = lookupTuples elif DispTextStyle.tupleType() == firstTupleType: self._textStyleLookups = lookupTuples else: raise NotImplementedError("Cache not implemented for %s" % firstTupleType) self._tupleObservable.notifyOfTupleUpdate( TupleSelector(firstTupleType, {}))
def _tellObserver(self, tuple_, tuples, session, payloadFilt): selector = {} # Copy any filter values into the selector # selector["lookupName"] = payloadFilt["lookupName"] tupleSelector = TupleSelector(StringIntTuple.tupleName(), selector) self._tupleDataObserver.notifyOfTupleUpdate(tupleSelector) return True
def _notifyOfTupleUpdateInMainOne(self, tupleSelector: TupleSelector): tsStr = tupleSelector.toJsonStr() if tsStr not in self._observerDataByTupleSelector: return # Filter out the offline observables onlineUuids = set(VortexFactory.getRemoteVortexUuids()) observers = self._observerDataByTupleSelector[tsStr].observers for od in list(observers): if od.vortexUuid not in onlineUuids: observers.remove(od) # Get / update the list of observing UUIDs if not observers: del self._observerDataByTupleSelector[tsStr] return # Create the vortexMsg filt = dict(tupleSelector=tupleSelector) filt.update(self._filt) vortexMsg = yield self._createVortexMsg(filt, tupleSelector) # We can have multiple Observable clients on the one vortex, so make sure # we only send one message for these. destVortexUuids = set([od.vortexUuid for od in observers]) # Send the vortex messages for destVortexUuid in destVortexUuids: d = VortexFactory.sendVortexMsg(vortexMsgs=vortexMsg, destVortexUuid=destVortexUuid) d.addErrback(vortexLogFailure, logger)
def _processCreateBranch(self, action: CreateBranchActionTuple): try: # Perform update using SQLALchemy dbSession = self._dbSessionCreator() dbItem = None mergeItem = BranchDetailTable.fromTuple(action.branchDetail) if action.branchDetail.id is not None: rows = dbSession.query(BranchDetailTable) \ .filter(BranchDetailTable.id == action.branchDetailId) \ .all() if rows: dbItem = rows[0] dbItem.merge(mergeItem) if not dbItem: newItem = BranchDetailTable.fromTuple(action.branchDetail) dbSession.add(newItem) dbSession.commit() # Notify the observer of the update # This tuple selector must exactly match what the UI observes tupleSelector = TupleSelector( BranchDetailTuple.tupleName(), dict(modelSetKey=newItem.modelSetKey)) self._tupleObservable.notifyOfTupleUpdate(tupleSelector) except Exception as e: logger.exception(e) finally: # Always close the dbSession after we create it dbSession.close()
def _loadTraceConfigIntoCache( self, modelSetKey: str, traceConfigTuples: List[GraphDbTraceConfigTuple], deletedTraceConfigKeys: Set[str] = set()): subCache = self._cache[modelSetKey] traceKeysUpdated: Set[str] = { traceConfig.key for traceConfig in traceConfigTuples } deletedTraceConfigKeys -= traceKeysUpdated for traceConfig in traceConfigTuples: subCache[traceConfig.key] = traceConfig for traceConfigKey in deletedTraceConfigKeys: if traceConfigKey in subCache: subCache.pop(traceConfigKey) logger.debug( "Received TraceConfig updates from server," "%s %s removed, %s added/updated", modelSetKey, len(deletedTraceConfigKeys), len(traceKeysUpdated)) self._tupleObservable.notifyOfTupleUpdate( TupleSelector(GraphDbTraceConfigTuple.tupleType(), dict(modelSetKey=modelSetKey)))
def processTupleAction(self, tupleAction: TupleActionABC) -> Deferred: yield None assert isinstance(tupleAction, BranchLiveEditTupleAction), \ "tupleAction is not BranchLiveEditTupleAction" # Ignore the updates if tupleAction.actionType == BranchLiveEditTupleAction.EDITING_STARTED: return coordSetId = tupleAction.branchTuple.coordSetId key = tupleAction.branchTuple.key tuple_ = BranchLiveEditTuple() tuple_.branchTuple = tupleAction.branchTuple tuple_.updatedByUser = tupleAction.updatedByUser tuple_.uiUpdateDate = tupleAction.dateTime tuple_.serverUpdateDate = datetime.now(pytz.utc) tuple_.updateFromActionType = tupleAction.actionType self._cache[(coordSetId, key)] = tuple_ self._tupleObservable.notifyOfTupleUpdate( TupleSelector(BranchLiveEditTuple.tupleName(), { "coordSetId": coordSetId, "key": key }) )
def _loadInPg(cls, plpy, filt: dict, tupleSelectorStr: str): tupleSelector = TupleSelector.fromJsonStr(tupleSelectorStr) tuples = cls.loadTuples(plpy, tupleSelector) payloadEnvelope = Payload(filt=filt, tuples=tuples).makePayloadEnvelope() vortexMsg = payloadEnvelope.toVortexMsg() return vortexMsg.decode()
def makeVortexMsg(self, filt: dict, tupleSelector: TupleSelector) -> Deferred: # Uncomment this to print the SQL being created # # criterias = tupleSelector..get('criteria', []) # newestDateTime = tupleSelector..get('newestDateTime') # oldestDateTime = tupleSelector..get('oldestDateTime') # # sql = self._makeSql(criterias, 0, newestDateTime, oldestDateTime) # logger.info(sql) logger.debug(tupleSelector.toJsonStr()) return (yield runPyInPg(logger, self._dbSessionCreator, self._loadInPg, self._importTuples, filt=filt, tupleSelectorStr=tupleSelector.toJsonStr())).encode()
def _processNewTuples(self, coordSetTuples): if not coordSetTuples: return self._coordSetCache = {c.id: c for c in coordSetTuples} self._tupleObservable.notifyOfTupleUpdate( TupleSelector(ModelCoordSet.tupleName(), {}))
def _makeCache(self, tupleSelector: TupleSelector) -> _CachedSubscribedData: tsStr = tupleSelector.toJsonStr() if tsStr in self.__cache: return self.__cache[tsStr] cache = _CachedSubscribedData(tupleSelector) self.__cache[tsStr] = cache return cache
def _afterCommit(self, tuple_, tuples, session, payloadFilt): selector = {} # Copy any filter values into the selector selector["likeTitle"] = payloadFilt["likeTitle"] tupleSelector = TupleSelector(InternalUserTuple.tupleName(), selector) self._tupleDataObserver.notifyOfTupleUpdate(tupleSelector) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(UserDetailTuple.tupleName(), {})) self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(UserListItemTuple.tupleName(), {})) for tuple in tuples: tuple.groupIds = [] for group in tuple.groups: tuple.groupIds.append(group.id) return True
def _forceLogout(self, ormSession, userName, deviceToken): ormSession.query(UserLoggedIn) \ .filter(UserLoggedIn.userName == userName) \ .filter(UserLoggedIn.deviceToken == deviceToken) \ .delete(synchronize_session=False) self._clientTupleObservable.notifyOfTupleUpdate( TupleSelector(UserLoggedInTuple.tupleType(), selector=dict(deviceToken=deviceToken)))
def start(self): (self._tupleObserver.subscribeToTupleSelector( TupleSelector(DispLevel.tupleType(), {})).subscribe(self._processNewTuples)) (self._tupleObserver.subscribeToTupleSelector( TupleSelector(DispLayer.tupleType(), {})).subscribe(self._processNewTuples)) (self._tupleObserver.subscribeToTupleSelector( TupleSelector(DispColor.tupleType(), {})).subscribe(self._processNewTuples)) (self._tupleObserver.subscribeToTupleSelector( TupleSelector(DispLineStyle.tupleType(), {})).subscribe(self._processNewTuples)) (self._tupleObserver.subscribeToTupleSelector( TupleSelector(DispTextStyle.tupleType(), {})).subscribe(self._processNewTuples))
def _removeTraceConfigFromCache(self, modelSetKey: str, traceConfigKeys: List[str]): subCache = self._cache[modelSetKey] logger.debug("Received TraceConfig deletes from server, %s %s removed", modelSetKey, len(traceConfigKeys)) for traceConfigKey in traceConfigKeys: if traceConfigKey in subCache: subCache.pop(traceConfigKey) self._tupleObservable.notifyOfTupleUpdate( TupleSelector(GraphDbTraceConfigTuple.tupleType(), dict(modelSetKey=modelSetKey)))
def _loadInPg(cls, plpy, tupleSelectorStr: str): tupleSelector = TupleSelector.fromJsonStr(tupleSelectorStr) columnPropKeys = tupleSelector.selector["columnPropKeys"] modelSetId = EventDBEventTupleProvider \ .getModelSetId(plpy, tupleSelector.selector['modelSetKey']) propByKey = cls._getPropertyByKey(plpy, modelSetId) tuples = EventDBEventTupleProvider \ .loadTuples(plpy, tupleSelector) data = '' # Write the heading cols = ['Date', 'Time', 'Milliseconds', 'UTC Offset'] for columnPropKey in columnPropKeys: cols.append('"%s"' % propByKey[columnPropKey].name) data += ','.join(cols) + '\r\n' # Write the data of the table for row in tuples: value = json.loads(row.value) tz = str(row.dateTime.strftime('%z')) cols = [row.dateTime.strftime('%d-%b-%Y'), row.dateTime.strftime('%H:%M:%S'), str(int(row.dateTime.microsecond / 1000)), '%s:%s' % (tz[:-2], tz[-2:])] for columnPropKey in columnPropKeys: prop = propByKey[columnPropKey] val = value.get(columnPropKey) if val is None: cols.append('') else: # Map the value if it has one. valKey = str(val).replace('null', 'none').lower() val = prop.nameByValueMap.get(valKey, val) if isinstance(val, int): cols.append(str(val)) else: cols.append('"%s"' % val) data += ','.join(cols) + '\r\n' return data
def login(self, loginTuple: UserLoginAction): """ Returns Deferred[UserLoginResponseTuple] """ loginResponse = None try: loginResponse = yield self._loginInDb(loginTuple) if loginTuple.isFieldService: yield self._fieldServiceHookApi.callLoginHooks(loginResponse) # except UserAlreadyLoggedInError as e: # pass # # except DeviceAlreadyLoggedInError as e: # pass # # except UserIsNotLoggedInToThisDeviceError as e: # pass except Exception as e: # Log the user out again if the hooks fail logoutTuple = UserLogoutAction(userName=loginTuple.userName, deviceToken=loginTuple.deviceToken) # Force logout, we don't care if it works or not. try: yield self._logoutInDb(logoutTuple) except UserIsNotLoggedInToThisDeviceError: pass raise e self._clientTupleObservable.notifyOfTupleUpdate( TupleSelector(UserLoggedInTuple.tupleType(), selector=dict(deviceToken=loginTuple.deviceToken))) self._adminTupleObservable.notifyOfTupleUpdateForTuple( LoggedInUserStatusTuple.tupleType()) return loginResponse
def updateBranch(self, branchTuple: BranchTuple): ts = TupleSelector( BranchLiveEditTuple.tupleType(), dict( coordSetId=branchTuple.coordSetId, key=branchTuple.key ) ) if not self._tupleObservable.hasTupleSubscribers(ts): return cacheKey = (branchTuple.coordSetId, branchTuple.key) liveEditTuple = BranchLiveEditTuple() liveEditTuple.branchTuple = branchTuple liveEditTuple.uiUpdateDate = branchTuple.updatedDate liveEditTuple.serverUpdateDate = datetime.now(pytz.utc) liveEditTuple.updateFromActionType = BranchLiveEditTuple.EDITING_SAVED self._cache[cacheKey] = liveEditTuple
def _getMappedTupleSelectors( self, tupleSelector: TupleSelector) -> List[TupleSelector]: # Get all tuple selectors allTupleSelectors = [ data.tupleSelector for data in self._observerDataByTupleSelector.values() ] # Create a dict so we end up with only unique ones tupleSelectorByStr = {tupleSelector.toJsonStr(): tupleSelector} # Run through the mappers for mapper in self._tupleSelectorUpdateMappers: mappedSelectors = mapper.mapTupleSelector(tupleSelector, allTupleSelectors) if mappedSelectors: tupleSelectorByStr.update( {ts.toJsonStr(): ts for ts in mappedSelectors}) # Return a list of tuple selectors return list(tupleSelectorByStr.values())
def __processUpdateFromClient(self, tupleAction: TupleActionABC) -> Deferred: dbSession = self._dbSessionCreator() try: encodedPayload = yield Payload(tuples=[tupleAction.branchTuple]) \ .toEncodedPayloadDefer() yield updateBranches.delay(tupleAction.modelSetId, encodedPayload) # Load the branch from the DB and tell the LiveDb update # that there is an update. branchIndex = dbSession.query(BranchIndex) \ .filter(BranchIndex.coordSetId == tupleAction.branchTuple.coordSetId) \ .filter(BranchIndex.key == tupleAction.branchTuple.key) \ .one() branchTuple = BranchTuple.loadFromJson(branchIndex.packedJson, branchIndex.importHash, branchIndex.importGroupHash) self._liveEditController.updateBranch(branchTuple) self._updateBranchKeyToIdMap() self._tupleObservable.notifyOfTupleUpdate( TupleSelector( BranchLiveEditTuple.tupleType(), dict(coordSetId=tupleAction.branchTuple.coordSetId, key=tupleAction.branchTuple.key))) except Exception as e: logger.exception(e) finally: dbSession.close()
def _sendLogoutUpdate(self, deviceToken: str): self._clientTupleObservable.notifyOfTupleUpdate( TupleSelector(UserLoggedInTuple.tupleType(), selector=dict(deviceToken=deviceToken)))
def _afterCommit(self, tuple_, tuples, session, payloadFilt): self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(UserLoginUiSettingTuple.tupleName(), {})) return True
def _tellObserver(self, tuple_, tuples, session, payloadFilt): self._tupleDataObserver.notifyOfTupleUpdate( TupleSelector(DocDbPropertyTuple.tupleName(), {}) ) return True
def _updateBranchKeyToIdMap(self): self._tupleObservable.notifyOfTupleUpdate( TupleSelector(BranchKeyToIdMapTuple.tupleType(), {}))
def _notifyObserver(self, tupleName: str, userId: str) -> None: self._tupleObserver.notifyOfTupleUpdate( TupleSelector(tupleName, {"userId": userId}))
def start(self): (self._tupleObserver .subscribeToTupleSelector(TupleSelector(ModelSet.tupleName(), {})) .subscribe(self._processNewTuples))
def _tellObserver(self, tuple_, tuples, session, payloadFilt): self._tupleObservable.notifyOfTupleUpdate( TupleSelector(SettingProperty.tupleName(), {})) return True