-
Notifications
You must be signed in to change notification settings - Fork 2
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
SEARCH-8423 Fix time field type #34
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,24 +57,24 @@ func collapseToSingleLine(query string) string { | |
return regexp.MustCompile("[\r\n\t]+").ReplaceAllString(query, " ") | ||
} | ||
|
||
// Convert the value of the "_time" field to an ISO timestamp string | ||
func timeToIsoString(timeValue interface{}) (bool, string) { | ||
// Convert the value of the "_time" field (expected to be in seconds) to a time.Time in UTC | ||
func criblTimeToGrafanaTime(timeValue interface{}) (bool, time.Time) { | ||
var seconds float64 | ||
switch v := timeValue.(type) { | ||
case float64: | ||
seconds = v | ||
case string: | ||
s, err := strconv.ParseFloat(v, 64) | ||
if err != nil { | ||
return false, "" | ||
return false, time.Time{} | ||
} | ||
seconds = s | ||
default: | ||
return false, "" | ||
return false, time.Time{} | ||
} | ||
wholeSec := int64(seconds) | ||
nanoSec := int64(math.Round((seconds-float64(wholeSec))*1000.0)) * 1000000 // ms precision | ||
return true, time.Unix(wholeSec, nanoSec).UTC().Format(time.RFC3339Nano) | ||
nanoSec := int64(math.Round((seconds-float64(wholeSec))*1000000.0)) * 1000 // microsec precision | ||
return true, time.Unix(wholeSec, nanoSec).UTC() | ||
} | ||
|
||
// Grafana's data.NewField() is super finicky. You're force to supply an array of values, | ||
|
@@ -90,6 +90,8 @@ func makeEmptyConcreteTypeArray(val interface{}) (interface{}, error) { | |
return []float64{}, nil | ||
case bool: | ||
return []bool{}, nil | ||
case time.Time: | ||
return []time.Time{}, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really key. As you populate fields in a data frame, Grafana's data layer infers the field type from this array type. |
||
default: | ||
return nil, fmt.Errorf("unsupported type: %T (%v)", t, t) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: I changed this to microsec because we can, and it provides additional granularly should it be needed. I tried nanoseconds and was getting some floating point precision garbage (i.e. 122999 instead of 123000), so I figured this is good enough for ~100% of the likely scenarios.