-
Notifications
You must be signed in to change notification settings - Fork 10
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
Cursor Helper #491
Cursor Helper #491
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Hiya @josephjclark here is my feedback 1st. You need to expandReference on cursor value
My quick fix was
2nd. Inaccurate cursor dates for some valuesMy test code
CLI logs
3rd. Timezone ?This is more of a questions + suggestion
|
Thank you @mtuchi - I'll look into your feedback. But what I really need to know is: does this actually help anything? Maybe I can fix the issues and we can try and test some existing cursor jobs with it, see whether it helps at all. The date conversions in the water-aid (?) workflow we were looking at seem pretty hairy - we need to work out how this function can help |
For some reason i am tempted to ask for support for a callback in a cursor function. I think there was a case in AKF workflow were i need to create a cursor at the end of upsert operation then post it to inbox. The triggered workflow was using that cursor to clear some data in salesforce. I guess my point here is the cursor that was created after the upsert operation is not related to that workflow. So the final state |
@josephjclark I think it's useful because it just cut down these lines of code 👇🏽 into one line of code
To
Which can even be something like this 👇🏽 if initial input is
I think using the |
I just got another light bulk moment 💡, Sometimes i need to set the cursor to todays date or a specific date but i don't need the timestamp. So i usually have this pattern I was wondering if the |
@mtuchi I think you're starting to get it! 💡 I'm adding an options object shortly, so you'll be able to do this (which is super readable)
I really don't think you need to explicitly use a manual cursor key. But sure, if you absolutely need it, that pattern works. tbh I can't see why you'd need a callback - it's a synchronous function after all. But you can set or clear it at the end of the job:
Instead of |
My wishful thinking, i am not sure if it's a good pattern but here is how i would use the callback
|
Regarding timezones, well, you've hurt my brain 🤯 For now, all timezones will be in UTC, and I think we'll release in that state. I would like to support this: But I have two problems with this:
We will need to document that if you I suppose this is an argument for |
On timezone, i think we should lock it to UTC or (which ever timezone lighting is using). And add in CLI logs the timezone. |
@mtuchi I'm still investigating but with regard to those time errors you reported, I actually think time zones are the cause! The dates you're seeing in the console are UTC times, not local times. So what it reports for "today" isn't the start of your local today, it's the start of your today in UTC time 🤯 I suppose when you run in the CLI, all these dates wil be in you sytem time (I've just verified this), so outputting the locale time, rather than UTC time, would make sense. But when running in Lightning, and Lightning is on UTC time, these relative times get very complex |
@josephjclark by the way, not to add more complexity i saw this library that is related to |
@mtuchi Thank you - I know about date-fns-tz and I've been looking at it this morning. I think it might be part of the solution, but I can't quite see how it goes yet. Again, I think for now we just have to say "all dates are generated in UTC". If that doesn't work for you, should should be able to pass a date string like |
Ok @mtuchi can you do a retest?
I'm going to update the main text in the PR to reflect the most recent styles. I'll tweak docs too. |
Hiya @josephjclark, Loving the 1.
|
Also don't forget to build to generate the latest |
This required a refactor to make parsedate return a date, not a string
@mtuchi this is amazing feedback - you've caught some brilliant issues there! It's all been addressed. I've also added a seperate PR for docs - we should merge both at about the same time. I've still got to do versioning stuff - I'll create a release branch for that |
Hey @josephjclark tested the cursor everything works perfect ✅ 🥳 , I find the date logs eg: My quick suggestion to fix this, can we adjust the logs to be like this
That will turn the entire date into human readable form which is very easy to relate to. Eg |
@mtuchi thanks - I think if we're gonna do that we'll just use date.format, or maybe date-fns.format. But I agree and I'll make that change now! |
This PR adds a
cursor()
function designed to make the timestamp and pagination cursor pattern easier.Closes #486
This approach leans heavily on input state and works best in v2.
See the docs PR at OpenFn/docs#465
General approach
Unlike v1, Lightning (and the CLI) give us good control of the input state. We need to use that.
So, if you want to run a job with the cursor in a specific place, just set the input to
{ cursor: '<date>' }
and that will be respected by the job.To make date management easier, you pass human friendly strings
now
,today
,yesterday
,n hours ago
,n days ago
, andstart
(the time at which the run started). Note that these will parse relative to system time - which in lightning's case probably means UTC.Usage
Use the
cursor
function from common to set a cursor value on state.This pattern will allow you to use the value on state if provided, but if no cursor is passed will default to "today".
You can update the cursor within the job by setting
state.cursor
. Or, if you want to use a natural language timestamp, use thecursor
operation again.The value that gets written to
state.cursor
will be an actual ISO date, so when the next job runs, the cursor will be set to this start time.Note that adaptors will need to be updated to export
cursor
from common.Options
The second argument accepts options:
options.defaultValue
is the value that will be used if the value argument is falsyoptions.key
lets you change the state key used (ie, usepage
instead ofcursor
)Later we might introduce a timezone and a flag to disable natural language date parsing.
Usage examples
Basic usage: a cursor which starts from the start of the current day
Typical usage: use the input state if provided, or else default to 'now'. At the end of the job, set the cursor to the start time, ready for the next run.
Pagination cursor which defaults to page 0