Format last entries from stream #4

This commit is contained in:
Freezed 2022-09-28 01:44:10 +02:00
parent c656f4f1bd
commit 645fd3a5da
3 changed files with 80 additions and 9 deletions

View File

@ -24,4 +24,3 @@ lint:
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
- make --no-print-directory --quiet lint
- make --no-print-directory --quiet test

40
ldpy.py
View File

@ -13,11 +13,13 @@ Licenses: GNU AGPL v3: http://www.gnu.org/licenses/
import argparse
import logging
import os
from pprint import pformat as pf
import sys
import client # pylint: disable=import-error
LAST_E_CHOICES = range(1, 21)
# Ranqe for the request retrieving the last entries.
LAST_E_CHOICES = range(1, 51)
logger = logging.getLogger(os.path.splitext(os.path.basename(sys.argv[0]))[0])
@ -94,11 +96,12 @@ def parse_args(args=sys.argv[1:]):
def get_last_entries(entries_nb):
"""Get stream last n entries."""
"""Get the last n entries from a stream."""
if entries_nb in LAST_E_CHOICES:
logger.debug("Wait before getting '%s' entries!", entries_nb)
query = {"size": entries_nb}
last_entries = client.opnsrch_clt.search(body=query)
logger.debug(pf(last_entries))
else:
logger.critical("'%s' is not in '%s'", entries_nb, LAST_E_CHOICES)
@ -133,13 +136,42 @@ def get_map_props():
return map_props
def strip_demo_entries(raw_data):
"""Remove keys in entries to ease human reading.
Returns a list populated with a dict for each entry.
This is a specific function for demo stream /!\
"""
stripped_data = []
for raw_hit in raw_data["hits"]["hits"]:
stripped_hit = {}
for key in [
"source",
"category",
"title",
"message",
"rating_num",
"timestamp",
]:
stripped_hit[key] = raw_hit["_source"][key]
stripped_data.append(stripped_hit)
return stripped_data
def main(options):
"""Execute as script. Functions related to the arguments passed."""
"""Execute as script. Functions related to the arguments passed.
Data stripper use a function build only for demo stream: `strip_demo_entries()`
"""
if options.mapping:
response = get_map_props()
logger.debug("Mapping for '%s' stream: %s", client.LDP_STREAM_NAME, response)
elif options.last:
response = get_last_entries(options.last)
response = pf(strip_demo_entries(get_last_entries(options.last)))
logger.debug(
"Last '%s' entries for '%s' stream:", options.last, client.LDP_STREAM_NAME
)

View File

@ -12,6 +12,11 @@ import logging
from pytest import mark, raises
import ldpy
from ldpy import LAST_E_CHOICES
# Min and Max values for the request retrieving the last entries.
LAST_E_MIN = LAST_E_CHOICES[0] - 1
LAST_E_MAX = len(LAST_E_CHOICES) + 1
# Faking options from argparse
@ -77,17 +82,53 @@ def test_parse_args_last_const():
assert cli_options.last == 3
# ###
# Testing strip_demo_entries()
# ###
def test_strip_demo_entries():
"""Remove keys/values to ease human readinq in demo stream."""
payload = {
"hits": {
"hits": [
{
"_id": "-",
"_source": {
"X-OVH-": "-",
"X-OVH-INPUT": "-",
"gl2_source": "-",
"gl2_source_node": "-",
"id": "-",
"source": "-",
"category": "-",
"title": "-",
"message": "-",
"rating_num": 42,
"streams": "-",
"timestamp": "-",
},
},
],
},
}
stripped_entry = ldpy.strip_demo_entries(payload)[0]
for key in ["source", "category", "title", "message", "rating_num", "timestamp"]:
stripped_entry.pop(key)
assert len(stripped_entry) == 0
# ###
# Testing get_last_entries()
# ###
@mark.parametrize("entry_np", [0, 21])
@mark.parametrize("entry_np", [LAST_E_MIN, LAST_E_MAX])
def test_get_last_entries_out_of_range(entry_np):
"""Value is out of range for the last entries."""
with raises(ValueError):
ldpy.get_last_entries(entry_np)
@mark.parametrize("entry_np", range(1, 21))
@mark.parametrize("entry_np", LAST_E_CHOICES)
def test_get_last_entries_in_range(entry_np):
"""Value is in range for the last entries."""
response = ldpy.get_last_entries(entry_np)
@ -121,5 +162,4 @@ def test_main_demo_with_last_const():
options = FakeOptions(["last"])
response = ldpy.main(options)
assert isinstance(response, dict)
assert isinstance(response["hits"]["hits"], list)
assert isinstance(response, str)