# Buggy code
def hostinfo(ip):
    hostinfo = {}
    count, context = current_app.elastic.gethost(ip)
    if count == 0:
        return abort(404)
    hostinfo["history"] = count
    screenshot_count = current_app.elastic.count_host_screenshots(ip)
    hostinfo["screenshot_count"] = screenshot_count
    screenshots = 0
    screenshotTypes = [
        "screenshots",
        "headshot",
        "vncheadshot",
        "httpheadshot",
        "httpsheadshot",
    ]
    for hs in screenshotTypes:
        if context.get(hs):
            if hs == "screenshots":
                # 0.6.5 iterating screenshots instead of screenshot types
                for item in context.get(hs):
                    screenshots += 1
            else:
                screenshots += 1
    hostinfo["screenshots"] = screenshots
    if context.get("hostname"):
        hostinfo["hostname"] = context.get("hostname")
    return hostinfo, context


# Traceback (TypeError)
Nov 05 17:22:50 natlas-server bash[10026]: [2019-11-05 17:22:50,681] ERROR in app: Exception on /api/getwork [GET]
Nov 05 17:22:50 natlas-server bash[10026]: Traceback (most recent call last):
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
Nov 05 17:22:50 natlas-server bash[10026]:     response = self.full_dispatch_request()
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     rv = self.handle_user_exception(e)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
Nov 05 17:22:50 natlas-server bash[10026]:     reraise(exc_type, exc_value, tb)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
Nov 05 17:22:50 natlas-server bash[10026]:     raise value
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     rv = self.dispatch_request()
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     return self.view_functions[rule.endpoint](**req.view_args)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/auth/wrappers.py", line 41, in decorated_function
Nov 05 17:22:50 natlas-server bash[10026]:     return f(*args, **kwargs)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/api/routes.py", line 70, in getwork
Nov 05 17:22:50 natlas-server bash[10026]:     count, context = current_app.elastic.gethost_scan_id(rand)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 238, in gethost_scan_id
Nov 05 17:22:50 natlas-server bash[10026]:     if not self.checkStatus():
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 60, in checkStatus
Nov 05 17:22:50 natlas-server bash[10026]:     if not self.attemptReconnect():
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 43, in attemptReconnect
Nov 05 17:22:50 natlas-server bash[10026]:     delta = now - self.lastReconnectAttempt
Nov 05 17:22:50 natlas-server bash[10026]: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType

# Fixed code
def hostinfo(ip):
    hostinfo = {}
    count, context = current_app.elastic.get_host(ip)
    if count == 0:
        return abort(404)
    hostinfo["history"] = count
    screenshot_count = current_app.elastic.count_host_screenshots(ip)
    hostinfo["screenshot_count"] = screenshot_count
    screenshots = 0
    screenshotTypes = [
        "screenshots",
        "headshot",
        "vncheadshot",
        "httpheadshot",
        "httpsheadshot",
    ]
    for hs in screenshotTypes:
        if context.get(hs):
            if hs == "screenshots":
                # 0.6.5 iterating screenshots instead of screenshot types
                for item in context.get(hs):
                    screenshots += 1
            else:
                screenshots += 1
    hostinfo["screenshots"] = screenshots
    if context.get("hostname"):
        hostinfo["hostname"] = context.get("hostname")
    return hostinfo, context