Skip to content
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

Add Result wrapper when querying #12

Open
dzunk opened this issue Jun 30, 2020 · 0 comments · May be fixed by #64
Open

Add Result wrapper when querying #12

dzunk opened this issue Jun 30, 2020 · 0 comments · May be fixed by #64
Labels
enhancement New feature or request
Milestone

Comments

@dzunk
Copy link
Owner

dzunk commented Jun 30, 2020

Right now, the result of queries like TS.RANGE is an array of value objects. To make this more user-friendly for using the raw data (e.g. displaying on a chart), it would be nice to return a Redis::TimeSeries::Result wrapper to hold those value objects, and provide convenience wrappers for converting them into hashes and arrays.

[9] pry(main)> result
=> [#<Redis::TimeSeries::Sample:0x00007f803088b7f8 @time=2020-06-29 23:43:12 -0700, @value=0.1e1>,
 #<Redis::TimeSeries::Sample:0x00007f803088b640 @time=2020-06-29 23:43:12 -0700, @value=0.2e1>,
 #<Redis::TimeSeries::Sample:0x00007f803088b488 @time=2020-06-29 23:43:12 -0700, @value=0.3e1>]

# Could return 2d array/hash of times and values (Time object probably more useful elsewhere in a Ruby app)
[10] pry(main)> result.map { |r| [r.time, r.value] }
=> [[2020-06-29 23:43:12 -0700, 0.1e1], [2020-06-29 23:43:12 -0700, 0.2e1], [2020-06-29 23:43:12 -0700, 0.3e1]]
[11] pry(main)> result.to_h { |r| [r.time, r.value] }
=> {2020-06-29 23:43:12 -0700=>0.1e1, 2020-06-29 23:43:12 -0700=>0.2e1, 2020-06-29 23:43:12 -0700=>0.3e1}

# Or, convert to raw milliseconds first (closer to raw reply from Redis)
[12] pry(main)> result.map { |r| [r.time.ts_msec, r.value] }
=> [[1593499392263, 0.1e1], [1593499392276, 0.2e1], [1593499392298, 0.3e1]]
[13] pry(main)> result.to_h { |r| [r.time.ts_msec, r.value] }
=> {1593499392263=>0.1e1, 1593499392276=>0.2e1, 1593499392298=>0.3e1}
@dzunk dzunk added the enhancement New feature or request label Jun 30, 2020
@dzunk dzunk added this to the 1.0.0 milestone Jun 30, 2020
@dzunk dzunk linked a pull request Jun 19, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant