4747logger = logging .getLogger (__name__ )
4848
4949
50- def stock_tags_need_update (stock_tags : StockTags , set_stock_tags_model : SetStockTagsModel ):
50+ def _stock_tags_need_update (stock_tags : StockTags , set_stock_tags_model : SetStockTagsModel ):
5151 if (
5252 stock_tags .main_tag != set_stock_tags_model .main_tag
5353 or stock_tags .main_tag_reason != set_stock_tags_model .main_tag_reason
@@ -59,7 +59,7 @@ def stock_tags_need_update(stock_tags: StockTags, set_stock_tags_model: SetStock
5959 return False
6060
6161
62- def get_stock_tag_options (entity_id ) :
62+ def get_stock_tag_options (entity_id : str ) -> StockTagOptions :
6363 with contract_api .DBSession (provider = "zvt" , data_schema = StockTags )() as session :
6464 datas : List [StockTags ] = StockTags .query_data (
6565 entity_id = entity_id , order = StockTags .timestamp .desc (), limit = 1 , return_type = "domain" , session = session
@@ -175,7 +175,7 @@ def build_stock_tags(
175175 current_stock_tags : StockTags = datas [0 ]
176176
177177 # nothing change
178- if not stock_tags_need_update (current_stock_tags , set_stock_tags_model ):
178+ if not _stock_tags_need_update (current_stock_tags , set_stock_tags_model ):
179179 logger .info (f"Not change stock_tags for { set_stock_tags_model .entity_id } " )
180180 return current_stock_tags
181181
@@ -224,6 +224,9 @@ def build_stock_tags(
224224
225225
226226def build_tag_parameter (tag_type : TagType , tag , tag_reason , stock_tag : StockTags ):
227+ hidden_tag = None
228+ hidden_tag_reason = None
229+
227230 if tag_type == TagType .main_tag :
228231 main_tag = tag
229232 if main_tag in stock_tag .main_tags :
@@ -240,11 +243,29 @@ def build_tag_parameter(tag_type: TagType, tag, tag_reason, stock_tag: StockTags
240243 sub_tag_reason = tag_reason
241244 main_tag = stock_tag .main_tag
242245 main_tag_reason = stock_tag .main_tag_reason
246+ elif tag_type == TagType .hidden_tag :
247+ hidden_tag = tag
248+ if stock_tag .hidden_tags and (hidden_tag in stock_tag .hidden_tags ):
249+ hidden_tag_reason = stock_tag .hidden_tags .get (hidden_tag , tag_reason )
250+ else :
251+ hidden_tag_reason = tag_reason
252+
253+ sub_tag = stock_tag .sub_tag
254+ sub_tag_reason = stock_tag .sub_tag_reason
255+
256+ main_tag = stock_tag .main_tag
257+ main_tag_reason = stock_tag .main_tag_reason
258+
243259 else :
244260 assert False
245261
246262 return TagParameter (
247- main_tag = main_tag , main_tag_reason = main_tag_reason , sub_tag = sub_tag , sub_tag_reason = sub_tag_reason
263+ main_tag = main_tag ,
264+ main_tag_reason = main_tag_reason ,
265+ sub_tag = sub_tag ,
266+ sub_tag_reason = sub_tag_reason ,
267+ hidden_tag = hidden_tag ,
268+ hidden_tag_reason = hidden_tag_reason ,
248269 )
249270
250271
@@ -274,6 +295,15 @@ def batch_set_stock_tags(batch_set_stock_tags_model: BatchSetStockTagsModel):
274295 session = session ,
275296 return_type = "domain" ,
276297 )
298+ elif tag_type == TagType .hidden_tag :
299+ hidden_tag = batch_set_stock_tags_model .tag
300+ stock_tags : List [StockTags ] = StockTags .query_data (
301+ entity_ids = batch_set_stock_tags_model .entity_ids ,
302+ # 需要sqlite3版本>=3.37.0
303+ filters = [func .json_extract (StockTags .active_hidden_tags , f'$."{ hidden_tag } "' ) == None ],
304+ session = session ,
305+ return_type = "domain" ,
306+ )
277307
278308 for stock_tag in stock_tags :
279309 tag_parameter : TagParameter = build_tag_parameter (
@@ -282,13 +312,18 @@ def batch_set_stock_tags(batch_set_stock_tags_model: BatchSetStockTagsModel):
282312 tag_reason = batch_set_stock_tags_model .tag_reason ,
283313 stock_tag = stock_tag ,
284314 )
315+ if tag_type == TagType .hidden_tag :
316+ active_hidden_tags = {batch_set_stock_tags_model .tag : batch_set_stock_tags_model .tag_reason }
317+ else :
318+ active_hidden_tags = stock_tag .active_hidden_tags
319+
285320 set_stock_tags_model = SetStockTagsModel (
286321 entity_id = stock_tag .entity_id ,
287322 main_tag = tag_parameter .main_tag ,
288323 main_tag_reason = tag_parameter .main_tag_reason ,
289324 sub_tag = tag_parameter .sub_tag ,
290325 sub_tag_reason = tag_parameter .sub_tag_reason ,
291- active_hidden_tags = stock_tag . active_hidden_tags ,
326+ active_hidden_tags = active_hidden_tags ,
292327 )
293328
294329 build_stock_tags (
@@ -521,6 +556,23 @@ def build_stock_pool(create_stock_pools_model: CreateStockPoolsModel, target_dat
521556 return stock_pool
522557
523558
559+ def del_stock_pool (stock_pool_name : str ):
560+ with contract_api .DBSession (provider = "zvt" , data_schema = StockPoolInfo )() as session :
561+ stock_pool_info = StockPoolInfo .query_data (
562+ session = session ,
563+ filters = [StockPoolInfo .stock_pool_name == stock_pool_name ],
564+ return_type = "domain" ,
565+ )
566+
567+ contract_api .del_data (data_schema = StockPools , filters = [StockPools .stock_pool_name == stock_pool_name ])
568+
569+ if stock_pool_info :
570+ session .delete (stock_pool_info [0 ])
571+ session .commit ()
572+ return "success"
573+ return "not found"
574+
575+
524576def query_stock_tag_stats (query_stock_tag_stats_model : QueryStockTagStatsModel ):
525577 with contract_api .DBSession (provider = "zvt" , data_schema = TagStats )() as session :
526578 datas = TagStats .query_data (
@@ -701,6 +753,43 @@ def activate_sub_tags(activate_sub_tags_model: ActivateSubTagsModel):
701753 return result
702754
703755
756+ def remove_hidden_tag (hidden_tag : str ):
757+ with contract_api .DBSession (provider = "zvt" , data_schema = StockTags )() as session :
758+ stock_tags = StockTags .query_data (
759+ session = session ,
760+ # 需要sqlite3版本>=3.37.0
761+ filters = [func .json_extract (StockTags .hidden_tags , f'$."{ hidden_tag } "' ) != None ],
762+ return_type = "domain" ,
763+ )
764+ if not stock_tags :
765+ logger .info (f"all stocks with hidden_tag: { hidden_tag } has been removed" )
766+ return []
767+ for stock_tag in stock_tags :
768+ hidden_tags = dict (stock_tag .hidden_tags )
769+ hidden_tags .pop (hidden_tag )
770+ stock_tag .hidden_tags = hidden_tags
771+ session .commit ()
772+ session .refresh (stock_tag )
773+ return stock_tags
774+
775+
776+ def del_hidden_tag (tag : str ):
777+ with contract_api .DBSession (provider = "zvt" , data_schema = HiddenTagInfo )() as session :
778+ hidden_tag_info = HiddenTagInfo .query_data (
779+ session = session ,
780+ filters = [HiddenTagInfo .tag == tag ],
781+ return_type = "domain" ,
782+ )
783+ if not hidden_tag_info :
784+ logger .info (f"hidden_tag: { tag } has been removed" )
785+ return []
786+
787+ result = remove_hidden_tag (hidden_tag = tag )
788+ session .delete (hidden_tag_info [0 ])
789+ session .commit ()
790+ return result
791+
792+
704793def _create_main_tag_if_not_existed (main_tag , main_tag_reason ):
705794 main_tag_info = CreateTagInfoModel (tag = main_tag , tag_reason = main_tag_reason )
706795 if not is_tag_info_existed (tag_info = main_tag_info , tag_type = TagType .main_tag ):
@@ -818,13 +907,13 @@ def change_main_tag(change_main_tag_model: ChangeMainTagModel):
818907
819908
820909if __name__ == "__main__" :
821- activate_industry_list (industry_list = ["半导体" ])
910+ print (del_hidden_tag (tag = "妖" ))
911+ # activate_industry_list(industry_list=["半导体"])
822912 # activate_sub_tags(ActivateSubTagsModel(sub_tags=["航天概念", "天基互联", "北斗导航", "通用航空"]))
823913
824914
825915# the __all__ is generated
826916__all__ = [
827- "stock_tags_need_update" ,
828917 "get_stock_tag_options" ,
829918 "build_stock_tags" ,
830919 "build_tag_parameter" ,
0 commit comments