Commit db69e5a792bf5d1f5d1af265d9fac0924c6de322

Authored by Goutte
1 parent 8d4f8bc2

Continue implementing THE GREAT REFACTOR to try to decrease the CPU load of Catalogs.

Showing 2 changed files with 74 additions and 51 deletions   Show diff stats
web/static/js/main.js
1 -// Generated by LiveScript 1.5.0 1 +// ES6
  2 +//
  3 +//
  4 +
  5 +/**
  6 + *
  7 + * All the javascript code for canvases is in this file,
  8 + * and inline scripts in templates, such as in `home.html.jinja2` contain the
  9 + * DOM listeners (the UI stuff, not the business logic, which is here).
  10 + *
  11 + * Note: We use Promises and ES6.
  12 + *
  13 + * D3.js
  14 + * -----
  15 + *
  16 + * You also WILL NEED d3js v4 documentation : https://d3js.org/
  17 + * We're using a custom build of 4.9.1, one line changed, see d3-custom.js
  18 + * Event bubbling cannot trigger two rects unless we make an event dispatcher,
  19 + * and d3's brush is stopping propagation, as it should by default.
  20 + *
  21 + *
  22 + * Code is weird, with ref$
  23 + * ------------------------
  24 + *
  25 + * Lots of machine-generated micro-optimizations. Most are unconsequential.
  26 + * The complex exporting pattern is ~ technical debt. Simplify it if you can.
  27 + */
  28 +
