def _getRecent_worker(host): urlSuffix = '/recent/0-' try: recent = httpGet('http://' + host + urlSuffix) except URLError: return with Session() as s: fileNames = set() for line in str2recordInfo(recent): timestamp, recordId, fileName = line[0:3] if fileName.split('_')[0] not in ('thread'): continue fileNames.add(fileName) for fileName in sorted(fileNames): try: threadTitle = a2b_hex(fileName.split('_')[1]) except (ValueError, binascii.Error): continue thread = Thread.get(s, title=threadTitle).first() if thread is None: continue log.isEnabledFor(logging.INFO) and log.info('getRecent: found {} {}'.format(thread.id, host)) MessageQueue.enqueue(s, msgtype='get_thread', msg=' '.join((host, fileName))) s.commit() notify()
def updateRecord(thread_id, hex_id, timestamp, nodeName=settings.NODE_NAME): log.isEnabledFor(logging.INFO) and log.info( 'updateRecord: threadId:{} hexId:{} timestamp:{}'.format(thread_id, hex_id, timestamp)) with Session() as s: timestamp = str(int(float(timestamp))) hex_id = str(hex_id) thread_id = str(thread_id) msg = ' '.join((thread_id, hex_id, timestamp, nodeName)) MessageQueue.enqueue(s, msgtype='update_record', msg=msg) s.commit() notify()
def getAndUpdateRecord(addr, thread_id, hex_id, timestamp): log.isEnabledFor(logging.INFO) and log.info( 'getAndUpdateRecord: threadId:{} hexId:{} timestamp:{}'.format(thread_id, hex_id, timestamp)) with Session() as s: timestamp = int(float(timestamp)) msg = ' '.join((addr, str(thread_id), str(hex_id), str(timestamp))) if addr is not None: MessageQueue.enqueue(s, msgtype='get_record', msg=msg) msg = ' '.join((str(thread_id), str(hex_id), str(timestamp), settings.NODE_NAME)) MessageQueue.enqueue(s, msgtype='update_record', msg=msg) s.commit() notify()
def post(self, request, *args, **kwargs): title = request.POST['title'] with Session() as s: query = Thread.get(s, title=title).all() if len(query) == 0: thread = Thread.add(s, title) MessageQueue.enqueue(s, msgtype='get_thread', msg=Thread.getFileName(title)) s.commit() notify() else: thread = query[0] return JsonResponse({ 'thread': { "id": thread.id, "title": thread.title, "timestamp": thread.timestamp, "records": thread.records, } })
def getThread(msg): log.isEnabledFor(logging.INFO) and log.info('getThread: {}'.format(msg.msg)) # ファイル名だけなら、全てのノードから順にスレッドを取得する if len(msg.msg.split()) == 1: fileName = msg.msg with Session() as s: for node in Node.getLinkedNode(s): MessageQueue.enqueue(s, msgtype='get_thread', msg=' '.join([ node.host, fileName, ])) s.commit() notify() return True with Session() as s: host, fileName = msg.msg.split() threadTitle = a2b_hex(fileName.split('_')[1]).decode('utf-8') thread = Thread.get(s, title=threadTitle).first() if thread is None: return lastTime = Record.getLastTime(s, thread.id) firstTime = Record.getFirstTime(s, thread.id) # 最新のレコードと、より古いレコードを取得する MessageQueue.enqueue(s, msgtype='get_record', msg=' '.join([ host, str(thread.id), str(lastTime)+'-', ])) if firstTime: MessageQueue.enqueue(s, msgtype='get_record', msg=' '.join([ host, str(thread.id), '-'+str(firstTime), ])) if firstTime and lastTime: # 未取得レコードが30%以上なら、その範囲をまとめて取得 # 30%未満なら、一つずつ取得 url = 'http://{}/head/{}/{}-{}'.format( host, fileName, str(firstTime), str(lastTime), ) records = [] notExistsRecordCount = 0 existsRecordCount = 0 _existsRecordCount = 0 rate = None try: for timestamp, recordId in str2recordInfo(httpGet(url)): timestamp = int(timestamp) if Record.get(s, thread.id, a2b_hex(recordId), timestamp).first(): if notExistsRecordCount: _existsRecordCount += 1 else: records.append((timestamp, recordId,)) notExistsRecordCount += 1 existsRecordCount += _existsRecordCount _existsRecordCount = 0 oldRate = rate rate = notExistsRecordCount / (notExistsRecordCount + existsRecordCount) if rate < 0.3 and oldRate >= 0.3: # records[0:-1]の範囲のレコードをまとめて取得 newRecords = records.pop() MessageQueue.enqueue(s, msgtype='get_record', msg=' '.join([ host, str(thread.id), str(records[0][0]) + '-' + str(records[-1][0]), ])) records = [newRecords] notExistsRecordCount = 1 existsRecordCount = 0 if rate is None: pass elif rate >= 0.3: # まとめて取得 MessageQueue.enqueue(s, msgtype='get_record', msg=' '.join([ host, str(thread.id), str(records[0][0]) + '-' + str(records[-1][0]), ])) else: # 一つずつ取得 for timestamp, recordId in records: MessageQueue.enqueue(s, msgtype='get_record', msg=' '.join([ host, str(thread.id), recordId, str(timestamp), ])) except URLError as e: log.isEnabledFor(logging.INFO) and log.info('getThread: Fail {} {} {}'.format(thread.id, url, str(e))) s.commit() notify()