Skip to content
Snippets Groups Projects
user avatar
Leonardo Christino authored
02c89c1a
History

License Pipeline Status main Pipeline Status develop Coverage develop Coverage main

Project icon

Query Service

This service handles the conversion and execution of queries sent from the frontend. The conversion and execution code was moved into packages, to be able to re-use this service as the shell. Query conversion code (to convert from our JSON format to AQL or Cypher) lives in this repo and query execution code lives in this repo. For handing queries with Machine Learning algorithms, the ML service is used.

The reason the conversion and execution code are seperated is because there can be multiple databases using the same query language, while they each have their own driver.

Dev Dependencies

Here are the developer dependencies. Coding can be done without these tools, but they sure make your life easier.

  • Make
  • Docker

Dependencies

This service depends on quite some other services, as it is essentially just a shell for the query conversion and execution packages.

  • RabbitMQ
  • Redis
  • Minio
  • User management service (for database credentials)
  • Machine Learning service (for handling queries with ML)

Environment Variables

To decide which conversion and execution packages will be used the service uses environment variables. The QUERY_CONVERSION_LANGUAGE variable determines which query conversion package will be used. Currently only aql and cypher are supported. The QUERY_EXECUTION variable determines the executor that will be used. This variable also changes the queue and routing key the service listens to, as can be seen here on line 41.

There are some more environment variables that are used in this service, which are listed below:

  • RABBIT_HOST address of the RabbitMQ instance (string)
  • RABBIT_PORT RabbitMQ port (int)
  • RABBIT_USER RabbitMQ username (string)
  • RABBIT_PASSWORD RabbitMQ password (string)
  • REDIS_ADDRESS Redis address (string)
  • LOG_MESSAGES Whether to log messages (bool)
  • MINIO_ADDRESS Minio address (string)
  • MINIO_ACCESSKEYID Minio username (string)
  • MINIO_ACCESSKEY Minio password (string)

Building the Service

To compile the service into a single binary, the following commands can be used. Go can build for many platforms, so we have three build commands, for the three main platforms.

make linux
make windows
make macos

To build the service and push it to the Docker registry the make docker command can be used.

Docker

To build the docker image with make docker or make rollout you need to create a ssh key pair inside the keys folder and add the public key to your gitlab instance. This is needed because this package has a private dependency to a package inside the graphpolaris group, which means that it needs to authenticate with gitlab to download the dependencies.

Testing and Coverage

To test the make test command can be used. It will run every test in the repo. To get the code coverage the command make coverage can be used. This command will run all tests and display total coverage over all files. It will also generate an html file which displays exactly which lines have been covered.

Kubernetes

All the Kubernetes config files live in the /deployments directory. This service has two deployments, which only differ in the environment variables being set for query conversion and execution. This service also has two horizontal pod autoscalers, which will scale the service based on CPU load.

Service Flow

Query service flow

Service Input

Here is the input message format.

{
  "return": {
    "entities": [29, 30],
    "relations": [28],
    "groupBys": []
    },
    "entities": [
      {
        "name": "parliament",
        "ID": 29,
        "constraints": []
      },
      {
        "name": "parties",
        "ID": 30,
        "constraints": [
          {
            "attribute": "seats",
            "value": "5",
            "dataType": "int",
            "matchType": "LT"
          }
        ]
      }
    ],
    "relations": [
      {
        "ID": 28,
        "name": "member_of",
        "depth": {
          "min": 1,
          "max": 1
        },
        "fromType": "entity",
        "fromID": 29,
        "toType": "entity",
        "toID": 30,
        "constraints": []
      }
    ],
    "groupBys": [],
    "machineLearning": [
      {
        "queuename": "cdt_queue",
        "parameters": []
      }
    ],
    "limit": 5000,
    "databaseName": "Tweede Kamer Dataset"
  }
}

Some additional headers also have to be set in the RabbitMQ message, these should be set by the query orchestrator.

{
	"sessionID": "12345",
	"clientID": "12345",
	"queryID": "12345"
}

Service Output

Successful Execution

