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

NOISSUE - Refactor InfluxDB Reader: explicit check event + add safe conversion #1460

Merged
merged 12 commits into from
Nov 8, 2021

Conversation

tzzed
Copy link
Contributor

@tzzed tzzed commented Sep 10, 2021

What does this do?

This PR is a simple refactoring of influxdb measurement data.

Which issue(s) does this PR fix/relate to?

No issue related to this PR.

List any changes that modify/break current functionality

Have you included tests for your changes?

Still working

Did you document any new/modified functionality?

No

Notes

@tzzed tzzed requested a review from a team as a code owner September 10, 2021 21:38
readers/influxdb/messages.go Outdated Show resolved Hide resolved
readers/influxdb/messages_test.go Outdated Show resolved Hide resolved
@drasko
Copy link
Contributor

drasko commented Sep 10, 2021

@tzzed thanks for the PR. Please sync your branch to the master.

@arvindh123 please review this PR

@tzzed tzzed force-pushed the refactore/reader_influxdb branch 2 times, most recently from b2b2916 to 04e569e Compare September 11, 2021 12:16
@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2021

Codecov Report

Merging #1460 (5767423) into master (b570c38) will decrease coverage by 0.02%.
The diff coverage is 61.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1460      +/-   ##
==========================================
- Coverage   68.80%   68.78%   -0.03%     
==========================================
  Files         132      132              
  Lines       10581    10587       +6     
==========================================
+ Hits         7280     7282       +2     
- Misses       2726     2727       +1     
- Partials      575      578       +3     
Impacted Files Coverage Δ
readers/influxdb/messages.go 78.44% <61.11%> (-2.93%) ⬇️
things/service.go 74.45% <0.00%> (+0.86%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b570c38...5767423. Read the comment docs.

@tzzed tzzed force-pushed the refactore/reader_influxdb branch 2 times, most recently from 0efd3fc to 0508757 Compare September 11, 2021 12:23
Signed-off-by: tzzed <zerouali.t@gmail.com>
readers/influxdb/messages.go Outdated Show resolved Hide resolved
Copy link
Contributor

@arvindh123 arvindh123 left a comment

Choose a reason for hiding this comment

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

@drasko, It looks good

@drasko drasko changed the title NOISSUE - refactore(reader/influxdb): explicit check event + add safe conversion NOISSUE - Refactor InfluxDB Reader: explicit check event + add safe conversion Sep 13, 2021
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

Honestly, I do not see value in this PR. I would like that @dusanb94 reviews, but I do not find new code better or more comprehensible.

for i := 0; i < len(result.Messages); i++ {
m := result.Messages[i]
// Remove time as it is not sent by the client.
delete(m.(map[string]interface{}), "time")

result.Messages[i] = m
}
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %s", desc, err))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this assert removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The assert checks the error after the loop but it should be done before just after getting the result, err := reader.ReadAll(tc.chanID, tc.pageMeta) .

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it done?

Copy link
Contributor

Choose a reason for hiding this comment

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

Again - is this done?

Copy link
Contributor Author

@tzzed tzzed Oct 21, 2021

Choose a reason for hiding this comment

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

I put it after reader.ReadAll(tc.chanID, tc.pageMeta)

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks ok but in any case don't resolve remarks by hand please.

@tzzed
Copy link
Contributor Author

tzzed commented Sep 15, 2021

the len's comparison to zero is more clear because we want to check the 0 value an empty array. len(v) < 1 includes negatives values.

About the safe cast is just to avoid panic.

I just let myself to do this PR as a "good first issue" but you are free to close it.

Signed-off-by: tzzed <zerouali.t@gmail.com>
@drasko
Copy link
Contributor

drasko commented Sep 16, 2021

@nmarcetic and @dusanb94 need to review and approve this one.

readers/influxdb/messages.go Outdated Show resolved Hide resolved
return readers.MessagesPage{}, nil
}

var messages []readers.Message

Copy link
Contributor

Choose a reason for hiding this comment

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

No need for blank line

