Add metrics in the event resource.

You can query the DB only once by moving ``query`` outside ``if``.