Exemplo n.º 1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.PRINCIPAL = config('PRINCIPAL', default=self.PRINCIPAL)
        self.METHODS_PREFIX = config('METHODS_PREFIX',
                                     default=self.METHODS_PREFIX)
        self.METHODS_SUFFIX = config('METHODS_SUFFIX',
                                     default=self.METHODS_SUFFIX)

        self.instance_id = ulid.new().str
        self.health_check_topic = f'system.app.{self.APP_NAME}.alive'

        self.exit_status = 0
        self.init()
        self.methods = {}
        self.thread_pool_executor = ThreadPoolExecutor()
        self.tasks_queue = asyncio.Queue()

        for thing_name in dir(self):
            thing = getattr(self, thing_name)
            method_name = getattr(thing, 'wamp_name', None)
            if method_name:
                self.methods[method_name] = (thing, thing.wamp_options)

        self.post_init()
Exemplo n.º 2
0
    def save(self, user_id: str = None):
        if self.id is None:
            self.id = str(ulid.new())
            self.created_at = int(datetime.now().timestamp())
            self.updated_at = int(datetime.now().timestamp())
            item = self.to_savable_object()
            item['id'] = 'Task:{}'.format(self.id)
            try:
                table.put_item(Item=item,
                               ConditionExpression='attribute_not_exists(id)')
                return self.to_returnable_object()
            except ClientError as e:
                logging.error(e)
                raise e
            except Exception as e:
                logging.error(e)
                raise e

        else:
            if user_id is None or user_id != self.owner:
                raise NotTaskOwnerError
            self.updated_at = int(datetime.now().timestamp())
            item = self.to_savable_object()
            item['id'] = 'Task:{}'.format(self.id)
            try:
                table.put_item(Item=item,
                               ConditionExpression='attribute_exists(id)')
                return self.to_returnable_object()
            except ClientError as e:
                logging.error(e)
                raise e
            except Exception as e:
                logging.error(e)
                raise e
Exemplo n.º 3
0
 def test_add_tasks(self):
     job_key = ulid.new().str
     TestLiveJobLog.job_log.add_job(job_key, 'an_image', 'www.example.com')
     tasks = [{
         'task_name': 'first',
         'task_args': [1, 2, 3]
     }, {
         'task_name': 'second',
         'task_args': [3, 4, 5]
     }]
     TestLiveJobLog.job_log.add_tasks(job_key, tasks)
     actual = TestLiveJobLog.job_log.get_job(job_key)
     assert actual == {
         '__callback':
         'www.example.com',
         '__image':
         'an_image',
         '__task_count_complete':
         '0',
         '__task_count_started':
         '0',
         '__task_count_total':
         '2',
         'tasks':
         '[{"args": [1, 2, 3], "status": 500, "result": {"stdout": null, "stderr": null}, "name": "first"}, {"args": [3, 4, 5], "status": 500, "result": {"stdout": null, "stderr": null}, "name": "second"}]'
     }
     TestLiveJobLog.job_log.clear_job(job_key)
     with pytest.raises(ValueError):
         TestLiveJobLog.job_log.get_job(job_key) is None
Exemplo n.º 4
0
def register(name, query):
    print('register')
    print(f'"name": "{name}"')
    print(f'"query": "{query}"')
    assert name
    assert query

    data = (
        # ulid.new().int,
        ulid.new().str,
        name,
        query,
        # int(hashlib.sha256(name.encode()).hexdigest(), 16)
        hashlib.sha256(name.encode()).digest())

    conn = sqlite3.connect(os.environ.get('INTERNAL_STORAGE'))

    cursor = None
    try:
        cursor = conn.cursor()
        cursor.execute(
            'INSERT INTO queries (id, name, query, hashed_name) VALUES (?, ?, ?, ?)',
            data)
        result = cursor.fetchall()
        conn.commit()
    except Exception:
        conn.rollback()
        raise
    finally:
        if cursor:
            cursor.close()
        conn.close()
