-
Notifications
You must be signed in to change notification settings - Fork 3
Change default plotly version used to display plots #287
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Plot Initialization | ||
| * Reads plot configuration from JSON script tag and initializes dynamic plot updates | ||
| */ | ||
|
|
||
| (function() { | ||
| 'use strict'; | ||
|
|
||
| function getJsonScriptData(elementId) { | ||
| var element = document.getElementById(elementId); | ||
| if (element) { | ||
| return JSON.parse(element.textContent); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| var updateUrl = getJsonScriptData('plot-update-url'); | ||
| if (updateUrl) { | ||
| PlotlyDynamicLoader.initializePlotUpdates(updateUrl, 60000); | ||
| } | ||
| })(); |
115 changes: 115 additions & 0 deletions
115
src/webmon_app/reporting/static/plotly_dynamic_loader.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * Plotly Dynamic Loader | ||
| * Dynamically loads appropriate Plotly.js versions based on plot requirements | ||
| * and handles live plot updates from a REST API endpoint | ||
| */ | ||
|
|
||
| const PlotlyDynamicLoader = (function() { | ||
| 'use strict'; | ||
|
|
||
| const loadedPlotlyScriptUrls = new Set(); | ||
| const defaultPlotlyVersion = '3.0.1'; | ||
| const plotlyBaseUrl = '/static/thirdparty/'; | ||
| const plotlyV2ScriptPath = plotlyBaseUrl + 'plotly-2.9.0.min.js'; | ||
| const plotlyV3ScriptPath = plotlyBaseUrl + 'plotly-3.0.1.min.js'; | ||
| let currentPlotHtml = ''; | ||
|
|
||
| function insertPlotHtml(plotHtml) { | ||
| const graphContainer = $('#graph'); | ||
| if (graphContainer.length === 0) { | ||
| console.error("#graph container not found in DOM for plot insertion."); | ||
| return; | ||
| } | ||
| graphContainer.html(plotHtml); | ||
| console.log("Plot HTML inserted into #graph."); | ||
| } | ||
|
|
||
| function loadPlotlyScriptAndRender(requiredVersion, plotHtmlToRender) { | ||
| if (!requiredVersion || typeof requiredVersion !== 'string' || requiredVersion.trim() === '') { | ||
| console.warn("Plotly version requirement is missing or invalid. Using default:", defaultPlotlyVersion); | ||
| requiredVersion = defaultPlotlyVersion; | ||
| } | ||
|
|
||
| const majorVersion = parseInt(requiredVersion.trim().split('.')[0], 10); | ||
| let scriptUrlToLoad = (majorVersion >= 3) ? plotlyV3ScriptPath : plotlyV2ScriptPath; | ||
| if (requiredVersion.trim().startsWith('3.')) { | ||
| scriptUrlToLoad = plotlyV3ScriptPath; | ||
| } | ||
|
|
||
| console.log("Plotly.js version required by plot data:", requiredVersion, "-> Mapped to script URL:", scriptUrlToLoad); | ||
|
|
||
| if (loadedPlotlyScriptUrls.has(scriptUrlToLoad)) { | ||
| console.log("Required Plotly script already loaded:", scriptUrlToLoad); | ||
| insertPlotHtml(plotHtmlToRender); | ||
| return; | ||
| } | ||
|
|
||
| console.log("Attempting to load Plotly.js from:", scriptUrlToLoad); | ||
| const script = document.createElement('script'); | ||
| script.src = scriptUrlToLoad; | ||
| script.async = false; | ||
| script.onload = function() { | ||
| console.log("Successfully loaded Plotly script:", scriptUrlToLoad); | ||
| loadedPlotlyScriptUrls.add(scriptUrlToLoad); | ||
| insertPlotHtml(plotHtmlToRender); | ||
| }; | ||
| script.onerror = function() { | ||
| console.error("CRITICAL ERROR: Failed to load Plotly script:", scriptUrlToLoad); | ||
| $('#graph').html('<p class="error" style="color:red; font-weight:bold;">Error: Could not load required plotting library. Plot cannot be displayed.</p>'); | ||
| }; | ||
| document.head.appendChild(script); | ||
| } | ||
|
|
||
| function getPlotUpdate(updateUrl) { | ||
| $.ajax({ | ||
| type: "GET", | ||
| url: updateUrl, | ||
| success: function(newPlotHtml) { | ||
| if (currentPlotHtml.localeCompare(newPlotHtml) !== 0) { | ||
| currentPlotHtml = newPlotHtml; | ||
| console.log("New plot HTML received from server. Determining Plotly.js version requirement..."); | ||
|
|
||
| const tempDiv = $('<div>').html(newPlotHtml); | ||
| const plotGraphDiv = tempDiv.find('div.plotly-graph-div'); | ||
| let requiredVersion = null; | ||
|
|
||
| if (plotGraphDiv.length > 0 && plotGraphDiv.attr('plotlyjs-version')) { | ||
| requiredVersion = String(plotGraphDiv.attr('plotlyjs-version')); | ||
| console.log("Extracted 'plotlyjs-version' from plot div:", requiredVersion); | ||
| } else { | ||
| console.log("No 'plotlyjs-version' attribute found on the main plot div. Will use default."); | ||
| requiredVersion = defaultPlotlyVersion; | ||
| } | ||
|
|
||
| tempDiv.remove(); | ||
|
|
||
| loadPlotlyScriptAndRender(requiredVersion, newPlotHtml); | ||
| } | ||
| }, | ||
| error: function(jqXHR, textStatus, errorThrown) { | ||
| console.error("Failed to fetch plot update from " + updateUrl + ":", textStatus, errorThrown); | ||
| $('#graph').html('<p class="error" style="color:red;">Failed to load plot data. Please try again later.</p>'); | ||
| }, | ||
| dataType: "html", | ||
| timeout: 25000 | ||
| }); | ||
| } | ||
|
|
||
| function initializePlotUpdates(updateUrl, intervalMs) { | ||
| if (!updateUrl) { | ||
| console.warn("PlotlyDynamicLoader: No update URL provided, skipping initialization."); | ||
| return; | ||
| } | ||
|
|
||
| intervalMs = intervalMs || 60000; | ||
| getPlotUpdate(updateUrl); | ||
| setInterval(function() { | ||
| getPlotUpdate(updateUrl); | ||
| }, intervalMs); | ||
| } | ||
|
|
||
| return { | ||
| initializePlotUpdates: initializePlotUpdates, | ||
| getPlotUpdate: getPlotUpdate | ||
| }; | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.