influxdb: simplify and document pattern matching. Closes #166

This commit is contained in:
Sebastien Bourdeauducq 2016-03-31 00:28:28 +08:00
parent deb9a60ca3
commit 29a76bb02d
2 changed files with 38 additions and 34 deletions

View File

@ -3,6 +3,20 @@
Release notes
=============
unreleased [2.x]
----------------
* The format of the influxdb pattern file is simplified. The procedure to
edit patterns is also changed to modifying the pattern file and calling:
``artiq_rpctool.py ::1 3248 call scan_patterns`` (or restarting the bridge)
The patterns can be converted to the new format using this code snippet::
from artiq.protocols import pyon
patterns = pyon.load_file("influxdb_patterns.pyon")
for p in patterns:
print(p)
unreleased
----------

View File

@ -21,7 +21,15 @@ logger = logging.getLogger(__name__)
def get_argparser():
parser = argparse.ArgumentParser(
description="ARTIQ data to InfluxDB bridge")
description="ARTIQ data to InfluxDB bridge",
epilog="Pattern matching works as follows. "
"The default action on a key (dataset name) is to log it. "
"Then the patterns are traversed in order and glob-matched "
"with the key. "
"Optional + and - pattern prefixes specify whether to ignore or "
"log keys matching the rest of the pattern. "
"Default (in the absence of prefix) is to ignore. Last matched "
"pattern takes precedence.")
group = parser.add_argument_group("master")
group.add_argument(
"--server-master", default="::1",
@ -46,8 +54,10 @@ def get_argparser():
"--table", default="lab", help="table name to use")
group = parser.add_argument_group("filter")
group.add_argument(
"--pattern-file", default="influxdb_patterns.pyon",
help="file to save the patterns in (default: %(default)s)")
"--pattern-file", default="influxdb_patterns.cfg",
help="file to load the patterns from (default: %(default)s). "
"If the file is not found, no patterns are loaded "
"(everything is logged).")
simple_network_args(parser, [("control", "control", 3248)])
verbosity_args(parser)
return parser
@ -177,14 +187,20 @@ class MasterReader(TaskObject):
class Filter:
def __init__(self, pattern_file):
self.pattern_file = pattern_file
self.patterns = []
self.scan_patterns()
def scan_patterns(self):
"""(Re)load the patterns file."""
try:
self.patterns = pyon.load_file(self.pattern_file)
with open(self.pattern_file, "r") as f:
self.patterns = []
for line in f:
line = line.rstrip()
if line:
self.patterns.append(line)
except FileNotFoundError:
logger.info("no pattern file found, logging everything")
def _save(self):
pyon.store_file(self.pattern_file, self.patterns)
self.patterns = []
# Privatize so that it is not shown in artiq_rpctool list-methods.
def _filter(self, k):
@ -197,32 +213,6 @@ class Filter:
take = sign
return take == "+"
def add_pattern(self, pattern, index=None):
"""Add a pattern.
Optional + and - pattern prefixes specify whether to ignore or log
keys matching the rest of the pattern.
Default (in the absence of prefix) is to ignore. Keys that match no
pattern are logged. Last matched pattern takes precedence.
The optional index parameter specifies where to insert the pattern.
By default, patterns are added at the end. If index is an integer, it
specifies the index where the pattern is inserted. If it is a string,
that string must match an existing pattern and the new pattern is
inserted immediately after it."""
if pattern not in self.patterns:
if index is None:
index = len(self.patterns)
if isinstance(index, str):
index = self.patterns.index(index) + 1
self.patterns.insert(index, pattern)
self._save()
def remove_pattern(self, pattern):
"""Remove a pattern."""
self.patterns.remove(pattern)
self._save()
def get_patterns(self):
"""Show existing patterns."""
return self.patterns