2 (function () { 29 (function () {
3 let Target; 30 let Target;
4 - let SpaceWeather; 31 + // let SpaceWeather;
5 let TimeSeries; 32 let TimeSeries;
6 let Orbits; 33 let Orbits;
7 - const out$ = typeof exports != 'undefined' && exports || this;  
8 - console.debug(out$); 34 + const global = typeof exports !== 'undefined' && exports || this;
9 const GOLDEN_RATIO = 2 / (1 + Math.sqrt(5)); 35 const GOLDEN_RATIO = 2 / (1 + Math.sqrt(5));
10 Target = ((() => { 36 Target = ((() => {
11 // Target.displayName = 'Target'; 37 // Target.displayName = 'Target';
@@ -20,11 +46,11 @@ @@ -20,11 +46,11 @@
20 46
21 return Target; 47 return Target;
22 })()); 48 })());
23 - out$.SpaceWeather = SpaceWeather = ((() => { 49 +
  50 +
  51 + global.SpaceWeather = ((() => {
24 // "The main app, instanciated from an inline script.\nIt defaults to an interval starting two months ago, and ending in a month.\n(both at midnight)"; 52 // "The main app, instanciated from an inline script.\nIt defaults to an interval starting two months ago, and ending in a month.\n(both at midnight)";
25 // SpaceWeather.displayName = 'SpaceWeather'; 53 // SpaceWeather.displayName = 'SpaceWeather';
26 - // let API_TIME_FORMAT;  
27 - // let INPUT_TIME_FORMAT;  
28 // const prototype = SpaceWeather.prototype; 54 // const prototype = SpaceWeather.prototype;
29 // const constructor = SpaceWeather; 55 // const constructor = SpaceWeather;
30 const API_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ss"; 56 const API_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ss";
@@ -32,21 +58,19 @@ @@ -32,21 +58,19 @@
32 58
33 class SpaceWeather { 59 class SpaceWeather {
34 constructor(configuration) { 60 constructor(configuration) {
35 - let configs;  
36 - let res$;  
37 - let k;  
38 - const this$ = this; 61 + const that = this;
39 this.configuration = configuration; 62 this.configuration = configuration;
40 - console.info("©2017\n _ _ _ _ ____\n | | | | ___| (_) ___ | _ \\ _ __ ___ _ __ __ _\n | |_| |/ _ \\ | |/ _ \\| |_) | '__/ _ \\| '_ \\ / _` |\n | _ | __/ | | (_) | __/| | | (_) | |_) | (_| |\n |_| |_|\\___|_|_|\\___/|_|_ |_|_ \\___/| .__/ \\__,_|\n | |__ _ _ / ___| _ \\| _ \\| _ \\_|\n | '_ \\| | | | | | | | | | |_) | |_) |\n | |_) | |_| | | |___| |_| | __/| __/\n |_.__/ \\__, | \\____|____/|_| |_|\n |___/\n\nThe full source of this website is available at :\nhttps://gitlab.irap.omp.eu/CDPP/SPACEWEATHERONLINE"); 63 + this.parameters = {};
41 this.targets = {}; 64 this.targets = {};
42 - res$ = [];  
43 - for (k in this.configuration.targets) {  
44 - res$.push(this.configuration.targets[k]); 65 + console.info("©2017\n _ _ _ _ ____\n | | | | ___| (_) ___ | _ \\ _ __ ___ _ __ __ _\n | |_| |/ _ \\ | |/ _ \\| |_) | '__/ _ \\| '_ \\ / _` |\n | _ | __/ | | (_) | __/| | | (_) | |_) | (_| |\n |_| |_|\\___|_|_|\\___/|_|_ |_|_ \\___/| .__/ \\__,_|\n | |__ _ _ / ___| _ \\| _ \\| _ \\_|\n | '_ \\| | | | | | | | | | |_) | |_) |\n | |_) | |_| | | |___| |_| | __/| __/\n |_.__/ \\__, | \\____|____/|_| |_|\n |___/\n\nThe full source of this website is available at :\nhttps://gitlab.irap.omp.eu/CDPP/SPACEWEATHERONLINE");
  66 +
  67 + let targets_configs = [];
  68 + for (let k in this.configuration.targets) {
  69 + targets_configs.push(this.configuration.targets[k]);
45 } 70 }
46 - configs = res$;  
47 - configs.forEach(target_config => this$.addTarget(new Target(target_config.slug, target_config.name, target_config)));  
48 - this.parameters = {};  
49 - this.configuration['parameters'].forEach(p => this$.parameters[p['id']] = p); 71 + targets_configs.forEach(tc => that.addTarget(new Target(tc.slug, tc.name, tc)));
  72 +
  73 + this.configuration['parameters'].forEach(p => that.parameters[p['id']] = p);
50 this.orbits = null; 74 this.orbits = null;
51 this.time_series = []; 75 this.time_series = [];
52 } 76 }
@@ -218,7 +242,7 @@ @@ -218,7 +242,7 @@
218 } 242 }
219 243
220 loadData(target_slug, started_at, stopped_at) { 244 loadData(target_slug, started_at, stopped_at) {
221 - "Load the data as CSV for the specified target and interval,\nand return it in a Promise."; 245 + // "Load the data as CSV for the specified target and interval,\nand return it in a Promise.";
222 let sw; 246 let sw;
223 sw = this; 247 sw = this;
224 return new Promise((resolve, reject) => { 248 return new Promise((resolve, reject) => {
@@ -268,7 +292,7 @@ @@ -268,7 +292,7 @@
268 } 292 }
269 293
270 loadAndCreatePlots(started_at, stopped_at) { 294 loadAndCreatePlots(started_at, stopped_at) {
271 - "started_at: moment(.js) datetime object\nstopped_at: moment(.js) datetime object"; 295 + // "started_at: moment(.js) datetime object\nstopped_at: moment(.js) datetime object";
272 let targets; 296 let targets;
273 let res$; 297 let res$;
274 let k; 298 let k;
@@ -406,6 +430,7 @@ @@ -406,6 +430,7 @@
406 results$.push(p); 430 results$.push(p);
407 } 431 }
408 } 432 }
  433 +
409 return results$; 434 return results$;
410 } 435 }
411 436
@@ -420,6 +445,7 @@ @@ -420,6 +445,7 @@
420 return ts.show(); 445 return ts.show();
421 } 446 }
422 }); 447 });
  448 +
423 return this; 449 return this;
424 } 450 }
425 451
@@ -433,6 +459,7 @@ @@ -433,6 +459,7 @@
433 return ts.hide(); 459 return ts.hide();
434 } 460 }
435 }); 461 });
  462 +
436 return this; 463 return this;
437 } 464 }
438 465
@@ -516,6 +543,7 @@ @@ -516,6 +543,7 @@
516 this.time_series.forEach(ts => ts.resetZoom()); 543 this.time_series.forEach(ts => ts.resetZoom());
517 this.orbits.resetZoom(); 544 this.orbits.resetZoom();
518 this.setStartAndStop(this.started_at, this.stopped_at); 545 this.setStartAndStop(this.started_at, this.stopped_at);
  546 +
519 return this; 547 return this;
520 } 548 }
521 549
@@ -525,6 +553,7 @@ @@ -525,6 +553,7 @@
525 this.current_stopped_at = stopped_at; 553 this.current_stopped_at = stopped_at;
526 $("#started_at").val(started_at.format(INPUT_TIME_FORMAT)); 554 $("#started_at").val(started_at.format(INPUT_TIME_FORMAT));
527 $("#stopped_at").val(stopped_at.format(INPUT_TIME_FORMAT)); 555 $("#stopped_at").val(stopped_at.format(INPUT_TIME_FORMAT));
  556 +
