Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timestamp mismatch #2

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

RyanL1997
Copy link

@RyanL1997 RyanL1997 commented Oct 17, 2024

Ryan's brain dump for the problem

First you got a function in the src/plugins/home/server/services/sample_data/routes/install.ts to update the log timestamp to current time

  // Function to update timestamps
  function updateTimestamps(doc: any) {
    dataIndexConfig.timeFields
      .filter((timeFieldName: string) => getNestedField(doc, timeFieldName))
      .forEach((timeFieldName: string) => {
        const timeValue = getNestedField(doc, timeFieldName);
        const updatedTime = dataIndexConfig.preserveDayOfWeekTimeOfDay
          ? translateTimeRelativeToWeek(timeValue, dataIndexConfig.currentTimeMarker, nowReference)
          : translateTimeRelativeToDifference(
              timeValue,
              dataIndexConfig.currentTimeMarker,
              nowReference
            );
        setNestedField(doc, timeFieldName, updatedTime);
      });
    return doc;
  }

In the Current Logic:
The function src/plugins/home/server/services/sample_data/lib/translate_timestamp.ts calculates the difference between source and sourceReference, and then applies that difference to targetReference:

// Translate source timestamp by targetReference timestamp,
// perserving the distance between source and sourceReference
export function translateTimeRelativeToDifference(
  source: string,
  sourceReference: any,
  targetReference: any
) {
  const sourceDate = iso8601ToDateIgnoringTime(source);
  const sourceReferenceDate = iso8601ToDateIgnoringTime(sourceReference);
  const targetReferenceDate = iso8601ToDateIgnoringTime(targetReference);

  const timeDelta = sourceDate.getTime() - sourceReferenceDate.getTime();
  const translatedDate = new Date(targetReferenceDate.getTime() + timeDelta);

  return `${dateToIso8601IgnoringTime(translatedDate)}T${source.substring(11)}`;
}

If sourceReference and source are not on the same date, the time difference between them gets added when translating to the targetReference, potentially introducing an unintended shift. Since the source in the log is 2024-10-16 (you can console log it), and the sourceReference in code you setup is 2024-10-15 so that, in your case, the time delta is 1 day. These two days will be added into the targetReference, for the query we tried so that it is the now for your machine's local time. The return of the function will be now + time delta, which will be 1 days in the future. Thats explained why when I installed it at midnight of OCT 17 it shows OCT 18 in index timestamp.

Solution: Set sourceReference to the Same Date as source

If our goal is to maintain the same date for source during the translation, then setting sourceReference to the same date as source would prevent any additional time from being added. Thats why I made this commit, so that the currentTimeMarker as the sourceReference matches what you got in the log. - You can try out by console log every single line in translateTimeRelativeToDifference() with/without my change.

Signed-off-by: Ryan Liang <jiallian@amazon.com>
@mengweieric mengweieric merged commit f49d464 into mengweieric:pr-8587-fork Oct 17, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants