Created
May 20, 2012 14:29
-
-
Save shirou/2758310 to your computer and use it in GitHub Desktop.
fluen.py plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
""" | |
fluenpy.plugins.out_sampling_filter | |
~~~~~~~~~~~~~~~~~~~~~~~~ | |
:copyright: (c) 2012 by WAKAYAMA Shirou | |
:license: Apache v2 | |
original author: tagomoris | |
https://github.com/tagomoris/fluent-plugin-sampling-filter | |
""" | |
from __future__ import print_function, division, absolute_import, with_statement | |
import logging | |
log = logging.getLogger(__name__) | |
import traceback | |
from fluenpy import error | |
from fluenpy.output import Output | |
from fluenpy.plugin import Plugin | |
from fluenpy.config import config_param | |
from fluenpy.engine import Engine | |
class SamplingFilterOutput(Output): | |
interval = config_param('integer') | |
sample_unit = config_param('string', 'tag') | |
remove_prefix = config_param('string') | |
add_prefix = config_param('string', 'sampled') | |
def __init__(self): | |
super(SamplingFilterOutput, self).__init__() | |
def configure(self, conf): | |
super(SamplingFilterOutput, self).configure(conf) | |
if self.remove_prefix: | |
self.removed_prefix_string = self.remove_prefix + '.' | |
self.removed_length = len(self.removed_prefix_string) | |
self.added_prefix_string = self.add_prefix + '.' | |
if self.sample_unit not in ['tag', 'all']: | |
raise Exception("sample_unit allows only 'tag' or 'all'") | |
self.counts = {} | |
def emit_sampled(self, tag, time_record_pairs): | |
if self.remove_prefix and ((tag.startswith(self.removed_prefix_string) and len(tag) > self.removed_length) or tag | |
== self.remove_prefix): | |
tag = tag[self.removed_length:] | |
if len(tag) > 0: | |
tag = self.added_prefix_string + tag | |
else: | |
tag = self.added_prefix_string | |
for t, r in time_record_pairs: | |
Engine.emit(tag, t, r) | |
def emit(self, tag, es): | |
if self.sample_unit == 'all': | |
t = 'all' | |
else: | |
t = tag | |
pairs = [] | |
for time, record in es: | |
if t in self.counts: | |
self.counts[t] += 1 | |
else: | |
self.counts[t] = 0 | |
c = self.counts[t] | |
if c % int(self.interval) == 0: | |
pairs.append((time, record)) | |
# reset only just before @counts[t] is to be bignum from fixnum | |
if c > 0x6fffffff: | |
self.counts[t] = 0 | |
self.emit_sampled(tag, pairs) | |
## chain.next() ## what should I do? | |
Plugin.register_output('sampling_filter', SamplingFilterOutput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment