From b575eddddb64b059c9526588c739b67620bd0bf8 Mon Sep 17 00:00:00 2001 From: Mauricio Matias Date: Wed, 26 May 2021 15:47:38 -0400 Subject: [PATCH 1/2] fix: adding new visualization mappings --- README.md | 79 ++++-------- .../mappings/{histogram.json => area.json} | 27 +---- kibana_api/mappings/heatmap.json | 44 +++++++ kibana_api/mappings/line.json | 89 ++++++++++++++ kibana_api/mappings/metric.json | 4 +- kibana_api/mappings/pie.json | 26 ++++ kibana_api/mappings/table.json | 25 ++++ kibana_api/mappings/tagcloud.json | 20 ++++ kibana_api/objects.py | 58 ++++++--- setup.py | 2 +- tests/.env | 3 + tests/docker-compose.yml | 71 +++-------- tests/tests.py | 112 +++++++++--------- 13 files changed, 350 insertions(+), 210 deletions(-) rename kibana_api/mappings/{histogram.json => area.json} (75%) create mode 100644 kibana_api/mappings/heatmap.json create mode 100644 kibana_api/mappings/line.json create mode 100644 kibana_api/mappings/pie.json create mode 100644 kibana_api/mappings/table.json create mode 100644 kibana_api/mappings/tagcloud.json create mode 100644 tests/.env diff --git a/README.md b/README.md index 085edb1..0b02c2b 100644 --- a/README.md +++ b/README.md @@ -68,83 +68,44 @@ testing purposes): ```yaml version: '2.2' + services: - es01: + elastic: + hostname: elasticsearch image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es01 + container_name: elastic environment: - - node.name=es01 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es02,es03 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + - discovery.type=single-node + - xpack.security.enabled=true + - xpack.security.audit.enabled=true + - ELASTIC_PASSWORD=${ELASTIC_PASSWORD} ulimits: memlock: soft: -1 hard: -1 volumes: - - data01:/usr/share/elasticsearch/data + - elastic_volume:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - elastic - es02: - image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es02 - environment: - - node.name=es02 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es01,es03 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - ulimits: - memlock: - soft: -1 - hard: -1 - volumes: - - data02:/usr/share/elasticsearch/data - networks: - - elastic - - es03: - image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es03 - environment: - - node.name=es03 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es01,es02 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - ulimits: - memlock: - soft: -1 - hard: -1 - volumes: - - data03:/usr/share/elasticsearch/data - networks: - - elastic - - kib01: + kibana: image: docker.elastic.co/kibana/kibana:${VERSION} - container_name: kib01 + container_name: kibana ports: - 5601:5601 environment: - ELASTICSEARCH_URL: http://es01:9200 - ELASTICSEARCH_HOSTS: '["http://es01:9200","http://es02:9200","http://es03:9200"]' + ELASTICSEARCH_URL: http://elasticsearch:9200 + ELASTICSEARCH_USERNAME: ${ELASTIC_USERNAME} + ELASTICSEARCH_PASSWORD: ${ELASTIC_PASSWORD} + ADMIN_PRIVILEGES: "true" networks: - elastic volumes: - data01: - driver: local - data02: - driver: local - data03: + elastic_volume: driver: local networks: @@ -152,6 +113,14 @@ networks: driver: bridge ``` +The `.env` file cointains: + +```bash +VERSION=7.8.0 +ELASTIC_USERNAME=elastic +ELASTIC_PASSWORD=elastic +``` + Once the container is up you can validate every unit test: ```bash diff --git a/kibana_api/mappings/histogram.json b/kibana_api/mappings/area.json similarity index 75% rename from kibana_api/mappings/histogram.json rename to kibana_api/mappings/area.json index e7b6fe5..a85c50a 100644 --- a/kibana_api/mappings/histogram.json +++ b/kibana_api/mappings/area.json @@ -1,5 +1,5 @@ { - "type": "histogram", + "type": "area", "aggs": [ { "id": "1", @@ -7,25 +7,6 @@ "type": "count", "schema": "metric", "params": {} - }, - { - "id": "2", - "enabled": true, - "type": "date_histogram", - "schema": "segment", - "params": { - "field": "@timestamp", - "timeRange": { - "from": "now-1h", - "to": "now" - }, - "useNormalizedEsInterval": true, - "scaleMetricValues": false, - "interval": "auto", - "drop_partials": false, - "min_doc_count": 1, - "extended_bounds": {} - } } ], "params": { @@ -77,8 +58,8 @@ "seriesParams": [ { "show": true, - "type": "histogram", - "mode": "normal", + "type": "area", + "mode": "stacked", "data": { "label": "Count", "id": "1" @@ -104,5 +85,5 @@ }, "labels": {} }, - "title": "" + "title": "area" } \ No newline at end of file diff --git a/kibana_api/mappings/heatmap.json b/kibana_api/mappings/heatmap.json new file mode 100644 index 0000000..9a27915 --- /dev/null +++ b/kibana_api/mappings/heatmap.json @@ -0,0 +1,44 @@ +{ + "type": "heatmap", + "aggs": [ + { + "id": "1", + "enabled": true, + "type": "count", + "schema": "metric", + "params": {} + } + ], + "params": { + "type": "heatmap", + "addTooltip": true, + "addLegend": true, + "enableHover": false, + "legendPosition": "right", + "times": [], + "colorsNumber": 4, + "colorSchema": "Greens", + "setColorRange": false, + "colorsRange": [], + "invertColors": false, + "percentageMode": false, + "valueAxes": [ + { + "show": false, + "id": "ValueAxis-1", + "type": "value", + "scale": { + "type": "linear", + "defaultYExtents": false + }, + "labels": { + "show": false, + "rotate": 0, + "overwriteColor": false, + "color": "black" + } + } + ] + }, + "title": "heatmap" +} diff --git a/kibana_api/mappings/line.json b/kibana_api/mappings/line.json new file mode 100644 index 0000000..86a6cd1 --- /dev/null +++ b/kibana_api/mappings/line.json @@ -0,0 +1,89 @@ +{ + "type": "line", + "aggs": [ + { + "id": "1", + "enabled": true, + "type": "count", + "schema": "metric", + "params": {} + } + ], + "params": { + "type": "line", + "grid": { + "categoryLines": false + }, + "categoryAxes": [ + { + "id": "CategoryAxis-1", + "type": "category", + "position": "bottom", + "show": true, + "style": {}, + "scale": { + "type": "linear" + }, + "labels": { + "show": true, + "filter": true, + "truncate": 100 + }, + "title": {} + } + ], + "valueAxes": [ + { + "id": "ValueAxis-1", + "name": "LeftAxis-1", + "type": "value", + "position": "left", + "show": true, + "style": {}, + "scale": { + "type": "linear", + "mode": "normal" + }, + "labels": { + "show": true, + "rotate": 0, + "filter": false, + "truncate": 100 + }, + "title": { + "text": "Count" + } + } + ], + "seriesParams": [ + { + "show": true, + "type": "line", + "mode": "normal", + "data": { + "label": "Count", + "id": "1" + }, + "valueAxis": "ValueAxis-1", + "drawLinesBetweenPoints": true, + "lineWidth": 2, + "interpolate": "linear", + "showCircles": true + } + ], + "addTooltip": true, + "addLegend": true, + "legendPosition": "right", + "times": [], + "addTimeMarker": false, + "labels": {}, + "thresholdLine": { + "show": false, + "value": 10, + "width": 1, + "style": "full", + "color": "#E7664C" + } + }, + "title": "line" +} \ No newline at end of file diff --git a/kibana_api/mappings/metric.json b/kibana_api/mappings/metric.json index cac9310..1f1f6b4 100644 --- a/kibana_api/mappings/metric.json +++ b/kibana_api/mappings/metric.json @@ -37,5 +37,5 @@ } } }, - "title": "" -} \ No newline at end of file + "title": "metric" +} diff --git a/kibana_api/mappings/pie.json b/kibana_api/mappings/pie.json new file mode 100644 index 0000000..a4d7aab --- /dev/null +++ b/kibana_api/mappings/pie.json @@ -0,0 +1,26 @@ +{ + "type": "pie", + "aggs": [ + { + "id": "1", + "enabled": true, + "type": "count", + "schema": "metric", + "params": {} + } + ], + "params": { + "type": "pie", + "addTooltip": true, + "addLegend": true, + "legendPosition": "right", + "isDonut": true, + "labels": { + "show": false, + "values": true, + "last_level": true, + "truncate": 100 + } + }, + "title": "pie" +} diff --git a/kibana_api/mappings/table.json b/kibana_api/mappings/table.json new file mode 100644 index 0000000..a5792cd --- /dev/null +++ b/kibana_api/mappings/table.json @@ -0,0 +1,25 @@ +{ + "type": "table", + "aggs": [ + { + "id": "1", + "enabled": true, + "type": "count", + "schema": "metric", + "params": {} + } + ], + "params": { + "perPage": 10, + "showPartialRows": false, + "showMetricsAtAllLevels": false, + "sort": { + "columnIndex": null, + "direction": null + }, + "showTotal": false, + "totalFunc": "sum", + "percentageCol": "" + }, + "title": "table" +} diff --git a/kibana_api/mappings/tagcloud.json b/kibana_api/mappings/tagcloud.json new file mode 100644 index 0000000..3afa77d --- /dev/null +++ b/kibana_api/mappings/tagcloud.json @@ -0,0 +1,20 @@ +{ + "type": "tagcloud", + "aggs": [ + { + "id": "1", + "enabled": true, + "type": "count", + "schema": "metric", + "params": {} + } + ], + "params": { + "scale": "linear", + "orientation": "single", + "minFontSize": 18, + "maxFontSize": 72, + "showLabel": true + }, + "title": "tagcloud" +} diff --git a/kibana_api/objects.py b/kibana_api/objects.py index 8fecf7a..936c4ac 100644 --- a/kibana_api/objects.py +++ b/kibana_api/objects.py @@ -1,7 +1,7 @@ import json import secrets import os -from .base import BaseModel +from .base import BaseModel, Utils class Space(BaseModel): def __init__(self, id=None, name=None, description=None, color=None, initials=None, disabledFeatures=None, _reserved=None, kibana=None) -> None: @@ -135,49 +135,71 @@ def __querier(self): } return json.dumps(data) -class Visualization(): +class Visualization(Utils): - def __init__(self, type, title, index_pattern_id, query="", mappings_filepath=None) -> None: + def __init__(self, index_pattern_id:str, title:str="", query:str="", mappings_dir_path:str=None, type:str=None) -> None: CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) - MAPPINGS_DIR = os.path.join(CURRENT_DIR, 'mappings') - print("mapping_dir", MAPPINGS_DIR) self.primitive_type="visualization" - self.type= type + self.types=["area", "heatmap", "line", "metric", "pie", "table", "tagcloud"] + self.type= type.lower() self.title = title self.index_pattern_id = index_pattern_id self.query = query - self.mappings_file_path = os.path.join(MAPPINGS_DIR, "{}.json".format(self.type)) if not mappings_filepath else mappings_filepath + self.mappings_dir_path = os.path.join(CURRENT_DIR, 'mappings') if not mappings_dir_path else mappings_dir_path + self.data = self.load_visualizations() - def create(self): + def load_visualizations(self): + data = {} + for type in self.types: + file_path = os.path.join(self.mappings_dir_path, "{}.json".format(type)) + data[type] = self.__read_json_file(file_path) + return data + + def create(self, index_pattern_id:str=None, title:str="", body={}, query:str="") : + title = self.title if not title else title + visualization_state = self.__templater(title) if not body else self.__templater_json(title, body) + search_state = self.__querier(self.query if not query else query) + index_pattern_id = self.index_pattern_id if not index_pattern_id else index_pattern_id + print(visualization_state) return { "attributes": { - "title": self.title, - "visState": self.__templater(self.title), + "title": title, + "visState": visualization_state, "uiStateJSON": "{}", "description": "", "version": 1, "kibanaSavedObjectMeta": { - "searchSourceJSON": self.__querier() + "searchSourceJSON": search_state } }, "references": [{ "name": "kibanaSavedObjectMeta.searchSourceJSON.index", "type": "index-pattern", - "id": self.index_pattern_id + "id": index_pattern_id }] } - def __templater(self, title): - file = open(self.mappings_file_path, 'r') - data = json.load(file) - data["title"] = title + def __read_json_file(self, file_path): + file = open(file_path, 'r') + read = file.read() file.close() + return json.loads(read) + + def __templater_json(self, title, _json) -> str: + data = _json + data["title"] = title return json.dumps(data) - def __querier(self): + def __templater(self, title) -> str: + if self.validate_type(self.type, self.types): + data = self.data[self.type] + data["title"] = title + return json.dumps(data) + + def __querier(self, query=None) -> str: data = { "query": { - "query": self.query, + "query": query, "language": "kuery" }, "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.index", diff --git a/setup.py b/setup.py index b90e3c5..b11e1f0 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="kibana-api", - version="0.0.1", + version="0.0.2", author="Mauricio Matias Conde", author_email="mcm.crw@gmail.com", description="This is an API mapping library for Kibana API to generate visualizations and dashboards automatically", diff --git a/tests/.env b/tests/.env new file mode 100644 index 0000000..8470d34 --- /dev/null +++ b/tests/.env @@ -0,0 +1,3 @@ +VERSION=7.8.0 +ELASTIC_USERNAME=elastic +ELASTIC_PASSWORD=elastic \ No newline at end of file diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 4a0b1bb..90c9258 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -1,81 +1,42 @@ version: '2.2' + services: - es01: + elastic: + hostname: elasticsearch image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es01 + container_name: elastic environment: - - node.name=es01 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es02,es03 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + - discovery.type=single-node + - xpack.security.enabled=true + - xpack.security.audit.enabled=true + - ELASTIC_PASSWORD=${ELASTIC_PASSWORD} ulimits: memlock: soft: -1 hard: -1 volumes: - - data01:/usr/share/elasticsearch/data + - elastic_volume:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - elastic - es02: - image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es02 - environment: - - node.name=es02 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es01,es03 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - ulimits: - memlock: - soft: -1 - hard: -1 - volumes: - - data02:/usr/share/elasticsearch/data - networks: - - elastic - - es03: - image: docker.elastic.co/elasticsearch/elasticsearch:${VERSION} - container_name: es03 - environment: - - node.name=es03 - - cluster.name=es-docker-cluster - - discovery.seed_hosts=es01,es02 - - cluster.initial_master_nodes=es01,es02,es03 - - bootstrap.memory_lock=true - - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - ulimits: - memlock: - soft: -1 - hard: -1 - volumes: - - data03:/usr/share/elasticsearch/data - networks: - - elastic - - kib01: + kibana: image: docker.elastic.co/kibana/kibana:${VERSION} - container_name: kib01 + container_name: kibana ports: - 5601:5601 environment: - ELASTICSEARCH_URL: http://es01:9200 - ELASTICSEARCH_HOSTS: '["http://es01:9200","http://es02:9200","http://es03:9200"]' + ELASTICSEARCH_URL: http://elasticsearch:9200 + ELASTICSEARCH_USERNAME: ${ELASTIC_USERNAME} + ELASTICSEARCH_PASSWORD: ${ELASTIC_PASSWORD} + ADMIN_PRIVILEGES: "true" networks: - elastic volumes: - data01: - driver: local - data02: - driver: local - data03: + elastic_volume: driver: local networks: diff --git a/tests/tests.py b/tests/tests.py index feb9f8d..b88d679 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -3,83 +3,83 @@ import unittest import os - URL = "http://localhost:5601" -USERNAME = "" -PASSWORD = "" - +USERNAME = "elastic" +PASSWORD = "elastic" class TestStringMethods(unittest.TestCase): def test_url_parser(self): pass - kibana = Kibana(base_url=URL) - url = kibana.url(URL, "1", "2", "3") - self.assertEqual("http://localhost:5601/1/2/3", url) + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # url = kibana.url(URL, "1", "2", "3") + # self.assertEqual("http://localhost:5601/1/2/3", url) def test_create_space(self): pass - kibana = Kibana(base_url=URL) - id = f"test-{int(random.randint(0,100)*0.33)}" - name = "test" + id - description = "descripcion del espacio de pruebas" - color = "#000000" - response = kibana.space(id=id, name=name, description=description, color=color).create() - response_json = { - "id": id, - "name": name, - "description": description, - "color": color, - "disabledFeatures": [] - } - self.assertEqual(response.json(), response_json) + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # id = f"test-{int(random.randint(0,100)*0.33)}" + # name = "test" + id + # description = "descripcion del espacio de pruebas" + # color = "#000000" + # response = kibana.space(id=id, name=name, description=description, color=color).create() + # response_json = { + # "id": id, + # "name": name, + # "description": description, + # "color": color, + # "disabledFeatures": [] + # } + # self.assertEqual(response.json(), response_json) def test_create_index_pattern(self): pass - pattern_json = { - "title":"demo*", - "timeFieldName": "@timestamp", - "fields":"[]" - } - kibana = Kibana(base_url=URL) - res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern') - self.assertEqual(res.json()["attributes"], pattern_json) + # pattern_json = { + # "title":"demo*", + # "timeFieldName": "@timestamp", + # "fields":"[]" + # } + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern') + # self.assertEqual(res.json()["attributes"], pattern_json) def test_import(self): pass - CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) - FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson') - file = open(FILE_PATH, 'r') - response = Kibana(base_url=URL).object().loads(file=file) - file.close() - print(response.json()) + # CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + # FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson') + # file = open(FILE_PATH, 'r') + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # response = kibana.object().loads(file=file) + # file.close() + # print(response.json()) def test_get_all_objects(self): pass - response = Kibana(base_url=URL).object(space_id="demo").all(type="index-pattern") - print(response.json()) + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # response = kibana.object(space_id="demo").all(type="index-pattern") + # print(response.json()) def test_create_panel(self): pass - test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'} - result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="asdasdasd") - print(result.get_reference()) - self.assertEqual(test, result.create()) + # test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'} + # result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="asdasdasd") + # print(result.get_reference()) + # self.assertEqual(test, result.create()) def test_create_visualization(self): pass - pattern_json = { - "title":"demo*", - "timeFieldName": "@timestamp", - "fields":"[]" - } - kibana = Kibana(base_url=URL) - res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json() - index_pattern = res["id"] - type = "histogram" - title = "hello this is a visualization :D 2" - visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() - res = kibana.object(space_id="demo").create('visualization', body=visualization).json() - print(res) + # pattern_json = { + # "title":"demo*", + # "timeFieldName": "@timestamp", + # "fields":"[]" + # } + # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + # res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json() + # index_pattern = res["id"] + # type = "line" + # title = "hello this is a visualization :D 2" + # visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() + # res = kibana.object(space_id="demo").create('visualization', body=visualization).json() + # print(res) def test_create_dashboard(self): pass @@ -88,10 +88,10 @@ def test_create_dashboard(self): "timeFieldName": "@timestamp", "fields":"[]" } - kibana = Kibana(base_url=URL) + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json() index_pattern = res["id"] - type = "histogram" + type = "line" title = "hello this is a visualization :D 3" visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() res = kibana.object(space_id="demo").create('visualization', body=visualization).json() From 2bcba60176d6d83948f46b93c083e4b7651bd07c Mon Sep 17 00:00:00 2001 From: Mauricio Matias Date: Wed, 26 May 2021 16:38:11 -0400 Subject: [PATCH 2/2] fix: tests --- README.md | 88 +++++++++++++++++++++++++++++----------- tests/tests.py | 106 ++++++++++++++++++++++++------------------------- 2 files changed, 117 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 0b02c2b..e16d4ce 100644 --- a/README.md +++ b/README.md @@ -16,49 +16,89 @@ URL = "http://localhost:5601" USERNAME = "XXXX" PASSWORD = "XXXX" +kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) ``` ### Create Space ```python -"" +id = "demo" +name = "demo" +description = "descripcion del espacio de pruebas" +color = "#000000" +space = kibana.space(id=id, name=name, description=description, color=color) +space_response = space.create() ``` -out: -```bash -"" -``` - ### Create Object (index-pattern) ```python -"" -``` -out: -```bash -"" +pattern_json = { + "title":"demo*", + "timeFieldName": "@timestamp", #timefiledname is important, it taken as a reference to time + "fields":"[]" +} +kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) +index_pattern_response = kibana.object(space_id="demo").create('index-pattern', attribs=pattern_json) ``` ### Create Object (visualization) ```python -"" +type = "metric" +title = "Hello this is a basic metric visualization" +index_pattern_id = "XXXX-XXX-XXXX" # every visualization needs an index pattern to work +visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() +visualization_response = kibana.object(space_id="demo").create('visualization', body=visualization).json() ``` -out: -```bash -"" +### Visualization Modelation +```python +index_pattern = "XXXXX-XXXXXX-XXXXXX" +type = "line" +title = "Hello this is a basic line visualization" +visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern) +visulization_json = visualization.create() ``` -### Create Object (dashboard) + +### Panel Modelation ```python -"" +width=48 +height=12 +pos_x=0 +pos_y=1 +panel = Panel("panel_0", width, height, pos_x, pos_y, visualization_id=visualization_id) +panel_json = panel.create() +references = panel.get_references() ``` -out: -```bash -"" + +### Create Object (dashboard) +```python +index_pattern = "XXXXX-XXXXXX-XXXXXX" +type = "line" +title = "Hello this is a basic line visualization" +visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() +visualization_response = kibana.object(space_id="demo").create('visualization', body=visualization).json() +visualization_id = visualization_response["id"] +panel = Panel("panel_0", 48, 12, 0, 2, visualization_id=visualization_id) +panels = [panel.create()] +references = [panel.get_reference()] +dashboard = Dashboard(title="Demo Dashboard", panels=panels, references=references) +dashboard_response = dashboard.create() ``` + ### List all objects ```python -"" +objects_response = kibana.object(space_id="demo").all() # All objects +print(objects_response.json()) +# Filter by types: "visualization", "dashboard", "search", "index-pattern", +# "config", "timelion-sheet", "url", "query", "canvas-element", "canvas-workpad", "lens", +# "infrastructure-ui-source", "metrics-explorer-view", "inventory-view" +objects_response = kibana.object(space_id="demo").all(type="index-pattern") # Type in specific +print(objects_response.json()) + ``` -out: -```bash -"" + +### Import Objects +```python +file = open("demo.ndjson", 'r') +response = kibana.object().loads(file=file) +file.close() ``` ## Development diff --git a/tests/tests.py b/tests/tests.py index b88d679..154a563 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -10,76 +10,77 @@ class TestStringMethods(unittest.TestCase): def test_url_parser(self): pass - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # url = kibana.url(URL, "1", "2", "3") - # self.assertEqual("http://localhost:5601/1/2/3", url) + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + url = kibana.url(URL, "1", "2", "3") + self.assertEqual("http://localhost:5601/1/2/3", url) def test_create_space(self): pass - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # id = f"test-{int(random.randint(0,100)*0.33)}" - # name = "test" + id - # description = "descripcion del espacio de pruebas" - # color = "#000000" - # response = kibana.space(id=id, name=name, description=description, color=color).create() - # response_json = { - # "id": id, - # "name": name, - # "description": description, - # "color": color, - # "disabledFeatures": [] - # } - # self.assertEqual(response.json(), response_json) + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + id = f"test-{int(random.randint(0,100)*0.33)}" + name = "test" + id + description = "descripcion del espacio de pruebas" + color = "#000000" + response = kibana.space(id=id, name=name, description=description, color=color).create() + response_json = { + "id": id, + "name": name, + "description": description, + "color": color, + "disabledFeatures": [] + } + self.assertEqual(response.json(), response_json) def test_create_index_pattern(self): pass - # pattern_json = { - # "title":"demo*", - # "timeFieldName": "@timestamp", - # "fields":"[]" - # } - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern') - # self.assertEqual(res.json()["attributes"], pattern_json) + pattern_json = { + "title":"demo*", + "timeFieldName": "@timestamp", + "fields":"[]" + } + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + res = kibana.object(space_id="demo").create('index-pattern', attribs=pattern_json) + self.assertEqual(res.json()["attributes"], pattern_json) def test_import(self): pass - # CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) - # FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson') - # file = open(FILE_PATH, 'r') - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # response = kibana.object().loads(file=file) - # file.close() - # print(response.json()) + CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson') + file = open(FILE_PATH, 'r') + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + response = kibana.object().loads(file=file) + file.close() + print(response.json()) def test_get_all_objects(self): pass - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # response = kibana.object(space_id="demo").all(type="index-pattern") - # print(response.json()) + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + response = kibana.object(space_id="demo").all(type="index-pattern") + print(response.json()) def test_create_panel(self): pass - # test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'} - # result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="asdasdasd") - # print(result.get_reference()) - # self.assertEqual(test, result.create()) + test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'} + result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="XXXXXXXXXXXX") + references = result.get_reference() + print(references) + self.assertEqual(test, result.create()) def test_create_visualization(self): pass - # pattern_json = { - # "title":"demo*", - # "timeFieldName": "@timestamp", - # "fields":"[]" - # } - # kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) - # res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json() - # index_pattern = res["id"] - # type = "line" - # title = "hello this is a visualization :D 2" - # visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() - # res = kibana.object(space_id="demo").create('visualization', body=visualization).json() - # print(res) + pattern_json = { + "title":"demo*", + "timeFieldName": "@timestamp", + "fields":"[]" + } + kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD) + res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json() + index_pattern = res["id"] + type = "line" + title = "hello this is a visualization :D 2" + visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create() + res = kibana.object(space_id="demo").create('visualization', body=visualization).json() + print(res) def test_create_dashboard(self): pass @@ -99,7 +100,6 @@ def test_create_dashboard(self): panel = Panel("panel_0", 48, 12, 0, 2, visualization_id=visualization_id) panels = [panel.create()] references = [panel.get_reference()] - print(panels, references) dasboard = Dashboard(title="hola mundo", panels=panels, references=references, query="user.name: mat*").create() res = kibana.object(space_id="demo").create('dashboard', body=dasboard).json() print(res)