You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.6 KiB
58 lines
1.6 KiB
import time
|
|
|
|
from couchpotato import tryInt
|
|
from couchpotato.core.logger import CPLog
|
|
from couchpotato.api import addApiView
|
|
from couchpotato.core.event import addEvent,fireEvent
|
|
from couchpotato.core.plugins.base import Plugin
|
|
|
|
|
|
log = CPLog(__name__)
|
|
|
|
|
|
class Charts(Plugin):
|
|
|
|
update_in_progress = False
|
|
|
|
def __init__(self):
|
|
addApiView('charts.view', self.automationView)
|
|
addEvent('app.load', self.setCrons)
|
|
|
|
def setCrons(self):
|
|
fireEvent('schedule.interval', 'charts.update_cache', self.updateViewCache, hours = self.conf('update_interval', default = 12))
|
|
|
|
def automationView(self, force_update = False, **kwargs):
|
|
|
|
if force_update:
|
|
charts = self.updateViewCache()
|
|
else:
|
|
charts = self.getCache('charts_cached')
|
|
if not charts:
|
|
charts = self.updateViewCache()
|
|
|
|
return {
|
|
'success': True,
|
|
'count': len(charts),
|
|
'charts': charts
|
|
}
|
|
|
|
|
|
def updateViewCache(self):
|
|
|
|
if self.update_in_progress:
|
|
while self.update_in_progress:
|
|
time.sleep(1)
|
|
catched_charts = self.getCache('charts_cached')
|
|
if catched_charts:
|
|
return catched_charts
|
|
|
|
try:
|
|
self.update_in_progress = True
|
|
charts = fireEvent('automation.get_chart_list', merge = True)
|
|
self.setCache('charts_cached', charts, timeout = 7200 * tryInt(self.conf('update_interval', default = 12)))
|
|
except:
|
|
log.error('Failed refreshing charts')
|
|
|
|
self.update_in_progress = False
|
|
|
|
return charts
|
|
|