Skip to content

[WIP] Add Unix Timestamp support for last_read_at in notifications API #24918

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

Closed

Conversation

sonjek
Copy link
Contributor

@sonjek sonjek commented May 24, 2023

Should fix 2nd point of the issue #24574

Now last_read_at parameter in notification API supports following value formats:

  • 2006-01-02T15:04:05Z
  • 2006-01-02T15:04:05.999Z
  • 2006-01-02T15:04:05-07:00
  • 2006-01-02T15:04:05.999-07:00
  • 1136214245

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 24, 2023
@silverwind
Copy link
Member

The swagger docs still specify this parameter as date-time format and normally I would put a oneOf around it but swagger 2.0 does not have this openapi 3.0 feature unfortunately.

@wxiaoguang
Copy link
Contributor

IMO the new logic could be put into a separate util function, and use more specific tests to cover it. It would benefit other "date/time" parameters in the future.

@silverwind
Copy link
Member

silverwind commented May 25, 2023

Yes, call it ParseDateGraceful or similar and add some tests. And yes, it could apply to all date-time parsing on the API, but that can be done in another PR.

@silverwind
Copy link
Member

silverwind commented May 25, 2023

Also, I think it'd be better to support millisecond precision, e.g. parse via UnixMilli. This is also the default precision in JavaScript, fwiw. Should be noted in the API docs, e.g. update the field comment.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented May 25, 2023

Also, I think it'd be better to support millisecond precision, e.g. parse via UnixMilli. This is also the default precision in JavaScript, fwiw. Should be noted in the API docs, e.g. update the field comment.

That needs more designs. The problem is: when you see an integer or number, what do you think it should be for a "time"?

I prefer to use International System of Units (SI) , eg: 1 means 1 second. 1.001 means 1 second and 1 millisecond, then microsecond and nanosecond could also be supported.

@silverwind
Copy link
Member

I prefer to use International System of Units (SI) , eg: 1 means 1 second. 1.001 means 1 second and 1 millisecond, then microsecond and nanosecond could also be supported.

Floating point numbers are a bit unergonomic, but possible, yes. Whatever we do, we should specify in the API docs. I personally would still do milliseconds, it's just precise enough to be generally useful.

For example, Grafana also accepts unix timestamps as milliseconds as well as special identifiers like now or now-6h for relative time, which in theory could also be supported.

@sonjek sonjek changed the title Add Unix Timestamp support for last_read_at in notifications API [WIP] Add Unix Timestamp support for last_read_at in notifications API May 25, 2023
@sonjek sonjek force-pushed the nitification_api_timestamp_for_last_read_at branch from 99640d7 to 550ac86 Compare May 27, 2023 14:45
@pull-request-size pull-request-size bot added size/L and removed size/S labels May 27, 2023
Comment on lines 75 to 83
case int64:
switch {
case val > 946684800000000: // 2999-12-31 00:00:00 in milliseconds
return time.UnixMicro(val), nil
case val > 946645200000: // 2000-01-01 00:00:00 in milliseconds
return time.UnixMilli(val), nil
default:
return time.Unix(val, 0), nil
}
Copy link
Contributor

@wxiaoguang wxiaoguang May 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have objection for doing so. Keeping "guessing the unit" is quite fragile.

Either it strictly follows the Golang's time (int64 as nanosecond)

Or it strictly follows SI, use second as its unit, and use "0.001" for 1 millisecond (I prefer this way). There is no "float number" problem, because the time could be passed as a string, and split it by the dot "." if the caller doesn't want to use float.

@wxiaoguang
Copy link
Contributor

I personally would still do milliseconds, it's just precise enough to be generally useful.

I do not think it should introduce another time unit in Gitea. At the moment, Gitea only uses 2 kinds of units:

  • The standard Golang time: int64 as nanosecond
  • The timeutil.TimeStamp, int64 as second (unix timestamp)

Introducing more units only increases the difficulty of the maintainability

The SI unit (second) works well and is consistent with timeutil.TimeStamp, and it doesn't need to use float, the time "1.001" could be just passed as string, the parser can split it by the dot "."

@sonjek
Copy link
Contributor Author

sonjek commented May 27, 2023

I personally would still do milliseconds, it's just precise enough to be generally useful.

I do not think it should introduce another time unit in Gitea. At the moment, Gitea only uses 2 kinds of units:

* The standard Golang time: int64 as nanosecond

* The `timeutil.TimeStamp`, int64 as second (unix timestamp)

Introducing more units only increases the difficulty of the maintainability

The SI unit (second) works well and is consistent with timeutil.TimeStamp, and it doesn't need to use float, the time "1.001" could be just passed as string, the parser can split it by the dot "."

Support for the unix timestamp (seconds) is useful because it's a common standard and it is easier to get the desired value than a formatted date in the form of a date.
Originally I plan to add a support for the timestamp in seconds only.

But in JavaScript standard for the timestamp is in milliseconds.
That is why somehow we need to check that the user filled in the value exactly in seconds.
So, if we can detect dimension of the timestamps, then we can handle them.

Ideally I'd limit to the extra unix timestamp and timestamp is in milliseconds support.

I looked at the GitHub API and they only use date in format: YYYY-MM-DDTHH:MM:SSZ.
Maybe we can let everything as it is.

@wxiaoguang
Copy link
Contributor

I think the UnixMilli should also be removed. I don't see a real case why it is useful, it only introduces inconsistency.

@silverwind
Copy link
Member

I'm fine with float seconds instead of int milliseconds I guess.

@sonjek sonjek changed the title [WIP] Add Unix Timestamp support for last_read_at in notifications API Add Unix Timestamp support for last_read_at in notifications API May 27, 2023
@sonjek
Copy link
Contributor Author

sonjek commented May 27, 2023

I think the UnixMilli should also be removed. I don't see a real case why it is useful, it only introduces inconsistency.

I removed UnixMilli support.
I think now it's not critical to support this functionality.

From now last_read_at in notifications API will supports following value formats:

  • 2006-01-02T15:04:05Z
  • 2006-01-02T15:04:05.999Z
  • 2006-01-02T15:04:05-07:00
  • 2006-01-02T15:04:05.999-07:00
  • 1136214245

@sonjek sonjek requested a review from wxiaoguang May 29, 2023 18:04
@silverwind
Copy link
Member

silverwind commented May 29, 2023

Would it be much work to have all date-time that the APIs accept parsed by this? I think if we do it, we should do it everywhere, not just on 1-2 cherry-picked APIs.

Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Some nits.

},
{
value: 0,
expected: int64(-62135596800),
Copy link
Contributor

@wxiaoguang wxiaoguang May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an error, there shouldn't be expected (it can be covered by assert.True(t, actual.IsZero()))

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels May 29, 2023
@sonjek sonjek changed the title Add Unix Timestamp support for last_read_at in notifications API [WIP] Add Unix Timestamp support for last_read_at in notifications API Jun 4, 2023
@lunny lunny added type/enhancement An improvement of existing functionality modifies/api This PR adds API routes or modifies them labels Jul 27, 2023
@wxiaoguang
Copy link
Contributor

Stale?

@wxiaoguang wxiaoguang added the issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail label Aug 16, 2023
@wxiaoguang
Copy link
Contributor

Stale. Feel free to reopen.

@wxiaoguang wxiaoguang closed this Sep 7, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Dec 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/api This PR adds API routes or modifies them type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants