Ejemplo n.º 1
0
class raitings_by_shop_id(Model):
    __keyspace__="shop_raitings"
    store_id = columns.SmallInt(primary_key=True)
    product_name = columns.Text(primary_key=True)
    ts = columns.DateTime()
    price = columns.Float()
    raiting = columns.SmallInt()
Ejemplo n.º 2
0
class Response(UserType):
    """
    Response type
    """
    content_type = columns.Text()
    content_body = columns.Text()
    status_code = columns.SmallInt()
    headers = columns.Text()
    response_on = columns.DateTime()

    @classmethod
    def validate_response(cls, response):
        try:
            r = cls()
            if hasattr(response, 'content_type'):
                r.content_type = response.content_type
            if hasattr(response, 'data'):
                r.content_body = response.data
            if hasattr(response, 'status_code'):
                r.status_code = response.status_code
            if hasattr(response, 'headers'):
                r.headers = json.dumps(response.headers.items(),
                                       cls=HttpHeadersEncoder)
            r.response_on = datetime.utcnow()
            return r
        except Exception as e:
            logger.error('Log response error: %s' % e)
Ejemplo n.º 3
0
class sparkServerModel(DjangoCassandraModel):
    serverid = columns.Integer(primary_key=True, default=uuid.uuid4)
    address = columns.Text()
    port = columns.SmallInt()

    def generate_address(self):
        return "spark://" + self.address + ":" + str(self.port)
Ejemplo n.º 4
0
class FeedEntryModel(DjangoCassandraModel):
    class Meta:
        get_pk_field = 'id'

    userid = columns.UUID(primary_key=True)
    id = columns.TimeUUID(primary_key=True,
                          default=timeuuid_now,
                          clustering_order="DESC")
    comment_count = columns.SmallInt(default=0)

    @property
    def published(self):
        return datetime_from_uuid1(self.id)
Ejemplo n.º 5
0
class SeismicEvent(DjangoCassandraModel):
    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    mag = columns.Double()
    place = columns.Text()
    time = columns.DateTime()
    updated = columns.DateTime()
    tz = columns.SmallInt()
    status = columns.Text()
    tsunami = columns.TinyInt()
    sig = columns.SmallInt()
    net = columns.Text()
    code = columns.Text()
    ids = columns.Text()
    sources = columns.Text()
    types = columns.Text()
    nst = columns.SmallInt()
    dmin = columns.Double()
    rms = columns.Double()
    gap = columns.SmallInt()
    magType = columns.Text()
    type = columns.Text()
    latitude = columns.Double()
    longitude = columns.Double()
    depth = columns.Double()
Ejemplo n.º 6
0
class Match(Base):
    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    date = columns.Text()
    venue = columns.Text()
    tournament = columns.Text()
    competitor_1 = columns.Map(columns.Text(), columns.Integer())
    competitor_2 = columns.Map(columns.Text(), columns.Integer())
    result = columns.SmallInt()

    def get_data(self):
        return {
            'id': str(self.id),
            'date': self.date,
            'venue': self.venue,
            'tournament': self.tournament,
            'competitor_1': self.competitor_1,
            'competitor_2': self.competitor_2,
            'result': self.result
        }
Ejemplo n.º 7
0
class tweepyServerModel(DjangoCassandraModel):
    serverid = columns.Integer(primary_key=True, default=uuid.uuid4)
    address = columns.Text()
    port = columns.SmallInt()

    def writeInstructs(self, sentimentId, mode, keywords):
        import sys
        sys.path.append('/Users/melihozkan/Desktop/Projects/BitirmeProjesi/')
        from utils.InstructManager import InstructManager
        im = InstructManager()
        return im.writeInstructions(sentimentId, mode, keywords, True)

    def writeStopFile(self, sentimentId):
        import sys
        sys.path.append('/Users/melihozkan/Desktop/Projects/BitirmeProjesi/')
        from utils.hdfsClient import client
        tmp = client()
        tmp.overwrite(path="/tweepy/" + str(sentimentId) + "_stop.txt",
                      data="test")

    def generate_address(self):
        return self.address + ":" + str(self.port)
Ejemplo n.º 8
0
 class Allv4Datatypes(UserType):
     a = columns.Date()
     b = columns.SmallInt()
     c = columns.Time()
     d = columns.TinyInt()
