{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# TimestampDiffer\n", "\n", "This presentations goal it to introduce the features of the `timestamp_differ` and how to configure it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The challenge\n", "\n", "I want calculate the time delta between two timestamps with different timestamp formats." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "from this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "document = {\n", " \"times\": {\n", " \"ingest\": \"06-12-2022T10:00:00\",\n", " \"processed\": \"2022-12-06 10:00:05\", \n", " },\n", " \"more\": \"event data\"\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expected = {\n", " \"times\": {\n", " \"ingest\": \"06-12-2022T10:00:00\",\n", " \"processed\": \"2022-12-06 10:00:05\", \n", " \"processing_time\": \"5000.0\",\n", " },\n", " \"more\": \"event data\"\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create rule and processor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the rule:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append(\"../../../../../\")\n", "import tempfile\n", "from pathlib import Path\n", "\n", "rule_yaml = \"\"\"---\n", "filter: 'times.ingest AND times.processed'\n", "timestamp_differ:\n", " diff: ${times.processed:%Y-%m-%d %H:%M:%S} - ${times.ingest:%d-%m-%YT%H:%M:%S}\n", " target_field: times.processing_time\n", " output_format: milliseconds\n", "description: '...'\n", "\"\"\"\n", "\n", "rule_path = Path(tempfile.gettempdir()) / \"timestamp_differ\"\n", "rule_path.mkdir(exist_ok=True)\n", "rule_file = rule_path / \"timestamp_differ.yml\"\n", "rule_file.write_text(rule_yaml)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the processor config:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "processor_config = {\n", " \"my_timestampdiffer\":{ \n", " \"type\": \"timestamp_differ\",\n", " \"rules\": [str(rule_path), \"/dev\"],\n", " }\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the processor with the factory:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from unittest import mock\n", "from logprep.factory import Factory\n", "\n", "mock_logger = mock.MagicMock()\n", "processor = Factory.create(processor_config)\n", "processor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Process event" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from copy import deepcopy\n", "mydocument = deepcopy(document)\n", "\n", "\n", "print(f\"before: {mydocument}\")\n", "processor.process(mydocument)\n", "print(f\"after: {mydocument}\")\n", "print(mydocument == expected)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.11.2" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }