rda-python-metrics 1.0.22__py3-none-any.whl → 1.0.24__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rda-python-metrics might be problematic. Click here for more details.

@@ -104,6 +104,23 @@ def domain_ipinfo_record(dmname):
104
104
 
105
105
  return None
106
106
 
107
+ #
108
+ # try to get hostname via socket for given ip address
109
+ #
110
+ def get_ip_hostname(ip, iprec, record):
111
+
112
+ if iprec and 'hostname' in iprec:
113
+ record['hostname'] = iprec['hostname']
114
+ record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
115
+ else:
116
+ record['hostname'] = ip
117
+ try:
118
+ hostrec = socket.gethostbyaddr(ip)
119
+ record['hostname'] = hostrec[1][0] if hostrec[1] else hostrec[0]
120
+ record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
121
+ except Exception as e:
122
+ PgLOG.pglog("socket: {} - {}".format(ip, str(e)), PgLOG.LOGWRN)
123
+
107
124
  #
108
125
  # get a ipinfo record for given ip address
109
126
  #
@@ -117,9 +134,7 @@ def get_ipinfo_record(ip):
117
134
  return None
118
135
 
119
136
  record = {'ip' : ip, 'stat_flag' : 'A', 'hostname' : ip, 'org_type' : '-'}
120
- if 'hostname' in iprec:
121
- record['hostname'] = iprec['hostname']
122
- record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
137
+ get_ip_hostname(ip, iprec, record)
123
138
  record['lat'] = float(iprec['latitude']) if iprec['latitude'] else 0
124
139
  record['lon'] = float(iprec['longitude']) if iprec['longitude'] else 0
125
140
  if 'org' in iprec: record['org_name'] = iprec['org']
@@ -153,6 +168,7 @@ def get_geoip2_record(ip):
153
168
  return None
154
169
 
155
170
  record = {'ip' : ip, 'stat_flag' : 'M', 'org_type' : '-'}
171
+ get_ip_hostname(ip, None, record)
156
172
  record['lat'] = float(city.location.latitude) if city.location.latitude else 0
157
173
  record['lon'] = float(city.location.longitude) if city.location.longitude else 0
158
174
  record['country'] = get_country_name_code(city.country.name)
@@ -160,17 +176,8 @@ def get_geoip2_record(ip):
160
176
  record['region'] = PgLOG.convert_chars(city.subdivisions.most_specific.name) if city.subdivisions.most_specific.name else None
161
177
  record['postal'] = city.postal.code
162
178
  record['timezone'] = city.location.time_zone
163
- record['hostname'] = ip
164
179
  record['ipinfo'] = json.dumps(object_to_dict(city))
165
180
 
166
- try:
167
- hostrec = socket.gethostbyaddr(ip)
168
- except Exception as e:
169
- PgLOG.pglog("socket: {} - {}".format(ip, str(e)), PgLOG.LOGWRN)
170
- return record
171
- record['hostname'] = hostrec[1][0] if hostrec[1] else hostrec[0]
172
- record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
173
-
174
181
  return record
175
182
 
176
183
  #
@@ -223,11 +230,11 @@ def set_ipinfo(ip, ipopt = True):
223
230
 
224
231
  if ip in IPRECS:
225
232
  pgrec = IPRECS[ip]
226
- if not pgrec or not ipopt or pgrec['stat_flag'] == 'A': return pgrec
233
+ if pgrec or not ipopt: return pgrec
227
234
  else:
228
235
  pgrec = PgDBI.pgget('ipinfo', '*', "ip = '{}'".format(ip))
229
236
 
230
- if not pgrec or ipopt and pgrec['stat_flag'] == 'M':
237
+ if not pgrec:
231
238
  record = get_ipinfo_record(ip) if ipopt else None
232
239
  if not record: record = get_geoip2_record(ip)
233
240
  if record and update_ipinfo_record(record, pgrec): pgrec = record
@@ -25,10 +25,11 @@ from rda_python_common import PgDBI
25
25
  from . import PgIPInfo
26
26
 
27
27
  # the define options for gathering ipinfo data
28
+ DATES = 0x01 # fix data usages for given dates
28
29
  MONTH = 0x02 # fix data usages for given months
