milesmcc / shynet

Modern, privacy-friendly, and detailed web analytics that works without cookies or JS.
Apache License 2.0
2.89k stars 183 forks source link

refactor(core): make model methods smaller #232

Closed sumit4613 closed 11 months ago

sumit4613 commented 1 year ago

Was trying to write tests for methods in core.models but the methods are too big and are doing a lot of things. That's why refactored in smaller methods.

@milesmcc If you think this MR is creating noise or not required, I'll close it. Please let me know.

milesmcc commented 1 year ago

I like it! We've definitely accumulated cruft over time. I just need to properly test it locally.

milesmcc commented 1 year ago

@sumit4613 apologies for the delay. Any interest in rebasing this PR?

sumit4613 commented 1 year ago

@milesmcc Rebased.

milesmcc commented 1 year ago

Thank you.

milesmcc commented 1 year ago

Thanks again for this contribution! I'm running into this error when I test your change. Is it possible there's an edge case missing here?

Environment:

Request Method: GET
Request URL: http://localhost:8000/dashboard/service/1d2d1404-73c9-4b71-b48f-8370bb4f6b8e/

Django Version: 4.1.6
Python Version: 3.10.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.humanize',
 'health_check',
 'health_check.db',
 'health_check.cache',
 'rules.apps.AutodiscoverRulesConfig',
 'a17t',
 'core',
 'dashboard.apps.DashboardConfig',
 'analytics',
 'api',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'debug_toolbar',
 'corsheaders']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.sites.middleware.CurrentSiteMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']

Template error:
In template /Users/miles/Source/shynet/shynet/dashboard/templates/dashboard/includes/stats_status_chip.html, error at line 3
   can't compare datetime.datetime to datetime.date
   1 : {% load humanize %}
   2 : 
   3 :  {% with stats=object.get_daily_stats %} 
   4 : {% if stats.currently_online > 0 %}
   5 : <span class="chip ~positive !high whitespace-nowrap">
   6 :     {{stats.currently_online|intcomma}} online
   7 : </span>
   8 : {% endif %}
   9 : {% endwith %}

Traceback (most recent call last):
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/response.py", line 114, in render
    self.content = self.rendered_content
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/response.py", line 92, in rendered_content
    return template.render(context, self._request)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/backends/django.py", line 62, in render
    return self.template.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/loader_tags.py", line 208, in render
    return template.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 177, in render
    return self._render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/defaulttags.py", line 540, in render
    values = {key: val.resolve(context) for key, val in self.extra_context.items()}
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/defaulttags.py", line 540, in <dictcomp>
    values = {key: val.resolve(context) for key, val in self.extra_context.items()}
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 715, in resolve
    obj = self.var.resolve(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 847, in resolve
    value = self._resolve_lookup(context)
  File "/Users/miles/Library/Caches/pypoetry/virtualenvs/shynet--o1YXJxF-py3.10/lib/python3.10/site-packages/django/template/base.py", line 914, in _resolve_lookup
    current = current()
  File "/Users/miles/Source/shynet/shynet/core/models.py", line 144, in get_daily_stats
    return self.get_core_stats(
  File "/Users/miles/Source/shynet/shynet/core/models.py", line 158, in get_core_stats
    main_data = self.get_relative_stats(start_time, end_time)
  File "/Users/miles/Source/shynet/shynet/core/models.py", line 282, in get_relative_stats
    chart_data, chart_tooltip_format, chart_granularity = self._get_chart_data(
  File "/Users/miles/Source/shynet/shynet/core/models.py", line 439, in _get_chart_data
    sorted_chart_data = sorted(chart_data.items(), key=lambda k: k[0])

Exception Type: TypeError at /dashboard/service/1d2d1404-73c9-4b71-b48f-8370bb4f6b8e/
Exception Value: can't compare datetime.datetime to datetime.date
sumit4613 commented 1 year ago

@milesmcc I'll check it out.

milesmcc commented 11 months ago

Going to close this for now but go ahead and reopen when the bug is fixed.