Ejemplo n.º 9
0
class Transfer(AbstractBaseModel, TaskQueueMixin, MachineMixin):
    """
    Transfer
    """
    __table_name__ = 'transfer'

    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    description = columns.Text()
    value = columns.UserDefinedType(Amount, required=True)
    account_id = columns.UUID(required=True)
    destination_id = columns.UUID(required=True)
    # signatures
    signatures = columns.Map(columns.Text, columns.Text)
    #
    type = columns.Text(discriminator_column=True)
    status = columns.TinyInt(default=TRANSFER_CREATED[0])
    # reverse
    reversed = columns.Boolean(default=False)
    value_reversed = columns.UserDefinedType(Amount)
    # failure
    failure_code = columns.SmallInt()
    # cancellation data
    cancellation_data = columns.UserDefinedType(Cancellation)
    # tasks
    tasks = columns.Map(columns.Text, columns.Text)

    def __init__(self, *args, **kwargs):
        super(Transfer, self).__init__(*args, **kwargs)
        self._init_machine()

    # ---------------
    # Machine Methods
    # ---------------
    def _init_machine(self):
        """
        Method to hook a state machine to the instance
        """
        states = list(TRANSFER_STATUS_STRING_CHOICES)
        transitions = TRANSFER_STATE_TRANSITIONS
        self.machine = Machine(model=self, states=states, transitions=transitions,
                               auto_transitions=False, send_event=True,
                               initial=TRANSFER_STATUS_MAP[self.status],
                               after_state_change='_state_changed')

    def _state_changed(self, event):
        """
        callback from state machine to change status on instance and persist
        :param event: EventData
        :return:
        """
        self.status = TRANSFER_STATUS_STRING_MAP[event.state.name]
        persist = event.kwargs.get('persist', False)
        if persist:
            self.save()

    def _is_txn_valid(self, txn=None):
        return txn is not None and self.id == txn.source_id

    def _get_act_txn(self, event):
        act_txn = event.kwargs.get('act_txn')
        if self._is_txn_valid(act_txn):
            return act_txn

        act_txn = DebitAccountTransaction.objects.filter(account_id=self.account_id).get()
        if self._is_txn_valid(act_txn):
            return act_txn

        if act_txn is None:
            raise AccountTransactionNotAvailable

    def _get_dst_txn(self, event):
        dst_txn = event.kwargs.get('dst_txn')
        if self._is_txn_valid(dst_txn):
            return dst_txn

        dst_txn = CreditAccountTransaction.objects.filter(account_id=self.destination_id).get()
        if self._is_txn_valid(dst_txn):
            return dst_txn

        if dst_txn is None:
            raise DestinationTransactionNotAvailable

    def set_account_signature(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        act_signature = event.kwargs.get('act_signature')
        # TODO is signature valid
        if act_signature:
            self.signatures['act_signature'] = act_signature

    def set_destination_signature(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        dst_signature = event.kwargs.get('dst_signature')
        # TODO is signature valid
        if dst_signature:
            self.signatures['dst_signature'] = dst_signature

    def has_valid_account_signature(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        signature = self.signatures.get('act_signature')
        # TODO is signature valid
        if signature is not 'signature':
            self.failure_code = FAILURE_INVALID_ACCOUNT_SIGNATURE[0]
            return False
        return True

    def has_valid_destination_signature(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        signature = self.signatures.get('dst_signature')
        # TODO is signature valid
        if signature is not 'signature':
            self.failure_code = FAILURE_INVALID_DESTINATION_SIGNATURE[0]
            return False
        return True

    def has_transaction_account_succeed(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        act_txn = self._get_act_txn(event)
        if act_txn.is_succeed():
            return True
        if act_txn.is_failed():
            self.failure_code = FAILURE_INVALID_ACCOUNT_OPERATION_ERROR[0]
        return False

    def has_transaction_destination_succeed(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        dst_txn = self._get_dst_txn(event)
        if dst_txn.is_succeed():
            return True
        if dst_txn.is_failed():
            self.failure_code = FAILURE_INVALID_ACCOUNT_OPERATION_ERROR[0]
        return False

    def has_failure_code(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        return self.failure_code is not None

    def set_cancellation_data(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        reason = event.kwargs.get('reason')
        if reason is None:
            raise ReasonNotAvailable

        user = event.kwargs.get('user')
        if user is not None:
            self.cancellation_data = Cancellation(reason=reason, user=CancellationByUser(id=user.id))
            return

        queue_name = event.kwargs.get('queue_name')
        task_name = event.kwargs.get('task_name')
        if queue_name is not None and task_name is not None:
            task = CancellationByTask(name=task_name, queue_name=queue_name)
            cancellation = Cancellation(reason=reason, task=task)
            self.cancellation_data = cancellation

    def has_cancellation_data(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        return self.cancellation_data.type is not None

    def execute_cancel(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        if self.has_cancellation_data(event):
            persist = event.kwargs.get('persist', False)
            #
            act_txn = self._get_act_txn(event)
            act_txn = self._cancel_txn(act_txn, persist)
            event.kwargs.update({'act_txn': act_txn})
            #
            dst_txn = self._get_dst_txn(event)
            dst_txn = self._cancel_txn(dst_txn, persist)
            event.kwargs.update({'dst_txn': dst_txn})

    def remove_expired_task(self, event, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        task_name = self.tasks.get('cancel')
        if task_name:
            queue_name = self.__table_name__
            self.remove_task(queue_name=queue_name, name=task_name)
            del self.tasks['cancel']

    # ---------------
    # Task Methods
    # ---------------
    def create_expired_task(self):
        kwargs = dict()
        action = 'cancel'
        url_params = {'id': self.id, 'action': action}
        url = url_for('tasks.transfer_actions', **url_params)
        kwargs['queue_name'] = self.__table_name__
        kwargs['method'] = 'PUT'
        kwargs['url'] = url
        kwargs['eta'] = datetime.utcnow() + timedelta(hours=24)
        kwargs['payload'] = urllib.urlencode({'action': action,
                                              'metadata': json.dumps({'reason': 'expired'})})
        # context['target'] = modules.get_current_module_name()
        task = self.add_task(**kwargs)
        if isinstance(task, taskqueue.Task):
            self.tasks['cancel'] = task.name
Ejemplo n.º 10
0
class AccountTransaction(AbstractBaseModel):
    """
    AccountTransaction
    """
    __table_name__ = 'account_transaction'

    account_id = columns.UUID(primary_key=True)
    id = columns.TimeUUID(primary_key=True,
                          clustering_order='DESC',
                          default=uuid.uuid1)
    description = columns.Text()
    value = columns.UserDefinedType(Amount)
    source_id = columns.UUID()
    signature = columns.Text()
    type = columns.Text(discriminator_column=True)
    status = columns.TinyInt(default=TRANSACTION_PENDING[0], index=True)
    # failure code
    failure_code = columns.SmallInt()

    def __init__(self, *args, **kwargs):
        super(AccountTransaction, self).__init__(*args, **kwargs)
        self._init_machine()

    # ---------------
    # Machine Methods
    # ---------------
    def _init_machine(self, transitions=TRANSACTION_STATE_TRANSITIONS):
        """
        Method to hook a state machine to the instance
        """
        states = list(TRANSACTION_STATUS_STRING_CHOICES)
        self.machine = Machine(model=self,
                               states=states,
                               transitions=transitions,
                               auto_transitions=False,
                               send_event=True,
                               initial=TRANSACTION_STATUS_MAP[self.status],
                               after_state_change='_state_changed')

    def _state_changed(self, event):
        """
        callback from state machine to change status on instance and persist
        :param event: EventData
        :return:
        """
        self.status = TRANSACTION_STATUS_STRING_MAP[event.state.name]
        persist = event.kwargs.get('persist', False)
        if persist:
            self.save()

    def execute_operation(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        transfer = event.kwargs.get('transfer')
        if transfer is None:
            raise TransferNotAvailable

        if transfer.is_sealed():
            account = CurrentAccount.objects(id=self.account_id).get()
            response = account.execute_pending(self.id)
            event.kwargs.update({'operation_response': response})
            account.save()
        else:
            event.kwargs.update({
                'operation_response':
                Response(error=FAILURE_TRANSFER_IS_NOT_SEALED)
            })

    def execute_cancel(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        transfer = event.kwargs.get('transfer')
        if transfer is None:
            raise TransferNotAvailable

        if transfer.is_created():
            account = CurrentAccount.objects(id=self.account_id).get()
            response = account.cancel_pending(self.id)
            event.kwargs.update({'operation_response': response})
            account.save()

    def has_execute_succeed(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        response = event.kwargs.get('operation_response')
        if not response.is_success:
            self.failure_code = response.error_code
            return False
        return True

    def has_failure_code(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        return self.failure_code is not None
Ejemplo n.º 11
0
class receipts_by_store_id(Model):
    __keyspace__="shop_receipts"
    store_id = columns.SmallInt(primary_key=True)
    topic = columns.Text(primary_key=True)
    ts = columns.DateTime()
    value = columns.SmallInt()
Ejemplo n.º 12
0
 class v4DatatypesModel(Model):
     id = columns.Integer(primary_key=True)
     a = columns.Date()
     b = columns.SmallInt()
     c = columns.Time()
     d = columns.TinyInt()