Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { dateFmt, measurementWaitingGap, ComponentState } from '../constants';
- import moment from 'moment-timezone';
- import {
- MeasurementCollection,
- WeeklyStatsCollection,
- UserCollection
- } from '../data-provider'
- import * as _ from 'lodash';
- // Get Participant Data
- const getParticipant = async (userId) => {
- return UserCollection.findOne({
- userId: userId
- });
- }
- // Remove All 1970 Measurements
- const removeAllInvalidMeasurements = async (participant) => {
- await MeasurementCollection.remove({
- $and: [
- { userId: String(participant._id) },
- { dateTime: { $lt: moment('2000-01-01').toDate() } }
- ]
- }, { justOne: false });
- }
- // Get Participant Measurements
- const getAllParticipantMeasurements = async (participant) => {
- return MeasurementCollection.find({
- $and: [
- { userId: String(participant._id) }
- ]
- }, {
- sort: {
- dateTime: 1
- }
- });
- }
- // Update Measurement
- const updateMeasurement = async (measurementId, batchId, runId) => {
- return await MeasurementCollection.update({
- _id: measurementId
- }, {
- $set: {
- batchId,
- runId
- }
- });
- }
- // Run The Patch
- export default async () => {
- // Get Participant
- const participant = await getParticipant('BN001');
- // Remove all 1970
- await removeAllInvalidMeasurements(participant);
- // Get All Users Measurements
- const measurements = await getAllParticipantMeasurements(participant);
- // Get Unique Dates
- const dates = _.uniq(_.map(measurements, (m) => {
- return moment(m.dateTime).format("MM-DD-YYYY").toString();
- }));
- let batchId = 1;
- let runId = 1;
- let previousMeasurement = null;
- let startIdentified = false;
- // Update Measurements
- for (const m of measurements) {
- // For the first measurement
- if (!startIdentified) {
- // console.log(`Batch Id: ${batchId}, Run Id: ${runId}`, moment(m.dateTime).format("MM-DD-YYYY"));
- await updateMeasurement(m._id, 1, 1);
- startIdentified = true;
- }
- // For the preceding measurements
- else {
- // Check if within 10 minutes
- const previous = moment(previousMeasurement.dateTime);
- const current = moment(m.dateTime);
- const timeDiff = current.diff(previous, 'minutes');
- const isSameCategory = previousMeasurement.category == m.category;
- if (timeDiff <= 10 && isSameCategory) {
- runId++;
- } else {
- runId = 1;
- batchId++;
- }
- await updateMeasurement(m._id, batchId, runId);
- // console.log(`Batch Id: ${batchId}, Run Id: ${runId}`, moment(m.dateTime).format("MM-DD-YYYY h:m:s"));
- }
- previousMeasurement = m;
- }
- console.log('SEQ 6 - Fixing BN001 BatchIds.');
- };
Add Comment
Please, Sign In to add comment