Exemplo n.º 5
0
async def mcstop(ctx):
    transaction_id = ulid.new().str
    update_transaction_id(handler, transaction_id)
    logger.info(f'start transaction {transaction_id}')

    logger.info('call mcstop')
    await ctx.send('停止する')
    shutdown = Shutdown(logger)

    try:
        logger.info('stop minecraft server...')
        res = shutdown.stop_instance(project, gcp_zone, instance)
        logger.info('done stopping minecraft server. ')
    except Exception as e:
        logger.warning(f'{e}')
        await ctx.send('失敗した')
        exit(1)

    try:
        logger.info('unregister dns...')
        res = shutdown.delete_dns_record(dns_auth_email, dns_zone_id,
                                         dns_auth_key, dns_name)
        logger.info('done unregistering dns. ')
    except IndexError as e:
        logger.warning(f'{e}')
        # await ctx.send('停止してた')

    await ctx.send('停止した')
    logger.info(f'end transaction {transaction_id}')
Exemplo n.º 6
0
def insert_user(email, password):
    """
    Insert a new 'users' record.
    """

    try:

        new_id = ulid.new().str
        hashed_password = bcrypt.hashpw(bytes(password, 'utf-8'),
                                        bcrypt.gensalt()).decode('utf-8')

        db.insert(
            'users', {
                'email': email.lower(),
                'display_email': email,
                'password': hashed_password
            })

        user_dict = {'user_id': new_id, 'email': email}
        return user_dict

    except Exception as e:

        log.error('db_users.py::insert_user', str(e))
        con.commit()
        return None
Exemplo n.º 7
0
def guid(formatter=None):
    """Generate a new unique ID"""
    id = ulid.new().str
    if formatter:
        return formatter.format(id=id)
    else:
        return id
Exemplo n.º 8
0
async def mcstart(ctx):
    transaction_id = ulid.new().str
    update_transaction_id(handler, transaction_id)
    logger.info(f'start transaction {transaction_id}')

    logger.info('call mcstart')
    await ctx.send('起動する')
    standup = Standup(logger)

    try:
        logger.info('start minecraft server...')
        res = standup.start_instance(project, gcp_zone, instance)
        logger.info('done starting minecraft server. ')
    except Exception as e:
        logger.warning(f'{e}')
        await ctx.send('失敗した')

    logger.info('get instance external ip...')
    ip = standup.get_instance_external_ip_address(project, gcp_zone, instance)
    logger.info(f'done getting instance external ip {ip}')

    logger.info('register dns...')
    res = standup.add_dns_record(dns_auth_email, dns_zone_id, dns_auth_key,
                                 dns_name, ip)
    logger.info('done registering dns. ')

    if res:
        await ctx.send(f'起動した 接続先-> {dns_name}')
    else:
        await ctx.send(f'起動してた 接続先-> {dns_name}')

    logger.info(f'end transaction {transaction_id}')
Exemplo n.º 9
0
class Model:
    """Base data model schema for Cannlytics data models."""
    created_at: datetime = datetime.now()
    updated_at: datetime = datetime.now()
    uid: str = ulid.new().str.lower()
    ref: str = ''

    @classmethod
    def from_dict(cls, data):
        """Initiate a document from a dictionary."""

        # Split the kwargs into native ones and new ones.
        native_args, new_args = {}, {}
        for key, value in data.items():
            if key in cls.__annotations__:
                native_args[key] = value
            else:
                new_args[key] = value

        # Use the native arguments to create the class.
        ret = cls(**native_args)

        # Add the arguments to the model.
        for new_name, new_val in new_args.items():
            setattr(ret, new_name, new_val)

        return ret

    def to_dict(self) -> dict:
        """Returns the model's properties as a dictionary."""
        return vars(self).copy()
Exemplo n.º 10
0
def add_pass(request):
    try:
        pass_id = ulid.new()
        master_pass = request.session['master_pass']
        operation = manager.DBOperation(master_pass)
        e_pass = operation.encrypt_pass(request.POST['password'])
        pw_user = request.user
        Password.objects.create(pw_user=pw_user,
                                pass_id=pass_id,
                                pw=e_pass,
                                purpose=request.POST['purpose'],
                                description=request.POST['description'])

        try:
            pw_dict = request.session['pw_dict']
        except KeyError:
            return redirect('p_manager:index')

        pw_dict.append({
            'id': pass_id.str,
            'password': request.POST['password'],
            'purpose': request.POST['purpose'],
            'description': request.POST['description']
        })
        request.session['pw_dict'] = pw_dict
        print(pw_dict)
        return redirect('p_manager:index')

    except KeyError:
        return render(request, 'p_manager/add.html')
Exemplo n.º 11
0
    def new(self, last_fulid=None):
        """
        Method to create a new friendly ulid

        Arguments:
            last_ulid (ulid): The last ulid created

        Returns:
            fulid (ulid object): Friendly ulid object
        """

        temp_ulid = ulid.new()

        if last_fulid is None:
            id = 7 * self.charset[0].upper()
        else:
            last_fulid = fulid(self.charset, self.forbidden_charset,
                               last_fulid)
            id = self._encode_id(
                self._decode_id(last_fulid.id()) + 1,
                pad=7,
            )

        self.str = ''.join([
            temp_ulid.timestamp().str,
            temp_ulid.randomness().str[:9],
            id,
        ])

        return fulid(self.charset, self.forbidden_charset, self.str)
Exemplo n.º 12
0
def get_book_by_title(title: str):
    request_id = request.headers.get('Request-Id', str(ulid.new()))
    log = logger.new(request_id=request_id)
    log.info("Looking up the book {title}.", data={'title': title})
    time.sleep(2)
    log.info("We have {numCopy} copies of the book {title}.", data={'title': title, 'numCopy': 4})
    return flask.jsonify({'requestId': request_id, 'book': title})
Exemplo n.º 13
0
 def test_can_specify_delete_subcommand(self):
     arguments = [
         'del',
         ulid.new().str
     ]
     parsed = self.parser.parse_args(arguments)
     assert parsed.subcommand == arguments[0]
     assert parsed.ulid == arguments[1]
Exemplo n.º 14
0
def before_request():
    """
    Before request gather log details
    """

    g.start_time = time.time()
    g.request_id = str(ulid.new())
    get_log().info("CorrelationId", correlationId=request.headers.get("x-linz-correlation-id", str(ulid.new())))
Exemplo n.º 15
0
    def test_delete_subcomand_deletes_task(self):
        arguments = ['del', ulid.new().str]
        self.parser_args.subcommand = arguments[0]
        self.parser_args.ulid = arguments[1]

        main()

        self.tm.return_value.delete.assert_called_once_with(id=arguments[1])
Exemplo n.º 16
0
def test_write_read_log() -> None:
    wal = new_wal(state_transition_noop)

    block_number = BlockNumber(1337)
    block_hash = make_block_hash()
    block = Block(block_number=block_number,
                  gas_limit=BlockGasLimit(1),
                  block_hash=block_hash)
    unlocked_amount = TokenAmount(10)
    returned_amount = TokenAmount(5)
    participant = make_address()
    partner = make_address()
    locksroot = make_locksroot()
    contract_receive_unlock = ContractReceiveChannelBatchUnlock(
        transaction_hash=make_transaction_hash(),
        canonical_identifier=make_canonical_identifier(
            token_network_address=make_address()),
        receiver=participant,
        sender=partner,
        locksroot=locksroot,
        unlocked_amount=unlocked_amount,
        returned_tokens=returned_amount,
        block_number=block_number,
        block_hash=block_hash,
    )

    state_changes1 = wal.storage.get_statechanges_by_range(
        RANGE_ALL_STATE_CHANGES)
    count1 = len(state_changes1)

    dispatch(wal, [block])

    state_changes2 = wal.storage.get_statechanges_by_range(
        RANGE_ALL_STATE_CHANGES)
    count2 = len(state_changes2)
    assert count1 + 1 == count2

    dispatch(wal, [contract_receive_unlock])

    state_changes3 = wal.storage.get_statechanges_by_range(
        RANGE_ALL_STATE_CHANGES)
    count3 = len(state_changes3)
    assert count2 + 1 == count3

    result1, result2 = state_changes3[-2:]
    assert isinstance(result1, Block)
    assert result1.block_number == block_number

    assert isinstance(result2, ContractReceiveChannelBatchUnlock)
    assert result2.receiver == participant
    assert result2.sender == partner
    assert result2.locksroot == locksroot
    assert result2.unlocked_amount == unlocked_amount
    assert result2.returned_tokens == returned_amount

    # Make sure state snapshot can only go for corresponding state change ids
    with pytest.raises(sqlite3.IntegrityError):
        wal.storage.write_state_snapshot(State(), StateChangeID(ulid.new()), 1)
