Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tickets/dm 45892 #8

Merged
merged 16 commits into from
Aug 28, 2024
358 changes: 358 additions & 0 deletions notebooks_tsqr/TEMPLATE_logrep.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0",
"metadata": {},
"outputs": [],
"source": [
"# Parameters. Set defaults here.\n",
"# Times Square replaces this cell with the user's parameters.\n",
"record_limit = '999'"
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"imports\"></a>\n",
"## Imports and General Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"# Only use packages available in the Rubin Science Platform\n",
"import requests\n",
"from collections import defaultdict\n",
"import pandas as pd\n",
"from pprint import pp, pformat\n",
"from urllib.parse import urlencode\n",
"from IPython.display import FileLink, display_markdown\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"env = 'usdf_dev' # usdf-dev, tucson, slac, summit\n",
"log_name = 'narrativelog'\n",
"log = log_name\n",
"limit = int(record_limit)\n",
"response_timeout = 3.05 # seconds, how long to wait for connection\n",
"read_timeout = 20 # seconds\n",
"\n",
"timeout = (float(response_timeout), float(read_timeout))\n",
"\n",
"# Env list comes from drop-down menu top of:\n",
"# https://rsp.lsst.io/v/usdfdev/guides/times-square/\n",
"envs = dict(\n",
" #rubin_usdf_dev = '',\n",
" #data_lsst_cloud = '',\n",
" #usdf = '',\n",
" #base_data_facility = '',\n",
" summit = 'https://summit-lsp.lsst.codes',\n",
" usdf_dev = 'https://usdf-rsp-dev.slac.stanford.edu',\n",
" #rubin_idf_int = '',\n",
" tucson = 'https://tucson-teststand.lsst.codes',\n",
")\n",
"server = envs[env]\n",
"service = f'{server}/{log}'\n",
"service"
]
},
{
"cell_type": "markdown",
"id": "4",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"setup_source\"></a>\n",
"## Setup Source"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"md = f'### Will retrieve from {service}'\n",
"display_markdown(md, raw=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"recs = None\n",
"ok = True\n",
"\n",
"# is_human=either&is_valid=either&offset=0&limit=50' \n",
"# site_ids=tucson&message_text=wubba&min_level=0&max_level=999&user_ids=spothier&user_agents=LOVE\n",
"# tags=love&exclude_tags=ignore_message\n",
"qparams = dict(is_human='either',\n",
" is_valid='either',\n",
" limit=limit,\n",
" )\n",
"qstr = urlencode(qparams)\n",
"url = f'{service}/messages?{qstr}'\n",
"\n",
"ignore_fields = set(['tags', 'urls', 'message_text', 'id', 'date_added', \n",
" 'obs_id', 'day_obs', 'seq_num', 'parent_id', 'user_id',\n",
" 'date_invalidated', 'date_begin', 'date_end',\n",
" 'time_lost', # float\n",
" #'systems','subsystems','cscs', # values are lists, special handling\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"display_markdown(f'## Get (up to {limit}) Records', raw=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"# TODO Often fails on first request. Find out why!\n",
"try:\n",
" response = requests.get(url, timeout=timeout)\n",
"except:\n",
" pass \n",
" \n",
"try:\n",
" print(f'Attempt to get logs from {url=}')\n",
" response = requests.get(url, timeout=timeout)\n",
" response.raise_for_status()\n",
" recs = response.json()\n",
" flds = set(recs[0].keys())\n",
" facflds = flds - ignore_fields\n",
" # facets(field) = set(value-1, value-2, ...)\n",
" facets = {fld: set([str(r[fld])\n",
" for r in recs if not isinstance(r[fld], list)]) \n",
" for fld in facflds}\n",
"except Exception as err:\n",
" ok = False\n",
" print(f'ERROR getting {log} from {env=} using {url=}: {err=}')\n",
"numf = len(flds) if ok else 0\n",
"numr = len(recs) if ok else 0\n",
"print(f'Retrieved {numr} records, each with {numf} fields.')"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"table\"></a>\n",
"## Tables of (mostly raw) results"
]
},
{
"cell_type": "markdown",
"id": "10",
"metadata": {},
"source": [
"### Fields names provided in records from log."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"pd.DataFrame(flds, columns=['Field Name'])"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"### Facets from log records.\n",
"A *facet* is the set all of values found for a field in the retrieved records. Facets are only calculated for some fields."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"display(pd.DataFrame.from_dict(facets, orient='index'))\n",
"display(facets)"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"### Table of selected log record fields.\n",
"Table can be retrieved as CSV file for local use."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"cols = ['date_added', 'time_lost']\n",
"df = pd.DataFrame(recs)[cols]\n",
"\n",
"# Allow download of CSV version of DataFrame\n",
"csvfile = 'tl.csv'\n",
"df.to_csv(csvfile)\n",
"myfile = FileLink(csvfile)\n",
"print('Table available as CSV file: ')\n",
"display(myfile)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(recs)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"plot\"></a>\n",
"## Plots from log"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"x = [r['date_added'] for r in recs]\n",
"y = [r['time_lost'] for r in recs]\n",
"plt.plot(x, y) \n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "19",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"raw_analysis\"></a>\n",
"## Raw Content Analysis"
]
},
{
"cell_type": "markdown",
"id": "20",
"metadata": {},
"source": [
"### Example of one record"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21",
"metadata": {},
"outputs": [],
"source": [
"display(recs[-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22",
"metadata": {},
"outputs": [],
"source": [
"msg = rec[\"message_text\"]\n",
"md = f'Message text from log:\\n> {msg}'\n",
"display_markdown(md, raw=True)"
]
},
{
"cell_type": "markdown",
"id": "23",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"elicitation\"></a>\n",
"## Stakeholder Elicitation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
16 changes: 16 additions & 0 deletions notebooks_tsqr/TEMPLATE_logrep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# For use with a Times Square notebook
title: LR mix
description: Prototype 1
authors:
- name: Steve Pothier
slack: Steve Pothier
tags:
- reporting
- prototype
parameters:
record_limit:
type: integer
description: Max number of records to output
default: 99
minimum: 1
maximum: 9999
3 changes: 2 additions & 1 deletion notebooks_tsqr/exposurelog.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# For use with a Times Square notebook
title: Logging and Reporting for Narrative Log
title: LR Exposure Log
description: Prototype 1
authors:
- name: Steve Pothier
slack: Steve Pothier
tags:
- reporting
- prototype
- exposure
parameters:
record_limit:
type: integer
Expand Down
2 changes: 1 addition & 1 deletion notebooks_tsqr/logrep_proto_1.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For use with a Times Square notebook
title: Logging and Reporting
title: LR mix
description: Prototype 1
authors:
- name: Steve Pothier
Expand Down
Loading