Skip to content

Instantly share code, notes, and snippets.

@vishnuvyas
Created June 15, 2018 22:57
Show Gist options
  • Save vishnuvyas/8b1065252e6df21f7842ef7d3fc3adb4 to your computer and use it in GitHub Desktop.
Save vishnuvyas/8b1065252e6df21f7842ef7d3fc3adb4 to your computer and use it in GitHub Desktop.
metrics api definition
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import apxapi\n",
"import uuid\n",
"from time import time\n",
"from prettytable import PrettyTable"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dev = apxapi.APXSession(\"[email protected]\", environment=apxapi.DEV, password=raw_input(\"password: \"))\n",
"# clear output to hide password"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"[{u'lastModifiedTime': 1528924115083,\n",
" u'metricDefinition': {u'definitionName': u'mock_coder_decision_0',\n",
" u'idName': u'metric_id',\n",
" u'idTag': {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" u'indices': [[u'coder_email'],\n",
" [u'phase'],\n",
" [u'when_measured'],\n",
" [u'node'],\n",
" [u'metric_id'],\n",
" [u'decision'],\n",
" [u'project_id'],\n",
" [u'docset_id'],\n",
" [u'hcc'],\n",
" [u'patient_id'],\n",
" [u'document_id']],\n",
" u'queryTerms': [{u'definitionName': u'mock_coder_decision_0',\n",
" u'terms': [u'project_id']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'metric_id']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'node']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'phase']},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'terms': [u'when_measured']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'coder_email']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'document_id']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'decision']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'patient_id']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'hcc']},\n",
" {u'definitionName': u'mock_coder_decision_0', u'terms': [u'docset_id']}],\n",
" u'referredLookupNames': [],\n",
" u'refers': {},\n",
" u'tags': [{u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'phase',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'patient_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'hcc',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'when_measured',\n",
" u'tagType': u'DATE'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'coder_email',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'document_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'decision',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'project_id',\n",
" u'tagType': u'XUUID'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'node',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'source',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'docset_id',\n",
" u'tagType': u'XUUID'}],\n",
" u'together': [[{u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'phase',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'patient_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'hcc',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'when_measured',\n",
" u'tagType': u'DATE'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'coder_email',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'document_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'decision',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'project_id',\n",
" u'tagType': u'XUUID'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'node',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'source',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'mock_coder_decision_0',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'docset_id',\n",
" u'tagType': u'XUUID'}]]}},\n",
" {u'lastModifiedTime': 1528743226372,\n",
" u'metricDefinition': {u'definitionName': u'ls_test_1',\n",
" u'idName': u'metric_id',\n",
" u'idTag': {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" u'indices': [[u'when_measured'],\n",
" [u'metric_id'],\n",
" [u'valuetag'],\n",
" [u'nametag']],\n",
" u'queryTerms': [{u'definitionName': u'ls_test_1',\n",
" u'terms': [u'when_measured']},\n",
" {u'definitionName': u'ls_test_1', u'terms': [u'valuetag']},\n",
" {u'definitionName': u'ls_test_1', u'terms': [u'nametag']},\n",
" {u'definitionName': u'ls_test_1', u'terms': [u'metric_id']}],\n",
" u'referredLookupNames': [],\n",
" u'refers': {},\n",
" u'tags': [{u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'nametag',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'source',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'valuetag',\n",
" u'tagType': u'INT'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'when_measured',\n",
" u'tagType': u'DATE'}],\n",
" u'together': [[{u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'nametag',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': True,\n",
" u'tagName': u'metric_id',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'source',\n",
" u'tagType': u'STRING'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'valuetag',\n",
" u'tagType': u'INT'},\n",
" {u'definitionName': u'ls_test_1',\n",
" u'isNullAllowed': False,\n",
" u'isReserved': False,\n",
" u'tagName': u'when_measured',\n",
" u'tagType': u'DATE'}]]}}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# test connection to API\n",
"dev.metrics.get_registered_metrics().json()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PART1: SCHEMA REGISTRATION"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"run_num = 1 # increment to make a new unique metric table"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"metric_name = \"mock_coder_decision_{}\".format(run_num)\n",
"\n",
"tags = [ {\"name\":\"coder_email\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"project_id\", \"type\":\"XUUID\", \"nullable\":False},\n",
" {\"name\":\"docset_id\", \"type\":\"XUUID\", \"nullable\":False},\n",
" {\"name\":\"phase\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"node\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"hcc\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"patient_id\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"document_id\", \"type\":\"STRING\", \"nullable\":False},\n",
" {\"name\":\"decision\", \"type\":\"STRING\", \"nullable\":False}]\n",
"\n",
"register = dev.metrics.post_register_metric(metric_name, tags)\n",
"\n",
"print register.status_code\n",
"print\n",
"dev.metrics.get_registered_metric(metric_name).json()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PART2: RECORD SOME METRICS"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# mock some data\n",
"multiplier = 8\n",
"mock_proj = \"PRHCC_00000000-0000-0000-0000-000000000001\"\n",
"mock_docset = \"DS_00000000-0000-0000-0000-000000000001\"\n",
"phase_nodes = [(\"code\", \"prediction\"), (\"code\", \"manual\")] * multiplier + [(\"qa1\", \"accept_review\")] * multiplier\n",
"hccs = [\"17\", \"18\", \"19\"] * multiplier\n",
"decisions = [\"accept\", \"reject\", \"reject\"] * multiplier\n",
"coders = [\"[email protected]\", \"[email protected]\"] * multiplier + [\"[email protected]\"] * multiplier"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# post the metrics\n",
"for i in range(len(decisions)):\n",
" phase, node = phase_nodes[i]\n",
" hcc = hccs[i]\n",
" dec = decisions[i]\n",
" coder = coders[i]\n",
" doc = str(uuid.uuid1()) #random\n",
" pat = str(uuid.uuid1()) #random\n",
" r = dev.metrics.post_record_metric(metric_name, [{'name':'coder_email', 'value':coder}, \n",
" {'name':'project_id', 'value':mock_proj},\n",
" {'name':'docset_id', 'value':mock_docset}, \n",
" {'name':'phase', 'value':phase}, \n",
" {'name':'node', 'value':node}, \n",
" {'name':'hcc', 'value':hcc}, \n",
" {'name':'patient_id', 'value':pat}, \n",
" {'name':'document_id', 'value':doc}, \n",
" {'name':'decision', 'value':dec}], \n",
" 'lschneider/jupyter', int(time()))\n",
" if r.status_code != 200:\n",
" print(\"error! {}\\n{}\".format(r.status_code, r.text))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PART3: QUERY THE METRICS"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# util to pretty up the results\n",
"def print_query_results(raw_results, sort_by=lambda x:x[-1]):\n",
" headers = [col['columnName'] for col in raw_results[0]]\n",
" rows = sorted([row['data'] for row in raw_results[1]], key=sort_by)\n",
" \n",
" table = PrettyTable(headers)\n",
" for header in headers:\n",
" table.align[header] = \"l\"\n",
" for row in rows:\n",
" table.add_row(row)\n",
" print(table)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# save a query\n",
"query_name = \"mock_decision_{}q1\".format(run_num)\n",
"query = \"select count(*), decision, phase, node, project_id from metric.{} group by decision, phase, node, project_id\".format(metric_name)\n",
"save_query = dev.metrics.post_saved_query(query_name, query)\n",
"print save_query.status_code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# execute the query\n",
"print_query_results(dev.metrics.get_execute_saved_query(query_name).json(), \n",
" sort_by=lambda x: x[3]+x[2]+x[1])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment