Skip to content

Commit

Permalink
Made prediction.js a module
Browse files Browse the repository at this point in the history
Change-Id: I868aea6c671e16cb1ca7db94cd29fa42fdb6276c
  • Loading branch information
LarsMichelsen committed Dec 20, 2018
1 parent 37d8883 commit 31c930e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 51 deletions.
21 changes: 12 additions & 9 deletions cmk/gui/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,32 +267,35 @@ def create_graph(name, size, bounds, v_range, legend):
html.write('<div class=color style="background-color: %s"></div><div class=entry>%s</div>' %
(color, title))
html.write('</div></td></tr></table>')
html.javascript('create_graph("content_%s", %.4f, %.4f, %.4f, %.4f);' %
html.javascript('cmk.prediction.create_graph("content_%s", %.4f, %.4f, %.4f, %.4f);' %
(name, bounds[0], bounds[1], v_range[0], v_range[1]))


def render_coordinates(v_scala, t_scala):
html.javascript('render_coordinates(%s, %s);' % (json.dumps(v_scala), json.dumps(t_scala)))
html.javascript(
'cmk.prediction.render_coordinates(%s, %s);' % (json.dumps(v_scala), json.dumps(t_scala)))


def render_curve(points, color, width=1, square=False):
html.javascript('render_curve(%s, %s, %d, %d);' % (json.dumps(points), json.dumps(color), width,
square and 1 or 0))
html.javascript('cmk.prediction.render_curve(%s, %s, %d, %d);' %
(json.dumps(points), json.dumps(color), width, square and 1 or 0))


def render_point(t, v, color):
html.javascript('render_point(%s, %s, %s);' % (json.dumps(t), json.dumps(v), json.dumps(color)))
html.javascript('cmk.prediction.render_point(%s, %s, %s);' % (json.dumps(t), json.dumps(v),
json.dumps(color)))


def render_area(points, color, alpha=1.0):
html.javascript('render_area(%s, %s, %f);' % (json.dumps(points), json.dumps(color), alpha))
html.javascript(
'cmk.prediction.render_area(%s, %s, %f);' % (json.dumps(points), json.dumps(color), alpha))


def render_area_reverse(points, color, alpha=1.0):
html.javascript(
'render_area_reverse(%s, %s, %f);' % (json.dumps(points), json.dumps(color), alpha))
html.javascript('cmk.prediction.render_area_reverse(%s, %s, %f);' % (json.dumps(points),
json.dumps(color), alpha))


def render_dual_area(lower_points, upper_points, color, alpha=1.0):
html.javascript('render_dual_area(%s, %s, %s, %f);' %
html.javascript('cmk.prediction.render_dual_area(%s, %s, %s, %f);' %
(json.dumps(lower_points), json.dumps(upper_points), json.dumps(color), alpha))
1 change: 0 additions & 1 deletion web/htdocs/js/checkmk.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.


//# .-General------------------------------------------------------------.
//# | ____ _ |
//# | / ___| ___ _ __ ___ _ __ __ _| | |
Expand Down
46 changes: 38 additions & 8 deletions web/htdocs/js/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
require('script-loader!./checkmk.js');
require('script-loader!./dashboard.js');
require('script-loader!./colorpicker.js');
require('script-loader!./prediction.js');
require('script-loader!./search.js');
require('script-loader!./sidebar.js');
require('script-loader!./wato.js');
// +------------------------------------------------------------------+
// | ____ _ _ __ __ _ __ |
// | / ___| |__ ___ ___| | __ | \/ | |/ / |
// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
// | | |___| | | | __/ (__| < | | | | . \ |
// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
// | |
// | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
// +------------------------------------------------------------------+
//
// This file is part of Check_MK.
// The official homepage is at http://mathias-kettner.de/check_mk.
//
// check_mk is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation in version 2. check_mk is distributed
// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more de-
// ails. You should have received a copy of the GNU General Public
// License along with GNU Make; see the file COPYING. If not, write
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.

require("script-loader!./checkmk.js");
require("script-loader!./dashboard.js");
require("colorpicker");
var prediction = require("prediction");
require("script-loader!./search.js");
require("script-loader!./sidebar.js");
require("script-loader!./wato.js");

// TODO: Find a better solution for this CEE specific include
try {
require('script-loader!../../../enterprise/web/htdocs/js/graphs.js');
require("script-loader!../../../enterprise/web/htdocs/js/graphs.js");
} catch(e) {}

module.exports = {
cmk: {
prediction: prediction
}
};
63 changes: 32 additions & 31 deletions web/htdocs/js/prediction.js → web/htdocs/js/modules/prediction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var right_border = 50;
var top_border = 40;
var bottom_border = 50;

function create_graph(cid, ft, ut, vmi, vma)
export function create_graph(cid, ft, ut, vmi, vma)
{
// Keep important data as global variables, needed by
// render_curve()
Expand Down Expand Up @@ -88,11 +88,11 @@ var lineb = "#bbbbbb";
var linec = "#bbbbbb";


function render_coordinates(v_scala, t_scala)
export function render_coordinates(v_scala, t_scala)
{
// Create canvas
var canvas = document.getElementById(canvas_id);
var c = canvas.getContext('2d');
var c = canvas.getContext("2d");
c.font = "20px sans-serif";

// Convert the coordinate system in a way, that we can directly
Expand Down Expand Up @@ -125,11 +125,12 @@ function render_coordinates(v_scala, t_scala)
c.fillStyle="#000000";

// Value scala (vertical)
var val, txt, p, w;
for (i=0; i<v_scala.length; i++) {
var val = v_scala[i][0];
var txt = v_scala[i][1];
var p = point(0, val);
var w = c.measureText(txt).width;
val = v_scala[i][0];
txt = v_scala[i][1];
p = point(0, val);
w = c.measureText(txt).width;
c.fillText(txt, left_border - w - 16, p[1] + 6);
if (i%2)
c.strokeStyle = lineb;
Expand All @@ -140,10 +141,10 @@ function render_coordinates(v_scala, t_scala)

// Time scala (horizontal)
for (i=0; i<t_scala.length; i++) {
var t = t_scala[i][0];
var txt = t_scala[i][1];
var p = point(t, 0);
var w = c.measureText(txt).width;
t = t_scala[i][0];
txt = t_scala[i][1];
p = point(t, 0);
w = c.measureText(txt).width;
c.fillText(txt, p[0] - (w/2), height - bottom_border + 28);
if (i%2)
c.strokeStyle = lineb;
Expand All @@ -162,10 +163,8 @@ function render_coordinates(v_scala, t_scala)

function point(t, v)
{
p = [ left_border + (t - from_time) / (until_time - from_time) * netto_width,
height - bottom_border - ((v - v_min) / (v_max - v_min) * netto_height) ];

return p;
return [ left_border + (t - from_time) / (until_time - from_time) * netto_width,
height - bottom_border - ((v - v_min) / (v_max - v_min) * netto_height) ];
}

function line(c, t0, v0, t1, v1)
Expand All @@ -178,10 +177,10 @@ function line(c, t0, v0, t1, v1)
c.stroke();
}

function render_point(t, v, color)
export function render_point(t, v, color)
{
var canvas = document.getElementById(canvas_id);
var c = canvas.getContext('2d');
var c = canvas.getContext("2d");
var p = point(t, v);
c.beginPath();
c.lineWidth = 4;
Expand All @@ -194,10 +193,10 @@ function render_point(t, v, color)
}


function render_curve(points, color, w, square)
export function render_curve(points, color, w, square)
{
var canvas = document.getElementById(canvas_id);
var c = canvas.getContext('2d');
var c = canvas.getContext("2d");

c.beginPath();
c.strokeStyle = color;
Expand All @@ -206,7 +205,7 @@ function render_curve(points, color, w, square)
var op;
var time_step = (until_time - from_time) / points.length;
var first = true;
for (i=0; i<points.length; i++) {
for (var i=0; i<points.length; i++) {
if (points[i] == null) {
c.stroke();
first = true;
Expand All @@ -227,23 +226,24 @@ function render_curve(points, color, w, square)
c.stroke();
}

function render_area(points, color, alpha)
export function render_area(points, color, alpha)
{
render_dual_area(null, points, color, alpha);
}

function render_area_reverse(points, color, alpha)
export function render_area_reverse(points, color, alpha)
{
render_dual_area(points, null, color, alpha);
}

function render_dual_area(lower_points, upper_points, color, alpha)
export function render_dual_area(lower_points, upper_points, color, alpha)
{
var canvas = document.getElementById(canvas_id);
var c = canvas.getContext('2d');
var c = canvas.getContext("2d");

c.fillStyle = color;
c.globalAlpha = alpha;
var num_points;
if (lower_points)
num_points = lower_points.length;
else
Expand All @@ -252,18 +252,19 @@ function render_dual_area(lower_points, upper_points, color, alpha)
var time_step = 1.0 * (until_time - from_time) / num_points;
var pix_step = 1.0 * netto_width / num_points;

for (i=0; i<num_points; i++) {
var x = point(from_time + time_step * i, 0)[0];
var x, yl, yu, h;
for (var i=0; i<num_points; i++) {
x = point(from_time + time_step * i, 0)[0];
if (lower_points)
var yl = point(0, lower_points[i])[1];
yl = point(0, lower_points[i])[1];
else
var yl = height - bottom_border;
yl = height - bottom_border;

if (upper_points)
var yu = point(0, upper_points[i])[1];
yu = point(0, upper_points[i])[1];
else
var yu = top_border;
var h = yu - yl;
yu = top_border;
h = yu - yl;
c.fillRect(x, yl, pix_step, h);
}
c.globalAlpha = 1;
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ module.exports = {
// Keep this until we have cleaned up our JS files to work as modules and changed all call sites
// from HTML code to work with the modules. Until then we need to keep the old behaviour of loading
// all JS code in the global namespace
//libraryTarget: 'window',
//library: 'checkmk'
libraryTarget: "window",
},
resolve: {
modules: [
"node_modules",
path.resolve(__dirname, "web/htdocs/js/modules"),
path.resolve(__dirname, "web/htdocs/js"),
path.resolve(__dirname, "enterprise/web/htdocs/js")
]
Expand Down

0 comments on commit 31c930e

Please sign in to comment.