Skip to content

Commit db9ffc3

Browse files
committed
Monitoring Logs with the Elastic Stack - Create Kibana Dashboard for Model Inputs
1 parent ef6cda0 commit db9ffc3

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

packages/ml_api/api/controller.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ def predict():
5454
if request.method == "POST":
5555
# Step 1: Extract POST data from request body as JSON
5656
json_data = request.get_json()
57-
_logger.info(
58-
f'Inputs for model: {ModelType.LASSO.name} '
59-
f'Input values: {json_data}')
57+
for entry in json_data:
58+
_logger.info(entry)
6059

6160
# Step 2a: Get and save live model predictions
6261
persistence = PredictionPersistence(db_session=current_app.db_session)

packages/ml_api/docker/kibana/config/kibana_example_inputs_dashboard.ndjson

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

packages/ml_api/docker/logstash/pipeline/logstash.conf

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ input {
88
}
99

1010
output {
11-
elasticsearch {
12-
hosts => "elasticsearch:9200"
13-
user => "elastic"
14-
password => "changeme"
15-
index => "webapp_logs-%{+YYYY.MM.dd}"
11+
if [LotArea] {
12+
elasticsearch {
13+
hosts => "elasticsearch:9200"
14+
user => "elastic"
15+
password => "changeme"
16+
index => "input_logs-%{+YYYY.MM.dd}"
17+
}
18+
} else {
19+
elasticsearch {
20+
hosts => "elasticsearch:9200"
21+
user => "elastic"
22+
password => "changeme"
23+
index => "webapp_logs-%{+YYYY.MM.dd}"
1624
}
17-
}
25+
}
26+
}

packages/ml_api/scripts/populate_database.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import time
44
import typing as t
5-
from random import randint
5+
from random import randint, choice
66

77
import pandas as pd
88
import requests
@@ -19,14 +19,21 @@
1919

2020
SECOND_FLR_SF_MAP = {"min": 0, "max": 1862}
2121

22+
BSMT_QUAL_VALUES = ('Gd', 'TA', 'Ex', 'Fa')
23+
2224

2325
def _generate_random_int(value: int, value_ranges: t.Mapping) -> int:
2426
"""Generate random integer within a min and max range."""
2527
random_value = randint(value_ranges["min"], value_ranges["max"])
26-
2728
return int(random_value)
2829

2930

31+
def _select_random_category(value: str, value_options: t.Sequence) -> str:
32+
"""Select random category given a sequence of categories."""
33+
random_category = choice(value_options)
34+
return random_category
35+
36+
3037
def _prepare_inputs(dataframe: pd.DataFrame) -> pd.DataFrame:
3138
"""Prepare input data by removing key rows with NA values."""
3239
clean_inputs_df = dataframe.dropna(
@@ -43,6 +50,10 @@ def _prepare_inputs(dataframe: pd.DataFrame) -> pd.DataFrame:
4350
_generate_random_int, value_ranges=LOT_AREA_MAP
4451
)
4552

53+
clean_inputs_df.loc[:, "BsmtQual"] = clean_inputs_df["BsmtQual"].apply(
54+
_select_random_category, value_options=BSMT_QUAL_VALUES
55+
)
56+
4657
return clean_inputs_df
4758

4859

@@ -74,6 +85,7 @@ def populate_database(n_predictions: int = 500, anomaly: bool = False) -> None:
7485
clean_inputs_df.loc[:, "OverallQual"] = 1
7586
clean_inputs_df.loc[:, "GrLivArea"] = 1
7687

88+
clean_inputs_df = clean_inputs_df.where(pd.notnull(clean_inputs_df), None)
7789
for index, data in clean_inputs_df.iterrows():
7890
if index > n_predictions:
7991
if anomaly:

packages/ml_api/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ passenv =
9898

9999
setenv =
100100
PYTHONPATH=.
101-
DB_HOST={env:DB_HOST:localhost}
101+
DB_HOST=localhost
102102

103103
commands = python scripts/populate_database.py {posargs}
104104

0 commit comments

Comments
 (0)