Guest User

Untitled

a guest
Nov 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. import Promise from 'bluebird';
  2. import moment from 'moment';
  3. import StatisticSecondModule from './model';
  4. import powerMeterModel from './../powermeter/model';
  5.  
  6. const StatisticSecond = StatisticSecondModule.StatisticSecond;
  7. const StatisticHour = StatisticSecondModule.StatisticHour;
  8. const powerMeter = powerMeterModel.PowerMeter;
  9.  
  10. export const StatisticController = {};
  11. export default { StatisticController };
  12.  
  13. // ========Menerima data dari power meter===========
  14. StatisticController.create = async (req, res, next) => {
  15. const { dev_id, amp, volt } = req.query;
  16. const ampArray = amp.match(/.{1,2}/g);
  17. const voltArray = volt.match(/.{1,2}/g);
  18. let dataPower = await powerMeter.byCode(dev_id);
  19. dataPower = dataPower[0];
  20. const dataInsert = [];
  21. const dataHours = {
  22. watt: 0,
  23. rate: 0,
  24. power_meter_id: dataPower.id,
  25. };
  26. for (let i = 0; i < ampArray.length; i += 1) {
  27. let voltvalue = 0;
  28. const ampString = ampArray[i].toString(16);
  29. const ampvalue = (parseInt(ampString, 16)) / 10;
  30. if (voltArray[i]) {
  31. const voltString = voltArray[i].toString(16);
  32. voltvalue = parseInt(voltString, 16);
  33. }
  34. const watt = (ampvalue * voltvalue * dataPower.power_factor) / 1000;
  35. const dataNew = {
  36. power_meter_id: dataPower.id,
  37. amp: ampvalue,
  38. volt: voltvalue,
  39. watt,
  40. rate: dataPower.rate_am,
  41. };
  42. dataInsert.push(dataNew);
  43. dataHours.watt += watt;
  44. dataHours.rate += (watt * dataPower.rate_am);
  45. }
  46. Promise.all(dataInsert.map(data => StatisticSecond.create(data)));
  47. const dateStart = moment().format('YYYY-MM-DD HH:00:00');
  48. const startDateIso = moment(dateStart).toISOString();
  49. const endDateIso = moment(dateStart).add(1, 'hours').toISOString();
  50. await StatisticHour.upsertData(dataPower.id, startDateIso, endDateIso, dataHours);
  51. req.resData = {
  52. status: true,
  53. message: 'OK',
  54. data: dataPower,
  55. };
  56. return next();
  57. };
  58.  
  59. // ========Projection===========
  60. StatisticController.perDay = async (req, res, next) => {
  61. const { power_meter_id, start_date, end_date } = req.body;
  62. /* eslint-disable camelcase*/
  63. const powerId = power_meter_id;
  64. /* eslint-enable camelcase*/
  65. const param = { power_meter_id: powerId, start_date, end_date };
  66. const dataPerday = await StatisticHour.perDay(param);
  67. const dataDay = [];
  68. let { index = 0, totalWatt = 0, totalValue = 0 } = 0;
  69. for (let m = moment(start_date); m.isSameOrBefore(end_date); m.add(1, 'days')) {
  70. dataDay[index] = { date: m.format('YYYY-MM-DD'), watt: 0, value: 0 };
  71. for (let i = 0; i < dataPerday.length; i += 1) {
  72. if (dataPerday[i].date === m.format('YYYY-MM-DD')) {
  73. dataDay[index].watt = dataPerday[i].watt;
  74. dataDay[index].value = dataPerday[i].value;
  75. totalWatt += dataPerday[i].watt;
  76. totalValue += dataPerday[i].value;
  77. }
  78. }
  79. index += 1;
  80. }
  81. const paramThisMonth = {
  82. power_meter_id: powerId,
  83. start_date: moment().format('YYYY-MM-01'),
  84. end_date: moment().format('YYYY-MM-DD'),
  85. };
  86. const paramLastMonth = {
  87. power_meter_id: powerId,
  88. start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
  89. end_date: moment().subtract(1, 'months').format('YYYY-MM-DD'),
  90. };
  91. const paramFullLastMonth = {
  92. power_meter_id: powerId,
  93. start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
  94. end_date: moment(paramLastMonth.start_date).endOf('month').format('YYYY-MM-DD'),
  95. };
  96. const [sumThisMonth, sumLastMonth, sumFullLastMonth] = await Promise.join(
  97. StatisticHour.sumDate(paramThisMonth),
  98. StatisticHour.sumDate(paramLastMonth),
  99. StatisticHour.sumDate(paramFullLastMonth));
  100.  
  101. let projection = 0;
  102. let projectionValue = 0;
  103. if (sumThisMonth[0]) {
  104. if (sumLastMonth[0]) {
  105. projection = (sumThisMonth[0].watt / sumLastMonth[0].watt) * sumFullLastMonth[0].watt;
  106. projectionValue = (sumThisMonth[0].value / sumLastMonth[0].value) * sumFullLastMonth[0].value;
  107. } else {
  108. projection = sumThisMonth[0].watt * (30 / parseInt(moment().format('DD'), 10));
  109. projectionValue = sumThisMonth[0].value * (30 / parseInt(moment().format('DD'), 10));
  110. }
  111. }
  112. const wattProjection = [];
  113. const costProjection = [];
  114. wattProjection[0] = { month: moment().format('MMMM'), bulan: moment().format('MM'), value: projection };
  115. costProjection[0] = { month: moment().format('MMMM'), bulan: moment().format('MM'), value: projectionValue };
  116. // =================MONTH AND YEAR===================
  117. param.year = moment().format('YYYY');
  118. param.month = moment().format('MM');
  119. const dataPerMonth = await StatisticHour.perMonth(param);
  120. let indexInt = 1;
  121. for (let i = (parseInt(moment().format('MM'), 10) - 1); i > 0; i -= 1) {
  122. wattProjection[indexInt] = {
  123. month: moment().subtract(indexInt, 'months').format('MMMM'),
  124. bulan: moment().subtract(indexInt, 'months').format('MM'),
  125. value: 0,
  126. };
  127. costProjection[indexInt] = {
  128. month: moment().subtract(indexInt, 'months').format('MMMM'),
  129. bulan: moment().subtract(indexInt, 'months').format('MM'),
  130. value: 0,
  131. };
  132. for (let ii = 0; ii < dataPerMonth.length; ii += 1) {
  133. if (parseInt(dataPerMonth[ii].month, 10) === parseInt(moment().subtract(indexInt, 'months').format('MM'), 10)) {
  134. wattProjection[indexInt].value = dataPerMonth[ii].watt;
  135. costProjection[indexInt].value = dataPerMonth[ii].value;
  136. }
  137. }
  138. indexInt += 1;
  139. }
  140. req.resData = {
  141. status: true,
  142. message: 'OK',
  143. data: {
  144. daily: dataDay,
  145. total: {
  146. watt: totalWatt,
  147. value: totalValue,
  148. },
  149. user_id: req.user.id,
  150. watt_projection: wattProjection,
  151. cost_projection: costProjection,
  152. },
  153. };
  154. return next();
  155. };
  156.  
  157. // ========Actual===========
  158. StatisticController.actual = async (req, res, next) => {
  159. const { power_meter_id, start_date, end_date } = req.body;
  160. /* eslint-disable camelcase*/
  161. const powerId = power_meter_id;
  162. /* eslint-enable camelcase*/
  163. const param = { power_meter_id: powerId, start_date, end_date };
  164. const dataPerday = await StatisticHour.perDay(param);
  165. const dataDay = [];
  166. /* eslint-disable no-unused-vars*/
  167. let { index = 0, totalWatt = 0, totalValue = 0 } = 0;
  168. for (let m = moment(start_date); m.isSameOrBefore(end_date); m.add(1, 'days')) {
  169. dataDay[index] = { date: m.format('YYYY-MM-DD'), watt: 0, value: 0 };
  170. for (let i = 0; i < dataPerday.length; i += 1) {
  171. if (dataPerday[i].date === m.format('YYYY-MM-DD')) {
  172. dataDay[index].watt = dataPerday[i].watt;
  173. dataDay[index].value = dataPerday[i].value;
  174. totalWatt += dataPerday[i].watt;
  175. totalValue += dataPerday[i].value;
  176. }
  177. }
  178. index += 1;
  179. }
  180. const namaGroup = [];
  181. const powerLimit = [];
  182. const powerMeterCode = [];
  183. const rate = [];
  184. const paramThisMonth = {
  185. power_meter_id: powerId,
  186. start_date: moment().format('YYYY-MM-01'),
  187. end_date: moment().format('YYYY-MM-DD'),
  188. };
  189. const paramLastMonth = {
  190. power_meter_id: powerId,
  191. start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
  192. end_date: moment().subtract(1, 'months').format('YYYY-MM-DD'),
  193. };
  194. const paramFullLastMonth = {
  195. power_meter_id: powerId,
  196. start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
  197. end_date: moment(paramLastMonth.start_date).endOf('month').format('YYYY-MM-DD'),
  198. };
  199. const [dataGroup, sumThisMonth, sumLastMonth, sumFullLastMonth] = await Promise.join(
  200. powerMeter.getGroupName(param),
  201. StatisticHour.sumDate(paramThisMonth),
  202. StatisticHour.sumDate(paramLastMonth),
  203. StatisticHour.sumDate(paramFullLastMonth));
  204. if (dataGroup) {
  205. dataGroup.map(data => [
  206. namaGroup.push(data.group_name),
  207. powerLimit.push(data.power_limit),
  208. powerMeterCode.push(data.power_meter),
  209. rate.push(data.rate),
  210. ]);
  211. }
  212. let projection = 0;
  213. let projectionValue = 0;
  214. let currentWatt = 0;
  215. let currentCost = 0;
  216. const wattLastMonth = (sumFullLastMonth[0]) ? sumFullLastMonth[0].watt : 0;
  217. if (sumThisMonth[0]) {
  218. currentWatt = sumThisMonth[0].watt;
  219. currentCost = sumThisMonth[0].value;
  220. if (sumLastMonth[0]) {
  221. projection = (sumThisMonth[0].watt / sumLastMonth[0].watt) * sumFullLastMonth[0].watt;
  222. projectionValue = (sumThisMonth[0].value / sumLastMonth[0].value) * sumFullLastMonth[0].value;
  223. } else {
  224. projection = sumThisMonth[0].watt * (30 / parseInt(moment().format('DD'), 10));
  225. projectionValue = sumThisMonth[0].value * (30 / parseInt(moment().format('DD'), 10));
  226. }
  227. }
  228. // ==========average=======================
  229. // select min date
  230. let minDate = moment().subtract(11, 'months').format('YYYY-MM-01');
  231. const dateMinData = await StatisticHour.minDate(param);
  232. if (dateMinData[0]) {
  233. if (moment(dateMinData[0].datemin).isAfter(minDate)) {
  234. minDate = dateMinData[0].datemin;
  235. }
  236. }
  237. const MonthMin = moment(minDate);
  238. const monthNow = moment();
  239. const diff = monthNow.diff(MonthMin, 'months') + 1;
  240. // data dalam max setahun/12 bulan
  241. param.minDate = moment(MonthMin).toISOString();
  242. const dataSumWatt = await StatisticHour.sumWatt(param);
  243. const averageWatt = (dataSumWatt[0]) ? dataSumWatt[0].watt / diff : 0;
  244. req.resData = {
  245. status: true,
  246. message: 'OK',
  247. data: {
  248. daily: dataDay,
  249. user_id: req.user.id,
  250. group_name: namaGroup,
  251. power_limit: powerLimit,
  252. rate,
  253. last_watt: wattLastMonth,
  254. current_watt: currentWatt,
  255. current_cost: currentCost,
  256. average_watt: averageWatt,
  257. watt_projection: projection,
  258. cost_projection: projectionValue,
  259. power_meter: powerMeterCode,
  260. },
  261. };
  262. return next();
  263. };
Add Comment
Please, Sign In to add comment