babybuddy / babybuddy

A :baby: buddy to help caregivers track sleep, feedings, diaper changes, tummy time and more to learn about and predict baby's needs without (as much) guess work.
http://docs.baby-buddy.net/
BSD 2-Clause "Simplified" License
2.1k stars 258 forks source link

KeyError 7 in /app/www/public/dashboard/templatetags/cards.py line 85, in card_diaperchange_types #792

Open AstroPhotoJay opened 6 months ago

AstroPhotoJay commented 6 months ago

After upgrading from v2.2.0-ls123 to latest, I am getting this error whenever I try to visit the front end.

Any help to try and solve this issue would be appreciated.

Below is further info with sensitive info edited out.

Traceback

Environment:
Request Method: GET
Request URL: http://sub.domain.tld/children/Firstname-Lastname/dashboard/

Django Version: 5.0.1
Python Version: 3.11.8
Installed Applications:
['api',
 'babybuddy.apps.BabyBuddyConfig',
 'core.apps.CoreConfig',
 'dashboard',
 'reports',
 'axes',
 'django_filters',
 'rest_framework',
 'rest_framework.authtoken',
 'widget_tweaks',
 'imagekit',
 'storages',
 'import_export',
 'qr_code',
 'dbsettings',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'babybuddy.middleware.RollingSessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'babybuddy.middleware.UserTimezoneMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'babybuddy.middleware.UserLanguageMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'axes.middleware.AxesMiddleware',
 'babybuddy.middleware.HomeAssistant']

Template error:
In template /app/www/public/dashboard/templates/dashboard/child.html, error at line 50
   7
   40 :         <div class="col-sm-6 col-lg-4">
   41 :             {% card_sleep_recent object %}
   42 :         </div>
   43 :         <div class="col-sm-6 col-lg-4">
   44 :             {% card_sleep_naps_day object %}
   45 :         </div>
   46 :         <div class="col-sm-6 col-lg-4">
   47 :             {% card_tummytime_day object %}
   48 :         </div>
   49 :         <div class="col-sm-6 col-lg-4">
   50 :              {% card_diaperchange_types object %} 
   51 :         </div>
   52 :     </div>
   53 : {% endblock %}
   54 : 
   55 : {% block javascript %}
   56 :     {% if user.settings.dashboard_refresh_rate %}
   57 :         <script type="application/javascript">
   58 :             BabyBuddy.Dashboard.watch('dashboard-child', {{ user.settings.dashboard_refresh_rate_milliseconds }});
   59 :         </script>
   60 :     {% else %}

