Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import { dateFmt, measurementWaitingGap, ComponentState } from '../constants';
  2. import moment from 'moment-timezone';
  3. import {
  4. MeasurementCollection,
  5. WeeklyStatsCollection,
  6. UserCollection
  7. } from '../data-provider'
  8. import * as _ from 'lodash';
  9.  
  10. // Get Participant Data
  11. const getParticipant = async (userId) => {
  12. return UserCollection.findOne({
  13. userId: userId
  14. });
  15. }
  16.  
  17. // Remove All 1970 Measurements
  18. const removeAllInvalidMeasurements = async (participant) => {
  19. await MeasurementCollection.remove({
  20. $and: [
  21. { userId: String(participant._id) },
  22. { dateTime: { $lt: moment('2000-01-01').toDate() } }
  23. ]
  24. }, { justOne: false });
  25. }
  26.  
  27. // Get Participant Measurements
  28. const getAllParticipantMeasurements = async (participant) => {
  29. return MeasurementCollection.find({
  30. $and: [
  31. { userId: String(participant._id) }
  32. ]
  33. }, {
  34. sort: {
  35. dateTime: 1
  36. }
  37. });
  38. }
  39.  
  40. // Update Measurement
  41. const updateMeasurement = async (measurementId, batchId, runId) => {
  42. return await MeasurementCollection.update({
  43. _id: measurementId
  44. }, {
  45. $set: {
  46. batchId,
  47. runId
  48. }
  49. });
  50. }
  51.  
  52. // Run The Patch
  53. export default async () => {
  54. // Get Participant
  55. const participant = await getParticipant('BN001');
  56.  
  57. // Remove all 1970
  58. await removeAllInvalidMeasurements(participant);
  59.  
  60. // Get All Users Measurements
  61. const measurements = await getAllParticipantMeasurements(participant);
  62.  
  63. // Get Unique Dates
  64. const dates = _.uniq(_.map(measurements, (m) => {
  65. return moment(m.dateTime).format("MM-DD-YYYY").toString();
  66. }));
  67.  
  68. let batchId = 1;
  69. let runId = 1;
  70. let previousMeasurement = null;
  71. let startIdentified = false;
  72.  
  73. // Update Measurements
  74. for (const m of measurements) {
  75.  
  76. // For the first measurement
  77. if (!startIdentified) {
  78. // console.log(`Batch Id: ${batchId}, Run Id: ${runId}`, moment(m.dateTime).format("MM-DD-YYYY"));
  79. await updateMeasurement(m._id, 1, 1);
  80. startIdentified = true;
  81. }
  82.  
  83. // For the preceding measurements
  84. else {
  85. // Check if within 10 minutes
  86. const previous = moment(previousMeasurement.dateTime);
  87. const current = moment(m.dateTime);
  88. const timeDiff = current.diff(previous, 'minutes');
  89. const isSameCategory = previousMeasurement.category == m.category;
  90.  
  91. if (timeDiff <= 10 && isSameCategory) {
  92. runId++;
  93. } else {
  94. runId = 1;
  95. batchId++;
  96. }
  97. await updateMeasurement(m._id, batchId, runId);
  98. // console.log(`Batch Id: ${batchId}, Run Id: ${runId}`, moment(m.dateTime).format("MM-DD-YYYY h:m:s"));
  99. }
  100. previousMeasurement = m;
  101. }
  102.  
  103. console.log('SEQ 6 - Fixing BN001 BatchIds.');
  104. };
Add Comment
Please, Sign In to add comment