forked from kernelci/kernelci-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move `Metrics` class implementation to separate file to reduce number of lines in `api.main` module and to keep the various modules more organized. Otherwise pylint will complain with the below error: "api/main.py: C0302: Too many lines in module (1044/1000) (too-many-lines)" Signed-off-by: Jeny Sadadia <[email protected]>
- Loading branch information
1 parent
7c0b59a
commit 63fb009
Showing
2 changed files
with
53 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
# | ||
# Copyright (C) 2024 Collabora Limited | ||
# Author: enys Fedoryshchenko <[email protected]> | ||
|
||
"""KernelCI API metrics module""" | ||
|
||
import threading | ||
|
||
|
||
class Metrics(): | ||
''' | ||
Class to store and update various metrics | ||
''' | ||
def __init__(self): | ||
''' | ||
Initialize metrics dictionary and lock | ||
''' | ||
self.metrics = {} | ||
self.metrics['http_requests_total'] = 0 | ||
self.lock = threading.Lock() | ||
|
||
# Various internal metrics | ||
def update(self): | ||
''' | ||
Update metrics (reserved for future use) | ||
''' | ||
|
||
def add(self, key, value): | ||
''' | ||
Add a value to a metric | ||
''' | ||
with self.lock: | ||
if key not in self.metrics: | ||
self.metrics[key] = 0 | ||
self.metrics[key] += value | ||
|
||
def get(self, key): | ||
''' | ||
Get the value of a metric | ||
''' | ||
self.update() | ||
with self.lock: | ||
return self.metrics.get(key, 0) | ||
|
||
def all(self): | ||
''' | ||
Get all the metrics | ||
''' | ||
self.update() | ||
with self.lock: | ||
return self.metrics |