Traceback (most recent call last):
  File "/lsiopy/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
               ^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/response.py", line 114, in render
    self.content = self.rendered_content
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/response.py", line 92, in rendered_content
    return template.render(context, self._request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/template/library.py", line 258, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/www/public/dashboard/templatetags/cards.py", line 85, in card_diaperchange_types
    stats[key]["changes"] += 1
    ^^^^^^^^^^

Exception Type: KeyError at /children/Firstname-Lastname/dashboard/
Exception Value: 7

API and /admin/ both work and are accessible.

With daylight savings having just ended for us, I altered the time of a nappy change which may have been an issue but this did not work.

I tried the solution suggested in https://github.com/babybuddy/babybuddy/issues/140 but received the below errors as well...

root@b7497f54d83c:/app/www/public# python manage.py makemigrations
Traceback (most recent call last):
  File "/app/www/public/manage.py", line 26, in <module>
    execute_from_command_line(sys.argv)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 453, in execute
    self.check()
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/core/checks/caches.py", line 17, in check_default_cache_is_configured
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
                                  ^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/lsiopy/lib/python3.11/site-packages/django/conf/__init__.py", line 69, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
root@b7497f54d83c:/app/www/public# python manage.py migrate
Traceback (most recent call last):
  File "/app/www/public/manage.py", line 26, in <module>
    execute_from_command_line(sys.argv)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/core/management/base.py", line 103, in wrapper
    saved_locale = translation.get_language()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/utils/translation/__init__.py", line 210, in get_language
    return _trans.get_language()
           ^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/utils/translation/__init__.py", line 65, in __getattr__
    if settings.USE_I18N:
       ^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.11/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/lsiopy/lib/python3.11/site-packages/django/conf/__init__.py", line 69, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Stack

Docker compose (deployed via portainer)

#https://github.com/babybuddy/babybuddy
version: "3"
services:
  babybuddy:
    image: linuxserver/babybuddy:latest
    container_name: babybuddy
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Australia/Sydney
      - SECURE_PROXY_SSL_HEADER=True
      - CSRF_TRUSTED_ORIGINS=http://xxx.xxx.xxx.xxx:8100,https://sub.domain.tld
      - CSRF_COOKIE_SECURE=True
      - SESSION_COOKIE_SECURE=True
      - DEBUG=true
    volumes:
      - /path/to/folder/babybuddy/appdata:/config
    ports:
      - 8100:8000
    restart: unless-stopped
#    command: python manage.py migrate #trying to fix error
    labels:
      - wud.watch.digest=true #for change monitoring
    deploy:
      resources:
        reservations:
          cpus: '0.25'
          memory: 300M

NGINX and cloudlfare tunnel used to expose service to the internet.

jfschmakeit commented 6 months ago

Same issue here started occurring overnight. (The dashboard stopped working between 2-7am. I believe there was no new diaper change or anything else recorded during that time.) I haven't updated the app or changed any settings recently either. I can still access all other screens directly, just the dashboard doesn't load anymore.

I do wonder if daylight savings is a potential issue here - our timezone changed to AEST (Australian Eastern Standard Time - "winter time" in Australia) two days ago. Happy to do more testing.

Digging into the stacktrace (mine looks identical to the one in included in the initial report), here's the relevant part:

/app/www/public/dashboard/templatetags/cards.py, line 85, in card_diaperchange_types 
        stats[key]["changes"] += 1

Local vars:

Variable Value
child
context [{'True': True, 'False': False, 'None': None}, {}, {}, {'object': , 'child': , 'view': <dashboard.views.ChildDashboard object at 0x7fdefe8ab490>, 'LANGUAGE_CODE': 'en-gb', 'LOCALE': 'en_GB', 'TIMEZONE': 'Australia/Sydney'}]
date None
empty False
instance
instances <QuerySet [, , , , , , , , , , , , , , , , , , , , '...(remaining elements truncated)...']>
key 7
max_date datetime.datetime(2024, 4, 10, 0, 0, 0, 529603, tzinfo=zoneinfo.ZoneInfo(key='Australia/Sydney'))
min_date datetime.datetime(2024, 4, 3, 0, 0, 0, 529603, tzinfo=zoneinfo.ZoneInfo(key='Australia/Sydney'))
stats {0: {'changes': 4.0, 'empty': 0.0, 'solid': 0.0, 'wet': 4.0},
1: {'changes': 8.0, 'empty': 0.0, 'solid': 1.0, 'wet': 7.0},
2: {'changes': 7.0, 'empty': 0.0, 'solid': 1.0, 'wet': 6.0},
3: {'changes': 7.0, 'empty': 0.0, 'solid': 1.0, 'wet': 7.0},
4: {'changes': 9.0, 'empty': 0.0, 'solid': 2.0, 'wet': 9.0},
5: {'changes': 8.0, 'empty': 0.0, 'solid': 2.0, 'wet': 8.0},
6: {'changes': 8.0, 'empty': 0.0, 'solid': 2.0, 'wet': 8.0}}
time datetime.datetime(2024, 4, 9, 13, 9, 41, 529603, tzinfo=zoneinfo.ZoneInfo(key='Australia/Sydney'))
week_total 0
x 6
AstroPhotoJay commented 6 months ago

This issue seems to have resolved itself as of 10 APR 2024 at 16:30 AEST.

I had to restart my server this morning, but I did not think to check BB at the time so am not sure if this helped or not. No other changes made (that I can think of).

jfschmakeit commented 6 months ago

It took an extra day for me, but the issue seems to have resolved itself on my end too. I did not restart BB or change any settings - the dashboard suddenly started working again this morning.

(I made a copy of the database when the issue occurred, just in case. The timing makes me think that it was daylight savings related for us in AEST. Happy to try to reproduce/debug this on my end if that'd be useful.)

cdubz commented 6 months ago

@jfschmakeit if you are comfortable sharing the database that may be helpful for troubleshooting.

The symptoms definitely sound like some data that was new enough to appear on the dashboard was causing the bug and once that data aged out the bug went away.

The last DST change was a month ago so I wasn’t thinking that but maybe it is related to some measure that looks at a month’s worth of data or something 🤔