29
30
  YEARS = 0x04 # fix data usages for given years
30
31
  NDAYS = 0x08 # fix data usages in recent number of days
31
- MULTI = (MONTH|YEARS)
32
+ MULTI = (DATES|MONTH|YEARS)
32
33
  SINGL = (NDAYS)
33
34
 
34
35
  IPINFO = {
@@ -49,8 +50,10 @@ def main():
49
50
  for arg in argv:
50
51
  if arg == "-b":
51
52
  PgLOG.PGLOG['BCKGRND'] = 1
52
- elif re.match(r'^-[mNy]$', arg) and option == 0:
53
- if arg == "-m":
53
+ elif re.match(r'^-[dmNy]$', arg) and option == 0:
54
+ if arg == "-d":
55
+ option = DATES
56
+ elif arg == "-m":
54
57
  option = MONTH
55
58
  elif arg == "-y":
56
59
  option = YEARS
@@ -74,17 +77,6 @@ def main():
74
77
  PgDBI.dssdb_dbname()
75
78
  PgLOG.cmdlog("fillipinfo {}".format(' '.join(argv)))
76
79
 
77
- if option&NDAYS:
78
- curdate = IPINFO['CDATE']
79
- datelimit = PgUtil.adddate(curdate, 0, 0, -int(inputs[0]))
80
- option = MONTH
81
- inputs = []
82
-
83
- while curdate >= datelimit:
84
- tms = curdate.split('-')
85
- inputs.append("{}-{}".format(tms[0], tms[1]))
86
- curdate = PgUtil.adddate(curdate, 0, 0, -int(tms[2]))
87
-
88
80
  fill_ip_info(option, inputs, table)
89
81
 
90
82
  sys.exit(0)
@@ -95,35 +87,49 @@ def main():
95
87
  def fill_ip_info(option, inputs, table):
96
88
 
97
89
  cntall = 0
98
- date = None
90
+ func = eval('fix_{}_records'.format(table))
99
91
  for input in inputs:
100
92
  if option&NDAYS:
101
93
  edate = IPINFO['CDATE']
102
- date = PgUtil.adddate(edate, 0, 0, -int(input))
94
+ date = PgUtil.adddate(edate, 0, 0, -int(input))
95
+ elif option&DATES:
96
+ edate = date = input
103
97
  elif option&MONTH:
104
98
  tms = input.split('-')
105
99
  date = "{}-{:02}-01".format(tms[0], int(tms[1]))
106
100
  edate = PgUtil.enddate(date, 0, 'M')
107
- elif option&YEARS:
101
+ else:
108
102
  date = input + "-01-01"
109
103
  edate = input + "-12-31"
104
+ while True:
105
+ (ndate, cond) = get_next_date(date, edate)
106
+ cntall += func(date, cond)
107
+ if ndate >= edate: break
108
+ date = PgUtil.adddate(ndate, 0, 0, 1)
110
109
 
111
- while date <= edate:
112
- func = eval('fix_{}_records'.format(table))
113
- cntall += func(date)
114
- date = PgUtil.adddate(date, 0, 0, 1)
115
-
116
110
  if cntall > 2:
117
111
  PgLOG.pglog("{}: Total {} records updated".format(table, cntall), PgLOG.LOGWRN)
118
112
 
113
+ def get_next_date(date, edate):
114
+
115
+ if date < edate:
116
+ ndate = PgUtil.enddate(date, 'M')
117
+ if ndate < edate: edate = ndate
118
+ if date < edate:
119
+ cond = f"BETWEEN '{date}' AND '{edate}'"
120
+ else:
121
+ cond = f"= '{date}'"
122
+
123
+ return (edate, cond)
124
+
119
125
 
120
- def fix_allusage_records(date):
126
+ def fix_allusage_records(date, cnd):
121
127
 
122
128
  cnt = 0
123
129
  ms = re.match(r'^(\d+)-', date)
124
130
  year = ms.group(1)
125
131
  table = 'allusage_' + year
126
- cond = "date = '{}' AND region IS NULL".format(date)
132
+ cond = f"date {cnd} AND region IS NULL"
127
133
  pgrecs = PgDBI.pgmget(table, 'aidx, email, ip', cond, PgLOG.LGEREX)
128
134
  if not pgrecs: return 0
129
135
  cnt = len(pgrecs['ip']) if pgrecs else 0
@@ -134,15 +140,15 @@ def fix_allusage_records(date):
134
140
  mcnt += PgDBI.pgupdt(table, record, "aidx = '{}'".format(pgrecs['aidx'][i]))
135
141
 
136
142
  s = 's' if cnt > 1 else ''
137
- PgLOG.pglog("{}: {} of {} record{} updated for {}".format(table, mcnt, cnt, s, date), PgLOG.LOGWRN)
143
+ PgLOG.pglog(f"{table}: {mcnt} of {cnt} record{s} updated for date {cnd}", PgLOG.LOGWRN)
138
144
 
139
145
  return mcnt
140
146
 
141
- def fix_tdsusage_records(date):
147
+ def fix_tdsusage_records(date, cnd):
142
148
 
143
149
  table = 'tdsusage'
144
- cond = "date = '{}' AND region IS NULL".format(date)
145
- pgrecs = PgDBI.pgmget(table, 'time, email, ip', cond, PgLOG.LGEREX)
150
+ cond = f"date {cnd} AND region IS NULL"
151
+ pgrecs = PgDBI.pgmget(table, 'date, time, email, ip', cond, PgLOG.LGEREX)
146
152
  if not pgrecs: return 0
147
153
  cnt = len(pgrecs['ip']) if pgrecs else 0
148
154
  mcnt = 0
@@ -150,18 +156,18 @@ def fix_tdsusage_records(date):
150
156
  ip = pgrecs['ip'][i]
151
157
  record = PgIPInfo.get_missing_ipinfo(ip, pgrecs['email'][i])
152
158
  if record:
153
- cond = "date = '{}' AND time = '{}' AND ip = '{}'".format(date, pgrecs['time'][i], ip)
159
+ cond = "date = '{}' AND time = '{}' AND ip = '{}'".format(pgrecs['date'][i], pgrecs['time'][i], ip)
154
160
  mcnt += PgDBI.pgupdt(table, record, cond)
155
161
 
156
162
  s = 's' if cnt > 1 else ''
157
- PgLOG.pglog("{}: {} of {} record{} updated for {}".format(table, mcnt, cnt, s, date), PgLOG.LOGWRN)
163
+ PgLOG.pglog(f"{table}: {mcnt} of {cnt} record{s} updated for date {cnd}", PgLOG.LOGWRN)
158
164
 
159
165
  return mcnt
160
166
 
161
- def fix_codusage_records(date):
167
+ def fix_codusage_records(date, cnd):
162
168
 
163
169
  table = 'codusage'
164
- cond = "date = '{}' AND region IS NULL".format(date)
170
+ cond = f"date {cnd} AND region IS NULL"
165
171
  pgrecs = PgDBI.pgmget(table, 'codidx, email, ip', cond, PgLOG.LGEREX)
166
172
  if not pgrecs: return 0
167
173
  cnt = len(pgrecs['ip']) if pgrecs else 0
@@ -172,14 +178,14 @@ def fix_codusage_records(date):
172
178
  mcnt += PgDBI.pgupdt(table, record, "codidx = '{}'".format(pgrecs['codidx'][i]))
173
179
 
174
180
  s = 's' if cnt > 1 else ''
175
- PgLOG.pglog("{}: {} of {} record{} updated for {}".format(table, mcnt, cnt, s, date), PgLOG.LOGWRN)
181
+ PgLOG.pglog(f"{table}: {mcnt} of {cnt} record{s} updated for date {cnd}", PgLOG.LOGWRN)
176
182
 
177
183
  return mcnt
178
184
 
179
- def fix_wuser_records(date):
185
+ def fix_wuser_records(date, cnd):
180
186
 
181
187
  table = 'wuser'
182
- cond = "start_date = '{}' AND region IS NULL".format(date)
188
+ cond = f"start_date {cnd} AND region IS NULL"
183
189
  pgrecs = PgDBI.pgmget(table, 'wuid, email, ip', cond, PgLOG.LGEREX)
184
190
  if not pgrecs: return 0
185
191
  cnt = len(pgrecs['ip']) if pgrecs else 0
@@ -192,14 +198,14 @@ def fix_wuser_records(date):
192
198
  mcnt += PgDBI.pgupdt(table, record, "wuid = '{}'".format(pgrecs['wuid'][i]))
193
199
 
194
200
  s = 's' if cnt > 1 else ''
195
- PgLOG.pglog("{}: {} of {} record{} updated for {}".format(table, mcnt, cnt, s, date), PgLOG.LOGWRN)
201
+ PgLOG.pglog(f"{table}: {mcnt} of {cnt} record{s} updated for start_date {cnd}", PgLOG.LOGWRN)
196
202
 
197
203
  return mcnt
198
204
 
199
- def fix_ipinfo_records(date):
205
+ def fix_ipinfo_records(date, cnd):
200
206
 
201
207
  table = 'ipinfo'
202
- cond = "adddate = '{}' AND region IS NULL".format(date)
208
+ cond = f"adddate {cnd} AND region IS NULL"
203
209
  pgrecs = PgDBI.pgmget(table, 'ip', cond, PgLOG.LGEREX)
204
210
  if not pgrecs: return 0
205
211
  cnt = len(pgrecs['ip']) if pgrecs else 0
@@ -208,7 +214,7 @@ def fix_ipinfo_records(date):
208
214
  if PgIPInfo.set_ipinfo(pgrecs['ip'][i]): mcnt +=1
209
215
 
210
216
  s = 's' if cnt > 1 else ''
211
- PgLOG.pglog("{}: {} of {} record{} updated for {}".format(table, mcnt, cnt, s, date), PgLOG.LOGWRN)
217
+ PgLOG.pglog(f"{table}: {mcnt} of {cnt} record{s} updated for adddate {cnd}", PgLOG.LOGWRN)
212
218
 
213
219
  return mcnt
214
220
 
@@ -3,7 +3,7 @@
3
3
 
4
4
  Usage: fillipinfo [-b] -t TableName [-N NumberDay] [-m YearMonths] [-y Years]
5
5
 
6
- select option, -t and -m, -N or -y to run this application.
6
+ select option, -t and -d, -m, -N or -y to run this application.
7
7
 
8
8
  - Option -b, log process information into logfile only;
9
9
 
@@ -13,6 +13,8 @@
13
13
 
14
14
  - Option -N, fix usage info in recent NumberDay days;
15
15
 
16
+ - Option -d, fix usage info in given dates;
17
+
16
18
  - Option -m, fix usage info in given months;
17
19
 
18
20
  - Option -y, fix usage info in given years.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rda_python_metrics
3
- Version: 1.0.22
3
+ Version: 1.0.24
4
4
  Summary: RDA Python Package to gather and view data usage metrics
5
5
  Author-email: Zaihua Ji <zji@ucar.edu>
6
6
  Project-URL: Homepage, https://github.com/NCAR/rda-python-metrics
@@ -1,4 +1,4 @@
1
- rda_python_metrics/PgIPInfo.py,sha256=SP8vTyCV6owV942xwLvS4CqnyPdkMTEVuQzyB-dkeNk,8644
1
+ rda_python_metrics/PgIPInfo.py,sha256=mvNIgHY-2fpGbGkrzM244x02Ab2OyiV2JVq7XqInwW4,8772
2
2
  rda_python_metrics/PgView.py,sha256=r6otb3DjfiaQJdg0z8bZQAOlhr4JnrXJzp9wgWh_8qQ,24369
3
3
  rda_python_metrics/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
4
4
  rda_python_metrics/fillawsusage.py,sha256=SZH9G4jruKvavqSr9DiVmHVW929E60tJGUkv8CK3GQI,8152
@@ -11,8 +11,8 @@ rda_python_metrics/fillcountry.py,sha256=7i5LNi3scRoyRCT6t7aeNTGKOpxzJ2mA9tnvUqj
11
11
  rda_python_metrics/fillendtime.py,sha256=skZttlpoY19g0dGwqGQI8t_1YPPTPEXwg3EfNlfL90I,2533
12
12
  rda_python_metrics/fillglobususage.py,sha256=ahz8XnnJdD_AbSYqJ34lWmDuzws_-SNmCR8QE20aovA,8539
13
13
  rda_python_metrics/fillglobususage.usg,sha256=1GgmCP22IQZdADwL5Mmkz3v8Ws-G7U3teQ1AxRJfV_4,637
14
- rda_python_metrics/fillipinfo.py,sha256=O2uTGCw13ITZn6tfhNT_9DQJ8-RM5TVZfYIJCYm01oY,6726
15
- rda_python_metrics/fillipinfo.usg,sha256=6NoOTStxsVXwycN8u8znnwC6AemKftR2ZGrW_RL41og,647
14
+ rda_python_metrics/fillipinfo.py,sha256=wFqyGYlD1gvIZ3e5qlIwjAz8LrhMKDfB-WWY5yE97-c,6770
15
+ rda_python_metrics/fillipinfo.usg,sha256=YeCR58xGv0emqHUZ_9R977HrqaeBwbd6j5QRF2Lc7TA,702
16
16
  rda_python_metrics/filloneorder.py,sha256=ADHbcKCDh9bJunnxYbkbjwU2QpC43hvGlLWaURHNxkg,5433
17
17
  rda_python_metrics/filloneorder.usg,sha256=mtOySKx6-D4k2bbTcmi6cSYtINiycRyHQkHozi0CQu0,1466
18
18
  rda_python_metrics/fillosdfusage.py,sha256=z62l7RtbcfnR84xn2ffQXq0cPfcBkvUciP3u6vb09JI,7855
@@ -44,9 +44,9 @@ rda_python_metrics/viewwebfile.py,sha256=HSMNkQQawonu6W3blV7g9UbJuNy9VAOn9COqgmj
44
44
  rda_python_metrics/viewwebfile.usg,sha256=lTNi8Yu8BUJuExEDJX-vsJyWUSUIQTS-DiiBEVFo33s,10054
45
45
  rda_python_metrics/viewwebusage.py,sha256=ES2lI8NaCeCpTGi94HU-cDRBxHMiUBbplyYsZf2KqF0,16650
46
46
  rda_python_metrics/viewwebusage.usg,sha256=OVDZ78p87E3HLW34ZhasNJ7Zmw8XXjmZPPWZfRhPLXo,9936
47
- rda_python_metrics-1.0.22.dist-info/licenses/LICENSE,sha256=1dck4EAQwv8QweDWCXDx-4Or0S8YwiCstaso_H57Pno,1097
48
- rda_python_metrics-1.0.22.dist-info/METADATA,sha256=1EgQwJ6ezL7DX5PRIPAP6tdEfG_3aIKdDstqjQ7NJN0,761
49
- rda_python_metrics-1.0.22.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
50
- rda_python_metrics-1.0.22.dist-info/entry_points.txt,sha256=YfFLzlE3rdufSV471VsDnfYptnt1lR08aSrxPXlKqlY,1185
51
- rda_python_metrics-1.0.22.dist-info/top_level.txt,sha256=aoBgbR_o70TP0QmMW0U6inRHYtfKld47OBmnWnLnDOs,19
52
- rda_python_metrics-1.0.22.dist-info/RECORD,,
47
+ rda_python_metrics-1.0.24.dist-info/licenses/LICENSE,sha256=1dck4EAQwv8QweDWCXDx-4Or0S8YwiCstaso_H57Pno,1097
48
+ rda_python_metrics-1.0.24.dist-info/METADATA,sha256=ye_l-YywMIqo8DOKhp0tpRAbkDoM-h6ofixlnWcY43U,761
49
+ rda_python_metrics-1.0.24.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
50
+ rda_python_metrics-1.0.24.dist-info/entry_points.txt,sha256=YfFLzlE3rdufSV471VsDnfYptnt1lR08aSrxPXlKqlY,1185
51
+ rda_python_metrics-1.0.24.dist-info/top_level.txt,sha256=aoBgbR_o70TP0QmMW0U6inRHYtfKld47OBmnWnLnDOs,19
52
+ rda_python_metrics-1.0.24.dist-info/RECORD,,