diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dfb9ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,168 @@ +.coverage* +pip-wheel-metadata/ +site/ +.venv/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# pycharm project settings +.idea/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..46473cb --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-yaml + - id: check-json + - id: check-toml + - id: check-xml + - id: forbid-new-submodules + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-merge-conflict + - id: no-commit-to-branch # blocks main commits. To bypass do git commit --allow-empty + - id: pretty-format-json + +- repo: https://github.com/google/yapf + rev: v0.40.2 # Use the sha / tag you want to point at + hooks: + - id: yapf + name: yapf + description: "A formatter for Python files." + entry: yapf + language: python + types: [ python ] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b7346b6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2024 Battelle Memorial Institute + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/README.md b/README.md index 970d955..150c974 100644 --- a/README.md +++ b/README.md @@ -1 +1,93 @@ -# volttron-log-statistics \ No newline at end of file +# volttron-log-statistics + +![Passing?](https://github.com/eclipse-volttron/volttron-log-statistics/actions/workflows/run-tests.yml/badge.svg) +[![pypi version](https://img.shields.io/pypi/v/volttron-log-statistics.svg)](https://pypi.org/project/volttron-log-statistics/) + +The Log Statistics agent periodically reads ".log" files based on the configured interval, computes the size delta from the previous interval and publishes the difference in bytes with a timestamp. It also publishes the standard deviation and mean of the size delta every 24 hours. This agent can be useful for detecting unexpected changes to the system which may be an indication of some sort of failure or breach. + +## Requires + +* python >= 3.10 +* volttron >= 10.0 + +## Installation + +Before installing, VOLTTRON should be installed and running. Its virtual environment should be active. +Information on how to install of the VOLTTRON platform can be found +[here](https://github.com/eclipse-volttron/volttron-core). + +Create a directory called `config` and use the change directory command to enter it. + +```shell +mkdir config +cd config +``` + +After entering the config directory, create a file called `log_stat_config.json`. Use the below configuration section to populate your new file. + +### Configuration + +The Log Statistics agent has 4 configuration parameters, all of which are required: + +- `file_path`: The file path to the log file. If no config provided, defaults to `'volttron.log'` located within your VOLTTRON_HOME environment variable. +- `analysis_interval_secs`: The interval in seconds between publishes of the size delta statistic to the message bus. If no config provided, defaults to 60 seconds. +- `publish_topic`: Used to specify a topic to publish log statistics to which does not get captured by the + historian framework (topics not prefixed by any of: "datalogger", "record", "analysis", "devices"). If no config provided, defaults to `"platform/log_statistics"`. +- `historian_topic`: Can be used to specify a topic to publish log statistics to which gets captured by the + historian framework ("datalogger", "record", "analysis", "devices"). If no config provided, defaults to `record/log_statistics`. +- `unit`: Can be used to specify units. Defaults to `bytes`. + - "bytes" + - "kb" + - "mb" + - "gb" + +Here is an example configuration file named `log_stat_config.json`. +```json +{ + "analysis_interval_sec": 60, + "file_path": "path/to/.log", + "historian_topic": "analysis/log_statistics", + "publish_topic": "platform/log_statistics", + "unit": "bytes" +} +``` +Store the configuration in the config store +```bash +vctl config store platform.log_statistics config /path/to/config +``` + +Install and start the log statistics agent + +```bash +vctl install volttron-log-statistics --vip-identity platform.log_statistics --start +``` + +View the status of the installed agent. + +```shell +vctl status +``` + +## Development + +Please see the following for contributing guidelines [contributing](https://github.com/eclipse-volttron/volttron-core/blob/develop/CONTRIBUTING.md). + +Please see the following helpful guide about [developing modular VOLTTRON agents](https://github.com/eclipse-volttron/volttron-core/blob/develop/DEVELOPING_ON_MODULAR.md) + +## Disclaimer Notice + +This material was prepared as an account of work sponsored by an agency of the +United States Government. Neither the United States Government nor the United +States Department of Energy, nor Battelle, nor any of their employees, nor any +jurisdiction or organization that has cooperated in the development of these +materials, makes any warranty, express or implied, or assumes any legal +liability or responsibility for the accuracy, completeness, or usefulness or any +information, apparatus, product, software, or process disclosed, or represents +that its use would not infringe privately owned rights. + +Reference herein to any specific commercial product, process, or service by +trade name, trademark, manufacturer, or otherwise does not necessarily +constitute or imply its endorsement, recommendation, or favoring by the United +States Government or any agency thereof, or Battelle Memorial Institute. The +views and opinions of authors expressed herein do not necessarily state or +reflect those of the United States Government or any agency thereof. diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d0c3cbf --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..bcae01e --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- {{{ +# ===----------------------------------------------------------------------=== +# +# Installable Component of Eclipse VOLTTRON +# +# ===----------------------------------------------------------------------=== +# +# Copyright 2022 Battelle Memorial Institute +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy +# of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# ===----------------------------------------------------------------------=== +# }}} + +# Configuration file for the Sphinx documentation builder. + +# -- Project information + +project = 'VOLTTRON Log Statistics Agent' +copyright = '2024, Pacific Northwest National Lab' +author = 'Pacific Northwest National Lab' + +release = '0.1' +version = '0.1.0' + +# -- General configuration + +extensions = [ + 'sphinx.ext.duration', + 'sphinx.ext.doctest', + 'sphinx.ext.autodoc', + 'sphinx.ext.autosummary', + 'sphinx.ext.intersphinx', +] + +intersphinx_mapping = { + 'python': ('https://docs.python.org/3/', None), + 'sphinx': ('https://www.sphinx-doc.org/en/master/', None), +} +intersphinx_disabled_domains = ['std'] + +templates_path = ['_templates'] + +# -- Options for HTML output + +html_theme = 'sphinx_rtd_theme' + +# -- Options for EPUB output +# epub_show_urls = 'footnote' diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..e3d3dd3 --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,84 @@ +volttron-log-statistics +======================= + +.. image:: https://github.com/eclipse-volttron/volttron-log-statistics/actions/workflows/run-tests.yml/badge.svg + :alt: Passing? +.. image:: https://img.shields.io/pypi/v/volttron-log-statistics.svg + :target: https://pypi.org/project/volttron-log-statistics/ + +The Log Statistics agent periodically reads ".log" files based on the configured interval, computes the size delta from the previous interval and publishes the difference in bytes with a timestamp. It also publishes the standard deviation and mean of the size delta every 24 hours. This agent can be useful for detecting unexpected changes to the system which may be an indication of some sort of failure or breach. + +Requires +-------- + +* python >= 3.10 +* volttron >= 10.0 + +Installation +------------ + +Before installing, VOLTTRON should be installed and running. Its virtual environment should be active. Information on how to install the VOLTTRON platform can be found `here `_. + +Create a directory called ``config`` and use the change directory command to enter it. + +.. code-block:: shell + + mkdir config + cd config + +After entering the config directory, create a file called ``log_stat_config.json``. Use the below configuration section to populate your new file. + +Configuration +~~~~~~~~~~~~~ + +The Log Statistics agent has 4 configuration parameters, all of which are required: + +- ``file_path``: The file path to the log file. If no config provided, defaults to ``'volttron.log'`` located within your VOLTTRON_HOME environment variable. +- ``analysis_interval_secs``: The interval in seconds between publishes of the size delta statistic to the message bus. If no config provided, defaults to 60 seconds. +- ``publish_topic``: Used to specify a topic to publish log statistics to which does not get captured by the historian framework (topics not prefixed by any of: "datalogger", "record", "analysis", "devices"). If no config provided, defaults to ``"platform/log_statistics"``. +- ``historian_topic``: Can be used to specify a topic to publish log statistics to which gets captured by the historian framework ("datalogger", "record", "analysis", "devices"). If no config provided, defaults to ``record/log_statistics``. +- ``unit``: Can be used to specify units. Defaults to ``bytes``. Possible values are "bytes", "kb", "mb", "gb". + +Here is an example configuration file named ``log_stat_config.json``. + +.. code-block:: json + + { + "analysis_interval_sec": 60, + "file_path": "path/to/.log", + "historian_topic": "analysis/log_statistics", + "publish_topic": "platform/log_statistics", + "unit": "bytes" + } + +Store the configuration in the config store: + +.. code-block:: bash + + vctl config store platform.log_statistics config /path/to/config + +Install and start the log statistics agent: + +.. code-block:: bash + + vctl install volttron-log-statistics --vip-identity platform.log_statistics --start + +View the status of the installed agent: + +.. code-block:: shell + + vctl status + +Development +----------- + +Please see the following for contributing guidelines `contributing `_. + +Please see the following helpful guide about `developing modular VOLTTRON agents `_. + +Disclaimer Notice +----------------- + +This material was prepared as an account of work sponsored by an agency of the United States Government. Neither the United States Government nor the United States Department of Energy, nor Battelle, nor any of their employees, nor any jurisdiction or organization that has cooperated in the development of these materials, makes any warranty, express or implied, or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness or any information, apparatus, product, software, or process disclosed, or represents that its use would not infringe privately owned rights. + +Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, or otherwise does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United States Government or any agency thereof, or Battelle Memorial Institute. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or any agency thereof. diff --git a/log_stat_config.json b/log_stat_config.json new file mode 100644 index 0000000..aaefa2a --- /dev/null +++ b/log_stat_config.json @@ -0,0 +1,7 @@ +{ + "analysis_interval_sec": 60, + "file_path": "path/to/.log", + "historian_topic": "analysis/log_statistics", + "publish_topic": "platform/log_statistics", + "unit": "bytes" +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..98aa38a --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,65 @@ +[build-system] +requires = ["poetry-core>=1.2.2"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +volttron-log-statistics = "log_statistics.agent:main" + +[tool.isort] +profile = "black" + +[tool.poetry] +include = ["src/log_statistics", "log_stat_config.json"] + +name = "volttron-log-statistics" +version = "0.1.0" +description = "" +authors = ["VOLTTRON Team "] +license = "Apache License 2.0" +readme = "README.md" +repository = "https://github.com/eclipse-volttron/volttron-log-statistics" +homepage = "https://github.com/eclipse-volttron/volttron-log-statistics" +keywords = [] +packages = [ { include = "log_statistics", from = "src" } ] +classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Intended Audience :: Science/Research", + "Intended Audience :: Information Technology", + "Intended Audience :: Developers", + "Intended Audience :: Other Audience", + "License :: OSI Approved :: Apache Software License" +] + +[tool.poetry.dependencies] +python = ">=3.10,<4.0" +volttron = "^10.0.4rc1" + +[tool.poetry.group.dev.dependencies] +volttron-testing = "^0.4.0rc0" +pytest = "^6.2.5" +pytest-cov = "^3.0.0" +mock = "^4.0.3" +pre-commit = "^2.17.0" +yapf = "^0.32.0" +toml = "^0.10.2" +mypy = "^0.942" +coverage = "^6.3.2" +isort = "^5.10.1" + +[tool.poetry.dev-dependencies] +Sphinx = "^6.0.0" +sphinx-rtd-theme = "^1.2.0" + +[tool.yapf] +based_on_style = "pep8" +spaces_before_comment = 4 +column_limit = 120 +split_before_logical_operator = true + +[tool.yapfignore] +ignore_patterns = [ + ".venv/**", + ".pytest_cache/**", + "dist/**", + "docs/**" +] diff --git a/src/log_statistics/agent.py b/src/log_statistics/agent.py new file mode 100644 index 0000000..4b76cfb --- /dev/null +++ b/src/log_statistics/agent.py @@ -0,0 +1,214 @@ +# -*- coding: utf-8 -*- {{{ +# ===----------------------------------------------------------------------=== +# +# Installable Component of Eclipse VOLTTRON +# +# ===----------------------------------------------------------------------=== +# +# Copyright 2024 Battelle Memorial Institute +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy +# of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# ===----------------------------------------------------------------------=== +# }}} + +import datetime +import logging +import os +import statistics +import sys + +from volttron import utils +from volttron.client.vip.agent import Agent, Core +from volttron.utils import ClientContext as cc, get_aware_utc_now + +utils.setup_logging() +_log = logging.getLogger(__name__) +__version__ = '1.1' + + +class LogStatisticsAgent(Agent): + """ + LogStatisticsAgent reads volttron.log file size every hour, compute the size delta from previous hour and publish + the difference with timestamp. It also publishes standard deviation every 24 hours. + :param config: Configuration dict + :type config: dict + Example configuration: + .. code-block:: python + { + "file_path" : "/home/volttron/volttron.log", + "analysis_interval_sec" : 60, + "publish_topic" : "platform/log_statistics", + "historian_topic" : "analysis/log_statistics" + } + """ + + def __init__(self, config_path=None, **kwargs): + super(LogStatisticsAgent, self).__init__(**kwargs) + + self.last_std_dev_time = get_aware_utc_now() + + volttron_home = cc.get_volttron_home() + + self.default_config = { + "file_path": f"{volttron_home}/volttron.log", + "analysis_interval_sec": 60, + "publish_topic": "platform/log_statistics", + "historian_topic": "analysis/log_statistics", + "unit": "bytes" + } + + if config_path: + self.default_config.update(utils.load_config(config_path)) + self.vip.config.set_default("config", self.default_config) + self.vip.config.subscribe(self.configure_main, actions=["NEW", "UPDATE"], pattern="config") + + def configure_main(self, config_name, action, contents): + config = self.default_config.copy() + config.update(contents) + if action == "NEW" or "UPDATE": + self.reset_parameters(config) + _log.info("Starting " + self.__class__.__name__ + " agent") + + def reset_parameters(self, config=None): + self.analysis_interval_sec = config["analysis_interval_sec"] + self.file_path = config["file_path"] + self.publish_topic = config["publish_topic"] + self.historian_topic = config["historian_topic"] + self.unit = config["unit"] + self.size_delta_list = [] + self.file_start_size = None + self.prev_file_size = None + self._scheduled_event = None + + self.publish_analysis() + + def publish_analysis(self): + """ + Publishes file's size increment in previous time interval (60 minutes) with timestamp. + Also publishes standard deviation of file's hourly size differences every 24 hour. + """ + if not hasattr(self, '_scheduled_event'): + # The settings haven't been initialized, so skip the rest of the method + return + + if self._scheduled_event is not None: + self._scheduled_event.cancel() + + if self.prev_file_size is None: + self.prev_file_size = self.get_file_size() + _log.debug(f"init_file_size = {self.convert_bytes(self.prev_file_size, self.unit)} {self.unit}") + else: + # read file size + curr_file_size = self.get_file_size() + + # calculate size delta + size_delta = curr_file_size - self.prev_file_size + size_delta = self.convert_bytes(size_delta, self.unit) + + self.prev_file_size = curr_file_size + + self.size_delta_list.append(size_delta) + + headers = {'Date': datetime.datetime.utcnow().isoformat() + 'Z'} + + publish_message = {'timestamp': datetime.datetime.utcnow().isoformat() + 'Z', 'log_size_delta': size_delta} + historian_message = [{ + "log_size_delta ": size_delta + }, { + "log_size_delta ": { + 'units': f'{self.unit}', + 'tz': 'UTC', + 'type': 'float' + } + }] + + now = get_aware_utc_now() + hours_since_last_std_dev = (now - self.last_std_dev_time).total_seconds() / 3600 + + if hours_since_last_std_dev >= 24: + if self.size_delta_list: # make sure it has something in it + if len(self.size_delta_list) >= 2: # make sure it has more than two items + mean = statistics.mean(self.size_delta_list) + standard_deviation = statistics.stdev(self.size_delta_list) + + publish_message['log_mean'] = mean + print(f"Calculated mean: {mean}") + publish_message['log_std_dev'] = standard_deviation + + historian_message[0]['log_mean'] = mean + historian_message[0]['log_std_dev'] = standard_deviation + + historian_message[1]['log_mean'] = {'units': f'{self.unit}', 'tz': 'UTC', 'type': 'float'} + historian_message[1]['log_std_dev'] = {'units': f'{self.unit}', 'tz': 'UTC', 'type': 'float'} + + else: + _log.info("Not enough data points to calculate standard deviation") + + else: + _log.info("Not enough data points to calculate mean and standard deviation") + + # Reset time + self.last_std_dev_time = now + + self.size_delta_list = [] + + _log.debug(f'publishing message {historian_message}' + f' with header {headers}' + f' on historian topic {self.historian_topic}') + self.vip.pubsub.publish(peer="pubsub", + topic=self.historian_topic, + headers=headers, + message=historian_message) + + _log.debug(f'publishing message {publish_message} {self.unit} on topic {self.publish_topic}') + self.vip.pubsub.publish(peer="pubsub", topic=self.publish_topic, message=publish_message) + + _log.debug('Scheduling next periodic call') + now = get_aware_utc_now() + next_update_time = now + datetime.timedelta(seconds=self.analysis_interval_sec) + + self._scheduled_event = self.core.schedule(next_update_time, self.publish_analysis) + + def get_file_size(self): + try: + return os.path.getsize(self.file_path) + except OSError as e: + _log.error(e) + def convert_bytes(self, size, unit): + """ + Converts size from bytes to the specified unit. + """ + unit = unit.lower() + if unit == 'kb': + return size / 1024 + elif unit == 'mb': + return size / 1024**2 + elif unit == 'gb': + return size / 1024**3 + return size + + +def main(argv=sys.argv): + """ + Main method called by the platform. + """ + utils.vip_main(LogStatisticsAgent, identity='platform.log_statistics') + + +if __name__ == '__main__': + # Entry point for script + try: + sys.exit(main()) + except KeyboardInterrupt: + pass diff --git a/tests/test_log_statistics.py b/tests/test_log_statistics.py new file mode 100644 index 0000000..1df551e --- /dev/null +++ b/tests/test_log_statistics.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- {{{ +# ===----------------------------------------------------------------------=== +# +# Installable Component of Eclipse VOLTTRON +# +# ===----------------------------------------------------------------------=== +# +# Copyright 2024 Battelle Memorial Institute +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy +# of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# ===----------------------------------------------------------------------=== +# }}} + +import json +import os +from pathlib import Path + +import pytest +from mock import MagicMock +from volttron.client.messaging.health import STATUS_GOOD +from volttron.client.vip.agent import Agent +from volttrontesting.fixtures.volttron_platform_fixtures import volttron_instance + +test_config = { + "analysis_interval_sec": 2, + "publish_topic": "platform/log_statistics", + "historian_topic": "analysis/log_statistics" +} + +@pytest.fixture(scope="module") +def publish_agent(request, volttron_instance): + test_config = { + "file_path": os.path.join(volttron_instance.volttron_home, "volttron.log"), + "analysis_interval_sec": 2, + "publish_topic": "platform/log_statistics", + "historian_topic": "analysis/log_statistics" + } + # 1: Start a fake agent to publish to message bus + agent = volttron_instance.build_agent() + + agent.callback = MagicMock(name="callback") + agent.callback.reset_mock() + + agent.vip.pubsub.subscribe(peer='pubsub', + prefix=test_config.get("publish_topic"), + callback=agent.callback).get() + + def stop_agent(): + print("In teardown method of publish_agent") + if isinstance(agent, Agent): + agent.core.stop() + + request.addfinalizer(stop_agent) + return agent + +def test_log_stats(volttron_instance, publish_agent): + test_config["file_path"] = volttron_instance.log_path + print(f"File path: {test_config['file_path']}") + + agent_path = Path(__file__).parents[1] + stats_uuid = volttron_instance.install_agent( + agent_dir=agent_path, + config_file=test_config, + start=True, + vip_identity="health_test") + + import gevent + gevent.sleep(1) + + # building another agent should populate the logs + volttron_instance.build_agent(identity="log_populate") + + gevent.sleep(1) + + assert publish_agent.callback.call_count >= 1 + + volttron_instance.remove_agent(stats_uuid)