def stats(self): tstamp = time.time() rusage = {} ru = resource.getrusage(resource.RUSAGE_SELF) for attr in (u'ru_idrss', u'ru_inblock', u'ru_isrss', u'ru_ixrss', u'ru_majflt', u'ru_maxrss', u'ru_minflt', u'ru_msgrcv', u'ru_msgsnd', u'ru_nivcsw', u'ru_nsignals', u'ru_nswap', u'ru_nvcsw', u'ru_oublock', u'ru_stime', u'ru_utime'): if hasattr(ru, attr): rusage[attr] = getattr(ru, attr) result = { u"address": self.tether.address, u"agent": self.tether.agent, u"type": self.tether.agent_type, u"timestamp": timestamp(tstamp * 1000), u"pid": self.pid, u"rusage": rusage, u"times": os.times(), u"command": map(unicode, [sys.executable] + sys.argv), u"incoming_count": app.incoming, u"outgoing_count": app.outgoing, u"incoming_count_lib": lib.incoming, u"outgoing_count_lib": lib.outgoing, u"incoming_rate": self.incoming.rate(app.incoming, tstamp), u"outgoing_rate": self.outgoing.rate(app.outgoing, tstamp), u"incoming_rate_lib": self.incoming_lib.rate(lib.incoming, tstamp), u"outgoing_rate_lib": self.outgoing_lib.rate(lib.outgoing, tstamp) } return result
def stats(self): tstamp = time.time() rusage = {} ru = resource.getrusage(resource.RUSAGE_SELF) for attr in (u'ru_idrss', u'ru_inblock', u'ru_isrss', u'ru_ixrss', u'ru_majflt', u'ru_maxrss', u'ru_minflt', u'ru_msgrcv', u'ru_msgsnd', u'ru_nivcsw', u'ru_nsignals', u'ru_nswap', u'ru_nvcsw', u'ru_oublock', u'ru_stime', u'ru_utime'): if hasattr(ru, attr): rusage[attr] = getattr(ru, attr) result = {u"address": self.tether.address, u"agent": self.tether.agent, u"type": self.tether.agent_type, u"timestamp": timestamp(tstamp*1000), u"pid": self.pid, u"rusage": rusage, u"times": os.times(), u"command": map(unicode, [sys.executable] + sys.argv), u"incoming_count": app.incoming, u"outgoing_count": app.outgoing, u"incoming_count_lib": lib.incoming, u"outgoing_count_lib": lib.outgoing, u"incoming_rate": self.incoming.rate(app.incoming, tstamp), u"outgoing_rate": self.outgoing.rate(app.outgoing, tstamp), u"incoming_rate_lib": self.incoming_lib.rate(lib.incoming, tstamp), u"outgoing_rate_lib": self.outgoing_lib.rate(lib.outgoing, tstamp)} return result
def ticket_created(self, ticket): _time = proton.timestamp(time.time() * 1000) event = {"category": "created", "time": _time, "ticket": ticket.id, "author": ticket['reporter']} event['state'] = dict((k, self._transform_value(k, ticket[k])) for k in ticket.values) QueueFeeder(self.env).send_events([event])
def ticket_comment_modified(self, ticket, cdate, author, comment, old_comment): _time = proton.timestamp(time.time() * 1000) event = {"category": "changed", "time": _time, "ticket": ticket.id, "author": author, "cdate": cdate, 'change': {"comment": comment}} QueueFeeder(self.env).send_events([event])
def encode_amqp_type(amqp_type, test_value): """Encode an AMQP type from a stringified test_value""" if amqp_type == 'null': return None if amqp_type == 'boolean': return True if test_value == 'True' else False if amqp_type == 'ubyte': return proton.ubyte(int(test_value, 16)) if amqp_type == 'ushort': return proton.ushort(int(test_value, 16)) if amqp_type == 'uint': return proton.uint(int(test_value, 16)) if amqp_type == 'ulong': return proton.ulong(int(test_value, 16)) if amqp_type == 'byte': return proton.byte(int(test_value, 16)) if amqp_type == 'short': return proton.short(int(test_value, 16)) if amqp_type == 'int': return proton.int32(int(test_value, 16)) if amqp_type == 'long': return _compat.str2long(test_value, 16) if amqp_type == 'float': return proton.float32(struct.unpack('!f', _compat.decode_hex(test_value[2:]))[0]) if amqp_type == 'double': return struct.unpack('!d', _compat.decode_hex(test_value[2:]))[0] if amqp_type == 'decimal32': return proton.decimal32(int(test_value[2:], 16)) if amqp_type == 'decimal64': return proton.decimal64(_compat.str2long(test_value[2:], 16)) if amqp_type == 'decimal128': return proton.decimal128(_compat.decode_hex(test_value[2:])) if amqp_type == 'char': if len(test_value) == 1: # Format 'a' return proton.char(test_value) return proton.char(_compat.unichr(int(test_value, 16))) if amqp_type == 'timestamp': return proton.timestamp(int(test_value, 16)) if amqp_type == 'uuid': return uuid.UUID(test_value) if amqp_type == 'binary': return test_value.encode('utf-8') if amqp_type == 'string': return _compat.unicode(test_value) if amqp_type == 'symbol': return proton.symbol(test_value) if amqp_type == 'list': return AmqpTypesTestSender.encode_amqp_list(test_value) if amqp_type == 'map': return AmqpTypesTestSender.encode_amqp_map(test_value) if amqp_type == 'array': #return AmqpTypesTestSender.encode_amqp_array(test_value) print('send: Unsupported AMQP type "%s"' % amqp_type) return None print('send: Unknown AMQP type "%s"' % amqp_type) return None
def ticket_changed(self, ticket, comment, author, old_values): _time = proton.timestamp(time.time() * 1000) event = {"category": "changed", "time": _time, "comment": comment, "ticket": ticket.id, "author": author} event['change'] = dict((k, self._transform_value(k, ticket[k])) for k in old_values) event['state'] = dict((k, self._transform_value(k, ticket[k])) for k in ticket.values) QueueFeeder(self.env).send_events([event])
def attachment_added(self, attachment): _time = proton.timestamp(time.time() * 1000) if attachment.parent_realm != "ticket": return event = {"category": "attachment-added", "time": _time, "ticket": attachment.parent_realm, "author": attachment.author, "filename": attachment.filename, "description": attachment.description, "size": attachment.size} QueueFeeder(self.env).send_events([event])
def selector(self): """ Creates a selector expression of the offset """ if isinstance(self.value, datetime.datetime): epoch = datetime.datetime.utcfromtimestamp(0) milli_seconds = timestamp( (self.value - epoch).total_seconds() * 1000.0) return Selector(u"amqp.annotation.x-opt-enqueued-time > '" + str(milli_seconds) + "'") elif isinstance(self.value, timestamp): return Selector(u"amqp.annotation.x-opt-enqueued-time > '" + str(self.value) + "'") else: operator = ">=" if self.inclusive else ">" return Selector(u"amqp.annotation.x-opt-offset " + operator + " '" + utf82unicode(self.value) + "'")
def _transform_value(self, field, value): if value is empty: return None if field in ("cc", "keywords"): # note, Trac uses '[;,\s]+' (see trac/ticket/model.py) # but CGI's fork doesn't include the whitespace return [x.strip() for x in re.split(r'[;,]+', value)] # TODO deal with integer, date, float fields (CGI extensions) # e.g., we have to convert value as string to value as float/integer, by looking # at the field datatype configuration # TODO ensure that 'changetime' is in UTC? if isinstance(value, datetime.datetime): # celery uses kombu, which uses pickle by default, which # fails to unpickle trac.util.datefmt.FixedOffset #value = value.astimezone(pytz.utc) # but then also proton then fails to seralize for sending to Service Bus... # TODO http://stackoverflow.com/a/7852891 says this is a bad idea value = proton.timestamp(time.mktime(value.timetuple()) * 1000) return value
def ticket_deleted(self, ticket): _time = proton.timestamp(time.time() * 1000) event = {"category": "deleted", "time": _time, "ticket": ticket.id} QueueFeeder(self.env).send_events([event])
def create_message(self, test_value): """ Creates a single message with the test value translated from its string representation to the appropriate AMQP value (set in self.amqp_type). """ if self.amqp_type == 'null': return Message(id=(self.sent + 1), body=None) elif self.amqp_type == 'boolean': return Message(id=(self.sent + 1), body=True if test_value == 'True' else False) elif self.amqp_type == 'ubyte': return Message(id=(self.sent + 1), body=ubyte(int(test_value, 16))) elif self.amqp_type == 'ushort': return Message(id=(self.sent + 1), body=ushort(int(test_value, 16))) elif self.amqp_type == 'uint': return Message(id=(self.sent + 1), body=uint(int(test_value, 16))) elif self.amqp_type == 'ulong': return Message(id=(self.sent + 1), body=ulong(int(test_value, 16))) elif self.amqp_type == 'byte': return Message(id=(self.sent + 1), body=byte(int(test_value, 16))) elif self.amqp_type == 'short': return Message(id=(self.sent + 1), body=short(int(test_value, 16))) elif self.amqp_type == 'int': return Message(id=(self.sent + 1), body=int32(int(test_value, 16))) elif self.amqp_type == 'long': return Message(id=(self.sent + 1), body=long(int(test_value, 16))) elif self.amqp_type == 'float': return Message(id=(self.sent + 1), body=float32( unpack('!f', test_value[2:].decode('hex'))[0])) elif self.amqp_type == 'double': return Message(id=(self.sent + 1), body=unpack('!d', test_value[2:].decode('hex'))[0]) elif self.amqp_type == 'decimal32': return Message(id=(self.sent + 1), body=decimal32(int(test_value[2:], 16))) elif self.amqp_type == 'decimal64': l64 = long(test_value[2:], 16) return Message(id=(self.sent + 1), body=decimal64(l64)) elif self.amqp_type == 'decimal128': return Message(id=(self.sent + 1), body=decimal128(test_value[2:].decode('hex'))) elif self.amqp_type == 'char': if len(test_value) == 1: # Format 'a' return Message(id=(self.sent + 1), body=char(test_value)) else: val = int(test_value, 16) return Message(id=(self.sent + 1), body=char(unichr(val))) elif self.amqp_type == 'timestamp': return Message(id=(self.sent + 1), body=timestamp(int(test_value, 16))) elif self.amqp_type == 'uuid': return Message(id=(self.sent + 1), body=UUID(test_value)) elif self.amqp_type == 'binary': return Message(id=(self.sent + 1), body=bytes(test_value)) elif self.amqp_type == 'string': return Message(id=(self.sent + 1), body=unicode(test_value)) elif self.amqp_type == 'symbol': return Message(id=(self.sent + 1), body=symbol(test_value)) elif self.amqp_type == 'list': return Message(id=(self.sent + 1), body=test_value) elif self.amqp_type == 'map': return Message(id=(self.sent + 1), body=test_value) else: print 'send: Unsupported AMQP type "%s"' % self.amqp_type return None