Ejemplo n.º 1
0
    def influxdb_to_json(self, sol_influxdb):
        """
        Converts an Influxdb query reply into a list of dicts.

        :param sol_influxdb dict: the result of a database query (sush as SELECT * FROM)
        :return: a list of JSON SOL objects
        :rtype: list

        """

        # verify influxdb data
        if not ("series" in sol_influxdb):
            raise ValueError("Influxdb data not recognized")

        # init
        json_list = []

        # remove unused headers
        for serie in sol_influxdb["series"]:
            for val in serie['values']:
                # convert to dict
                d_influxdb = dict(zip(serie['columns'], val))

                # unflat dict
                obj_value = flatdict.FlatDict(d_influxdb).as_dict()

                # parse specific HR_NEIGHBORS
                hr_nghb_name = SolDefines.solTypeToTypeName(
                    SolDefines, SolDefines.SOL_TYPE_DUST_NOTIF_HRNEIGHBORS)
                if serie['name'] == hr_nghb_name:
                    for i in range(0, len(obj_value["neighbors"]) + 1):
                        ngbr_id = str(i)

                        # new HR_NGBR parsing
                        if ngbr_id in obj_value["neighbors"]:
                            if obj_value["neighbors"][ngbr_id][
                                    "neighborFlag"] is None:
                                del obj_value["neighbors"][ngbr_id]

                        # old HR_NGBR parsing
                        if ngbr_id in obj_value:
                            if obj_value[ngbr_id]["neighborFlag"] is not None:
                                obj_value["neighbors"][ngbr_id] = obj_value[
                                    ngbr_id]
                            del obj_value[ngbr_id]

                # time is not passed in the "value" field
                del obj_value["time"]

                # create final dict
                jdic = {
                    'type': serie['name'],
                    'mac': serie['tags']['mac'],
                    'value': obj_value,
                    'timestamp': d_influxdb['time'],
                }
                json_list.append(jdic)
        return json_list
Ejemplo n.º 2
0
    def influxdb_to_json(self, sol_influxdb):
        """
        Converts an Influxdb query reply into a list of dicts.

        :param sol_influxdb dict: the result of a database query (sush as SELECT * FROM)
        :return: a list of JSON SOL objects
        :rtype: list

        """

        # verify influxdb data
        if not ("series" in sol_influxdb):
            raise ValueError("Influxdb data not recognized")

        # init
        json_list = []

        # remove unused headers
        for serie in sol_influxdb["series"]:
            for val in serie['values']:
                # convert to dict
                d_influxdb = dict(zip(serie['columns'], val))

                # unflat dict
                obj_value = flatdict.FlatDict(d_influxdb).as_dict()

                # parse specific HR_NEIGHBORS
                hr_nghb_name    = SolDefines.solTypeToTypeName(
                                    SolDefines,
                                    SolDefines.SOL_TYPE_DUST_NOTIF_HRNEIGHBORS)
                if serie['name'] == hr_nghb_name:
                    for i in range(0,len(obj_value["neighbors"])+1):
                        ngbr_id = str(i)

                        # new HR_NGBR parsing
                        if ngbr_id in obj_value["neighbors"]:
                            if obj_value["neighbors"][ngbr_id]["neighborFlag"] is None:
                                del obj_value["neighbors"][ngbr_id]

                        # old HR_NGBR parsing
                        if ngbr_id in obj_value:
                            if obj_value[ngbr_id]["neighborFlag"] is not None:
                                obj_value["neighbors"][ngbr_id] = obj_value[ngbr_id]
                            del obj_value[ngbr_id]

                # time is not passed in the "value" field
                del obj_value["time"]

                # create final dict
                jdic = {
                        'type'      : serie['name'],
                        'mac'       : serie['tags']['mac'],
                        'value'     : obj_value,
                        'timestamp' : d_influxdb['time'],
                        }
                json_list.append(jdic)
        return json_list