{
    "edges": [
        { "attributes": { "isChairman": false }, "from": "parliament/126", "id": "member_of/126", "to": "parties/SGP" },
        { "attributes": { "isChairman": true }, "from": "parliament/125", "id": "member_of/125", "to": "parties/SGP" },
        { "attributes": { "isChairman": false }, "from": "parliament/122", "id": "member_of/122", "to": "parties/GroepVanHaga" },
        { "attributes": { "isChairman": true }, "from": "parliament/120", "id": "member_of/120", "to": "parties/BIJ1" },
        { "attributes": { "isChairman": false }, "from": "parliament/112", "id": "member_of/112", "to": "parties/JA21" },
        { "attributes": { "isChairman": true }, "from": "parliament/110", "id": "member_of/110", "to": "parties/BBB" },
        { "attributes": { "isChairman": false }, "from": "parliament/84", "id": "member_of/84", "to": "parties/DENK" },
        { "attributes": { "isChairman": false }, "from": "parliament/78", "id": "member_of/78", "to": "parties/Volt" },
        { "attributes": { "isChairman": true }, "from": "parliament/50", "id": "member_of/50", "to": "parties/GroepVanHaga" },
        { "attributes": { "isChairman": true }, "from": "parliament/49", "id": "member_of/49", "to": "parties/FractieDenHaan" },
        { "attributes": { "isChairman": false }, "from": "parliament/48", "id": "member_of/48", "to": "parties/Volt" },
        { "attributes": { "isChairman": false }, "from": "parliament/37", "id": "member_of/37", "to": "parties/JA21" },
        { "attributes": { "isChairman": false }, "from": "parliament/36", "id": "member_of/36", "to": "parties/GroepVanHaga" },
        { "attributes": { "isChairman": true }, "from": "parliament/32", "id": "member_of/32", "to": "parties/JA21" },
        { "attributes": { "isChairman": true }, "from": "parliament/27", "id": "member_of/27", "to": "parties/Volt" },
        { "attributes": { "isChairman": false }, "from": "parliament/17", "id": "member_of/17", "to": "parties/SGP" },
        { "attributes": { "isChairman": false }, "from": "parliament/7", "id": "member_of/7", "to": "parties/DENK" },
        { "attributes": { "isChairman": true }, "from": "parliament/6", "id": "member_of/6", "to": "parties/DENK" }
    ],
    "nodes": [
        { "attributes": { "chairman": "Sylvana Simons", "chairmanId": 120, "name": "BIJ1", "seats": 1 }, "id": "parties/BIJ1", "mldata": 0 },
        { "attributes": { "chairman": "Caroline van der Plas", "chairmanId": 110, "name": "BoerBurgerBeweging", "seats": 1 }, "id": "parties/BBB", "mldata": 1 },
        { "attributes": { "chairman": "Liane den Haan", "chairmanId": 49, "name": "Fractie Den Haan", "seats": 1 }, "id": "parties/FractieDenHaan", "mldata": 2 },
        { "attributes": { "chairman": "Wybren van Haga", "chairmanId": 50, "name": "Groep Van Haga", "seats": 3 }, "id": "parties/GroepVanHaga", "mldata": 3 },
        { "attributes": { "chairman": "Joost Eerdmans", "chairmanId": 32, "name": "JA21", "seats": 3 }, "id": "parties/JA21", "mldata": 4 },
        { "attributes": { "chairman": "Laurens Dassen", "chairmanId": 27, "name": "Volt", "seats": 3 }, "id": "parties/Volt", "mldata": 5 },
        { "attributes": { "chairman": "Kees van der Staaij", "chairmanId": 125, "name": "Staatkundig Gereformeerde Partij", "seats": 3 }, "id": "parties/SGP", "mldata": 6 },
        { "attributes": { "chairman": "Farid Azarkan", "chairmanId": 6, "name": "DENK", "seats": 3 }, "id": "parties/DENK", "mldata": 7 },
        {
            "attributes": {
                "age": 46,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/98aa8bde-85b0-49ed-9795-e2eeb206e3b8.jpg?itok=CODn43Bf",
                "name": "Chris Stoffer",
                "party": "SGP",
                "residence": "Elspeet",
                "seniority": 1142
            },
            "id": "parliament/126",
            "mldata": 6
        },
        {
            "attributes": {
                "age": 52,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/1b59d69d-89a1-42c3-b5bf-52f38c39d88d.jpg?itok=o4nroqLI",
                "name": "Kees van der Staaij",
                "party": "SGP",
                "residence": "Benthuizen",
                "seniority": 8409
            },
            "id": "parliament/125",
            "mldata": 6
        },
        {
            "attributes": {
                "age": 60,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/eb79af3c-a66d-4374-b9e7-34d7f3e92dc7.jpg?itok=wMZmTmkc",
                "name": "Hans Smolders",
                "party": "Groep Van Haga",
                "residence": "Tilburg",
                "seniority": 309
            },
            "id": "parliament/122",
            "mldata": 3
        },
        {
            "attributes": {
                "age": 50,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/0ae6735e-4b1b-4302-a20e-bbe5b72155c3.jpg?itok=C1-541cc",
                "name": "Sylvana Simons",
                "party": "BIJ1",
                "residence": "Duivendrecht",
                "seniority": 57
            },
            "id": "parliament/120",
            "mldata": 0
        },
        {
            "attributes": {
                "age": 30,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/9f0c39d6-4a57-4849-9215-aa1147c7e0f6.jpg?itok=1JWOuIjk",
                "name": "Nicki Pouw-Verweij",
                "party": "JA21",
                "residence": "Maarssen",
                "seniority": 57
            },
            "id": "parliament/112",
            "mldata": 4
        },
        {
            "attributes": {
                "age": 53,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/b41b2403-9f06-4d55-adc5-b0777373ca7e.jpg?itok=rmudNELC",
                "name": "Caroline van der Plas",
                "party": "BBB",
                "residence": "Deventer",
                "seniority": 57
            },
            "id": "parliament/110",
            "mldata": 1
        },
        {
            "attributes": {
                "age": 39,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/16596afa-9e56-4d0a-a715-f40592825937.jpg?itok=xVPho7Yp",
                "name": "Tunahan Kuzu",
                "party": "DENK",
                "residence": "Rotterdam",
                "seniority": 3171
            },
            "id": "parliament/84",
            "mldata": 7
        },
        {
            "attributes": {
                "age": 32,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/d34e1efc-fbba-4ad8-823a-61bc3f25d580.jpg?itok=WXFGv3_8",
                "name": "Marieke Koekkoek",
                "party": "Volt",
                "residence": "Utrecht",
                "seniority": 57
            },
            "id": "parliament/78",
            "mldata": 5
        },
        {
            "attributes": {
                "age": 54,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/ab15ce32-a527-4526-a5b2-0f2cdb92feeb.jpg?itok=rPcdqouS",
                "name": "Wybren van Haga",
                "party": "Groep Van Haga",
                "residence": "Haarlem",
                "seniority": 1305
            },
            "id": "parliament/50",
            "mldata": 3
        },
        {
            "attributes": {
                "age": 53,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/3e48d4c9-0d66-4b50-9f9c-969b5e314a42.jpg?itok=0wlX1bse",
                "name": "Liane den Haan",
                "party": "Fractie Den Haan",
                "residence": "Woerden",
                "seniority": 58
            },
            "id": "parliament/49",
            "mldata": 2
        },
        {
            "attributes": {
                "age": 43,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/f31cabe4-e1f4-46db-88a9-b46ba44dded4.jpg?itok=FfoXTIug",
                "name": "Nilfer  Gndoan",
                "party": "Volt",
                "residence": "Amsterdam",
                "seniority": 57
            },
            "id": "parliament/48",
            "mldata": 5
        },
        {
            "attributes": {
                "age": 62,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/6e4a8d23-9d3b-4565-a3ab-086eb51d6c08.jpg?itok=RSKd0BLe",
                "name": "Derk Jan  Eppink",
                "party": "JA21",
                "residence": "'s-Gravenhage",
                "seniority": 57
            },
            "id": "parliament/37",
            "mldata": 4
        },
        {
            "attributes": {
                "age": 55,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/d59d1492-57b6-482b-88b6-b2b77c45c33d.jpg?itok=g4RdRfw2",
                "name": "Olaf Ephraim",
                "party": "Groep Van Haga",
                "residence": "Amsterdam",
                "seniority": 57
            },
            "id": "parliament/36",
            "mldata": 3
        },
        {
            "attributes": {
                "age": 50,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/c6e26bd3-724f-45d3-93af-ef431e6fcd42.jpg?itok=k6ziMwhD",
                "name": "Joost Eerdmans",
                "party": "JA21",
                "residence": "Rotterdam",
                "seniority": 1709
            },
            "id": "parliament/32",
            "mldata": 4
        },
        {
            "attributes": {
                "age": 35,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/a68468b6-51ea-440c-9387-b44ba10ad8b6.jpg?itok=UYMugaP_",
                "name": "Laurens Dassen",
                "party": "Volt",
                "residence": "Amsterdam",
                "seniority": 57
            },
            "id": "parliament/27",
            "mldata": 5
        },
        {
            "attributes": {
                "age": 64,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/9b13c8c8-eba4-411e-b095-858e352d3178.jpg?itok=qcZcaIXc",
                "name": "Roelof Bisschop",
                "party": "SGP",
                "residence": "Veenendaal",
                "seniority": 3171
            },
            "id": "parliament/17",
            "mldata": 6
        },
        {
            "attributes": {
                "age": 29,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/859e1824-cc60-4304-9293-dca932ebbed2.jpg?itok=WSmCeVtj",
                "name": "Stephan van Baarle",
                "party": "DENK",
                "residence": "Rotterdam",
                "seniority": 57
            },
            "id": "parliament/7",
            "mldata": 7
        },
        {
            "attributes": {
                "age": 49,
                "img": "https://www.tweedekamer.nl/sites/default/files/styles/member_parlement_profile_square/public/09155809-0b6e-4f9f-a75b-026aa4b35588.jpg?itok=oVDwOcW4",
                "name": "Farid Azarkan",
                "party": "DENK",
                "residence": "Culemborg",
                "seniority": 1526
            },
            "id": "parliament/6",
            "mldata": 7
        }
    ],
    "rows": []
}

Status Updates

The service will also publish updates regarding certain queries. These follow the following pattern:

{
	"status": "query_processing_database",
	"queryID": "12345"
}

Errors

If the service encounters an error, a message of the following form will be sent:

{
	"type": "query_error",
	"value":
  {
    "status": "invalid database credentials",
    "queryID": "12345"
  }
}

Extra Information

  • run GOPRIVATE=git.science.uu.nl/graphpolaris/* go get <privaterepo> to add a private repo
  • run git config --global url."git@git.science.uu.nl:".insteadOf "https://git.science.uu.nl/" to allow go to use ssh instead of https