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

Reuse graphite time format constant #2588

Merged
merged 1 commit into from
May 25, 2023

Conversation

stveit
Copy link
Contributor

@stveit stveit commented Mar 9, 2023

Fixes #2543.

Put constant for graphite time format in python/nav/metrics/data.py, as this seems to be the core module for getting data from graphite, so it makes sense to have it there instead of in the view of sortedstats.

Updates some logic that uses relative time (years/months etc) to instead use timestamp.

Tested this manually to see if it broke anything, and it seems like everything is fine

@stveit stveit marked this pull request as ready for review March 9, 2023 14:45
@stveit stveit self-assigned this Mar 9, 2023
@github-actions
Copy link

github-actions bot commented Mar 9, 2023

Test results

     12 files       12 suites   12m 20s ⏱️
3 238 tests 3 142 ✔️   96 💤 0
9 189 runs  8 901 ✔️ 288 💤 0

Results for commit 8b463ba.

♻️ This comment has been updated with latest results.

@codecov
Copy link

codecov bot commented Mar 9, 2023

Codecov Report

Merging #2588 (8635ba8) into master (b7f24ec) will increase coverage by 0.00%.
The diff coverage is 58.82%.

❗ Current head 8635ba8 differs from pull request most recent head 8b463ba. Consider uploading reports for the commit 8b463ba to get more accurate results

@@           Coverage Diff           @@
##           master    #2588   +/-   ##
=======================================
  Coverage   54.20%   54.20%           
=======================================
  Files         558      558           
  Lines       40634    40626    -8     
=======================================
- Hits        22026    22023    -3     
+ Misses      18608    18603    -5     
Impacted Files Coverage Δ
python/nav/metrics/data.py 56.03% <33.33%> (+0.38%) ⬆️
python/nav/metrics/thresholds.py 73.80% <61.53%> (+4.04%) ⬆️
python/nav/web/sortedstats/views.py 55.95% <100.00%> (ø)

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@stveit stveit marked this pull request as draft March 9, 2023 14:57
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 9, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stveit stveit force-pushed the graphite_time_format branch from 92ae92b to 084b762 Compare April 11, 2023 09:49
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stveit stveit marked this pull request as ready for review April 11, 2023 10:59
Comment on lines 237 to 246
def interval_to_graphite_start(delta):
"""Converts a timedelta to a usable Graphite time specification.

:type delta: datetime.timedelta

"""
secs = delta.total_seconds()
if secs % YEAR == 0:
return "{0}year".format(int(secs / YEAR))
elif secs % DAY == 0:
return "{0}day".format(int(secs / DAY))
elif secs % MINUTE == 0:
return "{0}min".format(int(secs / MINUTE))
else:
return "{0}s".format(int(secs))
now = datetime.now(tz=ZoneInfo(settings.TIME_ZONE))
start = now - delta
graphite_start = start.strftime(GRAPHITE_TIME_FORMAT)
return graphite_start
Copy link
Contributor

@hmpf hmpf May 24, 2023

Choose a reason for hiding this comment

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

I'd like to know why the previous version was so clunky, maybe graphite got a more sensible format and this is technical debt?

Copy link
Member

Choose a reason for hiding this comment

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

Graphite supports multiple formats, both absolute and relative. The original function becomes "clunky" because it specifically translates a timedelta to Graphite's "time delta syntax". Graphite probably also supports using a number of relative seconds, which could make this less clunky - but the URL would also be a lot less friendly if consumed by a human.

This function specifically works with relative time specifications for at least two reasons:

  1. The generated URL is reusable (and therefore cachable) at any time, and will always be relative to the current time.
  2. The Graphite server can potentially be in a different timezone from the NAV server, and we avoid trying to translate between them by working with relative time specs.

The refactored version will break both of these principles, unfortunately, so I wouldn't approve of it.

Copy link
Contributor

@hmpf hmpf left a comment

Choose a reason for hiding this comment

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

Looks good to me but I hope @lunkwill42 can shed some light on some clunk.

Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

For the reasons specifically stated in https://github.com/Uninett/nav/pull/2588/files#r1204178538, I think you should leave the interval_to_graphite() function untouched. It was written specifically to translate timedeltas to relative time specifications, and you just changed it to translate to absolutes instead.

Other than that, everything seems hunky-dory :)

@stveit stveit force-pushed the graphite_time_format branch from 8635ba8 to e779d49 Compare May 25, 2023 07:06
@stveit stveit force-pushed the graphite_time_format branch from e779d49 to 8b463ba Compare May 25, 2023 07:07
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stveit stveit merged commit ebd60a0 into Uninett:master May 25, 2023
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.

Re-use GRAPHITE_TIME_FORMAT constant
3 participants