def _update_collection(self, collection_name, query, set_statement, upsert, trx_record): dbcol = self.db[collection_name] ret = dbcol.update(query, set_statement, upsert=upsert) if "error" in ret and ret['error'] is not None: raise MongoUpdateException("Error updating object: %s, %s" % (str(ret), query)) if "ok" in ret and ret['ok'] != 1: raise MongoUpdateException("Error updating object: %s, %s" % (str(ret), query)) if trx_record and not ret['updatedExisting']: raise MongoNotCurrentObjectException( "Object with txn counter not found!: %s" % query) if not upsert and not ret['updatedExisting']: raise MongoNotFoundUpdatingException("Object not found: %s" % query) # if upsert and not ret['updatedExisting']: #Nonsense, if upsert does insert instead of # update updatedExisting is False # raise MongoUpsertException("Error upserting: %s\nRet: %s" % (query, ret)) if "jnote" in ret: if ret['jnote'] == "journaling not enabled on this server": logger.warning("Mongo journaling not enabled in this server!!") else: logger.debug(ret['jnote']) return ret
def _trace_user_action(action_name, auth_user, client_user, client_token, description, async_process): ip_address = get_user_ip() try: auth_user = auth_user or client_user or "UNKNOWN" action = UserTracedAction("REST", action_name, ip_address, auth_user, anonymous_user_token=client_token, description=description) register_user_action(action, async_process=async_process) except Exception as e: logger.warning("Error saving user trace: %s" % e)
def _trace_achievement_action(action_name, description, username, async_process): try: if username: if action_name == "publish": # Uses publish register_publish(username, BlockVersion.loads(description), async_process=async_process) elif action_name == "get_version_delta_info": # User uses biicode register_get_version_delta_info(username, async_process=async_process) except Exception as e: logger.warning("Error sending to action to achievement: %s" % e)
def _trace_mixpanel(action_name, auth_user, client_user, client_token, kwargs): from mixpanel import Mixpanel from mixpanel_async import AsyncBufferedConsumer some_user_id = auth_user or client_user or client_token try: if action_name in ["get_server_info", "publish"]: mp = Mixpanel(BII_MIXPANEL_TOKEN, consumer=AsyncBufferedConsumer()) properties = {'action': action_name, 'anonymous': (some_user_id == client_token)} if action_name == "get_server_info": properties["os"] = kwargs["bson_data"]["data"][0]["family"] properties["biicode_version"] = kwargs["bson_data"]["data"][1] mp.track(some_user_id, BII_API_MIXPANEL_EVENT_NAME, properties) except Exception as e: logger.warning("Error sending action to mixpanel: %s" % e)
def _trace_mixpanel(action_name, auth_user, client_user, client_token, kwargs): from mixpanel import Mixpanel from mixpanel_async import AsyncBufferedConsumer some_user_id = auth_user or client_user or client_token try: if action_name in ["get_server_info", "publish"]: mp = Mixpanel(BII_MIXPANEL_TOKEN, consumer=AsyncBufferedConsumer()) properties = { 'action': action_name, 'anonymous': (some_user_id == client_token) } if action_name == "get_server_info": properties["os"] = kwargs["bson_data"]["data"][0]["family"] properties["biicode_version"] = kwargs["bson_data"]["data"][1] mp.track(some_user_id, BII_API_MIXPANEL_EVENT_NAME, properties) except Exception as e: logger.warning("Error sending action to mixpanel: %s" % e)
def _trace_ga(action_name, auth_user, client_user, client_token, label): from google_measurement_protocol import Event, report try: username = auth_user or client_user or client_token logger.debug("Auth user / client_user: %s/%s/%s" % (auth_user, client_user, client_token)) logger.debug("Enter analytics with params: %s, %s, %s" % (action_name, username, label)) aes_manager = AESEncryption(BII_AES_SECRET_KEY) client_id = aes_manager.encrypt(username) logger.debug("Client id encoded: %s" % str(client_id)) event = Event('bii_api_call', action_name, label) ret = report(BII_GA_API_KEY, client_id, event) if ret[0].status_code == 200: logger.debug("ANALYTICS: Logged: %s to google analytics" " for visitor %s - %s" % (action_name, username, client_id)) else: logger.debug("Error logging to analytics: %s " % (ret[0].reason)) except Exception as exc: logger.warning("Error sending to action to ga: %s" % (exc))
def deserialize(self, data): if data is None: return None try: kls = Deserializer.get_polymorphic_class(data) r = kls.deserialize(data) r.name = BlockCellName(data[Cell.SERIAL_NAME_KEY]) r.root = ID.deserialize(data[Cell.SERIAL_ROOT_KEY]) r.ID = self.id_type.deserialize(data[Cell.SERIAL_ID_KEY]) r.type = BiiType(data[Cell.SERIAL_TYPE_KEY]) r.hasMain = data[Cell.SERIAL_MAIN_KEY] try: r.dependencies.cell_name = r.name except AttributeError: pass return r except Exception as e: tb = traceback.format_exc() logger.warning(tb) raise BiiSerializationException(e)
def _update_collection(self, collection_name, query, set_statement, upsert, trx_record): dbcol = self.db[collection_name] ret = dbcol.update(query, set_statement, upsert=upsert) if "error" in ret and ret['error'] is not None: raise MongoUpdateException("Error updating object: %s, %s" % (str(ret), query)) if "ok" in ret and ret['ok'] != 1: raise MongoUpdateException("Error updating object: %s, %s" % (str(ret), query)) if trx_record and not ret['updatedExisting']: raise MongoNotCurrentObjectException("Object with txn counter not found!: %s" % query) if not upsert and not ret['updatedExisting']: raise MongoNotFoundUpdatingException("Object not found: %s" % query) # if upsert and not ret['updatedExisting']: #Nonsense, if upsert does insert instead of # update updatedExisting is False # raise MongoUpsertException("Error upserting: %s\nRet: %s" % (query, ret)) if "jnote" in ret: if ret['jnote'] == "journaling not enabled on this server": logger.warning("Mongo journaling not enabled in this server!!") else: logger.debug(ret['jnote']) return ret
def _rollback_transaction(self, excp, brl_block): '''rollback transaction for publish''' logger.warning(str(excp) + '\nRolling back publish transaction') self._store.rollBackBlockTransaction(brl_block) self._store.finishBlockTransaction(brl_block)