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

[24.2] Fix workflow report time handling #19292

Open
wants to merge 14 commits into
base: release_24.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"sass-loader": "^13.3.2",
"store": "^2.0.12",
"style-loader": "^3.3.3",
"timezone-mock": "^1.3.6",
"ts-jest": "^29.2.3",
"ts-loader": "^9.5.0",
"tsconfig-paths-webpack-plugin": "^4.1.0",
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/Markdown/Elements/InvocationTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</template>

<script>
import { formatGalaxyPrettyDateString } from "@/utils/dates";

export default {
props: {
args: {
Expand All @@ -19,7 +21,7 @@ export default {
computed: {
content() {
const invocation = this.invocations[this.args.invocation_id];
return invocation && new Date(invocation["create_time"]).toUTCString();
return invocation && formatGalaxyPrettyDateString(invocation["create_time"]);
},
},
};
Expand Down
19 changes: 3 additions & 16 deletions client/src/components/Markdown/Markdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { mapActions } from "pinia";
import Vue from "vue";

import { useWorkflowStore } from "@/stores/workflowStore";
import { formatGalaxyPrettyDateString } from "@/utils/dates";

import { splitMarkdown as splitMarkdownUnrendered } from "./parse";

Expand Down Expand Up @@ -138,23 +139,9 @@ export default {
return this.enable_beta_markdown_export ? this.exportLink : null;
},
time() {
let generateTime = this.markdownConfig.generate_time;
const generateTime = this.markdownConfig.generate_time;
if (generateTime) {
if (!generateTime.endsWith("Z")) {
// We don't have tzinfo, but this will always be UTC coming
// from Galaxy so append Z to assert that prior to parsing
generateTime += "Z";
}
const date = new Date(generateTime);
return date.toLocaleString("default", {
day: "numeric",
month: "long",
year: "numeric",
minute: "numeric",
hour: "numeric",
timeZone: "UTC",
timeZoneName: "short",
});
return formatGalaxyPrettyDateString(generateTime);
}
return "unavailable";
},
Expand Down
8 changes: 5 additions & 3 deletions client/src/components/UtcDate.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup lang="ts">
import { format, formatDistanceToNow, parseISO } from "date-fns";
import { formatDistanceToNow } from "date-fns";
import { computed } from "vue";

import { galaxyTimeToDate, localizeUTCPretty } from "@/utils/dates";

interface UtcDateProps {
date: string;
mode?: "date" | "elapsed" | "pretty";
Expand All @@ -11,10 +13,10 @@ const props = withDefaults(defineProps<UtcDateProps>(), {
mode: "date",
});

const parsedDate = computed(() => parseISO(`${props.date}Z`));
const parsedDate = computed(() => galaxyTimeToDate(props.date));
const elapsedTime = computed(() => formatDistanceToNow(parsedDate.value, { addSuffix: true }));
const fullISO = computed(() => parsedDate.value.toISOString());
const pretty = computed(() => format(parsedDate.value, "eeee MMM do H:mm:ss yyyy zz"));
const pretty = computed(() => localizeUTCPretty(parsedDate.value));
</script>

<template>
Expand Down
51 changes: 51 additions & 0 deletions client/src/utils/dates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import MockDate from "timezone-mock";

import { formatGalaxyPrettyDateString, galaxyTimeToDate, localizeUTCPretty } from "./dates";

describe("dates.ts", () => {
beforeEach(() => {
MockDate.register("Etc/GMT+4");
});

afterEach(() => {
MockDate.unregister();
});

describe("galaxyTimeToDate", () => {
it("should convert valid galaxyTime string to Date object", () => {
const galaxyTime = "2023-10-01T12:00:00";
const date = galaxyTimeToDate(galaxyTime);
expect(date).toBeInstanceOf(Date);
expect(date.toISOString()).toBe("2023-10-01T12:00:00.000Z");
});

it("should append Z if missing and parse correctly", () => {
const galaxyTime = "2023-10-01T12:00:00";
const date = galaxyTimeToDate(galaxyTime);
expect(date.toISOString()).toBe("2023-10-01T12:00:00.000Z");
});

it("should throw an error for invalid galaxyTime string", () => {
const invalidGalaxyTime = "invalid-date-string";
expect(() => galaxyTimeToDate(invalidGalaxyTime)).toThrow(
`Invalid galaxyTime string: ${invalidGalaxyTime}`
);
});
});

describe("localizeUTCPretty", () => {
it("should format Date object into human-readable string", () => {
const date = new Date("2023-10-01T12:00:00Z");
const formatted = localizeUTCPretty(date);
expect(formatted).toBe("Sunday Oct 1st 8:00:00 2023 GMT-4");
});
});

describe("formatGalaxyPrettyDateString", () => {
it("should convert galaxyTime string to formatted date string", () => {
const galaxyTime = "2023-10-01T12:00:00";
const formatted = formatGalaxyPrettyDateString(galaxyTime);
expect(formatted).toBe("Sunday Oct 1st 8:00:00 2023 GMT-4");
});
});
});
39 changes: 39 additions & 0 deletions client/src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { format, parseISO } from "date-fns";

/**
* Converts a Galaxy time string to a Date object.
* @param {string} galaxyTime - The Galaxy time string in ISO format.
* @returns {Date} The parsed Date object.
*/
export function galaxyTimeToDate(galaxyTime: string): Date {
// Galaxy doesn't include Zulu time zone designator, but it's always UTC
// so we need to add it to parse the string correctly in JavaScript.
let time = galaxyTime;
if (!time.endsWith("Z")) {
time += "Z";
}
const date = parseISO(time);
if (isNaN(date.getTime())) {
throw new Error(`Invalid galaxyTime string: ${galaxyTime}`);
}
return date;
}

/**
* Formats a UTC Date object into a human-readable string, localized to the user's time zone.
* @param {Date} utcDate - The UTC Date object.
* @returns {string} The formatted date string.
*/
export function localizeUTCPretty(utcDate: Date): string {
return format(utcDate, "eeee MMM do H:mm:ss yyyy zz");
}

/**
* Converts a Galaxy time string to a human-readable formatted date string, localized to the user's time zone.
* @param {string} galaxyTime - The Galaxy time string in ISO format.
* @returns {string} The formatted date string.
*/
export function formatGalaxyPrettyDateString(galaxyTime: string): string {
const date = galaxyTimeToDate(galaxyTime);
return localizeUTCPretty(date);
}
5 changes: 5 additions & 0 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11381,6 +11381,11 @@ timers-browserify@^2.0.12:
dependencies:
setimmediate "^1.0.4"

timezone-mock@^1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/timezone-mock/-/timezone-mock-1.3.6.tgz#44e4c5aeb57e6c07ae630a05c528fc4d9aab86f4"
integrity sha512-YcloWmZfLD9Li5m2VcobkCDNVaLMx8ohAb/97l/wYS3m+0TIEK5PFNMZZfRcusc6sFjIfxu8qcJT0CNnOdpqmg==

[email protected]:
version "1.0.5"
resolved "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz"
Expand Down
11 changes: 6 additions & 5 deletions lib/galaxy/managers/markdown_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,7 @@ def handle_instance_organization_link(self, line, title, url):
pass

def handle_invocation_time(self, line, invocation):
self.ensure_rendering_data_for("invocations", invocation)["create_time"] = invocation.create_time.strftime(
"%Y-%m-%d, %H:%M:%S"
)
self.ensure_rendering_data_for("invocations", invocation)["create_time"] = invocation.create_time.isoformat()

def handle_dataset_type(self, line, hda):
self.extend_history_dataset_rendering_data(hda, "ext", hda.ext, "*Unknown dataset type*")
Expand Down Expand Up @@ -506,6 +504,9 @@ class ToBasicMarkdownDirectiveHandler(GalaxyInternalMarkdownDirectiveHandler):
def __init__(self, trans):
self.trans = trans

def _format_printable_time(self, time):
return time.strftime("%Y-%m-%d, %H:%M:%S UTC")

def handle_dataset_display(self, line, hda):
name = hda.name or ""
markdown = "---\n"
Expand Down Expand Up @@ -689,7 +690,7 @@ def handle_generate_galaxy_version(self, line, generate_version):
return (content, True)

def handle_generate_time(self, line, generate_time):
content = literal_via_fence(generate_time.isoformat())
content = literal_via_fence(self._format_printable_time(generate_time))
return (content, True)

def handle_instance_access_link(self, line, url):
Expand Down Expand Up @@ -722,7 +723,7 @@ def _handle_link(self, url, title=None):
return (f"[{title}]({url})", True)

def handle_invocation_time(self, line, invocation):
content = literal_via_fence(invocation.create_time.strftime("%Y-%m-%d, %H:%M:%S"))
content = literal_via_fence(self._format_printable_time(invocation.create_time))
return (content, True)

def handle_dataset_name(self, line, hda):
Expand Down
6 changes: 2 additions & 4 deletions test/unit/app/managers/test_markdown_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_generate_invocation_time(self):
invocation = self._new_invocation()
self.app.workflow_manager.get_invocation.side_effect = [invocation]
result = self._to_basic(example)
expectedtime = invocation.create_time.strftime("%Y-%m-%d, %H:%M:%S")
expectedtime = invocation.create_time.strftime("%Y-%m-%d, %H:%M:%S UTC")
assert f"\n {expectedtime}" in result

def test_job_parameters(self):
Expand Down Expand Up @@ -413,9 +413,7 @@ def test_get_invocation_time(self):
result, extra_data = self._ready_export(example)
assert "invocations" in extra_data
assert "create_time" in extra_data["invocations"]["be8be0fd2ce547f6"]
assert extra_data["invocations"]["be8be0fd2ce547f6"]["create_time"] == invocation.create_time.strftime(
"%Y-%m-%d, %H:%M:%S"
)
assert extra_data["invocations"]["be8be0fd2ce547f6"]["create_time"] == invocation.create_time.isoformat()

def _ready_export(self, example):
return ready_galaxy_markdown_for_export(self.trans, example)
Loading