@@ -140,7 +140,8 @@ func fmtCondition(chanID string, rpm readers.PageMetadata) string {
if err != nil {
return condition
}
json.Unmarshal(meta, &query)

Copy link
Contributor

Choose a reason for hiding this comment

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

No need for blank line.

@@ -140,7 +140,8 @@ func fmtCondition(chanID string, rpm readers.PageMetadata) string {
if err != nil {
return condition
}
json.Unmarshal(meta, &query)

_ = json.Unmarshal(meta, &query)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not ignore errors.

readers/influxdb/messages.go Outdated Show resolved Hide resolved
readers/influxdb/messages_test.go Outdated Show resolved Hide resolved
for i := 0; i < len(result.Messages); i++ {
m := result.Messages[i]
// Remove time as it is not sent by the client.
delete(m.(map[string]interface{}), "time")

result.Messages[i] = m
}
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %s", desc, err))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it done?

Signed-off-by: tzzed <zerouali.t@gmail.com>
@dborovcanin
Copy link
Collaborator

Honestly, I do not see value in this PR. I would like that @dusanb94 reviews, but I do not find new code better or more comprehensible.

The refactoring makes sense. @tzzed please consider adding better error handling and InfluxDB records parsing. Those improvements can be added to this PR to improve its value.

readers/influxdb/messages.go Outdated Show resolved Hide resolved
for i := 0; i < len(result.Messages); i++ {
m := result.Messages[i]
// Remove time as it is not sent by the client.
delete(m.(map[string]interface{}), "time")

result.Messages[i] = m
}
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %s", desc, err))
Copy link
Contributor

Choose a reason for hiding this comment

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

Again - is this done?

@drasko
Copy link
Contributor

drasko commented Oct 11, 2021

Honestly, I do not see value in this PR. I would like that @dusanb94 reviews, but I do not find new code better or more comprehensible.

The refactoring makes sense. @tzzed please consider adding better error handling and InfluxDB records parsing. Those improvements can be added to this PR to improve its value.

OK, then I left a few comments, and these must be corrected before we are ready to merge.

@drasko
Copy link
Contributor

drasko commented Oct 15, 2021

@tzzed if there is no action from your side to correct mentioned comment, this PR will be closed due to the inactivity.

@tzzed tzzed requested a review from drasko October 21, 2021 12:21
@@ -6,6 +6,7 @@ import (
"time"

influxdata "github.com/influxdata/influxdb/client/v2"

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@drasko
Copy link
Contributor

drasko commented Oct 21, 2021

@tzzed CI is failing. Please check your tests, you have introduced regression:

    messages_test.go:378: 
        	Error Trace:	messages_test.go:378
        	Error:      	Not equal: 
        	            	expected: 0x4f
        	            	actual  : 0x64
        	Test:       	TestReadAll
        	Messages:   	read message with to: expected 79 got 100
    messages_test.go:378: 
        	Error Trace:	messages_test.go:378
        	Error:      	Not equal: 
        	            	expected: 0x15
        	            	actual  : 0x64
        	Test:       	TestReadAll
        	Messages:   	read message with from: expected 21 got 100
--- FAIL: TestReadAll (0.62s)
=== RUN   TestReadJSON
--- PASS: TestReadJSON (0.12s)
FAIL
coverage: 86.4% of statements
FAIL	github.com/mainflux/mainflux/readers/influxdb	20.089s
FAIL

@tzzed
Copy link
Contributor Author

tzzed commented Oct 21, 2021

@drasko I did not introduced regression. This is because of the msg.Time setting. now is in second but when it is equal to 0, 5, or 20 it fails. I invite you to try to set it into this 3 values in the master branch and run the test.

now := float64(time.Now().UTC().Second())

	for i := 0; i < msgsNum; i++ {
		// Mix possible values as well as value sum.
		msg := m
		msg.Time = now - float64(i)  // This operations has a random fails or success.

I will change it tomorrow if you are ok @drasko ? It will add a real value to this PR.

Signed-off-by: tzzed <zerouali.t@gmail.com>
now := float64(time.Now().UTC().Second())
rand.Seed(time.Now().UnixNano())
to := msgsNum
from := 21
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use this as a constant in tests with a range search instead of literals, as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have seen this line but I have not enough context about this lines. How do you see the usage.Should I create a const for each value in test.

pageMeta: readers.PageMetadata{
				Offset: 0,
				Limit:  uint64(len(messages[21:])),
				To:     messages[20].Time,
			},

Copy link
Collaborator

Choose a reason for hiding this comment

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

No need, I think. Replacing literals 20 and 21 with from and from + 1 (and maybe rename from to offset) will be just fine.

Signed-off-by: tzzed <zerouali.t@gmail.com>
Signed-off-by: tzzed <zerouali.t@gmail.com>
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

LGTM

@drasko drasko merged commit 9f5a319 into absmach:master Nov 8, 2021
fbugarski pushed a commit to fbugarski/mainflux that referenced this pull request Nov 17, 2021
…onversion (absmach#1460)

* refactore(reader/influxdb): explicit check event + add safe conversion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Delete useless space.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion, replace require.NoError(t, err) by assert.Nil

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Fix tests when message time is randomly equal to 0 in tests cases.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion use constant

Signed-off-by: tzzed <zerouali.t@gmail.com>

* use const offset

Signed-off-by: tzzed <zerouali.t@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
mteodor pushed a commit to mteodor/mainflux that referenced this pull request Dec 23, 2021
…onversion (absmach#1460)

* refactore(reader/influxdb): explicit check event + add safe conversion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Delete useless space.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion, replace require.NoError(t, err) by assert.Nil

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Fix tests when message time is randomly equal to 0 in tests cases.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion use constant

Signed-off-by: tzzed <zerouali.t@gmail.com>

* use const offset

Signed-off-by: tzzed <zerouali.t@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
bioinformatx pushed a commit to bioinformatx/mainflux that referenced this pull request Jan 28, 2022
…onversion (absmach#1460)

* refactore(reader/influxdb): explicit check event + add safe conversion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Delete useless space.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion, replace require.NoError(t, err) by assert.Nil

Signed-off-by: tzzed <zerouali.t@gmail.com>

* Fix tests when message time is randomly equal to 0 in tests cases.

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion use constant

Signed-off-by: tzzed <zerouali.t@gmail.com>

* use const offset

Signed-off-by: tzzed <zerouali.t@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
Signed-off-by: skovacevic <stefan.kovacevic@mainflux.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants