diff --git a/.gitignore b/.gitignore index 2dd157b..8992282 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Alerts and logs +SNEWS_ALERTS/ +logs/ + # Visual Studio Code .vscode/ diff --git a/examples.ipynb b/examples.ipynb index a4d5d83..3c3f918 100644 --- a/examples.ipynb +++ b/examples.ipynb @@ -16,9 +16,15 @@ "## Publication" ] }, + { + "cell_type": "markdown", + "id": "3bacfedc", + "metadata": {}, + "source": [] + }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "34bebc1c-8272-4a87-b877-31b77700907d", "metadata": { "tags": [] @@ -28,28 +34,45 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'detector_name': 'LZ', 'machine_time': '22/03/07 16:18:09:185459', 'neutrino_time': '22/03/07 16:18:09:185459', 'p_val': 0.0007, 'p_values': [0.0007, 0.0008, 0.0009], 't_bin_width': 0.07, 'timing_series': None, 'which_tier': None, 'n_retract_latest': None, 'retraction_reason': None, 'detector_status': None, 'is_pre_sn': False, 'meta': {}}\n", + "{'detector_name': 'LZ', 'machine_time': '2022-08-05T16:01:00.786929', 'neutrino_time': '2022-08-05T16:01:00.786929', 'p_val': 0.0007, 'p_values': [0.0007, 0.0008, 0.0009], 't_bin_width': 0.07, 'timing_series': None, 'which_tier': None, 'n_retract_latest': None, 'retraction_reason': None, 'detector_status': None, 'is_pre_sn': False}\n", "['CoincidenceTier', 'SigTier']\n", "Sending message\n", - "\u001B[94m----------------------------------------------------------------\u001B[0m\n", - "\u001B[91mSending message to CoincidenceTier\u001B[0m\n", - "_id :14_CoincidenceTier_22/03/07 16:18:09:185459\n", + "\u001b[94m----------------------------------------------------------------\u001b[0m\n", + "\u001b[91mSending message to CoincidenceTier on kafka://kafka.scimma.org/snews.experiments-firedrill\u001b[0m\n", + "_id :14_CoincidenceTier_2022-08-05T16:01:00.786929\n", "detector_name :LZ\n", - "machine_time :22/03/07 16:18:09:185459\n", - "neutrino_time :22/03/07 16:18:09:185459\n", + "machine_time :2022-08-05T16:01:00.786929\n", + "neutrino_time :2022-08-05T16:01:00.786929\n", "p_val :0.0007\n", - "meta :{'meta': {}}\n", - "schema_version :1.1.0\n", - "\u001B[94m----------------------------------------------------------------\u001B[0m\n", - "\u001B[91mSending message to SigTier\u001B[0m\n", - "_id :14_SigTier_22/03/07 16:18:09:185459\n", + "meta :{}\n", + "schema_version :1.1.1\n", + "sent_time :2022-08-05T16:01:00.786982\n", + "\u001b[94m----------------------------------------------------------------\u001b[0m\n", + "\u001b[91mSending message to SigTier on kafka://kafka.scimma.org/snews.experiments-firedrill\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/joe/src/gitjoe/SNEWS_Publishing_Tools/snews_pt/__init__.py:19: UserWarning: \u001b[31mYou are using default detector name \"TEST\"\n", + "Please change this by snews_pt.snews_pt_utils.set_name()\u001b[0m\n", + " warnings.warn(warning_text, UserWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "_id :14_SigTier_2022-08-05T16:01:00.786929\n", "detector_name :LZ\n", - "machine_time :22/03/07 16:18:09:185459\n", - "neutrino_time :22/03/07 16:18:09:185459\n", + "machine_time :2022-08-05T16:01:00.786929\n", + "neutrino_time :2022-08-05T16:01:00.786929\n", "p_values :[0.0007, 0.0008, 0.0009]\n", "t_bin_width :0.07\n", - "meta :{'meta': {}}\n", - "schema_version :1.1.0\n" + "meta :{}\n", + "schema_version :1.1.1\n", + "sent_time :2022-08-05T16:01:00.786982\n" ] } ], @@ -57,7 +80,7 @@ "from snews_pt.snews_pub import SNEWSTiersPublisher\n", "from datetime import datetime\n", "\n", - "test_time = datetime.utcnow().strftime(\"%y/%m/%d %H:%M:%S:%f\")\n", + "test_time = datetime.utcnow().isoformat()\n", "message = SNEWSTiersPublisher(detector_name='LZ',machine_time=test_time ,neutrino_time=test_time, p_val=0.0007, p_values=[0.0007,0.0008,0.0009],t_bin_width=0.07)\n", "print(message.message_data)\n", "print(message.tiernames)\n", @@ -75,67 +98,47 @@ }, { "cell_type": "code", + "execution_count": 4, "id": "0ea6d89a", "metadata": { "pycharm": { "name": "#%%\n" } }, - "source": [ - "from snews_pt.snews_sub import Subscriber\n", - "\n", - "Subscriber().subscribe()" - ], - "execution_count": 2, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "You are subscribing to \u001B[41m\u001B[1mALERT\u001B[0m\n", - "Broker:\u001B[42mkafka://kafka.scimma.org/snews.alert-test\u001B[0m\n" - ] - }, - { - "ename": "ValueError", - "evalue": "topic snews.alert-test does not exist on the broker, so can't subscribe", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mKeyError\u001B[0m Traceback (most recent call last)", - "\u001B[0;32m~/opt/anaconda3/lib/python3.9/site-packages/adc/consumer.py\u001B[0m in \u001B[0;36msubscribe\u001B[0;34m(self, topics, timeout)\u001B[0m\n\u001B[1;32m 40\u001B[0m \u001B[0;32mtry\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 41\u001B[0;31m \u001B[0mtopic_meta\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mdescribe_topic\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mtopic\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mtimeout\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 42\u001B[0m \u001B[0;32mexcept\u001B[0m \u001B[0mKeyError\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;32m~/opt/anaconda3/lib/python3.9/site-packages/adc/consumer.py\u001B[0m in \u001B[0;36mdescribe_topic\u001B[0;34m(self, topic, timeout)\u001B[0m\n\u001B[1;32m 64\u001B[0m \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mlogger\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mdebug\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"cluster metadata: {cluster_meta.topics}\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 65\u001B[0;31m \u001B[0;32mreturn\u001B[0m \u001B[0mcluster_meta\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mtopics\u001B[0m\u001B[0;34m[\u001B[0m\u001B[0mtopic\u001B[0m\u001B[0;34m]\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 66\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;31mKeyError\u001B[0m: 'snews.alert-test'", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", - "\u001B[0;32m/var/folders/mr/0p5djr157hxdh7bb2gmzywyr0000gn/T/ipykernel_4462/1770358198.py\u001B[0m in \u001B[0;36m\u001B[0;34m\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[0;32mfrom\u001B[0m \u001B[0mSNEWS_PT\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0msnews_sub\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0mSubscriber\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 3\u001B[0;31m \u001B[0mSubscriber\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0msubscribe\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", - "\u001B[0;32m~/work/SNEWS_Publishing_Tools/SNEWS_PT/snews_sub.py\u001B[0m in \u001B[0;36msubscribe\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 91\u001B[0m \u001B[0mstream\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mStream\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0muntil_eos\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0;32mFalse\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 92\u001B[0m \u001B[0;32mtry\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 93\u001B[0;31m \u001B[0;32mwith\u001B[0m \u001B[0mstream\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mopen\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0malert_topic\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0;34m\"r\"\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0ms\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 94\u001B[0m \u001B[0;32mfor\u001B[0m \u001B[0mmessage\u001B[0m \u001B[0;32min\u001B[0m \u001B[0ms\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 95\u001B[0m \u001B[0msave_message\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mmessage\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;32m~/opt/anaconda3/lib/python3.9/site-packages/hop/io.py\u001B[0m in \u001B[0;36mopen\u001B[0;34m(self, url, mode, group_id, **kwargs)\u001B[0m\n\u001B[1;32m 114\u001B[0m \u001B[0mgroup_id\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0m_generate_group_id\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0musername\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0;36m10\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 115\u001B[0m \u001B[0mlogger\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0minfo\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"group ID not specified, generating a random group ID: {group_id}\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 116\u001B[0;31m return Consumer(\n\u001B[0m\u001B[1;32m 117\u001B[0m \u001B[0mgroup_id\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 118\u001B[0m \u001B[0mbroker_addresses\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;32m~/opt/anaconda3/lib/python3.9/site-packages/hop/io.py\u001B[0m in \u001B[0;36m__init__\u001B[0;34m(self, group_id, broker_addresses, topics, **kwargs)\u001B[0m\n\u001B[1;32m 291\u001B[0m ))\n\u001B[1;32m 292\u001B[0m \u001B[0mlogger\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0minfo\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"subscribing to topics: {topics}\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 293\u001B[0;31m \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_consumer\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0msubscribe\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mtopics\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 294\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 295\u001B[0m \u001B[0;32mdef\u001B[0m \u001B[0mread\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mself\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mmetadata\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0;32mFalse\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mautocommit\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0;32mTrue\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0;34m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;32m~/opt/anaconda3/lib/python3.9/site-packages/adc/consumer.py\u001B[0m in \u001B[0;36msubscribe\u001B[0;34m(self, topics, timeout)\u001B[0m\n\u001B[1;32m 41\u001B[0m \u001B[0mtopic_meta\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mdescribe_topic\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mtopic\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mtimeout\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 42\u001B[0m \u001B[0;32mexcept\u001B[0m \u001B[0mKeyError\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 43\u001B[0;31m \u001B[0;32mraise\u001B[0m \u001B[0mValueError\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"topic {topic} does not exist on the broker, so can't subscribe\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 44\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 45\u001B[0m \u001B[0;32mfor\u001B[0m \u001B[0mpartition_id\u001B[0m \u001B[0;32min\u001B[0m \u001B[0mtopic_meta\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mpartitions\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mkeys\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", - "\u001B[0;31mValueError\u001B[0m: topic snews.alert-test does not exist on the broker, so can't subscribe" + "You are subscribing to \u001b[41m\u001b[1mALERT\u001b[0m\n", + "Broker:\u001b[42mkafka://kafka.scimma.org/snews.alert-firedrill\u001b[0m\n" ] } + ], + "source": [ + "from snews_pt.snews_sub import Subscriber\n", + "\n", + "Subscriber().subscribe()" ] }, { "cell_type": "code", "execution_count": null, - "outputs": [], - "source": [], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "name": "python3", + "display_name": "Python 3.9.9 ('snews-pt')", "language": "python", - "display_name": "Python 3 (ipykernel)" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -147,9 +150,14 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.11" + "version": "3.9.9" + }, + "vscode": { + "interpreter": { + "hash": "f581a8651b7bc0666f0167af942e3b60152ddbb51a5d7dbac8fd8281a3c56e84" + } } }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} diff --git a/snews_pt/__init__.py b/snews_pt/__init__.py index e5d417b..109d079 100644 --- a/snews_pt/__init__.py +++ b/snews_pt/__init__.py @@ -1,3 +1,7 @@ +from .core.logging import initialize_logging + +initialize_logging("debug") + try: from ._version import version as __version__ except ImportError: diff --git a/snews_pt/__main__.py b/snews_pt/__main__.py index 916cada..65bacb4 100644 --- a/snews_pt/__main__.py +++ b/snews_pt/__main__.py @@ -3,7 +3,7 @@ Notes to dev team https://stackoverflow.com/questions/55099243/python3-dataclass-with-kwargsasterisk """ -import time +from datetime import datetime from . import __version__ from . import snews_pt_utils @@ -147,8 +147,7 @@ def test_connection(ctx, firedrill, start_at): """ from hop import Stream name = ctx.obj['DETECTOR_NAME'] - snewstimes = snews_pt_utils.TimeStuff() - stamp_time = snewstimes.get_utcnow() + stamp_time = datetime.utcnow().isoformat() message = {'_id': '0_test-connection', 'detector_name': name, 'time': stamp_time, diff --git a/snews_pt/_version.py b/snews_pt/_version.py index d66da44..813ff3b 100644 --- a/snews_pt/_version.py +++ b/snews_pt/_version.py @@ -1 +1 @@ -version = '1.1.0' +version = '1.1.1' diff --git a/snews_pt/auxiliary/make_scenarios.py b/snews_pt/auxiliary/make_scenarios.py index d8fa657..8eb4a5d 100644 --- a/snews_pt/auxiliary/make_scenarios.py +++ b/snews_pt/auxiliary/make_scenarios.py @@ -1,56 +1,56 @@ import json msg1 = {"detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98} msg2 = {"detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:34:47:678999", + "neutrino_time": "2030-01-01T12:34:47.678999", "p_val": 0.98} msg3 = {"detector_name": "DUNE", - "neutrino_time": "30/01/01 12:34:55:678999", + "neutrino_time": "2030-01-01T12:34:55.678999", "p_val": 0.98} msg4 = {"detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:55:678999", + "neutrino_time": "2030-01-01T12:30:55.678999", "p_val": 0.98} msg5 = {"detector_name": "Baksan", - "neutrino_time": "30/01/01 12:30:52:678999", + "neutrino_time": "2030-01-01T12:30:52.678999", "p_val": 0.98} msg6 = {"detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:56:678999", + "neutrino_time": "2030-01-01T12:30:56.678999", "p_val": 0.98} #### msg7 = {"detector_name": "Borexino", - "neutrino_time": "30/01/01 12:30:10:678999", + "neutrino_time": "2030-01-01T12:30:10.678999", "p_val": 0.98} msg8 = {"detector_name": "ICE", - "neutrino_time": "30/01/01 12:30:19:678999", + "neutrino_time": "2030-01-01T12:30:19.678999", "p_val": 0.98} msg9 = {"detector_name": "NOvA", - "neutrino_time": "30/01/01 12:30:21:678999", + "neutrino_time": "2030-01-01T12:30:21.678999", "p_val": 0.98} msg10 = {"detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:30:22:678999", + "neutrino_time": "2030-01-01T12:30:22.678999", "p_val": 0.98} msg11 = {"detector_name": "HALO", - "neutrino_time": "30/01/01 12:30:20:678999", + "neutrino_time": "2030-01-01T12:30:20.678999", "p_val": 0.98} msg12 = {"detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:30:29:678999", + "neutrino_time": "2030-01-01T12:30:29.678999", "p_val": 0.98} msg13 = {"detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:18:678999", + "neutrino_time": "2030-01-01T12:30:18.678999", "p_val": 0.98} diff --git a/snews_pt/auxiliary/scenarios.json b/snews_pt/auxiliary/scenarios.json index 7198b83..a9f4108 100644 --- a/snews_pt/auxiliary/scenarios.json +++ b/snews_pt/auxiliary/scenarios.json @@ -2,136 +2,136 @@ "simple coincidence": [ { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 }, { "detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:34:47:678999", + "neutrino_time": "2030-01-01T12:34:47.678999", "p_val": 0.98 } ], "exact 10s coincidence": [ { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 }, { "detector_name": "DUNE", - "neutrino_time": "30/01/01 12:34:55:678999", + "neutrino_time": "2030-01-01T12:34:55.678999", "p_val": 0.98 } ], "two coincidences": [ { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 }, { "detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:34:47:678999", + "neutrino_time": "2030-01-01T12:34:47.678999", "p_val": 0.98 }, { "detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:55:678999", + "neutrino_time": "2030-01-01T12:30:55.678999", "p_val": 0.98 }, { "detector_name": "Baksan", - "neutrino_time": "30/01/01 12:30:52:678999", + "neutrino_time": "2030-01-01T12:30:52.678999", "p_val": 0.98 } ], "same message submitted twice": [ { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 }, { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 } ], "same detector submits within 1sec": [ { "detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:55:678999", + "neutrino_time": "2030-01-01T12:30:55.678999", "p_val": 0.98 }, { "detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:56:678999", + "neutrino_time": "2030-01-01T12:30:56.678999", "p_val": 0.98 } ], "3 coincident message, out of order": [ { "detector_name": "DUNE", - "neutrino_time": "30/01/01 12:34:55:678999", + "neutrino_time": "2030-01-01T12:34:55.678999", "p_val": 0.98 }, { "detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:34:47:678999", + "neutrino_time": "2030-01-01T12:34:47.678999", "p_val": 0.98 }, { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 } ], "msgs at 10, 19, 21, 22, 20, 29, 18 seconds": [ { "detector_name": "Borexino", - "neutrino_time": "30/01/01 12:30:10:678999", + "neutrino_time": "2030-01-01T12:30:10.678999", "p_val": 0.98 }, { "detector_name": "ICE", - "neutrino_time": "30/01/01 12:30:19:678999", + "neutrino_time": "2030-01-01T12:30:19.678999", "p_val": 0.98 }, { "detector_name": "NOvA", - "neutrino_time": "30/01/01 12:30:21:678999", + "neutrino_time": "2030-01-01T12:30:21.678999", "p_val": 0.98 }, { "detector_name": "DS-20K", - "neutrino_time": "30/01/01 12:30:22:678999", + "neutrino_time": "2030-01-01T12:30:22.678999", "p_val": 0.98 }, { "detector_name": "HALO", - "neutrino_time": "30/01/01 12:30:20:678999", + "neutrino_time": "2030-01-01T12:30:20.678999", "p_val": 0.98 }, { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:30:29:678999", + "neutrino_time": "2030-01-01T12:30:29.678999", "p_val": 0.98 }, { "detector_name": "JUNO", - "neutrino_time": "30/01/01 12:30:18:678999", + "neutrino_time": "2030-01-01T12:30:18.678999", "p_val": 0.98 } ], "20ms apart": [ { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:678999", + "neutrino_time": "2030-01-01T12:34:45.678999", "p_val": 0.98 }, { "detector_name": "XENONnT", - "neutrino_time": "30/01/01 12:34:45:878999", + "neutrino_time": "2030-01-01T12:34:45.878999", "p_val": 0.98 }] } \ No newline at end of file diff --git a/snews_pt/auxiliary/test-config.env b/snews_pt/auxiliary/test-config.env index a9e750b..2103f21 100644 --- a/snews_pt/auxiliary/test-config.env +++ b/snews_pt/auxiliary/test-config.env @@ -1,6 +1,6 @@ DETECTOR_NAME='TEST' HAS_NAME_CHANGED='0' -TIME_STRING_FORMAT="%y/%m/%d %H:%M:%S:%f" + DATABASE_SERVER="" LOCAL_SERVER="mongodb://localhost:27017" ALERT_OUTPUT="SNEWS_ALERTS/" diff --git a/snews_pt/auxiliary/try_scenarios.py b/snews_pt/auxiliary/try_scenarios.py index 1c689e9..c8931f6 100644 --- a/snews_pt/auxiliary/try_scenarios.py +++ b/snews_pt/auxiliary/try_scenarios.py @@ -30,7 +30,7 @@ SNEWSTiersPublisher(**msg, firedrill_mode=fd_mode).send_to_snews() time.sleep(1) # clear cache after each scenario - with Publisher() as pub: + with Publisher(firedrill_mode=fd_mode, verbose=False) as pub: pub.send([{'_id': '0_hard-reset_', 'pass':'very1secret2password', 'detector_name':'TEST'}]) print('> Cache cleaned\n') diff --git a/snews_pt/core/__init__.py b/snews_pt/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/snews_pt/core/logging.py b/snews_pt/core/logging.py new file mode 100644 index 0000000..4cdae20 --- /dev/null +++ b/snews_pt/core/logging.py @@ -0,0 +1,59 @@ +""" +Modified version of Geoffrey Letner's python 201 logger. + +Ref: https://python-tutorial.dev/201/tutorial/logging.html +""" +import os +import time +from datetime import date +from socket import gethostname +from logging import ( + getLogger, + NullHandler, + Formatter, + FileHandler, + DEBUG, + INFO, + WARNING, + ERROR, + CRITICAL, +) + +HOST = gethostname() + +log_date = date.today().strftime("%Y-%m-%d") +log_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../logs") +log_file = f"{log_dir}/{log_date}.log" + +if not os.path.isdir(log_dir): + os.mkdir(log_dir) + +fh = FileHandler(log_file) + +formatter = Formatter( + f"%(asctime)s on {HOST}\n" f" %(levelname)s [%(name)s] %(message)s", + datefmt="%Y/%m/%dT%H:%M:%S.f", +) + +formatter.converter = time.gmtime + +fh.setFormatter(formatter) + +logger = getLogger("snews_pt") +logger.addHandler(NullHandler()) + +levels = { + "debug": DEBUG, + "info": INFO, + "warning": WARNING, + "error": ERROR, + "critical": CRITICAL, +} + + +def initialize_logging(level): + """Initialize top-level logger with the file handler and a `level`.""" + if fh not in logger.handlers: + logger.addHandler(fh) + logger.setLevel(levels.get(level)) + logger.propagate = False diff --git a/snews_pt/snews_pt_utils.py b/snews_pt/snews_pt_utils.py index 5ce1217..32310a3 100644 --- a/snews_pt/snews_pt_utils.py +++ b/snews_pt/snews_pt_utils.py @@ -8,7 +8,11 @@ import os, json, click import sys from inspect import signature +import warnings +from .core.logging import getLogger + +log = getLogger(__name__) def set_env(env_path=None): """ Set environment parameters @@ -26,26 +30,26 @@ def set_env(env_path=None): load_dotenv(env) -class TimeStuff: - """ SNEWS format datetime objects +# class TimeStuff: +# """ SNEWS format datetime objects - """ - def __init__(self, env_path=None): - set_env(env_path) - self.snews_t_format = os.getenv("TIME_STRING_FORMAT") - self.hour_fmt = "%H:%M:%S" - self.date_fmt = "%y_%m_%d" - self.get_utcnow = lambda fmt=self.snews_t_format: datetime.utcnow().strftime(fmt) - self.get_hour = lambda fmt=self.hour_fmt: datetime.utcnow().strftime(fmt) - self.get_date = lambda fmt=self.date_fmt: datetime.utcnow().strftime(fmt) +# """ +# def __init__(self, env_path=None): +# set_env(env_path) +# self.snews_t_format = os.getenv("TIME_STRING_FORMAT") +# self.hour_fmt = "%H:%M:%S" +# self.date_fmt = "%y_%m_%d" +# self.get_utcnow = lambda fmt=self.snews_t_format: datetime.utcnow().strftime(fmt) +# self.get_hour = lambda fmt=self.hour_fmt: datetime.utcnow().strftime(fmt) +# self.get_date = lambda fmt=self.date_fmt: datetime.utcnow().strftime(fmt) - def str_to_datetime(self, nu_time, fmt='%y/%m/%d %H:%M:%S:%f'): - """ string to datetime object """ - return datetime.strptime(nu_time, fmt) +# def str_to_datetime(self, nu_time, fmt='%y/%m/%d %H:%M:%S:%f'): +# """ string to datetime object """ +# return datetime.strptime(nu_time, fmt) - def str_to_hr(self, nu_time, fmt='%H:%M:%S:%f'): - """ string to datetime hour object """ - return datetime.strptime(nu_time, fmt) +# def str_to_hr(self, nu_time, fmt='%H:%M:%S:%f'): +# """ string to datetime hour object """ +# return datetime.strptime(nu_time, fmt) def set_topic_state(which_topic, env_path=None): @@ -423,3 +427,121 @@ def get_name(): """ return os.getenv("DETECTOR_NAME") + + +def is_snews_format(snews_message): + """ This method checks to see if message meets SNEWS standards. + + Parameters + ---------- + snews_message : dict + incoming SNEWS message + + Returns + ------- + bool + True if message meets SNEWS standards, else False + + """ + message_keys = snews_message.keys() + missing_key = False + contents_bad = False + time_bad = False + snews_format = True + + # Don't check reset messages for format + if snews_message['_id'] == '0_hard-reset_': + return True + + log.debug(f"\nChecking message: {snews_message}\n") + + warning = f'The following Message does not meet SNEWS 2.0 standards!\n{snews_message}\n' + + # Check if detector name is in registered list. + detector_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'auxiliary/detector_properties.json')) + with open(detector_file) as file: + snews_detectors = json.load(file) + snews_detectors = list(snews_detectors.keys()) + + if 'detector_name' not in message_keys: + warning += f'* Does not have required key: "detector_name"\n' + snews_format = False + missing_key = True + + elif snews_message['detector_name'] not in snews_detectors: + warning += f'* Detector not found: {snews_message["detector_name"]}\n' + warning += f"Detector options: {snews_detectors}\n" + snews_format = False + + # Check for missing keys + if 'neutrino_time' not in message_keys: + warning += f'* Does not have required key: "neutrino_time"\n' + missing_key = True + snews_format = False + + if missing_key: + warnings.warn(warning, UserWarning) + log.warning(warning) + + return snews_format + + # Check contents + if 'p_val' in message_keys and snews_message['p_val'] is not None: + log.debug(f"\nChecking p_val: {snews_message['p_val']}\n") + key_type = type(snews_message['p_val']) + key_val = snews_message['p_val'] + + if key_type is not float: + contents_bad = True + warning += f'* p value needs to be a float type, type given: {key_type}\n' + + if key_type is float and (key_val >= 1.0 or key_val <= 0): + warning += f'* {key_val} is not a valid p value !\n' + contents_bad = True + + if type(snews_message['neutrino_time']) is not str: + if snews_message['neutrino_time'] is not None: + contents_bad = True + warning += f'* neutrino time must be a str, type given: {type(snews_message["p_val"])}\n' + + if contents_bad: + warnings.warn(warning, UserWarning) + log.warning(warning) + + return False + + # Time format check + try: + datetime.fromisoformat(snews_message['neutrino_time']) + except: + if snews_message['neutrino_time'] is not None: + warning += f'* neutrino time: {snews_message["neutrino_time"]} does not match SNEWS 2.0 (ISO) format: "%Y-%m-%dT%H:%M:%S.%f"\n' + warnings.warn(warning, UserWarning) + log.warning(warning) + + return False + + if snews_message['neutrino_time'] is not None: + log.debug(f"\nChecking neutrino_time: {snews_message['neutrino_time']}\n") + + if (datetime.fromisoformat(snews_message['neutrino_time']) - datetime.utcnow()).total_seconds() <= -172800.0: + if not "this is a test" in snews_message['meta'].values(): + warning += f'* neutrino time is more than 48 hrs olds !\n' + time_bad = True + warnings.warn(warning, UserWarning) + log.warning(warning) + + if (datetime.fromisoformat(snews_message['neutrino_time']) - datetime.utcnow()).total_seconds() > 0: + if not "this is a test" in snews_message['meta'].values(): + warning += f'* neutrino time comes from the future, please stop breaking causality\n' + time_bad = True + warnings.warn(warning, UserWarning) + log.warning(warning) + + if time_bad: + warnings.warn(warning, UserWarning) + log.warning(warning) + + return False + + return True diff --git a/snews_pt/snews_pub.py b/snews_pt/snews_pub.py index 5b3d398..4318d8a 100644 --- a/snews_pt/snews_pub.py +++ b/snews_pt/snews_pub.py @@ -10,6 +10,7 @@ Joe Smolsky """ +from datetime import datetime import os, click from hop import Stream from . import snews_pt_utils @@ -37,7 +38,7 @@ def __init__(self, env_path=None, verbose=True, auth=True, firedrill_mode=True): self.obs_broker = os.getenv("OBSERVATION_TOPIC") if firedrill_mode: self.obs_broker = os.getenv("FIREDRILL_OBSERVATION_TOPIC") - self.times = snews_pt_utils.TimeStuff() + # self.times = snews_pt_utils.TimeStuff() self.verbose = verbose def __enter__(self): @@ -59,9 +60,15 @@ def send(self, messages): if type(messages) == dict: messages = list(messages) for message in messages: - self.stream.write(message) - self.display_message(message) - + if snews_pt_utils.is_snews_format(message): + self.stream.write(message) + self.display_message(message) + else: + click.secho(f'{"-" * 64}', fg='bright_red') + click.secho(f'Skipping message! Improper format!') + for k, v in message.items(): + print(f'{k:<20s}:{v}') + def display_message(self, message): if self.verbose: tier = message['_id'].split('_')[1] @@ -156,7 +163,7 @@ def __init__(self, env_file=None, # self.message_data['meta'] = self.meta # this is already done in tier decider self.message_data = {**self.message_data, ** self.meta} self.env_file = env_file - stamp_time = snews_pt_utils.TimeStuff().get_utcnow() + stamp_time = datetime.utcnow().isoformat() self.messages, self.tiernames = snews_pt_utils._tier_decider(self.message_data, sent_time=stamp_time, env_file=env_file) self.firedrill_mode = firedrill_mode @classmethod diff --git a/snews_pt/snews_sub.py b/snews_pt/snews_sub.py index 0729269..538c409 100644 --- a/snews_pt/snews_sub.py +++ b/snews_pt/snews_sub.py @@ -1,3 +1,4 @@ +from datetime import datetime import os, json, click from hop import Stream @@ -7,7 +8,8 @@ def make_file(outputfolder): """ Get a proper json file name at a given folder """ os.makedirs(outputfolder, exist_ok=True) - date = snews_pt_utils.TimeStuff().get_date() + # date = snews_pt_utils.TimeStuff().get_date() + date = datetime.utcnow().isoformat().split('T')[0] file = os.path.join(outputfolder, f"0-SNEWS_ALERT_{date}.json") while os.path.isfile(file): i = int(file.split('/')[-1].split('-')[0]) @@ -61,12 +63,13 @@ def __init__(self, env_path=None, firedrill_mode=True): self.alert_topic = os.getenv("ALERT_TOPIC") if firedrill_mode: self.alert_topic = os.getenv("FIREDRILL_ALERT_TOPIC") - self.times = snews_pt_utils.TimeStuff() + # self.times = snews_pt_utils.TimeStuff() # time object/strings - self.times = snews_pt_utils.TimeStuff(env_path) - self.hr = self.times.get_hour() - self.date = self.times.get_date() - self.snews_time = lambda: self.times.get_utcnow() + # self.times = snews_pt_utils.TimeStuff(env_path) + # self.hr = self.times.get_hour() + # self.date = self.times.get_date() + # self.snews_time = lambda: self.times.get_utcnow() + self.snews_time = datetime.utcnow().isoformat() self.default_output = os.path.join(os.getcwd(), os.getenv("ALERT_OUTPUT")) diff --git a/snews_pt/test/example2_coincidence_tier_message.json b/snews_pt/test/example2_coincidence_tier_message.json new file mode 100644 index 0000000..11b2ed1 --- /dev/null +++ b/snews_pt/test/example2_coincidence_tier_message.json @@ -0,0 +1,11 @@ +{ + "detector_name": "Borexino", + "machine_time": + "2022-08-23T14:53:16.238275", + "neutrino_time": + "2022-08-23T14:50:15.345275", + "p_val": + 0.03, + "testing": + "this is a test" +} \ No newline at end of file diff --git a/snews_pt/test/example_coincidence_tier_message.json b/snews_pt/test/example_coincidence_tier_message.json index 76aadbd..84ec808 100644 --- a/snews_pt/test/example_coincidence_tier_message.json +++ b/snews_pt/test/example_coincidence_tier_message.json @@ -1,10 +1,12 @@ { "machine_time": - "22/06/14 14:57:238275", + "2022-06-14T14:57:12.238275", "neutrino_time": - "22/06/14 14:50:345275", + "2022-06-14T14:50:12.345275", "p_val": 0.07, "extra_key": - "test extra" + "test extra", + "testing": + "this is a test" } \ No newline at end of file diff --git a/snews_pt/test/example_coincidence_tier_message2.json b/snews_pt/test/example_coincidence_tier_message2.json new file mode 100644 index 0000000..eb63945 --- /dev/null +++ b/snews_pt/test/example_coincidence_tier_message2.json @@ -0,0 +1,6 @@ +{ + "neutrino_time": + "22/08/16 23:49:12:345275", + "testing": + "this is a test" +} diff --git a/snews_pt/test/example_heartbeat_message.json b/snews_pt/test/example_heartbeat_message.json index a9ca49f..92e2c3e 100644 --- a/snews_pt/test/example_heartbeat_message.json +++ b/snews_pt/test/example_heartbeat_message.json @@ -1,6 +1,8 @@ { "machine_time": - "22/06/14 14:57:238275", + "2022-06-14T14:21:57.238275", "detector_status": - "OFF" + "OFF", + "testing": + "this is a test" } \ No newline at end of file diff --git a/snews_pt/test/example_significance_tier_message.json b/snews_pt/test/example_significance_tier_message.json index 3c658de..594c063 100644 --- a/snews_pt/test/example_significance_tier_message.json +++ b/snews_pt/test/example_significance_tier_message.json @@ -1,12 +1,14 @@ { "machine_time": - "22/06/14 14:57:238275", + "2022-06-14T14:21:57.238275", "neutrino_time": - "22/06/14 14:50:345275", + "2022-06-14T14:21:50.345275", "p_val": 0.05, "p_values": [0.4, 0.5, 0.09, 0.04], "t_bin_width": - 0.6 + 0.6, + "testing": + "this is a test" } \ No newline at end of file diff --git a/snews_pt/test/example_timing_tier_message.json b/snews_pt/test/example_timing_tier_message.json index 4074c28..f2c9fcc 100644 --- a/snews_pt/test/example_timing_tier_message.json +++ b/snews_pt/test/example_timing_tier_message.json @@ -1,8 +1,10 @@ { "machine_time": - "22/06/14 14:57:238275", + "2022-06-14T14:21:57.238275", "neutrino_time": - "22/06/14 14:50:345275", + "2022-06-14T14:21:50.345275", "timing_series": - ["22/06/14 14:50:345275", "22/06/14 14:50:648275", "22/06/14 14:51:004511"] + ["2022-06-14T14:21:50.345275", "2022-06-14T14:21:50.648275", "2022-06-14T14:21:51.004511"], + "testing": + "this is a test" } \ No newline at end of file diff --git a/snews_pt/test/test_coincidence_tier.py b/snews_pt/test/test_coincidence_tier.py index 111f122..f0d1075 100644 --- a/snews_pt/test/test_coincidence_tier.py +++ b/snews_pt/test/test_coincidence_tier.py @@ -1,26 +1,19 @@ """Test publishing coincidence tier messages.""" from snews_pt.snews_pub import SNEWSTiersPublisher -from snews_pt.snews_pt_utils import set_name +from snews_pt._version import version as __version__ + def test_coincidence_expected(): """Test with example of expected message type.""" # Create coincidence tier message. - set_name(detector_name="KamLAND") - coin = SNEWSTiersPublisher(detector_name='KamLAND', neutrino_time='12/06/09 15:31:08:1098', p_value=0.4, - firedrill_mode=False) + coin = SNEWSTiersPublisher(detector_name='KamLAND', neutrino_time='2012-06-09T15:31:08.891011', p_value=0.4, + firedrill_mode=False, testing="this is a test") # Check that message has expected structure. assert coin.tiernames == ['CoincidenceTier'] - assert coin.message_data == {'detector_name': 'KamLAND', 'machine_time': None, 'neutrino_time': '12/06/09 15:31:08:1098', + assert coin.message_data == {'detector_name': 'KamLAND', 'machine_time': None, 'neutrino_time': '2012-06-09T15:31:08.891011', 'p_val': None, 'p_values': None, 'timing_series': None, 'which_tier': None, 'n_retract_latest': None, 'retraction_reason': None, 'detector_status': None, 'is_pre_sn': False, 't_bin_width': None, - 'p_value': 0.4} - - input_messages = {'detector_name': 'KamLAND', 'machine_time': None, 'neutrino_time': '12/06/09 15:31:08:1098', - 'p_val': None, 'meta': {'p_value': 0.4}, 'schema_version': '1.1.0'} - for k,v in input_messages.items(): - if k in ['sent_time', 'machine_time']: - continue - assert coin.messages[0][k] == v + 'p_value': 0.4, 'testing': 'this is a test'} assert coin.env_file == None # Try to send message to SNEWS 2.0 server. diff --git a/snews_pt/test/test_significance_tier.py b/snews_pt/test/test_significance_tier.py index bfad8b0..b67f7b9 100644 --- a/snews_pt/test/test_significance_tier.py +++ b/snews_pt/test/test_significance_tier.py @@ -1,17 +1,17 @@ """Test publishing significane tier messages.""" from snews_pt.snews_pub import SNEWSTiersPublisher -from snews_pt.snews_pt_utils import set_name +from snews_pt._version import version as __version__ def test_significance_expected(): """Test with example of expected message type.""" # Create significance tier message. - set_name(detector_name='DS-20K') sign = SNEWSTiersPublisher(detector_name='DS-20K', - neutrino_times=['12/06/09 15:31:08:1098', - '12/06/09 15:33:07:8910'], + neutrino_times=['2012-06-09T15:31:08.1098', + '2012-06-09T15:33:07.8910'], p_values=[0.4, 0.5], t_bin_width=0.8, - firedrill_mode=False) + firedrill_mode=False, + testing='this is a test') # Check that message has expected structure. assert sign.tiernames == ['SigTier'] @@ -28,22 +28,9 @@ def test_significance_expected(): 'detector_status': None, 'is_pre_sn': False, 'neutrino_times': - ['12/06/09 15:31:08:1098', - '12/06/09 15:33:07:8910']} - input_message = {'detector_name': 'DS-20K', - 'machine_time': None, - 'neutrino_time': None, - 't_bin_width': 0.8, - 'p_values': [0.4, 0.5], - 'meta': {'neutrino_times': - ['12/06/09 15:31:08:1098', - '12/06/09 15:33:07:8910']}, - 'schema_version': '1.1.0'} - for k,v in input_message.items(): - if k in ['sent_time', 'machine_time']: - continue - assert sign.messages[0][k] == v - + ['2012-06-09T15:31:08.1098', + '2012-06-09T15:33:07.8910'], + 'testing': 'this is a test'} assert sign.env_file is None # Try to send message to SNEWS 2.0 server. diff --git a/snews_pt/test/test_timing_tier.py b/snews_pt/test/test_timing_tier.py index ff38b79..480676b 100644 --- a/snews_pt/test/test_timing_tier.py +++ b/snews_pt/test/test_timing_tier.py @@ -1,33 +1,20 @@ """Test publishing timing tier messages.""" from snews_pt.snews_pub import SNEWSTiersPublisher -from snews_pt.snews_pt_utils import set_name +from snews_pt._version import version as __version__ def test_timing_expected(): """Test with example of expected message type.""" # Create timing tier message. - set_name(detector_name="XENONnT") - tims = SNEWSTiersPublisher(detector_name='XENONnT', neutrino_time='12/06/09 15:31:08:1098', timing_series=['12/06/09 15:31:08:1098', '12/06/09 15:33:07:8910'], - firedrill_mode=False) + tims = SNEWSTiersPublisher(detector_name='XENONnT', neutrino_time='2012-06-09T15:31:08.109876', timing_series=['2012-06-09T15:31:08.109876', '2012-06-09T15:33:07.891011'], + firedrill_mode=False, testing="this is a test") # Check that message has expected structure. assert tims.tiernames == ['CoincidenceTier', 'TimeTier'] - assert tims.message_data == {'detector_name': 'XENONnT', 'machine_time': None, 'neutrino_time': '12/06/09 15:31:08:1098', 'p_val': None, 'p_values': None, - 'timing_series': ['12/06/09 15:31:08:1098', '12/06/09 15:33:07:8910'], 'which_tier': None, 'n_retract_latest': None, - 'retraction_reason': None, 'detector_status': None, 'is_pre_sn': False, 't_bin_width': None, } - - input_messages = [{'detector_name': 'XENONnT', 'machine_time': None, 'neutrino_time': '12/06/09 15:31:08:1098', - 'p_val': None, 'meta': {}, 'schema_version': '1.1.0'}, - {'detector_name': 'XENONnT', 'machine_time': None, - 'neutrino_time': '12/06/09 15:31:08:1098', - 'timing_series': ['12/06/09 15:31:08:1098', '12/06/09 15:33:07:8910'], - 'meta': {}, 'schema_version': '1.1.0'}] - for i, message in enumerate(input_messages): - for k,v in message.items(): - if k in ['sent_time', 'machine_time']: - continue - assert tims.messages[i][k] == v - + assert tims.message_data == {'detector_name': 'XENONnT', 'machine_time': None, 'neutrino_time': '2012-06-09T15:31:08.109876', 'p_val': None, 'p_values': None, + 'timing_series': ['2012-06-09T15:31:08.109876', '2012-06-09T15:33:07.891011'], 'which_tier': None, 'n_retract_latest': None, + 'retraction_reason': None, 'detector_status': None, 'is_pre_sn': False, 't_bin_width': None, 'testing': 'this is a test'} assert tims.env_file == None + # Try to send message to SNEWS 2.0 server. try: tims.send_to_snews()