2022-10-25 14:25:19 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Base Class for AIL Objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from abc import abstractmethod, ABC
|
|
|
|
|
2022-11-29 15:01:01 +00:00
|
|
|
# from flask import url_for
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.objects.abstract_object import AbstractObject
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
|
|
|
from lib.item_basic import is_crawled, get_item_domain
|
2022-12-19 15:38:20 +00:00
|
|
|
from lib.data_retention_engine import update_obj_date
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
from packages import Date
|
|
|
|
|
|
|
|
# LOAD CONFIG
|
|
|
|
config_loader = ConfigLoader()
|
|
|
|
r_object = config_loader.get_db_conn("Kvrocks_Objects")
|
|
|
|
config_loader = None
|
|
|
|
|
|
|
|
class AbstractDaterangeObject(AbstractObject, ABC):
|
|
|
|
"""
|
|
|
|
Abstract Subtype Object
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, obj_type, id):
|
|
|
|
""" Abstract for all the AIL object
|
|
|
|
|
|
|
|
:param obj_type: object type (item, ...)
|
|
|
|
:param id: Object ID
|
|
|
|
"""
|
|
|
|
super().__init__(obj_type, id)
|
|
|
|
|
|
|
|
def exists(self):
|
2022-12-19 15:38:20 +00:00
|
|
|
return r_object.exists(f'meta:{self.type}:{self.id}')
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
def get_first_seen(self, r_int=False):
|
2022-12-19 15:38:20 +00:00
|
|
|
first_seen = r_object.hget(f'meta:{self.type}:{self.id}', 'first_seen')
|
2022-10-25 14:25:19 +00:00
|
|
|
if r_int:
|
|
|
|
if first_seen:
|
|
|
|
return int(first_seen)
|
|
|
|
else:
|
|
|
|
return 99999999
|
|
|
|
else:
|
|
|
|
return first_seen
|
|
|
|
|
|
|
|
def get_last_seen(self, r_int=False):
|
2022-12-19 15:38:20 +00:00
|
|
|
last_seen = r_object.hget(f'meta:{self.type}:{self.id}', 'last_seen')
|
2022-10-25 14:25:19 +00:00
|
|
|
if r_int:
|
|
|
|
if last_seen:
|
|
|
|
return int(last_seen)
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return last_seen
|
|
|
|
|
|
|
|
def get_nb_seen(self):
|
2023-01-09 15:03:06 +00:00
|
|
|
return self.get_nb_correlation('item')
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
def get_nb_seen_by_date(self, date):
|
2022-12-21 13:20:13 +00:00
|
|
|
nb = r_object.zscore(f'{self.type}:date:{date}', self.id)
|
2022-10-25 14:25:19 +00:00
|
|
|
if nb is None:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return int(nb)
|
|
|
|
|
|
|
|
def _get_meta(self, options=[]):
|
|
|
|
meta_dict = {'first_seen': self.get_first_seen(),
|
|
|
|
'last_seen': self.get_last_seen(),
|
|
|
|
'nb_seen': self.get_nb_seen()}
|
|
|
|
if 'sparkline' in options:
|
|
|
|
meta_dict['sparkline'] = self.get_sparkline()
|
|
|
|
return meta_dict
|
|
|
|
|
|
|
|
def set_first_seen(self, first_seen):
|
2022-12-19 15:38:20 +00:00
|
|
|
r_object.hset(f'meta:{self.type}:{self.id}', 'first_seen', first_seen)
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
def set_last_seen(self, last_seen):
|
2022-12-19 15:38:20 +00:00
|
|
|
r_object.hset(f'meta:{self.type}:{self.id}', 'last_seen', last_seen)
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
def update_daterange(self, date):
|
|
|
|
date = int(date)
|
|
|
|
# obj don't exit
|
|
|
|
if not self.exists():
|
|
|
|
self.set_first_seen(date)
|
|
|
|
self.set_last_seen(date)
|
|
|
|
else:
|
|
|
|
first_seen = self.get_first_seen(r_int=True)
|
|
|
|
last_seen = self.get_last_seen(r_int=True)
|
|
|
|
if date < first_seen:
|
|
|
|
self.set_first_seen(date)
|
|
|
|
if date > last_seen:
|
|
|
|
self.set_last_seen(date)
|
|
|
|
|
|
|
|
def get_sparkline(self):
|
|
|
|
sparkline = []
|
|
|
|
for date in Date.get_previous_date_list(6):
|
|
|
|
sparkline.append(self.get_nb_seen_by_date(date))
|
|
|
|
return sparkline
|
|
|
|
|
2023-01-09 15:03:06 +00:00
|
|
|
def _add_create(self):
|
|
|
|
r_object.sadd(f'{self.type}:all', self.id)
|
|
|
|
|
|
|
|
# TODO don't increase nb if same hash in item with different encoding
|
|
|
|
# if hash already in item
|
2022-10-25 14:25:19 +00:00
|
|
|
def _add(self, date, item_id):
|
|
|
|
if not self.exists():
|
2023-01-09 15:03:06 +00:00
|
|
|
self._add_create(date)
|
2022-10-25 14:25:19 +00:00
|
|
|
self.set_first_seen(date)
|
|
|
|
self.set_last_seen(date)
|
|
|
|
else:
|
|
|
|
self.update_daterange(date)
|
2022-12-19 15:38:20 +00:00
|
|
|
update_obj_date(date, self.type)
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
# NB Object seen by day
|
2023-01-09 15:03:06 +00:00
|
|
|
if not self.is_correlated('item', '', item_id): # if decoded not already in object
|
|
|
|
r_object.zincrby(f'{self.type}:date:{date}', 1, self.id)
|
2022-10-25 14:25:19 +00:00
|
|
|
|
|
|
|
# Correlations
|
|
|
|
self.add_correlation('item', '', item_id)
|
|
|
|
if is_crawled(item_id): # Domain
|
|
|
|
domain = get_item_domain(item_id)
|
|
|
|
self.add_correlation('domain', '', domain)
|
|
|
|
|
|
|
|
# TODO:ADD objects + Stats
|
|
|
|
def _create(self, first_seen, last_seen):
|
|
|
|
self.set_first_seen(first_seen)
|
|
|
|
self.set_last_seen(last_seen)
|
|
|
|
r_object.sadd(f'{self.type}:all', self.id)
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
def _delete(self):
|
|
|
|
pass
|