odin-detector / odin-control

Prototype of ODIN framework
Apache License 2.0
4 stars 9 forks source link

Add support for parameter metadata #35

Closed timcnicholls closed 5 years ago

timcnicholls commented 5 years ago

This PR implements support for metadata in odin-control parameters. It ports the MetadataTree funtionality in other forks of odin-control into the existing ParameterTree class in a backwards-compatible fashion. Metadata fields are specified for parameters in ParameterTree by passing an optional dict as the third element in the parameter initialisation tuple. The SystemInfo adapter has been populated with metadata as an illustration. Here is an example:

...
        self.param_tree = ParameterTree({
            'name': 'system_info',
            'description': 'Information about the system hosting this odin server instance',
            'odin_version': (lambda: version_info['version'], {
                "name": "odin version",
                "description": "ODIN server version",
            }),
            'tornado_version': (lambda: tornado.version, {
                "name": "tornado version",
                "description": "version of tornado used in this server",
            }),
            'python_version': (lambda: platform.python_version(), {
                "name": "python version",
                "description": "version of python running this server",
            }),
            'platform': platform_tree,
            'server_uptime': (self.get_server_uptime, {
                "name": "server uptime",
                "description": "time since the ODIN server started",
                "units": "s",
                "display_precision": 2,
            }),
        })
...

Parameter metadata is returned to clients on GET requests when the Accept header has the right MIME type qualifier set (application/json;metadata=True), for instance:

$ curl -s -H "Accept:application/json;metadata=True" http://localhost:8888/api/0.1/system_info | python -m json.tool
{
    "description": "Information about the system hosting this odin server instance",
    "name": "system_info",
    "odin_version": {
        "description": "ODIN server version",
        "name": "odin version",
        "type": "str",
        "value": "0.3.1+26.ga00f21b.dirty",
        "writeable": false
    },
    "platform": {
        "description": "Information about the underlying platform",
        "name": "platform",
        "node": {
            "description": "node (host) name",
            "name": "node",
            "type": "str",
            "value": "te7bramley.te.rl.ac.uk",
            "writeable": false
        },
        "processor": {
            "description": "processor (CPU) name",
            "name": "processor",
            "type": "str",
            "value": "i386",
            "writeable": false
        },
        "release": {
            "description": "operating system release",
            "name": "release",
            "type": "str",
            "value": "18.2.0",
            "writeable": false
        },
        "system": {
            "description": "operating system name",
            "name": "system",
            "type": "str",
            "value": "Darwin",
            "writeable": false
        },
        "version": {
            "description": "operating system version",
            "name": "version",
            "type": "str",
            "value": "Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64",
            "writeable": false
        }
    },
    "python_version": {
        "description": "version of python running this server",
        "name": "python version",
        "type": "str",
        "value": "2.7.15",
        "writeable": true
    },
    "server_uptime": {
        "description": "time since the ODIN server started",
        "display_precision": 2,
        "name": "server uptime",
        "type": "float",
        "units": "s",
        "value": 239210.0796060562,
        "writeable": false
    },
    "tornado_version": {
        "description": "version of tornado used in this server",
        "name": "tornado version",
        "type": "str",
        "value": "4.5.3",
        "writeable": false
    }
}

whereas the default behaviour without the qualifier remains unchanged:

$ curl -s -H "Accept:application/json" http://localhost:8888/api/0.1/system_info | python -m json.tool
{
    "odin_version": "0.3.1+26.ga00f21b.dirty",
    "platform": {
        "node": "te7bramley.te.rl.ac.uk",
        "processor": "i386",
        "release": "18.2.0",
        "system": "Darwin",
        "version": "Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64"
    },
    "python_version": "2.7.15",
    "server_uptime": 239428.2710969448,
    "tornado_version": "4.5.3"
}
coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.02%) to 99.223% when pulling 049ab146678c2f4de42c8d808323289f854a0a9d on metadata-tree into 51949d6326b35ad8c20ea7ffad87b14797b47f7f on master.