Clever Tagline
Clever Tagline•3mo ago

Tracking time of an API call

I'd like to track the execution time of an API call made in an action, and save the duration into a field. This call could take anywhere from a couple seconds to well over a minute. I'm already creating a record at the start of the action right before calling the API, so my first thought was to just get a new Date instance (or using Date.now()), and then compare the value against the record's creation time. However, then I read this about dates in Convex functions: https://docs.convex.dev/functions/runtimes#using-randomness-and-time-in-queries-and-mutations Is there another way to pull this off and accurately track when I receive the response from the API call?
Runtimes | Convex Developer Hub
Convex functions can run in two runtimes:
8 Replies
Convex Bot
Convex Bot•3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Clever Tagline
Clever TaglineOP•3mo ago
Weird...not sure why the post isn't showing up. Is it because I included a link in the original post body? It's a link to the Convex docs if that makes a difference.
lee
lee•3mo ago
that doc is talking about queries and mutations. Date.now() in actions works more like in other systems -- it's the current time
Clever Tagline
Clever TaglineOP•3mo ago
@lee Thanks for clarifying that. Had to completely restart the Discord desktop app to get my original post to show up. Weird. @lee Something's off. I just tried to add this feature, but the timestamp isn't saving. Here's the relevant part of my action:
await ctx.runMutation(internal.it.logs.updateLog, {
id: logId,
status: response.status,
log: response.ok
? ((await response.json())?.log ?? ["Log not found"]).join("\n")
: response.statusText,
updatedAt: Date.now()
})
await ctx.runMutation(internal.it.logs.updateLog, {
id: logId,
status: response.status,
log: response.ok
? ((await response.json())?.log ?? ["Log not found"]).join("\n")
: response.statusText,
updatedAt: Date.now()
})
The updatedAt field is in the table schema as v.optional(v.number()), but in the two tests that I just ran, both records show a field value of unset.
Clever Tagline
Clever TaglineOP•3mo ago
And yet I can log the value, and it works.
No description
Clever Tagline
Clever TaglineOP•3mo ago
I also tried capturing the value into a variable and passing that, but it still ends up unset after the update.
lee
lee•3mo ago
can you share the code for the updateLog mutation
Clever Tagline
Clever TaglineOP•3mo ago
Doh! I knew I'd forgotten to update something 🫢 Clearly I need to slow down a little bit