# Buggy code
def stop_timer(self, start):
    db.metrics.update_one(
        {"group": self.group, "name": self.name},
        {
            "$set": {
                "group": self.group,
                "name": self.name,
                "title": self.title,
                "description": self.description,
                "type": "timer",
            },
            "$inc": {"count": 1, "totalTime": self._time_in_millis() - start},
        },
        True,
    )


# Traceback (ValueError)
5.172.237.230 - - [13/Jun/2015 19:50:25] "GET /management/status HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/decorator.py", line 120, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/alerta_server-4.4.4-py2.7.egg/alerta/app/management/views.py", line 121, in status
total_alert_gauge.set(db.get_count())
File "/usr/local/lib/python2.7/dist-packages/alerta_server-4.4.4-py2.7.egg/alerta/app/metrics.py", line 38, in set
True
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 635, in update_one
common.validate_ok_for_update(update)
File "/usr/local/lib/python2.7/dist-packages/pymongo/common.py", line 377, in validate_ok_for_update
raise ValueError('update only works with $ operators')
ValueError: update only works with $ operators

# Fixed code
def stop_timer(self, start):
    db.metrics.update_one(
        {"group": self.group, "name": self.name},
        {
            "$set": {
                "group": self.group,
                "name": self.name,
                "title": self.title,
                "description": self.description,
                "type": "timer",
            },
            "$inc": {"count": 1, "totalTime": self._time_in_millis() - start},
        },
        upsert=True,
    )