Commit 38fd88b9 authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

datastoream.loader.iter_datastreams()

parent d76aab3a
......@@ -11,6 +11,7 @@ from __future__ import absolute_import
import logging
import inspect
import threading
import os
# NOC modules
from .base import DataStream
......@@ -21,6 +22,7 @@ class DataStreamLoader(object):
def __init__(self):
self.datastreams = {} # Load datastreams
self.lock = threading.Lock()
self.all_datastreams = set()
def get_datastream(self, name):
"""
......@@ -57,6 +59,25 @@ class DataStreamLoader(object):
def is_valid_name(self, name):
return ".." not in name
def iter_datastreams(self):
with self.lock:
if not self.all_datastreams:
self.all_datastreams = self.find_datastreams()
for ds in sorted(self.all_datastreams):
yield ds
def find_datastreams(self):
"""
Scan all available datastreams
"""
names = set()
for dn in ["services/datastream/streams"]:
for file in os.listdir(dn):
if file.startswith("_") or not file.endswith(".py"):
continue
names.add(file[:-3])
return names
# Create singleton object
loader = DataStreamLoader()
......@@ -17,7 +17,7 @@ import tornado.ioloop
# NOC modules
from noc.core.service.base import Service
from noc.services.datastream.handler import DataStreamRequestHandler
from noc.services.datastream.streams.managedobject import ManagedObjectDataStream
from noc.core.datastream.loader import loader
from noc.config import config
......@@ -28,7 +28,15 @@ class DataStreamService(Service):
traefik_frontend_rule = "PathPrefix:/api/datastream"
def get_datastreams(self):
return [ManagedObjectDataStream]
r = []
for name in loader.iter_datastreams():
ds = loader.get_datastream(name)
if ds:
self.logger.info("[%s] Initializing datastream", name)
r += [ds]
else:
self.logger.info("[%s] Failed to initialize datastream", name)
return r
def get_handlers(self):
return [
......
......@@ -280,6 +280,11 @@ def test_loader_error():
assert ds is None
def test_loader_iter_datastreams(datastream_name):
dses = set(loader.iter_datastreams())
assert datastream_name in dses
def test_wait():
class WaitDS(DataStream):
name = "wait"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment