From ea393dcfa809d13388db88eee7e9326bfd809ecd Mon Sep 17 00:00:00 2001 From: teh_coderer Date: Mon, 24 Apr 2023 14:36:46 -0500 Subject: [PATCH] Feature/ pywry posthog (#4860) * posthog * Update index.html * remove console logs * fix feature flag getting sent * adding to frontend-components * updates * Update main.js * alias * Update backend.py * Update backend.py * tables handle null * Update helper_funcs.py * Update volatility_view.py --------- Co-authored-by: andrewkenreich --- frontend-components/tables/index.html | 56 ++++++++++++++++ frontend-components/tables/package-lock.json | 13 ++++ frontend-components/tables/package.json | 1 + frontend-components/tables/src/App.tsx | 2 +- frontend-components/tables/src/index.css | 4 ++ .../technical_analysis/volatility_view.py | 2 +- openbb_terminal/core/models/system_model.py | 1 + openbb_terminal/core/plots/backend.py | 39 +++++++++-- openbb_terminal/core/plots/plotly.html | 63 ++++++++++++++++- openbb_terminal/core/plots/plotly_helper.py | 27 +++++--- openbb_terminal/core/plots/table.html | 60 ++++++++++++++++- openbb_terminal/core/plots/web/helpers.js | 29 ++++---- openbb_terminal/core/plots/web/main.js | 67 +++++++++++++++---- openbb_terminal/core/plots/web/popups.js | 18 ++--- openbb_terminal/helper_funcs.py | 3 +- openbb_terminal/loggers.py | 8 ++- 16 files changed, 336 insertions(+), 57 deletions(-) diff --git a/frontend-components/tables/index.html b/frontend-components/tables/index.html index a0d02f39ceb..25e5b995180 100644 --- a/frontend-components/tables/index.html +++ b/frontend-components/tables/index.html @@ -5,6 +5,62 @@ OpenBB Interactive Tables + @@ -59,6 +97,7 @@ width: 100%; bottom: 0; max-height: 60px; + min-height: 60px; font-size: 0px; " > @@ -143,13 +182,17 @@
-
@@ -202,6 +245,7 @@ globals.cmd_src = ""; if (typeof OpenBBMain != "undefined" && window.plotly_figure) { clearInterval(interval); + const date = new Date(); const formattedDate = new Intl.DateTimeFormat("en-GB", { dateStyle: "full", @@ -244,6 +288,18 @@ } }); } + if (globals.cmd_src != "" && window.plotly_figure?.posthog?.collect_logs) { + posthog.capture("chart", { + INFO: { + command: globals.cmd_src, + title: window.plotly_figure.layout.title.text, + date: formattedDate, + python_version: window.plotly_figure.python_version, + pywry_version: window.plotly_figure.pywry_version, + terminal_version: window.plotly_figure.terminal_version, + }, + }); + } document .querySelector('meta[name="color-scheme"]') @@ -263,7 +319,8 @@ let style = "font-size: 12px; font-weight: 400; color: gray;"; document.getElementById("date").innerHTML = - formattedDate + `
${globals.cmd_src}`; + formattedDate + + `
${globals.cmd_src}`; document.getElementById("title").innerHTML = title; CHART_DIV = document.getElementById("openbb_chart"); diff --git a/openbb_terminal/core/plots/plotly_helper.py b/openbb_terminal/core/plots/plotly_helper.py index 4207f6931d1..adb64b1954e 100644 --- a/openbb_terminal/core/plots/plotly_helper.py +++ b/openbb_terminal/core/plots/plotly_helper.py @@ -5,13 +5,17 @@ import textwrap from datetime import datetime from math import floor from pathlib import Path -from typing import Any, Dict, List, Literal, Optional, Tuple, TypeVar, Union - -try: - # pylint: disable=W0611 # noqa: F401 - from darts import TimeSeries -except ImportError: - pass +from typing import ( + TYPE_CHECKING, + Any, + Dict, + List, + Literal, + Optional, + Tuple, + TypeVar, + Union, +) import numpy as np import pandas as pd @@ -35,6 +39,13 @@ from openbb_terminal.core.plots.config.openbb_styles import ( from openbb_terminal.core.session.current_system import get_current_system from openbb_terminal.core.session.current_user import get_current_user +if TYPE_CHECKING: + try: + # pylint: disable=W0611 # noqa: F401 + from darts import TimeSeries + except ImportError: + pass + TimeSeriesT = TypeVar("TimeSeriesT", bound="TimeSeries") @@ -85,6 +96,7 @@ class TerminalStyle: self.load_available_styles() self.load_style(plt_style) self.apply_console_style(console_style) + self.apply_style() def apply_console_style(self, style: Optional[str] = None) -> None: """Apply the style to the console.""" @@ -246,7 +258,6 @@ theme = TerminalStyle( get_current_user().preferences.CHART_STYLE, get_current_user().preferences.RICH_STYLE, ) -theme.apply_style() # pylint: disable=R0913 diff --git a/openbb_terminal/core/plots/table.html b/openbb_terminal/core/plots/table.html index 6877d956617..5372ba46a12 100644 --- a/openbb_terminal/core/plots/table.html +++ b/openbb_terminal/core/plots/table.html @@ -5,6 +5,62 @@ OpenBB Interactive Tables diff --git a/openbb_terminal/core/plots/web/helpers.js b/openbb_terminal/core/plots/web/helpers.js index 323f93a73d2..d032ff94602 100644 --- a/openbb_terminal/core/plots/web/helpers.js +++ b/openbb_terminal/core/plots/web/helpers.js @@ -1,9 +1,16 @@ const ICONS = { sunIcon: { + viewBox: "0 0 16 16", width: 16, height: 16, path: "M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z", }, + moonIcon: { + viewBox: "0 0 25 25", + width: 25, + height: 25, + path: "M21.752 15.002A9.718 9.718 0 0118 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 003 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 009.002-5.998z", + }, plotCsv: { width: 900, height: 900, @@ -384,12 +391,6 @@ function button_pressed(title, active = false) { } const DARK_CHARTS_TEMPLATE = { - line: { - up_color: "#00ACFF", - down_color: "#e4003a", - color: "#ffed00", - width: 1.5, - }, data: { candlestick: [ { @@ -493,12 +494,6 @@ const DARK_CHARTS_TEMPLATE = { }; const LIGHT_CHARTS_TEMPLATE = { - line: { - up_color: "#009600", - down_color: "#c80000", - color: "#0d0887", - width: 1.5, - }, data: { barpolar: [ { @@ -1039,7 +1034,7 @@ const LIGHT_CHARTS_TEMPLATE = { }, xaxis: { automargin: true, - ticks: "", + ticks: "outside", zerolinewidth: 2, rangeslider: { visible: false, @@ -1049,17 +1044,23 @@ const LIGHT_CHARTS_TEMPLATE = { tickfont: { size: 15, }, + title: { + standoff: 20, + }, mirror: true, zeroline: false, }, yaxis: { automargin: true, - ticks: "", + ticks: "outside", tickfont: { size: 15, }, zerolinewidth: 2, fixedrange: false, + title: { + standoff: 20, + }, nticks: 8, showgrid: true, showline: true, diff --git a/openbb_terminal/core/plots/web/main.js b/openbb_terminal/core/plots/web/main.js index ec8b3c04f39..352d9c264ac 100644 --- a/openbb_terminal/core/plots/web/main.js +++ b/openbb_terminal/core/plots/web/main.js @@ -97,7 +97,7 @@ function OpenBBMain( }, }, { - name: "Download PNG (Ctrl+S)", + name: "Download Chart as Image (Ctrl+S)", icon: ICONS.downloadImage, click: async function () { await downloadImageButton(); @@ -273,17 +273,16 @@ function OpenBBMain( // We set the plot config and plot the chart Plotly.setPlotConfig(CONFIG); Plotly.react(globals.CHART_DIV, graphs, { responsive: true }); - graphs.layout.automargin = false; - graphs.layout.autoexpand = false; - - let AXES = Object.keys(graphs.layout) - .filter((x) => x.startsWith("xaxis") || x.startsWith("yaxis")) + let AXES = Object.keys(graphs.layout).filter( + (x) => x.startsWith("xaxis") || x.startsWith("yaxis") + ); // We set all the axes automargin to false if (AXES.length > 0) { AXES.forEach((axis) => { - graphs.layout[axis].automargin = false; + let margin = axis.startsWith("y") ? "l+r" : "t+b"; + graphs.layout[axis].automargin = margin; }); Plotly.react(globals.CHART_DIV, graphs, { responsive: true }); } @@ -293,11 +292,24 @@ function OpenBBMain( const modebar_buttons = modebar.getElementsByClassName("modebar-btn"); globals.barButtons = {}; + const captureButtons = [ + "Download CSV", + "Download Chart as Image", + "Overlay chart from CSV", + "Add Text", + "Change Titles", + "Auto Scale (Ctrl+Shift+A)", + "Reset Axes", + ]; + for (let i = 0; i < modebar_buttons.length; i++) { // We add the buttons to the global variable for later use // and set the border to transparent so we can change the // color of the buttons when they are pressed let button = modebar_buttons[i]; + if (captureButtons.includes(button.getAttribute("data-title"))) { + button.classList.add("ph-capture"); + } button.style.border = "transparent"; globals.barButtons[button.getAttribute("data-title")] = button; } @@ -593,11 +605,42 @@ function OpenBBMain( } function changeTheme() { globals.dark_mode = !globals.dark_mode; - Plotly.relayout(globals.CHART_DIV, { - template: globals.dark_mode - ? DARK_CHARTS_TEMPLATE - : LIGHT_CHARTS_TEMPLATE, - }); document.body.style.backgroundColor = globals.dark_mode ? "#000" : "#fff"; + globals.CHART_DIV.layout.font.color = globals.dark_mode ? "#fff" : "#000"; + + const changeIcon = globals.dark_mode ? ICONS.sunIcon : ICONS.moonIcon; + + globals.barButtons["Change Theme"] + .getElementsByTagName("path")[0] + .setAttribute("d", changeIcon.path); + + globals.barButtons["Change Theme"] + .getElementsByTagName("svg")[0] + .setAttribute("viewBox", changeIcon.viewBox); + + const volumeColors = { + "#e4003a": "#c80000", + "#00ACFF": "#009600", + "#009600": "#00ACFF", + "#c80000": "#e4003a", + }; + + TRACES.forEach((trace) => { + if (trace.type == "bar") { + trace.marker.color.forEach((color, i) => { + trace.marker.color[i] = volumeColors[color] || color; + }); + } + }); + + Plotly.update( + globals.CHART_DIV, + {}, + { + template: globals.dark_mode + ? DARK_CHARTS_TEMPLATE + : LIGHT_CHARTS_TEMPLATE, + } + ); } } diff --git a/openbb_terminal/core/plots/web/popups.js b/openbb_terminal/core/plots/web/popups.js index e76457ec94e..90c17d26a8f 100644 --- a/openbb_terminal/core/plots/web/popups.js +++ b/openbb_terminal/core/plots/web/popups.js @@ -24,8 +24,8 @@ function get_popup(data = null, popup_id = null) {
- - + +
`; @@ -135,9 +135,9 @@ function get_popup(data = null, popup_id = null) { if (has_annotation) { globals.TEXT_DIV.innerHTML += `
- - - + + + @@ -146,8 +146,8 @@ function get_popup(data = null, popup_id = null) { } else { globals.TEXT_DIV.innerHTML += `
- - + +
`; @@ -631,8 +631,8 @@ function on_submit(popup_id, on_annotation = null) { xshift += xshift < -320 ? 10 + 2 * left_yaxis_ticks : 0; gd.layout.annotations[globals.cmd_src_idx].xshift = xshift; - gd.layout.margin.l += left_yaxis_ticks > 0 ? 50 + add_xshift : 45; } + gd.layout.margin.l += left_yaxis_ticks > 0 ? 50 + add_xshift : 45; } } @@ -659,8 +659,8 @@ function on_submit(popup_id, on_annotation = null) { xshift += xshift < -320 ? 10 + 2 * left_yaxis_ticks : 0; gd.layout.annotations[globals.cmd_src_idx].xshift = xshift; - gd.layout.margin.l += left_yaxis_ticks > 0 ? 50 : 45; } + gd.layout.margin.l += left_yaxis_ticks > 0 ? 50 : 45; } console.log("trace: ", trace); diff --git a/openbb_terminal/helper_funcs.py b/openbb_terminal/helper_funcs.py index 615a1c0f333..faa1bbf6c31 100644 --- a/openbb_terminal/helper_funcs.py +++ b/openbb_terminal/helper_funcs.py @@ -305,7 +305,8 @@ def print_rich_table( for col in df.columns: try: - df[col] = pd.to_numeric(df[col]) + if not isinstance(df[col].iloc[0], pd.Timestamp): + df[col] = pd.to_numeric(df[col]) except ValueError: pass diff --git a/openbb_terminal/loggers.py b/openbb_terminal/loggers.py index c2cac6534a4..4265ad537ae 100644 --- a/openbb_terminal/loggers.py +++ b/openbb_terminal/loggers.py @@ -32,7 +32,10 @@ from openbb_terminal.core.log.generation.settings import ( Settings, ) from openbb_terminal.core.log.generation.user_logger import get_user_uuid -from openbb_terminal.core.session.current_system import get_current_system +from openbb_terminal.core.session.current_system import ( + get_current_system, + set_current_system, +) logger = logging.getLogger(__name__) current_system = get_current_system() @@ -165,6 +168,9 @@ def setup_logging( session_id = get_session_id() user_id = get_user_uuid() + current_system.LOGGING_APP_ID = identifier + set_current_system(current_system) + # AWSSettings aws_access_key_id = current_system.LOGGING_AWS_ACCESS_KEY_ID aws_secret_access_key = current_system.LOGGING_AWS_SECRET_ACCESS_KEY