forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
69 lines (59 loc) · 2.46 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# ----------------------------------------------------------------------
# metrics uploading
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import argparse
import gzip
import os
import random
from typing import List
from functools import partial
# NOC modules
from noc.config import config
from noc.core.management.base import BaseCommand
from noc.core.liftbridge.base import LiftBridgeClient
from noc.core.ioloop.util import run_sync
class Command(BaseCommand):
TOPIC = "chwriter"
def add_arguments(self, parser):
subparsers = parser.add_subparsers(dest="cmd")
# load command
load_parser = subparsers.add_parser("load")
load_parser.add_argument("--fields", help="Data fields: <table>.<field1>.<fieldN>")
load_parser.add_argument(
"--chunk", type=int, default=config.nsqd.ch_chunk_size, help="Size on chunk"
)
load_parser.add_argument("--rm", action="store_true", help="Remove file after uploading")
load_parser.add_argument("input", nargs=argparse.REMAINDER, help="Input files")
def handle(self, cmd, *args, **options):
return getattr(self, "handle_%s" % cmd)(*args, **options)
def handle_load(self, fields, input, chunk, rm, *args, **kwargs):
async def upload(table: str, data: List[bytes]):
CHUNK = 1000
n_parts = len(config.clickhouse.cluster_topology.split(","))
async with LiftBridgeClient() as client:
while data:
chunk, data = data[:CHUNK], data[CHUNK:]
await client.publish(
b"\n".join(chunk),
stream=f"ch.{table}",
partition=random.randint(0, n_parts - 1),
)
for fn in input:
# Read data
self.print("Reading file %s" % fn)
if fn.endswith(".gz"):
with gzip.GzipFile(fn) as f:
records = f.read().replace("\r", "").splitlines()
else:
with open(fn) as f:
records = f.read().replace("\r", "").splitlines()
table = fn.split("-", 1)[0]
run_sync(partial(upload, table, records))
if rm:
os.unlink(fn)
if __name__ == "__main__":
Command().run()