You can download this code by clicking the button below.
This code is now available for download.
This function connects to the Grafana API to query the number of users in the specified organization ID within the past day.
Technology Stack : GrafanaClient, datetime
Code Type : The type of code
Code Difficulty : Intermediate
def generate_random_user_count(org_id):
from grafana import GrafanaClient
from datetime import datetime, timedelta
# Create a Grafana client instance
client = GrafanaClient("http://localhost:3000", "admin", "admin")
# Get the current date and one day ago
today = datetime.now()
one_day_ago = today - timedelta(days=1)
# Query the dashboard for user count
query = {
"orgId": org_id,
"range": {
"from": one_day_ago,
"to": today
},
"targets": [
{
"metric": "user_count",
"query": "user_count"
}
]
}
# Execute the query and get the results
results = client.query(query)
# Extract the user count
user_count = results[0]['data']['user_count']
return user_count