def record2str(query, include_body=True): if not include_body: query = query.with_entities(Record.bin_id, Record.timestamp, Record.raw_body) for r in query: if not include_body: yield '<>'.join(( str(datetime2timestamp(r.timestamp)), str(b2a_hex(r.bin_id).decode('utf-8')), ))+'\n' continue yield '<>'.join(( str(datetime2timestamp(r.timestamp)), str(b2a_hex(r.bin_id).decode('utf-8')), r.raw_body, ))+'\n'
def getFirstTime(cls, session, thread_id): rec = session.query(Record) \ .filter(Record.thread_id == thread_id) \ .order_by(Record.timestamp) \ .first() if rec: return int(datetime2timestamp(rec.timestamp)) return 0
def getTimeRange(atime, starttime, endtime): if atime: return (int(atime), int(atime)) if starttime: if endtime: return (int(starttime), int(endtime)) now = datetime2timestamp(datetime.now()) return (int(starttime), now) if endtime: return (0, int(endtime)) # All variable is None. raise BadTimeRange()
def get(self, request, *args, **kwargs): threads = [] with Session() as s: result = Thread.gets(s, limit=intOrNone(request.GET.get('limit')), stime=intOrNone(request.GET.get('start_time')), etime=intOrNone(request.GET.get('end_time')), title=request.GET.get('title'), ) for t in result: threads.append({ 'id': int(t.id), 'title': t.title, 'timestamp': int(datetime2timestamp(t.timestamp)), 'records': int(t.records), }) obj = { 'threads': threads } return JsonResponse(obj)
def dispatch(self, request, *args, **kwargs): with Session() as s: response = HttpResponse() queryArgs = {} if kwargs.get('time') is not None: queryArgs['stime'] = int(kwargs['time']) queryArgs['etime'] = queryArgs['stime'] else: if kwargs.get('stime') is not None: queryArgs['stime'] = int(kwargs['stime']) if kwargs.get('etime') is not None: queryArgs['etime'] = int(kwargs['etime']) result = Recent.gets(s, **queryArgs).values(Recent.timestamp, Recent.bin_id, Recent.file_name) for timestamp, binId, fileName in result: timestamp = datetime2timestamp(timestamp) hexId = b2a_hex(binId).decode('ascii') line = '<>'.join([ str(timestamp), hexId, fileName, ])+'\n' response.write(line) return response
def get(self, request, *args, **kwargs): records = [] thread_id = int(kwargs['thread_id']) with Session() as s: if 'timestamp' in kwargs: """ 単一のレコードを返す方のAPI """ timestamp = int(kwargs['timestamp']) bin_id = a2b_hex(kwargs['record_id']) r = Record.get(s, thread_id, bin_id, timestamp).with_entities( Record.bin_id, Record.timestamp, Record.name, Record.mail, Record.body, sql_func.length(Record.attach).label('attach_len'), Record.suffix).first() if r: records.append({ 'id': b2a_hex(r.bin_id).decode('ascii'), 'timestamp': int(datetime2timestamp(r.timestamp)), 'name': r.name, 'mail': r.mail, 'body': r.body, 'attach': bool(r.attach_len), 'suffix': r.suffix, }) else: """ 複数のレコードを返す方のAPI """ bin_id = request.GET.get('record_id') if bin_id: bin_id = a2b_hex(bin_id) matchRecords = Record.gets(s, thread_id=thread_id, stime=intOrNone(request.GET.get('start_time')), etime=intOrNone(request.GET.get('end_time')), bin_id=bin_id, limit=intOrNone(request.GET.get('limit')), ).with_entities( Record.bin_id, Record.timestamp, Record.name, Record.mail, Record.body, sql_func.length(Record.attach).label('attach_len'), Record.suffix) for r in matchRecords: records.append({ 'id': b2a_hex(r.bin_id).decode('ascii'), 'timestamp': int(datetime2timestamp(r.timestamp)), 'name': r.name, 'mail': r.mail, 'body': r.body, 'attach': bool(r.attach_len), 'suffix': r.suffix, }) obj = { 'records': records, } return JsonResponse(obj)