aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/js
diff options
context:
space:
mode:
authorManuel Luis SanmartĂ­n Rozada2015-04-03 16:47:54 +0200
committerPim van den Berg2015-06-22 11:01:20 +0200
commit937416fa325cfdd1eb9782281a37c2b44ce8a522 (patch)
tree11982d856915524dc0d18c1f52274950d6800ad4 /js
parentjsrrdgraph: Fix parse AREA in cmd line. (diff)
downloadapt-panopticon_cgp-937416fa325cfdd1eb9782281a37c2b44ce8a522.zip
apt-panopticon_cgp-937416fa325cfdd1eb9782281a37c2b44ce8a522.tar.gz
apt-panopticon_cgp-937416fa325cfdd1eb9782281a37c2b44ce8a522.tar.bz2
apt-panopticon_cgp-937416fa325cfdd1eb9782281a37c2b44ce8a522.tar.xz
jsrrdgraph: Rewrite data a graph commands parser.
Diffstat (limited to 'js')
-rw-r--r--js/RrdCmdLine.js426
1 files changed, 344 insertions, 82 deletions
diff --git a/js/RrdCmdLine.js b/js/RrdCmdLine.js
index 581e2f0..094596f 100644
--- a/js/RrdCmdLine.js
+++ b/js/RrdCmdLine.js
@@ -25,7 +25,6 @@
25 * @constructor 25 * @constructor
26 */ 26 */
27var RrdCmdLine = function() { 27var RrdCmdLine = function() {
28 // if (arguments.lenght === 3) // XXX
29 this.init.apply(this, arguments); 28 this.init.apply(this, arguments);
30}; 29};
31 30
@@ -37,7 +36,7 @@ RrdCmdLine.prototype = {
37 this.graph = new RrdGraph(gfx, fetch); 36 this.graph = new RrdGraph(gfx, fetch);
38 this.cmdline(line); 37 this.cmdline(line);
39 }, 38 },
40 cmdline: function(line) // FIXME 39 cmdline: function(line)
41 { 40 {
42 var i = 0; 41 var i = 0;
43 line = line.replace(/\n/g," "); 42 line = line.replace(/\n/g," ");
@@ -48,30 +47,30 @@ RrdCmdLine.prototype = {
48 var arg = lines[i]; 47 var arg = lines[i];
49 if (arg.charAt(0) === '"' && arg.charAt(arg.length-1) === '"') 48 if (arg.charAt(0) === '"' && arg.charAt(arg.length-1) === '"')
50 arg = arg.substr(1,arg.length-2); 49 arg = arg.substr(1,arg.length-2);
51 if (/^LINE[0-9.]+:/.test(arg)) { 50 if (arg.substring(0,4) === 'LINE') {
52 this.parse_line(arg); 51 this.parse_line(this.split(arg));
53 } else if (/^AREA:/.test(arg)) { 52 } else if (arg.substring(0,5) === 'AREA:') {
54 this.parse_area(arg); 53 this.parse_area(this.split(arg));
55 } else if (/^DEF:/.test(arg)) { 54 } else if (arg.substring(0,4) === 'DEF:') {
56 this.parse_def(arg); 55 this.parse_def(this.split(arg));
57 } else if (/^CDEF:/.test(arg)) { 56 } else if (arg.substring(0,5) === 'CDEF:') {
58 this.parse_cdef(arg); 57 this.parse_cdef(this.split(arg));
59 } else if (/^VDEF:/.test(arg)) { 58 } else if (arg.substring(0,5) === 'VDEF:') {
60 this.parse_vdef(arg); 59 this.parse_vdef(this.split(arg));
61 } else if (/^GPRINT:/.test(arg)) { 60 } else if (arg.substring(0,7) === 'GPRINT:') {
62 this.parse_gprint(arg); 61 this.parse_gprint(this.split(arg));
63 } else if (/^COMMENT:/.test(arg)) { 62 } else if (arg.substring(0,8) === 'COMMENT:') {
64 this.parse_comment(arg); 63 this.parse_comment(this.split(arg));
65 } else if (/^VRULE:/.test(arg)) { 64 } else if (arg.substring(0,6) === 'VRULE:') {
66 this.parse_vrule(arg); 65 this.parse_vrule(this.split(arg));
67 } else if (/^HRULE:/.test(arg)) { 66 } else if (arg.substring(0,6) === 'HRULE:') {
68 this.parse_hrule(arg); 67 this.parse_hrule(this.split(arg));
69 } else if (/^TICK:/.test(arg)) { 68 } else if (arg.substring(0,5) === 'TICK:') {
70 this.parse_tick(arg); 69 this.parse_tick(this.split(arg));
71 } else if (/^TEXTALIGN:/.test(arg)) { 70 } else if (arg.substring(0,10) === 'TEXTALIGN:') {
72 this.parse_textalign(arg); 71 this.parse_textalign(this.split(arg));
73 } else if (/^SHIFT:/.test(arg)) { 72 } else if (arg.substring(0,7) === 'SHIFT:') {
74 this.parse_shift(arg); 73 this.parse_shift(this.split(arg));
75 } else if (arg.charAt(0) === '-') { 74 } else if (arg.charAt(0) === '-') {
76 var strip = 1; 75 var strip = 1;
77 if (arg.length > 1 && arg.charAt(1) === '-') { 76 if (arg.length > 1 && arg.charAt(1) === '-') {
@@ -99,7 +98,7 @@ RrdCmdLine.prototype = {
99 } 98 }
100 i++; 99 i++;
101 } 100 }
102 var start_end = RrdTime.proc_start_end(this.graph.start_t, this.graph.end_t); // FIXME here? 101 var start_end = RrdTime.proc_start_end(this.graph.start_t, this.graph.end_t);
103 this.graph.start = start_end[0]; 102 this.graph.start = start_end[0];
104 this.graph.end = start_end[1]; 103 this.graph.end = start_end[1];
105 }, 104 },
@@ -387,79 +386,249 @@ RrdCmdLine.prototype = {
387 } 386 }
388 387
389 }, 388 },
389 split: function (line)
390 {
391 return line.replace(/:/g,'\0').replace(/\\\0/g,':').split('\0');
392 },
390 // DEF:<vname>=<rrdfile>:<ds-name>:<CF>[:step=<step>][:start=<time>][:end=<time>][:reduce=<CF>] 393 // DEF:<vname>=<rrdfile>:<ds-name>:<CF>[:step=<step>][:start=<time>][:end=<time>][:reduce=<CF>]
391 parse_def: function (line) 394 parse_def: function (args)
392 { 395 {
393 // Every character (except ':' and '\') are allowed within a value. The 396 if (args.length > 8)
394 // two exceptions must be escaped with a slash. 397 throw "Too many options for DEF: "+args.join(':');
395 var args = line.match(/(\\.|[^:\\])+/g); 398 if (args.length < 4)
396 var n = 1; 399 throw "Too few options for DEF: "+args.join(':');
397 var vnames = args[n++]; 400
398 var vname = vnames.substr(0, vnames.indexOf("=")); 401 var rrdfile;
399 var rrdfile = vnames.substr(vname.length + 1).replace(/\\(.)/g, "$1"); 402 var vname;
400 var name = args[n++]; 403 var index = args[1].indexOf('=');
401 var cf = args[n++]; 404 if (index > 0) {
405 vname = args[1].substr(0, index);
406 rrdfile = args[1].substr(index+1);
407 } else {
408 throw "Missing '=' in DEF: "+args[1];
409 }
410 rrdfile = rrdfile.replace(/\\\\/g, '\\');
411
412 var name = args[2];
413 var cf = args[3];
414
402 var step, reduce, start, end; 415 var step, reduce, start, end;
403 if (args.length > n) { 416 if (args.length > 4) {
404 for (var j = n, xlen = args.length ; j < xlen ; j++) { 417 for (var n = 4; n == args.length; n++) {
405 var opts = args[j].split("="); 418 if (args[n].substring(0,4) === "step") {
406 if (opts[0] === "step") step = opts[1]; 419 index = args[n].indexOf("=");
407 if (opts[0] === "reduce") reduce = opts[1]; 420 if (index > 0) {
408 if (opts[0] === "start") start = opts[1]; 421 step = args[n].substr(index+1);
409 if (opts[0] === "end") end = opts[1]; 422 } else {
423 throw "DEF step without value: "+args[n];
424 }
425 } else if (args[n].substring(0,6) === "reduce") {
426 index = args[n].indexOf("=");
427 if (index > 0) {
428 reduce = args[n].substr(index+1);
429 } else {
430 throw "DEF step without value: "+args[n];
431 }
432 } else if (args[n].substring(0,5) === "start") {
433 index = args[n].indexOf("=");
434 if (index > 0) {
435 start = args[n].substr(index+1);
436 } else {
437 throw "DEF step without value: "+args[n];
438 }
439 } else if (args[n].substring(0,3) === "end") {
440 index = args[n].indexOf("=");
441 if (index > 0) {
442 end = args[n].substr(index+1);
443 } else {
444 throw "DEF end without value: "+args[n];
445 }
446 } else {
447 throw "Unknown DEF option: "+args[n];
448 }
410 } 449 }
411 } 450 }
451
412 this.graph.gdes_add_def(vname, rrdfile, name, cf, step, start, end, reduce); 452 this.graph.gdes_add_def(vname, rrdfile, name, cf, step, start, end, reduce);
413 }, 453 },
414 // CDEF:vname=RPN expression 454 // CDEF:vname=RPN expression
415 parse_cdef: function (line) 455 parse_cdef: function (args)
416 { 456 {
417 var args = line.split(/:|=/); 457 var rpn, vname, index;
418 this.graph.gdes_add_cdef(args[1], args[2]); 458
459 if (args.length != 2)
460 throw "Wrong options for CDEF: "+args.join(':');
461
462 index = args[1].indexOf('=');
463 if (index > 0) {
464 vname = args[1].substr(0, index);
465 rpn = args[1].substr(index+1);
466 } else {
467 throw "Missing '=' in CDEF: "+args[1];
468 }
469
470 this.graph.gdes_add_cdef(vname, rpn);
419 }, 471 },
420 // VDEF:vname=RPN expression 472 // VDEF:vname=RPN expression
421 parse_vdef: function (line) 473 parse_vdef: function (args)
422 { 474 {
423 var args = line.split(/:|=/); 475 var rpn, vname, index;
424 this.graph.gdes_add_vdef(args[1], args[2]); 476
477 if (args.length != 2)
478 throw "Wrong options for VDEF: "+args.join(':');
479
480 index = args[1].indexOf('=');
481 if (index > 0) {
482 vname = args[1].substr(0, index);
483 rpn = args[1].substr(index+1);
484 } else {
485 throw "Missing '=' in VDEF: "+args[1];
486 }
487
488 this.graph.gdes_add_vdef(vname, rpn);
425 }, 489 },
426 // SHIFT:vname:offset 490 // SHIFT:vname:offset
427 parse_shift: function (line) 491 parse_shift: function (args)
428 { 492 {
429 var args = line.split(':'); 493 if (args.length != 3)
494 throw "Wrong options for SHIFT: "+args.join(':');
495
430 this.graph.gdes_add_shift(args[1], args[2]); 496 this.graph.gdes_add_shift(args[1], args[2]);
431 }, 497 },
432 // LINE[width]:value[#color][:[legend][:STACK]][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] 498 // LINE[width]:value[#color][:[legend][:STACK]][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]]
433 parse_line: function (line) 499 parse_line: function (args)
434 { 500 {
435 var args = line.split(/#|:/); 501 if (args.length > 6)
436 var width = parseFloat(args[0].substr(4)); 502 throw "Too many options for LINE: "+args.join(':');
437 var stack = args[4] === 'STACK' ? true : undefined; 503 if (args.length < 2)
438 var color = this.graph.parse_color(args[2]); 504 throw "Too few options for LINE: "+args.join(':');
439 this.graph.gdes_add_line(width, args[1], this.graph.color2rgba(color), args[3], stack); 505
506 var width = 1;
507 if (args[0].length > 4)
508 width = parseFloat(args[0].substr(4));
509
510 var color = undefined;
511 var value = args[1];
512 var index = args[1].indexOf('#');
513 if (index > 0) {
514 value = args[1].substr(0, index);
515 color = this.graph.parse_color(args[1].substr(index+1));
516 color = this.graph.color2rgba(color);
517 }
518
519 var stack = false;
520 var legend = undefined;
521 var dashes = undefined;
522 var dash_offset = undefined;
523 if (args.length == 3 && args[2] === 'STACK') {
524 stack = true;
525 } else if (args.length >= 3) {
526 legend = args[2];
527 for (var n = 3; n < args.length; n++) {
528 if (args[n] === 'STACK') {
529 stack = true;
530 } else if (args[n].substring(0,6) === 'dashes') {
531 index = args[n].indexOf('=');
532 if (index > 0) {
533 dashes = args[n].substr(index+1).split(',');
534 } else {
535 dashes = [5];
536 }
537 } else if (args[n].substring(0,11) === 'dash-offset') {
538 index = args[n].indexOf('=');
539 if (index > 0) {
540 dash_offset = args[n].substr(index+1);
541 } else {
542 throw "LINE dash-offset without value: "+args[n];
543 }
544 } else {
545 throw "Unknown LINE option: "+args[n];
546 }
547 }
548 }
549
550 if (legend != undefined && legend.length === 0)
551 legend = undefined;
552
553 this.graph.gdes_add_line(width, value, color, legend, stack, dashes, dash_offset);
440 }, 554 },
441 // AREA:value[#color][:[legend][:STACK]] 555 // AREA:value[#color][:[legend][:STACK]]
442 parse_area: function (line) 556 parse_area: function (args)
443 { 557 {
444 var args = line.split(/#|:/); 558 if (args.length > 4)
445 var stack = args[4] === 'STACK' ? true : undefined; 559 throw "Too many options for AREA: "+args.join(':');
446 var color = this.graph.parse_color(args[2]); 560 if (args.length < 2)
447 this.graph.gdes_add_area(args[1], this.graph.color2rgba(color), args[3], stack); 561 throw "Too few options for AREA: "+args.join(':');
562
563 var color = undefined;
564 var value = args[1];
565 var index = args[1].indexOf('#');
566 if (index > 0) {
567 value = args[1].substr(0, index);
568 color = this.graph.parse_color(args[1].substr(index+1));
569 color = this.graph.color2rgba(color);
570 }
571
572 var legend = undefined;
573 var stack = false;
574 if (args.length == 3) {
575 if (args[2] === 'STACK') {
576 stack = true;
577 } else {
578 legend = args[2];
579 }
580 } else if (args.length == 4) {
581 legend = args[2];
582 if (args[3] === 'STACK') {
583 stack = true;
584 } else {
585 throw "Unknown AREA option: "+args[3];
586 }
587 }
588
589 if (legend != undefined && legend.length === 0)
590 legend = undefined;
591
592 this.graph.gdes_add_area(value, color, legend, stack);
448 }, 593 },
449 // TICK:vname#rrggbb[aa][:fraction[:legend]] 594 // TICK:vname#rrggbb[aa][:fraction[:legend]]
450 parse_tick: function (line) 595 parse_tick: function (args)
451 { 596 {
452 var args = line.split(/:|#/); 597 if (args.length > 4)
453 var color = this.graph.parse_color(args[2]); 598 throw "Too many options for TICK: "+args.join(':');
454 this.graph.gdes_add_tick(args[1], this.graph.color2rgba(color), args[3], args[4]); 599 if (args.length < 2)
600 throw "Too few options for TICK: "+args.join(':');
601
602 var color = undefined;
603 var vname = args[1];
604 var index = args[1].indexOf('#');
605 if (index > 0) {
606 vname = args[1].substr(0, index);
607 color = this.graph.parse_color(args[1].substr(index+1));
608 color = this.graph.color2rgba(color);
609 }
610
611 var faction = undefined;
612 if (args.length == 3)
613 fraction = args[2];
614
615 var legend = undefined;
616 if (args.length == 4)
617 legend = args[3];
618
619 if (legend != undefined && legend.length === 0)
620 legend = undefined;
621
622 this.graph.gdes_add_tick(vname, color, fraction, legend);
455 }, 623 },
456 // GPRINT:vname:format 624 // GPRINT:vname:format[:strftime]
457 parse_gprint: function(line) 625 // GPRINT:vname:cf:format[:strftime]
626 parse_gprint: function(args)
458 { 627 {
459 var args = line.split(':');
460 var strftime = false; 628 var strftime = false;
461 var vname = args[1]; 629 var vname = args[1];
462 var cf = args[2]; 630 var cf = args[2];
631
463 var format = ""; 632 var format = "";
464 if (args.length > 3) { 633 if (args.length > 3) {
465 var m=0; 634 var m=0;
@@ -479,27 +648,120 @@ RrdCmdLine.prototype = {
479 this.graph.gdes_add_gprint(vname, cf, format, strftime); 648 this.graph.gdes_add_gprint(vname, cf, format, strftime);
480 }, 649 },
481 //COMMENT:text 650 //COMMENT:text
482 parse_comment: function (line) 651 parse_comment: function (args)
483 { 652 {
484 var index = line.indexOf(':'); 653 if (args.length < 2)
485 this.graph.gdes_add_comment(line.substr(index+1)); 654 throw "Wrong options for COMMENT: "+args.join(':');
655
656 if (args.length > 2) {
657 args.shift();
658 this.graph.gdes_add_comment(args.join(':'));
659 } else {
660 this.graph.gdes_add_comment(args[1]);
661 }
486 }, 662 },
487 // TEXTALIGN:{left|right|justified|center} 663 // TEXTALIGN:{left|right|justified|center}
488 parse_textalign: function (line) 664 parse_textalign: function (args)
489 { 665 {
490 var index = line.indexOf(':'); 666 if (args.length != 2)
491 this.graph.gdes_add_textalign(line.substr(index+1)); 667 throw "Wrong options for TESTALIGN: "+args.join(':');
668
669 this.graph.gdes_add_textalign(args[1]);
492 }, 670 },
493 // VRULE:time#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] 671 // VRULE:time#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]]
494 parse_vrule: function (line) 672 parse_vrule: function (args)
495 { 673 {
496 var args = line.split(/:|#/); 674 if (args.length > 5)
497 this.graph.gdes_add_vrule(args[1], '#'+args[2], args[3]); 675 throw "Too many options for VRULE: "+args.join(':');
676 if (args.length < 2)
677 throw "Too few options for VRULE: "+args.join(':');
678
679 var color = undefined;
680 var time = args[1];
681 var index = args[1].indexOf('#');
682 if (index > 0) {
683 time = args[1].substr(0, index);
684 color = this.graph.parse_color(args[1].substr(index+1));
685 color = this.graph.color2rgba(color);
686 }
687
688 var legend = undefined;
689 var dashes = undefined;
690 var dash_offset = undefined;
691 if (args.length >= 3) {
692 legend = args[2];
693 for (var n = 3; n < args.length; n++) {
694 if (args[n].substring(0,6) === 'dashes') {
695 index = args[n].indexOf('=');
696 if (index > 0) {
697 dashes = args[n].substr(index+1).split(',');
698 } else {
699 dashes = [5];
700 }
701 } else if (args[n].substring(0,11) === 'dash-offset') {
702 index = args[n].indexOf('=');
703 if (index > 0) {
704 dash_offset = args[n].substr(index+1);
705 } else {
706 throw "VRULE dash-offset without value: "+args[n];
707 }
708 } else {
709 throw "Unknown VRULE option: "+args[n];
710 }
711 }
712 }
713
714 if (legend != undefined && legend.length === 0)
715 legend = undefined;
716
717 this.graph.gdes_add_vrule(time, color, legend, dashes, dash_offset);
498 }, 718 },
499 // HRULE:value#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] 719 // HRULE:value#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]]
500 parse_hrule: function (line) 720 parse_hrule: function (args)
501 { 721 {
502 var args = line.split(/:|#/); 722 if (args.length > 5)
503 this.graph.gdes_add_hrule(args[1], '#'+args[2], args[3]); 723 throw "Too many options for HRULE: "+args.join(':');
724 if (args.length < 2)
725 throw "Too few options for HRULE: "+args.join(':');
726
727 var color = undefined;
728 var value = args[1];
729 var index = args[1].indexOf('#');
730 if (index > 0) {
731 value = args[1].substr(0, index);
732 color = this.graph.parse_color(args[1].substr(index+1));
733 color = this.graph.color2rgba(color);
734 }
735
736 var legend = undefined;
737 var dashes = undefined;
738 var dash_offset = undefined;
739 if (args.length >= 3) {
740 legend = args[2];
741 for (var n = 3; n < args.length; n++) {
742 if (args[n].substring(0,6) === 'dashes') {
743 index = args[n].indexOf('=');
744 if (index > 0) {
745 dashes = args[n].substr(index+1).split(',');
746 } else {
747 dashes = [5];
748 }
749 } else if (args[n].substring(0,11) === 'dash-offset') {
750 index = args[n].indexOf('=');
751 if (index > 0) {
752 dash_offset = args[n].substr(index+1);
753 } else {
754 throw "HRULE dash-offset without value: "+args[n];
755 }
756 } else {
757 throw "Unknown HRULE option: "+args[n];
758 }
759 }
760 }
761
762 if (legend != undefined && legend.length === 0)
763 legend = undefined;
764
765 this.graph.gdes_add_hrule(value, color, legend, dashes, dash_offset);
504 } 766 }
505}; 767};