Ejemplo n.º 3
0
    def json_to_influxdb(self, sol_json, tags):
        """
        Convert a JSON SOL object into a InfluxDB point

        :param list sol_json: JSON SOL object
        :return: InfluxDB point
        :rtpe: list
        """

        # fields
        if sol_json['type'] == SolDefines.SOL_TYPE_DUST_NOTIF_HRNEIGHBORS:
            fields = {}
            for n in sol_json["value"]['neighbors']:
                fields["neighbors:" + str(n['neighborId'])] = n
            fields['numItems'] = sol_json["value"]['numItems']
        elif sol_json['type'] == SolDefines.SOL_TYPE_DUST_NOTIF_HRDISCOVERED:
            fields = sol_json["value"]
        elif sol_json['type'] == SolDefines.SOL_TYPE_DUST_SNAPSHOT:
            fields = {}
            fields["mote"] = []
            for mote in sol_json["value"]:
                mote["macAddress"] = FormatUtils.formatBuffer(
                    mote["macAddress"])
                for path in mote["paths"]:
                    path["macAddress"] = FormatUtils.formatBuffer(
                        path["macAddress"])
                fields["mote"].append(mote)
        elif sol_json['type'] == SolDefines.SOL_TYPE_DUST_EVENTNETWORKRESET:
            fields = {'value': 'dummy'}
        else:
            fields = sol_json["value"]
            for (k, v) in fields.items():
                if type(v) == list:  # mac
                    if k in ['macAddress', 'source', 'dest']:
                        fields[k] = FormatUtils.formatBuffer(v)
                    elif k in [
                            'sol_version', 'sdk_version', 'solmanager_version'
                    ]:
                        fields[k] = ".".join(str(i) for i in v)

        f = flatdict.FlatDict(fields)
        fields = {}
        for (k, v) in f.items():
            fields[k] = v

        # add additionnal fiel if apply_function exists
        try:
            obj_struct = SolDefines.solStructure(sol_json['type'])
            if 'apply' in obj_struct:
                for ap in obj_struct['apply']:
                    arg_list = [fields[arg] for arg in ap["args"]]
                    fields[ap["name"]] = ap["function"](*arg_list)
        except ValueError:
            pass

        # get SOL type
        measurement = SolDefines.solTypeToTypeName(SolDefines,
                                                   sol_json['type'])

        # convert SOL timestamp to UTC
        utc_time = sol_json["timestamp"] * 1000000000

        sol_influxdb = {
            "time": utc_time,
            "tags": tags,
            "measurement": measurement,
            "fields": fields,
        }

        return sol_influxdb
Ejemplo n.º 4
0
    def json_to_influxdb(self,sol_json,tags):
        """
        Convert a JSON SOL object into a InfluxDB point

        :param list sol_json: JSON SOL object
        :return: InfluxDB point
        :rtpe: list
        """
        # tags
        obj_tags = copy.deepcopy(tags)

        # fields
        if   sol_json['type']==SolDefines.SOL_TYPE_DUST_NOTIF_HRNEIGHBORS:
            fields = {}
            for n in sol_json["value"]['neighbors']:
                fields["neighbors:" + str(n['neighborId'])] = n
            fields['numItems'] = sol_json["value"]['numItems']
        elif sol_json['type']==SolDefines.SOL_TYPE_DUST_NOTIF_HRDISCOVERED:
            fields = sol_json["value"]
        elif sol_json['type']==SolDefines.SOL_TYPE_DUST_SNAPSHOT:
            fields = {}
            fields["mote"] = []
            for mote in sol_json["value"]:
                mote["macAddress"] = FormatUtils.formatBuffer(mote["macAddress"])
                for path in mote["paths"]:
                    path["macAddress"] = FormatUtils.formatBuffer(path["macAddress"])
                fields["mote"].append(mote)
        elif sol_json['type']==SolDefines.SOL_TYPE_DUST_EVENTNETWORKRESET:
            fields = {'value':'dummy'}
        else:
            fields = sol_json["value"]
            for (k,v) in fields.items():
                if type(v)==list: # mac
                    if k in ['macAddress','source','dest']:
                        fields[k] = FormatUtils.formatBuffer(v)
                    elif k in ['sol_version','sdk_version','solmanager_version']:
                        fields[k] = ".".join(str(i) for i in v)

        f = flatdict.FlatDict(fields)
        fields = {}
        for (k,v) in f.items():
            fields[k] = v

        # add additionnal field or tag if apply_function exists
        try:
            obj_struct = SolDefines.solStructure(sol_json['type'])
            if 'apply' in obj_struct:
                for ap in obj_struct['apply']:
                    arg_list = [fields[arg] for arg in ap["args"]]
                    if "field" in ap:
                        fields[ap["field"]] = ap["function"](*arg_list)
                    if "tag" in ap:
                        obj_tags[ap["tag"]] = ap["function"](*arg_list)
        except ValueError:
            pass

        # get SOL type
        measurement = SolDefines.solTypeToTypeName(SolDefines,sol_json['type'])

        # convert SOL timestamp to UTC
        utc_time = sol_json["timestamp"]*1000000000

        sol_influxdb = {
                "time"          : utc_time,
                "tags"          : obj_tags,
                "measurement"   : measurement,
                "fields"        : fields,
                }

        return sol_influxdb