Skip to content

feat: Adds time_format param for httpd#26596

Merged
philjb merged 4 commits intomaster-1.xfrom
db/615/timestamp-csv
Jul 10, 2025
Merged

feat: Adds time_format param for httpd#26596
philjb merged 4 commits intomaster-1.xfrom
db/615/timestamp-csv

Conversation

@devanbenz
Copy link
Copy Markdown

  • This PR will add a time_format parameter which takes in the value "epoch" or "rfc3339". The default will be "epoch" depending on the value output timestamps will be formatted in epoch or rfc3339.

Closes FR#615

* This PR will add a time_format parameter which takes in the value "epoch" or "rfc3339". The default will be "epoch" depending on the value output timestamps will be formatted in epoch or rfc3339.

Closes FR#615
@devanbenz devanbenz force-pushed the db/615/timestamp-csv branch from 3d14493 to 7eb766f Compare July 8, 2025 20:09
@devanbenz devanbenz marked this pull request as ready for review July 8, 2025 20:14
Comment on lines +597 to +602
// timeFormat should default to "epoch"
timeFormat := strings.TrimSpace(r.FormValue("time_format"))
if timeFormat == "" {
timeFormat = "epoch"
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it should return an error if an invalid value is given for time_format. This will save a lot of support time.

Comment on lines +760 to +762
if timeFormat == "rfc3339" {
convertToRfc3339(r)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like we are pretty consistent in calling ns-precision RFC3339 simply RFC3339 in our docs and in the API. I wonder if we should allow rfc3339nano as a synonym?

Comment on lines -750 to +762
if epoch != "" {
if epoch != "" && timeFormat == "epoch" {
convertToEpoch(r, epoch)
}

if timeFormat == "rfc3339" {
convertToRfc3339(r)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are these two mutually exclusive? If so, it should probably be an if-then-else.

It seems like setting time_format=rfc3339 and epoch=foo should give the user an error to avoid support calls of people trying to combine them to get RFC3339 with second precision or such.

}

// Ensure the handler returns results with RFC3339 timestamp format for multiple series.
func TestHandler_Query_RFC3339_MultipleSeries(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If it was me, I would combine the two test functions to avoid copying and pasting the boilerplate.

Copy link
Copy Markdown
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

Some comments and questions.

devanbenz added 2 commits July 9, 2025 07:31
* error if incorrect param
* update naming for converter function
* combine tests
if timeFormat == "" {
timeFormat = "epoch"
} else if !slices.Contains(timeFormats, timeFormat) {
h.httpError(rw, `time format must be one of the following: "epoch", "rfc3339"`, http.StatusBadRequest)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Take advantage of the timeFormats that's already defined (and fix the parameter name). Example:

h.httpError(rw, fmt.Sprintf("time_format must be one of the following: %s", strings.Join(timeFormats, ",")), http.StatusBadRequest)

Comment on lines +1831 to +1840
func convertToRfc3339Nano(r *query.Result) {
for _, s := range r.Series {
for _, v := range s.Values {
if ts, ok := v[0].(time.Time); ok {
v[0] = ts.Format(time.RFC3339Nano)
}
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not required, but the time format (time.RFC3339Nano) could be made a parameter instead of hard-coded. The call would then be something like convertToTimeFormat(r, time.RFC3339Nano). The code would be ready to add different time formats later if requested.

convertToEpoch is sufficiently different that trying to combine a convertToTimeFormat with it would probably make the code harder to read. It's possible by passing the conversion function as a function parameter, but I don't think that would really improve the code.

Comment on lines +607 to +624
name: "single series",
testFunc: func(t *testing.T) {
h := NewHandler(false)
testTime := time.Date(2021, 1, 1, 12, 0, 0, 0, time.UTC)

h.StatementExecutor.ExecuteStatementFn = func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
ctx.Results <- &query.Result{
StatementID: 1,
Series: []*models.Row{{
Name: "series0",
Columns: []string{"time", "value"},
Values: [][]interface{}{
{testTime, 42},
},
}},
}
return nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What I was thinking was that all the tests could use the same data set. They're almost doing that already. There's also still a lot of the same boilerplate code around the test bodies.

Copy link
Copy Markdown
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

Added some comments on the latest commits.

* Rename convertToRfc3339Nano to convertToTimeFormat
* allow time formatting to be passed as parameter
* adjust error handling to use already defined timeFormats
* merge test data for test cases to reduce boilerplate
@devanbenz devanbenz requested a review from philjb July 10, 2025 17:57
Copy link
Copy Markdown
Contributor

@philjb philjb left a comment

Choose a reason for hiding this comment

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

LGTM - nice tests.

@philjb philjb merged commit 57da7aa into master-1.x Jul 10, 2025
9 checks passed
devanbenz added a commit that referenced this pull request Jul 25, 2025
* feat: Adds time_format param for httpd

* This PR will add a time_format parameter which takes in the value "epoch" or "rfc3339". The default will be "epoch" depending on the value output timestamps will be formatted in epoch or rfc3339.

Closes FR#615

* feat: Adding some changes
* error if incorrect param
* update naming for converter function
* combine tests

* chore: fmt'ing

* feat: A few modifications
* Rename convertToRfc3339Nano to convertToTimeFormat
* allow time formatting to be passed as parameter
* adjust error handling to use already defined timeFormats
* merge test data for test cases to reduce boilerplate

(cherry picked from commit 57da7aa)
devanbenz added a commit that referenced this pull request Sep 24, 2025
* feat: Adds time_format param for httpd

* This PR will add a time_format parameter which takes in the value "epoch" or "rfc3339". The default will be "epoch" depending on the value output timestamps will be formatted in epoch or rfc3339.

Closes FR#615

* feat: Adding some changes
* error if incorrect param
* update naming for converter function
* combine tests

* chore: fmt'ing

* feat: A few modifications
* Rename convertToRfc3339Nano to convertToTimeFormat
* allow time formatting to be passed as parameter
* adjust error handling to use already defined timeFormats
* merge test data for test cases to reduce boilerplate

(cherry picked from commit 57da7aa)
devanbenz added a commit that referenced this pull request Sep 25, 2025
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.

3 participants