Exemplo n.º 17
0
    def create_new_job(self, image_name, callback, tasks):
        self._log_operation(
            'Creating new job with image {img} and callback {cb}'.format(
                img=image_name, cb=callback))

        identifier = ulid.new().str
        self._job_queue.add_new_job(identifier, image_name, callback, tasks)
        self._run_tasks()
        return identifier
Exemplo n.º 18
0
    def post(self):
        """
        Insert an issue into the database. Parse the provided JSON data to
        validate that we have the correct fields.

        Additional server-side validation is left for a future exercise.
        """
        args = self.parser.parse_args(strict=True)
        query_db('INSERT INTO issues(id, data) VALUES(?, ?)', (str(ulid.new()), json.dumps(args)))
        return { "message": "Success" }, 200
Exemplo n.º 19
0
def _generate_id() -> str:
    """Create ULID

    See: https://github.com/ahawker/ulid

    Returns:
        str: ULID random, unique identifier
    """

    return ulid.new()
Exemplo n.º 20
0
class Holding(Consumer):
    class Meta(Consumer.Meta):
        table_name = os.environ['STAGE'] + '-stonks-holding'

    holdingId = UnicodeAttribute(hash_key=True,
                                 default_for_new=str(ulid.new()))
    symbol = UnicodeAttribute(default="fresh")
    symbolIndex = SymbolIndex()
    units = NumberAttribute(default=0)
    averagePrice = NumberAttribute(default=0)
    latestTradePrice = NumberAttribute(default=0)
    bookCost = NumberAttribute(default=0)

    # Consumes the event
    @classmethod
    def consume(cls, event, consumer=None):
        if (consumer == None):
            symbol = event['symbol']
            results = cls.symbolIndex.query(symbol)
            results = [result for result in results]
            if (len(results) == 0):
                consumer = cls(symbol=symbol, holdingId=str(ulid.new()))
                consumer.save()
            else:
                consumer = results[0]

        consumer.refresh(consistent_read=True)

        # Bit of business logic
        action = event['action']
        units = int(event['units'])
        tradePrice = int(event['tradePrice'])
        holdingBookCost = int(consumer.averagePrice) * int(consumer.units)
        eventAveragePrice = int(event['averagePrice'])
        eventTotalCost = eventAveragePrice * units

        if (action == ActionEnum.BUY):
            units += int(consumer.units)
            holdingBookCost += eventTotalCost
        elif (action == ActionEnum.SELL):
            units = int(consumer.units) - units
            holdingBookCost -= eventTotalCost

        try:
            averagePrice = int(holdingBookCost / units)
        except ZeroDivisionError:
            averagePrice = 0
        # Update fields
        consumer.update(actions=[
            cls.units.set(units),
            cls.averagePrice.set(averagePrice),
            cls.bookCost.set(holdingBookCost),
            cls.latestTradePrice.set(tradePrice),
            cls.version.set(event['eventId'])
        ])
Exemplo n.º 21
0
 def new(
     self,
     name: str,
     id: str = None,
     created_at: datetime = None,
 ) -> Org:
     return Org(
         id=ulid.new().str if id is None else id,
         created_at=datetime.now() if created_at is None else created_at,
         name=name,
     )
