最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datadog - Getting only the last variable value ordered by timestamp - Stack Overflow

programmeradmin2浏览0评论

I need to create a dashboard that displays the latest status of a product. For example, if a product is logged 10 times a day, I only need to see its most recent status Here's a simple example of what I need: Logs (Only change is the status and timestamp)

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "http_issue"
    }
  ],
  "timestamp": "2025-02-13T10:48:01Z"
}

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "cookie_issue"
    }
  ],
  "timestamp": "2025-02-14T10:48:01Z"
}

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "product_issue"
    }
  ],
  "timestamp": "2025-02-15T10:48:01Z"
}

The expected output has to be a table in the dashboard with this:

total_unique_products_with_http_issues: 0 total_unique_products_with_cookie_issues: 0 total_unique_products_with_product_issues: 1

Do you know the best way to achieve this? I saw something similar with metrics but since the values is a string I don't know if it is possible or not.

I need to create a dashboard that displays the latest status of a product. For example, if a product is logged 10 times a day, I only need to see its most recent status Here's a simple example of what I need: Logs (Only change is the status and timestamp)

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "http_issue"
    }
  ],
  "timestamp": "2025-02-13T10:48:01Z"
}

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "cookie_issue"
    }
  ],
  "timestamp": "2025-02-14T10:48:01Z"
}

{
  "storeId": 3434,
  "products": [
    {
      "code": "my_code",
      "status": "product_issue"
    }
  ],
  "timestamp": "2025-02-15T10:48:01Z"
}

The expected output has to be a table in the dashboard with this:

total_unique_products_with_http_issues: 0 total_unique_products_with_cookie_issues: 0 total_unique_products_with_product_issues: 1

Do you know the best way to achieve this? I saw something similar with metrics but since the values is a string I don't know if it is possible or not.

Share Improve this question edited 2 days ago Xus asked 2 days ago XusXus 4174 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Solution Approach: Sort the Logs by Timestamp – Ensure your dataset is ordered chronologically. Get the Latest Status for Each Product – Use a grouping mechanism to retain only the most recent log entry for each product. Aggregate the Results – Count the occurrences of each unique status. Implementation Example (Python + Pandas) You can use Pandas to process the data efficiently:

python Copy Edit import pandas as pd

Sample log data

logs = [ {"storeId": 3434, "products": [{"code": "my_code", "status": "http_issue"}], "timestamp": "2025-02-13T10:48:01Z"}, {"storeId": 3434, "products": [{"code": "my_code", "status": "cookie_issue"}], "timestamp": "2025-02-14T10:48:01Z"}, {"storeId": 3434, "products": [{"code": "my_code", "status": "product_issue"}], "timestamp": "2025-02-15T10:48:01Z"} ]

Convert logs to a DataFrame

df = pd.DataFrame([ {"storeId": log["storeId"], "code": p["code"], "status": p["status"], "timestamp": log["timestamp"]} for log in logs for p in log["products"] ])

Convert timestamp to datetime for sorting

df["timestamp"] = pd.to_datetime(df["timestamp"])

Get latest status per product

latest_status = df.sort_values("timestamp").groupby("code").last().reset_index()

Count unique occurrences

status_counts = latest_status["status"].value_counts().to_dict()

Format output

output = { f"total_unique_products_with_{key}": value for key, value in status_counts.items() }

print(output) Expected Output json Copy Edit { "total_unique_products_with_http_issues": 0, "total_unique_products_with_cookie_issues": 0, "total_unique_products_with_product_issues": 1 } Alternative SQL Approach If you're working with a SQL database, you can use a window function to get the latest status:

sql Copy Edit WITH ranked_logs AS ( SELECT storeId, code, status, timestamp, ROW_NUMBER() OVER (PARTITION BY code ORDER BY timestamp DESC) AS rn FROM logs_table ) SELECT SUM(CASE WHEN status = 'http_issue' THEN 1 ELSE 0 END) AS total_unique_products_with_http_issues, SUM(CASE WHEN status = 'cookie_issue' THEN 1 ELSE 0 END) AS total_unique_products_with_cookie_issues, SUM(CASE WHEN status = 'product_issue' THEN 1 ELSE 0 END) AS total_unique_products_with_product_issues FROM ranked_logs WHERE rn = 1; This ensures you always select the most recent log entry for each product before performing the status count. source:https://sonicmnu/sonic-drive-in-order-online/

发布评论

评论列表(0)

  1. 暂无评论