528 return this; 557 return this;
529 } 558 }
530 } 559 }
@@ -535,17 +564,16 @@ @@ -535,17 +564,16 @@
535 ///////////////////////////////////////////////////////////////////////////// 564 /////////////////////////////////////////////////////////////////////////////
536 //// TIME SERIES //////////////////////////////////////////////////////////// 565 //// TIME SERIES ////////////////////////////////////////////////////////////
537 566
538 - out$.TimeSeries = TimeSeries = ((() => { 567 + global.TimeSeries = TimeSeries = ((() => {
539 const RATIO = GOLDEN_RATIO ** 4; 568 const RATIO = GOLDEN_RATIO ** 4;
540 569
541 class TimeSeries { 570 class TimeSeries {
542 constructor(parameter, title, target, data, visible, container, options) { 571 constructor(parameter, title, target, data, visible, container, options) {
543 - let now;  
544 - let res$;  
545 - let i$;  
546 - let ref$;  
547 - let len$;  
548 - let d; 572 + this.onBrushEnd = this.onBrushEnd.bind(this);
  573 + this.onDoubleClick = this.onDoubleClick.bind(this);
  574 + this.onMouseOut = this.onMouseOut.bind(this);
  575 + this.onMouseOver = this.onMouseOver.bind(this);
  576 + this.onMouseMove = this.onMouseMove.bind(this);
549 this.parameter = parameter; 577 this.parameter = parameter;
550 this.title = title; 578 this.title = title;
551 this.target = target; 579 this.target = target;
@@ -553,20 +581,17 @@ @@ -553,20 +581,17 @@
553 this.visible = visible; 581 this.visible = visible;
554 this.container = container; 582 this.container = container;
555 this.options = options || {}; 583 this.options = options || {};
556 - this.onBrushEnd = bind$(this, 'onBrushEnd', this.prototype);  
557 - this.onDoubleClick = bind$(this, 'onDoubleClick', this.prototype);  
558 - this.onMouseOut = bind$(this, 'onMouseOut', this.prototype);  
559 - this.onMouseOver = bind$(this, 'onMouseOver', this.prototype);  
560 - this.onMouseMove = bind$(this, 'onMouseMove', this.prototype);  
561 - now = moment();  
562 - res$ = [];  
563 - for (i$ = 0, len$ = (ref$ = this.data).length; i$ < len$; ++i$) {  
564 - d = ref$[i$]; 584 + let now = moment();
  585 + const dataLength = this.data.length;
  586 + const predictiveData = [];
  587 + let d;
  588 + for (let i = 0; i < dataLength; ++i) {
  589 + d = this.data[i];
565 if (moment(d.x) >= now) { 590 if (moment(d.x) >= now) {
566 - res$.push(d); 591 + predictiveData.push(d);
567 } 592 }
568 } 593 }
569 - this.predictiveData = res$; 594 + this.predictiveData = predictiveData;
570 this.init(); 595 this.init();
571 } 596 }
572 597
@@ -957,7 +982,6 @@ @@ -957,7 +982,6 @@
957 let d; 982 let d;
958 let xx; 983 let xx;
959 let yy; 984 let yy;
960 - let mirrored;  
961 let dx; 985 let dx;
962 let transform; 986 let transform;
963 i = this.bisectDate(this.data, x0, 1); 987 i = this.bisectDate(this.data, x0, 1);
@@ -970,7 +994,7 @@ @@ -970,7 +994,7 @@
970 d = x0 - d0.x > d1.x - x0 ? d1 : d0; 994 d = x0 - d0.x > d1.x - x0 ? d1 : d0;
971 xx = this.xScale(d.x); 995 xx = this.xScale(d.x);
972 yy = this.yScale(d.y); 996 yy = this.yScale(d.y);
973 - mirrored = this.plotWidth != null && xx > this.plotWidth / 2; 997 + const mirrored = this.plotWidth != null && xx > this.plotWidth / 2;
974 dx = 8; 998 dx = 8;
975 if (mirrored) { 999 if (mirrored) {
976 dx = -1 * dx; 1000 dx = -1 * dx;
@@ -990,7 +1014,12 @@ @@ -990,7 +1014,12 @@
990 TimeSeries.prototype.timeFormat = d3.utcFormat("%Y-%m-%d %H:%M"); 1014 TimeSeries.prototype.timeFormat = d3.utcFormat("%Y-%m-%d %H:%M");
991 return TimeSeries; 1015 return TimeSeries;
992 })()); 1016 })());
993 - out$.Orbits = Orbits = ((() => { 1017 +
  1018 +
  1019 + ///////////////////////////////////////////////////////////////////////////
  1020 + //// ORBITS PLOT //////////////////////////////////////////////////////////
  1021 +
  1022 + global.Orbits = Orbits = ((() => {
994 // "View of the solar system from above, with orbits segments for selected time\ninterval, from real data."; 1023 // "View of the solar system from above, with orbits segments for selected time\ninterval, from real data.";
995 // Orbits.displayName = 'Orbits'; 1024 // Orbits.displayName = 'Orbits';
996 // const prototype = Orbits.prototype; 1025 // const prototype = Orbits.prototype;
@@ -1041,11 +1070,11 @@ @@ -1041,11 +1070,11 @@
1041 } 1070 }
1042 1071
1043 initOrbiter(slug, config, data) { 1072 initOrbiter(slug, config, data) {
  1073 + const thisOrbit = this;
1044 let orbit_ellipse; 1074 let orbit_ellipse;
1045 let orbiter; 1075 let orbiter;
1046 let orbit_line; 1076 let orbit_line;
1047 let orbit_section; 1077 let orbit_section;
1048 - const this$ = this;  
1049 this.data[slug] = data; 1078 this.data[slug] = data;
1050 this.orbiters[slug] = config; 1079 this.orbiters[slug] = config;
1051 if (data.length) { 1080 if (data.length) {
@@ -1060,7 +1089,7 @@ @@ -1060,7 +1089,7 @@
1060 orbit_ellipse = this.plotWrapper.append("svg:ellipse").classed('orbit orbit_ellipse', true); 1089 orbit_ellipse = this.plotWrapper.append("svg:ellipse").classed('orbit orbit_ellipse', true);
1061 orbiter = this.plotWrapper.append("svg:image").attr('xlink:href', config['img']).attr('width', '32px').attr('height', '32px'); 1090 orbiter = this.plotWrapper.append("svg:image").attr('xlink:href', config['img']).attr('width', '32px').attr('height', '32px');
1062 orbiter.append('svg:title').text(config.name); 1091 orbiter.append('svg:title').text(config.name);
1063 - orbit_line = d3.line().x(d => this$.xScale(d.y)).y(d => this$.yScale(d.x)); 1092 + orbit_line = d3.line().x(d => thisOrbit.xScale(d.y)).y(d => thisOrbit.yScale(d.x));
1064 orbit_section = this.plotWrapper.append('path').datum(data).classed('orbit orbit_section', true); 1093 orbit_section = this.plotWrapper.append('path').datum(data).classed('orbit orbit_section', true);
1065 this.orbitersElements[slug] = { 1094 this.orbitersElements[slug] = {
1066 orbiter, 1095 orbiter,
@@ -1316,10 +1345,4 @@ @@ -1316,10 +1345,4 @@
1316 Orbits.prototype.bisectDate = d3.bisector(d => d.t).left; 1345 Orbits.prototype.bisectDate = d3.bisector(d => d.t).left;
1317 return Orbits; 1346 return Orbits;
1318 })()); 1347 })());
1319 -  
1320 - function bind$(obj, key, target) {  
1321 - return function () {  
1322 - return (target || obj)[key].apply(obj, arguments)  
1323 - };  
1324 - }  
1325 }).call(this); 1348 }).call(this);
web/view/home.html.jinja2
@@ -607,7 +607,7 @@ @@ -607,7 +607,7 @@
607 <script type="application/javascript" src="{{ static('js/vendor/d3-custom.js') }}"></script> 607 <script type="application/javascript" src="{{ static('js/vendor/d3-custom.js') }}"></script>
608 <script type="application/javascript" src="{{ static('js/vendor/moment.min.js') }}"></script> 608 <script type="application/javascript" src="{{ static('js/vendor/moment.min.js') }}"></script>
609 <script type="application/javascript" src="{{ static('js/vendor/samp.js') }}"></script> 609 <script type="application/javascript" src="{{ static('js/vendor/samp.js') }}"></script>
610 -<script type="application/javascript" src="{{ static('js/swapp.js') }}"></script> 610 +<script type="application/javascript" src="{{ static('js/main.js') }}"></script>
611 <script type="application/javascript"> 611 <script type="application/javascript">
612 612
613 var configuration = { 613 var configuration = {