Skip to content

Commit

Permalink
[fix] Fixed sql.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Aug 10, 2023
1 parent a3adacc commit 892a5b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
2 changes: 2 additions & 0 deletions jpnic_admin/resource/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def get_queryset(self, page=1):
if page != 1:
prev_page = page - 1

input_array.append(jpnic_id.id)
input_array.append(jpnic_id.id)
# データ出力
with connection.cursor() as cursor:
input_array.append(list_count)
Expand Down
32 changes: 22 additions & 10 deletions jpnic_admin/resource/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def sqlDateSelect(abuse_match=False):
t1.address AS address,
t1.address_en AS address_en,
t1.admin_handle AS admin_handle,
tb_admin_handle.name AS admin_name,
tb_admin_handle.email AS admin_email,
GROUP_CONCAT(tech_handle.jpnic_handle) AS tech_handle,
GROUP_CONCAT(tech_handle.name) AS tech_name,
GROUP_CONCAT(tech_handle.email) AS tech_email
tb_admin_handle1.name AS admin_name,
tb_admin_handle1.email AS admin_email,
GROUP_CONCAT(tech_handle1.jpnic_handle) AS tech_handle,
GROUP_CONCAT(tech_handle1.name) AS tech_name,
GROUP_CONCAT(tech_handle1.email) AS tech_email
FROM resource_addrlist AS t1
JOIN (SELECT MAX(id) AS id, admin_handle
FROM resource_addrlist
Expand All @@ -37,11 +37,23 @@ def sqlDateSelect(abuse_match=False):
# HAVING MAX(id)
ORDER BY ip_address
LIMIT %s OFFSET %s) AS t2 ON t1.id = t2.id
INNER JOIN resource_jpnichandle AS tb_admin_handle ON t2.admin_handle = tb_admin_handle.jpnic_handle
INNER JOIN resource_addrlisttechhandle ON t2.id = resource_addrlisttechhandle.addr_list_id
INNER JOIN resource_jpnichandle AS tech_handle
ON resource_addrlisttechhandle.jpnic_handle = tech_handle.jpnic_handle
GROUP BY id, ip_address, admin_name, admin_email
JOIN resource_addrlisttechhandle ON t1.id = resource_addrlisttechhandle.addr_list_id
JOIN resource_jpnichandle AS tech_handle1
ON (t1.admin_handle = tech_handle1.jpnic_handle AND tech_handle1.jpnic_id = %s)
LEFT JOIN resource_jpnichandle AS tech_handle2
ON (t1.admin_handle = tech_handle2.jpnic_handle AND tech_handle2.jpnic_id = tech_handle1.jpnic_id AND
(tech_handle1.last_checked_at < tech_handle2.last_checked_at))
JOIN resource_jpnichandle AS tb_admin_handle1
ON (t1.admin_handle = tb_admin_handle1.jpnic_handle AND tb_admin_handle1.jpnic_id = %s)
LEFT JOIN resource_jpnichandle AS tb_admin_handle2
ON (t1.admin_handle = tb_admin_handle2.jpnic_handle AND
tb_admin_handle2.jpnic_id = tb_admin_handle1.jpnic_id AND
(tb_admin_handle1.last_checked_at < tb_admin_handle2.last_checked_at))
WHERE (tech_handle2.id IS NULL
or tech_handle1.id = tech_handle2.id)
AND (tb_admin_handle2.id IS NULL
or tb_admin_handle1.id = tb_admin_handle2.id)
GROUP BY id, ip_address, tb_admin_handle1.name, tb_admin_handle1.email
ORDER BY ip_address
"""
return sql
Expand Down
41 changes: 30 additions & 11 deletions jpnic_admin/resource/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ def search_list(self):

# addr_listを新規登録
addr_list_id = self.insert_addr_list(addr_info)
self.insert_jpnic_handle(jpnic_handles)
print("UPDATE_LATEST_DATA")
self.update_latest_data(handles=jpnic_handles)
for tech_handle in addr_info["tech_handle"]:
AddrListTechHandle(addr_list_id=addr_list_id, jpnic_handle=tech_handle).save()

Expand Down Expand Up @@ -579,9 +580,10 @@ def get_recept(self, url=None, recept_no=None, info={}):
info["fax"] = body
return info

def insert_jpnic_handle(self, jpnic_handles, recep_number=""):
def insert_jpnic_handle(self, jpnic_handles):
for jpnic_handle in jpnic_handles:
org_name = jpnic_handle.get("org")
recep_number = jpnic_handle.get("recep_number", "")
new_handle_model = JPNICHandle(
created_at=self.now,
last_checked_at=self.now,
Expand Down Expand Up @@ -745,12 +747,29 @@ def update_handle(self):
print("ERROR")
return

# TODO: JPNICハンドルとlast_checked_at=Nullを使って対象のテーブルを探し当てて、last_checked_atを更新する
tmp_handle = JPNICHandle.objects.get(
jpnic_handle=handle_info["jpnic_handle"],
last_checked_at__gt=self.log.last_checked_at,
jpnic_id=self.base.id,
)
tmp_handle.last_checked_at = self.now
tmp_handle.save()
self.insert_jpnic_handle(jpnic_handles=[].append(handle_info), recep_number=info["recept_no"])
handle_info["recep_number"] = info["recept_no"]

def update_latest_data(self, handles=None):
last_handle = JPNICHandle.objects.filter(jpnic_id=self.base.id).order_by("-last_checked_at").first()
base_handle_lists = []
if last_handle:
base_handle_lists = JPNICHandle.objects.filter(
jpnic_id=self.base.id, last_checked_at__exact=last_handle.last_checked_at
)
handle_update_lists = []

if not handles:
for base_handle in base_handle_lists:
is_exists = False
for input_handle in handles:
if input_handle["jpnic_handle"] == base_handle.jpnic_handle:
is_exists = True
break
if not is_exists:
handle_update_lists.append(base_handle)

for handle_update in handle_update_lists:
handle_update.last_checked_at = self.now
handle_update.save()

self.insert_jpnic_handle(handles)

0 comments on commit 892a5b1

Please sign in to comment.