Debian Codegraph @

src/python/ping-nosql-query-example

raw
1 #!/usr/bin/env python3
2 # ==========================================================================
3 # _ _ _ ____ ____ _____
4 # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __
5 # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
6 # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ |
7 # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_|
8 #
9 # --- High-Performance Connectivity Tracer (HiPerConTracer) ---
10 # https://www.nntb.no/~dreibh/hipercontracer/
11 # ==========================================================================
12 #
13 # High-Performance Connectivity Tracer (HiPerConTracer)
14 # Copyright (C) 2015-2026 by Thomas Dreibholz
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # Contact: [email protected]
30
31 import datetime
32 import json
33 import os
34 import sys
35 import time
36
37 import HiPerConTracer
38 import QueryHelper
39
40 from typing import Any, Final, TextIO
41
42
43 # ###### Print log message ##################################################
44 def log(logstring : str) -> None:
45 sys.stdout.write('\x1b[34m' +
46 datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') + ': ' +
47 logstring +
48 '\x1b[0m\n')
49
50
51
52 # ###### Main program #######################################################
53
54 # ====== Handle arguments ===================================================
55 if len(sys.argv) != 5:
56 sys.stderr.write('Usage: ' + sys.argv[0] + ' database_configuration start_timestamp end_timestamp output_file\n')
57 sys.stderr.write('Example: ' + sys.argv[0] + ' ~/test4hpct-researcher-postgresql.conf "2020-01-01 00:00:00" "2025-01-01 00:00:00" output.csv.gz\n')
58 sys.exit(1)
59 databaseConfigurationFile : Final[str] = sys.argv[1]
60 startTimeStampString : Final[str] = sys.argv[2]
61 endTimeStampString : Final[str] = sys.argv[3]
62 outputFileName : Final[str] = sys.argv[4]
63 outputFileNameTmp : Final[str] = outputFileName + '.tmp'
64
65 startTimeStamp : Final[int] = HiPerConTracer.string_to_unix_time(startTimeStampString)
66 endTimeStamp : Final[int] = HiPerConTracer.string_to_unix_time(endTimeStampString)
67 if not startTimeStamp < endTimeStamp:
68 sys.stderr.write('ERROR: start_timestamp must be less than end_timestamp!\n')
69 sys.exit(1)
70
71
72 # ====== Connect to database ================================================
73 configuration = QueryHelper.DatabaseConfiguration(sys.argv[1])
74 client = configuration.createClient()
75
76
77 # ====== Prepare query ======================================================
78 table : Final[str] = 'ping'
79 query : Final[dict[str, Any]] = {
80 "$and": [
81 {
82 "sendTimestamp": {
83 "$gte": startTimeStamp
84 }
85 },
86 {
87 "sendTimestamp": {
88 "$lt": endTimeStamp
89 }
90 }
91 ]
92 }
93 orderBy : Final[list[tuple[str, int]]] = [
94 ( "sendTimestamp", 1 ),
95 ( "measurementID", 1 ),
96 ( "sourceIP", 1 ),
97 ( "destinationIP", 1 ),
98 ( "protocol", 1 ),
99 ( "trafficClass", 1 ),
100 ( "burstSeq", 1 )
101 ]
102
103
104 try:
105 # ====== Run query =======================================================
106 log(f'Querying {startTimeStamp} - {endTimeStamp} ...')
107
108 queryStartTime : Final[float] = time.perf_counter()
109 cursor = configuration.queryMongoDB(table, query, orderBy = orderBy)
110 queryFinishTime : Final[float] = time.perf_counter()
111 queryRuntime : Final[float] = queryFinishTime - queryStartTime
112
113 for document in cursor:
114 print(json.dumps(document, indent=3, default = str))
115
116 except KeyboardInterrupt:
117 sys.stdout.write('\nSIGINT\n')
118 sys.exit(1)