newCommon.js 6.5 KB

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