newCommon.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const DEFAULT = 2;
  2. const ONDEMAND = 4;
  3. const AUTO = 8;
  4. const FAST = 16;
  5. const PASSIVE = 32;
  6. /** *
  7. * DataObject DO
  8. * @param cname
  9. * @param serverJsonObj
  10. * @constructor
  11. */
  12. const DataObject = function (cname, serverJsonObj) {
  13. this.ctlName = cname;
  14. this.jsonObj = serverJsonObj;
  15. this.getVal = function (key) {
  16. return this.sonObj[key];
  17. };
  18. this.serVal = function (key, val) {
  19. this.jsonObj[key] = val;
  20. };
  21. };
  22. /** *
  23. * DataObjectList
  24. * @param name
  25. * @constructor
  26. */
  27. const DataObjectList = function (name) {
  28. this.ctlName = name;
  29. this.list = null;
  30. this.isSorted = false;
  31. this.preSort = [];
  32. this.comparisonBasis = null;
  33. this.isSorting = false;
  34. /** *
  35. * sorting
  36. * @param comparator (a,b) =====> a - b
  37. */
  38. this.sortBy = function (comparator) {
  39. this.isSorted = true;
  40. this.comparisonBasis = comparator;
  41. this.preSort = [];
  42. for (let i = 0; i < this.list.length; i++) {
  43. this.preSort.push(this.list[i]);
  44. }
  45. if(this.isSorting){
  46. return;
  47. }
  48. this.isSorting = true;
  49. this.quickSort(this.preSort, 0, this.preSort.length - 1, this.comparisonBasis, this);
  50. this.isSorting = false;
  51. };
  52. this.clearData = function () {
  53. this.isSorted = false;
  54. this.preSort = null;
  55. this.list = null;
  56. };
  57. this.len = function () {
  58. if (this.list == null) return 0;
  59. return this.list.length;
  60. };
  61. this.mergeJsonData = function (data, extdata) {
  62. this.list = [];
  63. for (let i = 0; i < data.length; i++) this.addObject(data[i]);
  64. if (this.comparisonBasis != null) this.sortBy(this.comparisonBasis);
  65. };
  66. this.addObject = function (jsonObj) {
  67. this.list.push(new DataObject(this.ctlName, jsonObj));
  68. };
  69. this.assignJsonData = function (data) {
  70. this.clearData();
  71. this.mergeJsonData(data);
  72. };
  73. this.getRaw = function (index) {
  74. if (this.list == null) return null;
  75. return this.list[index];
  76. };
  77. this.getSorted = function (index) {
  78. if (this.list == null) return null;
  79. if (!this.isSorted) return this.getRaw(index);
  80. return this.preSort[index];
  81. };
  82. this.quickSort = function (a, left, right, comp, obj) {
  83. var inputVar = a;
  84. if (left > right) { // 一定要有这个判断,因为有递归left和i-1,若没有这个判断条件,该函数会进入无限死错位递归
  85. return;
  86. }
  87. let i = left;
  88. let j = right;
  89. const pivot = inputVar[left]; // 基准总是取序列开头的元素
  90. while (i !== j) {
  91. while ((comp(inputVar[j], pivot) > 0) && i < j) { j--; }
  92. while ((comp(inputVar[i], pivot) <= 0) && i < j) { i++; }
  93. if (i < j) { // 如果i==j跳出外层while
  94. const t = inputVar[i];
  95. inputVar[i] = inputVar[j];
  96. inputVar[j] = t;
  97. }
  98. }
  99. inputVar[left] = inputVar[i];// 交换基准数和k位置上的数
  100. inputVar[i] = pivot;
  101. obj.quickSort(inputVar, left, i - 1, comp, obj);
  102. obj.quickSort(inputVar, i + 1, right, comp, obj);
  103. };
  104. };
  105. /** *
  106. * DOService
  107. * @param controlName
  108. * @constructor
  109. */
  110. const DataObjectService = function (controlName, syncType, extparm) {
  111. this.ctlName = controlName;
  112. // 2=default 4=on-demand, 8=auto, 16:fast, 32=passive
  113. this.syncType = syncType;
  114. this.extParm = extparm;
  115. this.isDefault = ((syncType & DEFAULT) !== 0);
  116. this.isOnDemand = ((syncType & ONDEMAND) !== 0);
  117. this.isAuto = ((syncType & AUTO) !== 0);
  118. this.isFast = ((syncType & FAST) !== 0);
  119. this.isPassive = ((syncType & PASSIVE) !== 0);
  120. this.failedCount = 0;
  121. this.list = new DataObjectList(controlName);
  122. this.autoUpdateTimer = 0;
  123. this.autoInterval = 1000;
  124. this.callerThis = this;
  125. this.setUpInterval = function () {
  126. // URL + exparm
  127. if (this.autoUpdateTimer !== 0) clearInterval(this.autoUpdateTimer);
  128. this.autoUpdateTimer = setInterval(this.selfUpdateTick, this.autoInterval, this);
  129. setTimeout(this.selfUpdateTick, 1000, this);
  130. };
  131. this.init = function () {
  132. this.appUrl = `${controlName}/list${extparm}`;
  133. if (!this.isFast || this.isOnDemand) {
  134. this.autoInterval = 300;
  135. }
  136. if (this.isFast) {
  137. this.appUrl = `${controlName}/get${extparm}`;
  138. }
  139. if (this.isAuto) {
  140. // timer
  141. this.setUpInterval();
  142. }
  143. };
  144. this.stopTimers = function () {
  145. clearInterval(this.autoUpdateTimer);
  146. this.autoUpdateTimer = 0;
  147. };
  148. /** *
  149. * Automatica Update Tick, where obj is a replacement for "this" where "this" might be incorrect
  150. * @param obj : DataObjectService
  151. */
  152. this.selfUpdateTick = function (obj) {
  153. get_data(obj.appUrl, obj.onDataReceived, obj);
  154. };
  155. this.onDemandUpdate = function (obj) {
  156. get_data(obj.appUrl, obj.onDataReceived, obj);
  157. };
  158. this.onDataReceived = function (data, sta) {
  159. const obj = this.callerThis;
  160. if (obj === undefined) { console.error('Cannot get callerThis in onDataReceived, please check'); console.error(this); }
  161. const output = first_parse(this, data);
  162. if (output.list === undefined || output.list.length === 0) {
  163. obj.failedCount++;
  164. if (obj.failedCount > 100) {
  165. obj.failedCount = 0;
  166. obj.stopTimers();
  167. console.log('[排行榜返回为空git clone https://github.com/pentaho/mondrian-tck.git次数过多],暂停刷新');
  168. }
  169. if (obj.failedCount === 2) {
  170. send_alert(`排行榜多次返回没有数据,请尝试重新加载! [${obj.appUrl}]`);
  171. }
  172. } else {
  173. obj.list.assignJsonData(output.list);
  174. }
  175. };
  176. this.getList = function () {
  177. return this.list;
  178. };
  179. this.passiveUpdate = function (data) {
  180. this.failedCount = 0;
  181. this.list.assignJsonData(data);
  182. };
  183. this.init();
  184. };
  185. const SinglePage = function () {
  186. this.modules = [];
  187. };
  188. const Module = function (chartContainerSelector, dataCtlName) {
  189. this.selector = chartContainerSelector;
  190. this.dataCtl = dataCtlName;
  191. this.activeData = null;
  192. this.lastActive = 0;
  193. this.tickInterval = 1000;
  194. /** *
  195. * bindData to UI
  196. * @param data DataObjectList
  197. */
  198. this.setData = function (data) {
  199. if (data.ctlName === this.dataCtl) {
  200. if(showNetwork) console.log(`-------- Module ${this.selector} got data ----------- `);
  201. this.activeData = data;
  202. this.drawData(this.activeData);
  203. } else {
  204. if(showNetwork) console.log(`-------- Module ${this.selector} cannot use data of ${data.ctlName} ----------- `);
  205. }
  206. };
  207. this.init = function () {
  208. // do init
  209. this.setupInterval();
  210. };
  211. this.tick = function () {
  212. // do tick
  213. };
  214. this.setupInterval = function () {
  215. // do tick
  216. if (this.tickInterval !== 0) clearInterval(this.tickInterval);
  217. this.tickInterval = setInterval(this.tick, this.tickInterval, this);
  218. };
  219. this.drawData = function () {
  220. // do drawData
  221. };
  222. };