{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "metadata": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import nexussdk as nxs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# Example:\n",
    "# https://nexus.domain.org/v1\n",
    "with open(\"DEPLOYMENT\", \"r\") as f:\n",
    "    DEPLOYMENT = f.readline().rstrip()\n",
    "    nxs.config.set_environment(DEPLOYMENT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "with open(\"TOKEN\", \"r\") as f:\n",
    "    TOKEN = f.readline().rstrip()\n",
    "    nxs.config.set_token(TOKEN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "pp = nxs.tools.pretty_print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "### Examples setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def utcnow() -> str:\n",
    "    return datetime.utcnow().strftime(\"%Y%m%d%H%M%S%f\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def generate_compacted_id() -> str:\n",
    "    return \"nxv:myview{}\".format(utcnow())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def generate_expanded_id() -> str:\n",
    "    return \"{}/myview{}\".format(\"https://knowledgegraph.domain.org\", utcnow())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "ORGANIZATION = \"myorg{}\".format(utcnow())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "nxs.organizations.create(ORGANIZATION)[\"@id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "PROJECT = \"myproject\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "nxs.projects.create(ORGANIZATION, PROJECT)[\"@id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "PROJECT2 = \"{}/myproject2\".format(ORGANIZATION)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "nxs.projects.create(*PROJECT2.split(\"/\"))[\"@id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "project3 = \"{}/myproject3\".format(ORGANIZATION)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "nxs.projects.create(*project3.split(\"/\"))[\"@id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "projects = [PROJECT2, project3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "mappings = {\n",
    "    \"doc\": {\n",
    "      \"properties\": {\n",
    "        \"@id\": {\n",
    "          \"type\": \"keyword\"\n",
    "        },\n",
    "        \"_createdAt\":  {\n",
    "            \"type\": \"date\"\n",
    "        },\n",
    "      }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "### User API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "es_view1 = nxs.views.create_es(ORGANIZATION, PROJECT, mappings)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "es_view2 = nxs.views.create_es(ORGANIZATION, PROJECT, mappings, tag=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "es_view3 = nxs.views.create_es(ORGANIZATION, PROJECT, mappings,\n",
    "                               resource_schemas = [\"https://bluebrain.github.io/nexus/schemas/view.json\"],\n",
    "                               resource_types = [\"https://bluebrain.github.io/nexus/schemas/view.json\"],\n",
    "                               source_as_text = True, include_metadata = True, include_deprecated = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(es_view3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agg_es_view = nxs.views.create_es_aggregated(ORGANIZATION, PROJECT, [es_view1, es_view2, es_view3], generate_compacted_id())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sparql_view1 = nxs.views.create_sparql(ORGANIZATION, PROJECT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sparql_view2 = nxs.views.create_sparql(ORGANIZATION, PROJECT, tag=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sparql_view3 = nxs.views.create_sparql(ORGANIZATION, PROJECT,\n",
    "                                       resource_schemas = [\"https://bluebrain.github.io/nexus/schemas/view.json\"],\n",
    "                                       resource_types = [\"https://bluebrain.github.io/nexus/schemas/view.json\"],\n",
    "                                       include_metadata = True, include_deprecated = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agg_sparql_view =  nxs.views.create_sparql_aggregated(ORGANIZATION, PROJECT, [sparql_view1, sparql_view2, sparql_view3], generate_compacted_id())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "es_query = {\n",
    "    \"query\": {\n",
    "        \"match_all\": {}\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_es(ORGANIZATION, PROJECT, es_query))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_es(ORGANIZATION, PROJECT,es_query, es_view1[\"@id\"] ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_es(ORGANIZATION, PROJECT,es_query, agg_es_view[\"@id\"] ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sparql_query = \"select ?s ?o where {  ?s rdf:type ?o . }\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_sparql(ORGANIZATION, PROJECT,sparql_query))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_sparql(ORGANIZATION, PROJECT,sparql_query, sparql_view1[\"@id\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.query_sparql(ORGANIZATION, PROJECT,sparql_query, agg_sparql_view[\"@id\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp(nxs.views.stats(ORGANIZATION, PROJECT, sparql_view1[\"@id\"]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
