newCommon.js 6.4 KB

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