Exemplo n.º 22
0
 def __init__(self,
              builder_cats=(),
              source_object=None,
              builder_predicate=None):
     self.builder_predicate = builder_predicate
     self.id = ulid.new().str
     # link class with builder cat
     # insert in the class
     for cat in builder_cats:
         BuilderGraph.add_edge(cat.id, self.id, weight=None)
     ClassSet[self.id] = self
def main():
    # outputディレクトリ配下のjsonファイルを順番に読み込みデータベースに投入する
    for json_file_name in listup_files('./output/*'):
        with open(json_file_name, 'r', encoding='utf-8') as f:
            print(f)
            dict_data = dict()
            for data in json.load(f):
                dict_data[str(ulid.new())] = data
                print(data)

            import_to_db(dict_data)
Exemplo n.º 24
0
def index():
    """ index req """

    dt = datetime.datetime.now()
    dtstr = str(dt)
    myulid = ulid.new()
    strmyulid = myulid.str
    intmyulid = myulid.int
    bmyulid = bazed_ulid(intmyulid)

    return template(tpl, **params)
Exemplo n.º 25
0
async def index():
    dt = datetime.datetime.now()
    dtstr = str(dt)
    myulid = ulid.new()
    strmyulid = myulid.str
    intmyulid = myulid.int
    bmyulid = bazed_ulid(intmyulid)

    return await render_template_string(
        "<tt>The %s hello from engine %s with ver.%s at %s<br />as long %s [len%d] and short %s [len%d]</tt>"
        % (params["web_mode"], params["web_driver"], version, dtstr, strmyulid,
           len(strmyulid), bmyulid, len(bmyulid)))
Exemplo n.º 26
0
    def test_freeze_subcommand_freezes_task(self):
        arguments = ['freeze', ulid.new().str]
        self.parser_args.subcommand = arguments[0]
        self.parser_args.ulid = arguments[1]
        self.parser_args.parent = False

        main()

        self.tm.return_value.freeze.assert_called_once_with(
            id=arguments[1],
            parent=False,
        )
Exemplo n.º 27
0
 def test_add_job_and_delete(self):
     job_key = ulid.new().str
     TestLiveJobLog.job_log.add_job(job_key, 'an_image', 'www.example.com')
     actual = TestLiveJobLog.job_log.get_job(job_key)
     assert actual == {
         '__image': 'an_image',
         '__callback': 'www.example.com',
         'tasks': '[]'
     }
     TestLiveJobLog.job_log.clear_job(job_key)
     with pytest.raises(ValueError):
         TestLiveJobLog.job_log.get_job(job_key) is None
Exemplo n.º 28
0
    def test_unfreeze_parent_subcommand_unfreezes_task(self):
        arguments = ['unfreeze', '-p', ulid.new().str]
        self.parser_args.subcommand = arguments[0]
        self.parser_args.ulid = arguments[1]
        self.parser_args.parent = True

        main()

        self.tm.return_value.unfreeze.assert_called_once_with(
            id=arguments[1],
            parent=True,
        )
Exemplo n.º 29
0
def index():
    """ index req """

    dt = datetime.datetime.now()
    dtstr = str(dt)
    myulid = ulid.new()
    strmyulid = myulid.str
    intmyulid = myulid.int
    bmyulid = bazed_ulid(intmyulid)

    return "<tt>The {{python_mode}} hello from engine ver.%s at %s<br />as long %s [len%d] and short %s [len%d]</tt>" % (
        version, dtstr, strmyulid, len(strmyulid), bmyulid, len(bmyulid))
Exemplo n.º 30
0
    def generate_rand(author, authorId):
        likes = random.randint(0, 20000)
        reads = random.randint(0, 10000) + likes
        popularity = round(random.random(), 2)
        id = ulid.new().str
        tags = Post.generate_tags()

        return Post(author, authorId, id, likes, popularity, reads,
                    tags).generate()


# print(Post.generate_rand())