from __future__ import absolute_import

import vaping
import vaping.config

try:
    import vodka
    import vodka.data
except ImportError:
    pass

try:
    import graphsrv
    import graphsrv.group
except ImportError:
    graphsrv = None



def probe_to_graphsrv(probe):
    """
    takes a probe instance and generates
    a graphsrv data group for it using the
    probe's config
    """

    config = probe.config

    # manual group set up via `group` config key

    if "group" in config:
        source, group = config["group"].split(".")
        host = config["host"]
        graphsrv.group.add(source, group, {host:{"host":host}}, **config)
        return

    # automatic group setup for fping
    # FIXME: this should be somehow more dynamic

    for k, v in list(config.items()):
        if isinstance(v, dict) and "hosts" in v:
            r = {}
            for host in v.get("hosts"):
                if isinstance(host, dict):
                    r[host["host"]] = host
                else:
                    r[host] = {"host":host}
            graphsrv.group.add(probe.name, k, r, **v)


@vaping.plugin.register('vodka')
class VodkaPlugin(vaping.plugins.EmitBase):

    def init(self):
        self._is_started = False

    def start(self):
        if self._is_started:
            return
        vodka.run(self.config, self.vaping.config)

        if graphsrv:
            # if graphsrv is installed proceed to generate
            # target configurations for it from probe config

            for node in self.vaping.config.get("probes", []):
                probe = vaping.plugin.get_probe(node, self.vaping)
                probe_to_graphsrv(probe)

        self._is_started = True

    def emit(self, message):
        if not self._is_started:
            self.start()

        vodka.data.handle(message.get("type"), message, data_id=message.get("source"), caller=self)

@@ -553,28 +553,96 @@
     
-host = config%5B%22host%22
+group_field = config.get(%22group_field%22, %22host%22)%0A        group_value = config%5Bgroup_field
 %5D%0A  
@@ -686,25 +686,44 @@
 p, %7B
-host:%7B%22host%22:host
+group_value:%7Bgroup_field:group_value
 %7D%7D, 

allow other fields to be used as group id