fwiz
fwiz

Reputation: 21

Openwrt Adblock Statistics

I am trying to log blocked Requests by Adblock/openwrt

I am using this package:

1.https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md

Hey, I am trying to log total "blocked" requests in a json file like this :

   {
  "TOTAL": 39100,
  "BLOCKED": 16172
   }

which is being extracted from "adb_reports.json".

But, when i refresh the report to generate current report json file, start_timestamp and end_timestamp changes (dont know how and when it changes), so i cant compute actual total numbers.

my code is attached below.

#!/bin/bash

# Path to the JSON file
JSON_S_FILE="adb_report.json"
# Path to extracted datd file
JSON_O_FILE="stored_data.json"

# Check if the JSON file exists
if [ ! -f "$JSON_S_FILE" ]; then
    echo "Error: JSON file '$JSON_S_FILE' not found!"
    exit 1
fi

# Extract specific values using jq
TOTAL=$(jq -r '.total' "$JSON_S_FILE")
BLOCKED=$(jq -r '.blocked' "$JSON_S_FILE")

#updating values in extracted data file

if [ -s $JSON_O_FILE ] && jq empty $JSON_O_FILE >/dev/null 2>&1; then
#if exists & valid
    echo "The JSON file is valid and not empty."
    jq --arg total "$TOTAL" --arg blocked "$BLOCKED" \
       '{"TOTAL": ((.TOTAL|tonumber) + ($total|tonumber)), 
         "BLOCKED": ((.BLOCKED|tonumber) + ($blocked|tonumber))}' \
       "$JSON_O_FILE" > temp.json && mv temp.json "$JSON_O_FILE"
else
#if not, create & write!!
    echo "The JSON file is either empty or not valid."
    echo "{\"TOTAL\": \"$TOTAL\", \"BLOCKED\": \"$BLOCKED\"}" >stored_data.json
fi

Problem is that this package doesn't generate adb_report.json on its own, when I generate that json file the time stamps changes, don't know when and how,

sample Json is like below:

  "start_date": "2024-12-24",
        "start_time": "22:13:12",
        "end_date": "2024-12-25",
        "end_time": "01:05:04",
        "total": "6076",
        "blocked": "4355",
        "percent": "71.68%",

anyone know how the starting and ending time stamps change; or is there any better way to log total block requests??

thank you!

Upvotes: 0

Views: 51

Answers (0)

Related Questions