Fix calculation algorithm
This commit is contained in:
@@ -3,6 +3,7 @@ speedmeter.py
|
|||||||
チャットの勢いを算出するChatProcessor
|
チャットの勢いを算出するChatProcessor
|
||||||
Calculate speed of chat.
|
Calculate speed of chat.
|
||||||
"""
|
"""
|
||||||
|
import calendar, datetime, pytz
|
||||||
|
|
||||||
class RingQueue:
|
class RingQueue:
|
||||||
"""
|
"""
|
||||||
@@ -20,7 +21,7 @@ class RingQueue:
|
|||||||
キュー内に余裕があるか。キュー内のアイテム個数が、キューの最大個数未満であればTrue。
|
キュー内に余裕があるか。キュー内のアイテム個数が、キューの最大個数未満であればTrue。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self,capacity):
|
def __init__(self, capacity = 10):
|
||||||
"""
|
"""
|
||||||
コンストラクタ
|
コンストラクタ
|
||||||
|
|
||||||
@@ -30,13 +31,15 @@ class RingQueue:
|
|||||||
格納時に最大個数を超える場合は一番古いアイテムから
|
格納時に最大個数を超える場合は一番古いアイテムから
|
||||||
上書きする。
|
上書きする。
|
||||||
"""
|
"""
|
||||||
|
if capacity <= 0:
|
||||||
|
raise ValueError
|
||||||
self.items = list()
|
self.items = list()
|
||||||
self.capacity = capacity
|
self.capacity = capacity
|
||||||
self.first_pos = 0
|
self.first_pos = 0
|
||||||
self.last_pos = 0
|
self.last_pos = 0
|
||||||
self.mergin = True
|
self.mergin = True
|
||||||
|
|
||||||
def put(self,item):
|
def put(self, item):
|
||||||
"""
|
"""
|
||||||
引数itemに指定されたアイテムをこのキューに格納する。
|
引数itemに指定されたアイテムをこのキューに格納する。
|
||||||
キューの最大個数を超える場合は、一番古いアイテムの位置に上書きする。
|
キューの最大個数を超える場合は、一番古いアイテムの位置に上書きする。
|
||||||
@@ -81,9 +84,11 @@ class SpeedCalculator(RingQueue):
|
|||||||
----------
|
----------
|
||||||
格納するチャットブロックの数
|
格納するチャットブロックの数
|
||||||
"""
|
"""
|
||||||
def __init__(self, capacity,video_id):
|
|
||||||
|
def __init__(self, capacity, video_id):
|
||||||
super().__init__(capacity)
|
super().__init__(capacity)
|
||||||
self.video_id=video_id
|
self.video_id=video_id
|
||||||
|
self.speed = 0
|
||||||
|
|
||||||
def process(self, chat_components: list):
|
def process(self, chat_components: list):
|
||||||
if chat_components:
|
if chat_components:
|
||||||
@@ -92,8 +97,9 @@ class SpeedCalculator(RingQueue):
|
|||||||
chatdata = component.get('chatdata')
|
chatdata = component.get('chatdata')
|
||||||
|
|
||||||
if chatdata is None:
|
if chatdata is None:
|
||||||
return
|
return self.speed
|
||||||
self.calc(chatdata)
|
self.speed = self.calc(chatdata)
|
||||||
|
return self.speed
|
||||||
|
|
||||||
def _value(self):
|
def _value(self):
|
||||||
|
|
||||||
@@ -131,8 +137,23 @@ class SpeedCalculator(RingQueue):
|
|||||||
return timestamp
|
return timestamp
|
||||||
|
|
||||||
def calc(self,actions):
|
def calc(self,actions):
|
||||||
if len(actions)==0:
|
|
||||||
return None
|
def empty_data():
|
||||||
|
'''
|
||||||
|
データがない場合にゼロのデータをリングキューに入れる
|
||||||
|
'''
|
||||||
|
timestamp_now = calendar.timegm(datetime.datetime.
|
||||||
|
now(pytz.utc).utctimetuple())
|
||||||
|
self.put({
|
||||||
|
'chat_count':0,
|
||||||
|
'starttime':int(timestamp_now),
|
||||||
|
'endtime':int(timestamp_now)
|
||||||
|
})
|
||||||
|
return self._value()
|
||||||
|
|
||||||
|
if actions is None or len(actions)==0:
|
||||||
|
return empty_data
|
||||||
|
|
||||||
#actions内の時刻データを持つチャットデータの数(tickerは除く)
|
#actions内の時刻データを持つチャットデータの数(tickerは除く)
|
||||||
counter=0
|
counter=0
|
||||||
#actions内の最初のチャットデータの時刻
|
#actions内の最初のチャットデータの時刻
|
||||||
@@ -160,13 +181,12 @@ class SpeedCalculator(RingQueue):
|
|||||||
|
|
||||||
#チャット速度用のデータをリングキューに送る
|
#チャット速度用のデータをリングキューに送る
|
||||||
if starttime is None or endtime is None:
|
if starttime is None or endtime is None:
|
||||||
return None
|
return empty_data
|
||||||
|
|
||||||
self.put({
|
self.put({
|
||||||
'chat_count':counter,
|
'chat_count':counter,
|
||||||
#'timestamp':int(time.time()),
|
|
||||||
'starttime':int(starttime/1000000),
|
'starttime':int(starttime/1000000),
|
||||||
'endtime':int(endtime/1000000)
|
'endtime':int(endtime/1000000)
|
||||||
})
|
})
|
||||||
|
|
||||||
return self._value()
|
return self._value()
|
||||||
#print({'chat_count':counter,'timestamp':time.time()})
|
|
||||||
Reference in New Issue
Block a user