Skip to content

Instantly share code, notes, and snippets.

@HosseyNJF
Last active November 24, 2023 12:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HosseyNJF/5ca033f594469253f7d49525c1a02e78 to your computer and use it in GitHub Desktop.
Save HosseyNJF/5ca033f594469253f7d49525c1a02e78 to your computer and use it in GitHub Desktop.
Delete data from exported time-series from Prometheus / VictoriaMetrics in a time range.

Usage

Note that the response cache must be deleted after these steps in order to remove previously cached results - see more details here.

#!/usr/bin/python3
import json
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-i", "--input", dest="input",
help="Input JSON file from VictoriaMetrics / Prometheus", metavar="FILE", required=True)
parser.add_argument("-o", "--output", dest="output",
help="Output JSON file", metavar="FILE", required=True)
parser.add_argument("--from", dest="from_date",
help="Start of the unwanted time-range (UNIX timestamp)", type=int, required=True)
parser.add_argument("--to", dest="to_date",
help="End of the unwanted time-range (UNIX timestamp)", type=int, required=True)
args = parser.parse_args()
with open(args.input, "r") as input_file:
with open(args.output, "w+") as output_file:
for line in input_file:
data = json.loads(line)
indices = []
for idx, timestamp in enumerate(data['timestamps']):
if args.from_date <= timestamp <= args.to_date:
indices.append(idx)
data['timestamps'] = [v for i, v in enumerate(data['timestamps']) if i not in indices]
data['values'] = [v for i, v in enumerate(data['values']) if i not in indices]
output_file.write(json.dumps(data, separators=(',', ':')) + "\n")
if __name__ == '__main__':
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment