Skip to content

Commit

Permalink
chore: 添加 sshared
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Mar 30, 2024
1 parent 6a828e0 commit 3d06c2a
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 327 deletions.
2 changes: 1 addition & 1 deletion migrate_01_article_fp_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def main() -> None:

data_to_save: List[ArticleEarningRankingRecordDocument] = []
async for item in OldArticleFPRank.find_many(
sort={"date": "ASC", "ranking": "ASC"}
{}, sort={"date": "ASC", "ranking": "ASC"}
):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))
Expand Down
4 changes: 3 additions & 1 deletion migrate_02_assets_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ async def main() -> None:
)

data_to_save: List[AssetsRankingRecordDocument] = []
async for item in OldAssetsRank.find_many(sort={"date": "ASC", "ranking": "ASC"}):
async for item in OldAssetsRank.find_many(
{}, sort={"date": "ASC", "ranking": "ASC"}
):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))

Expand Down
2 changes: 1 addition & 1 deletion migrate_03_daily_update_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def main() -> None:

data_to_save: List[DailyUpdateRankingRecordDocument] = []
async for item in OldDailyUpdateRank.find_many(
sort={"date": "ASC", "ranking": "ASC"}
{}, sort={"date": "ASC", "ranking": "ASC"}
):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))
Expand Down
2 changes: 1 addition & 1 deletion migrate_04_lp_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def main() -> None:

data_to_save: List[LPRecommendedArticleRecordDocument] = []
async for item in OldLPCollections.find_many(
sort={"date": "ASC", "ranking": "ASC"}
{}, sort={"date": "ASC", "ranking": "ASC"}
):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))
Expand Down
4 changes: 3 additions & 1 deletion migrate_05_lottery_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ async def main() -> None:
logger.info(f"旧集合数据量:{old_data_count}")

data_to_save: List[LotteryWinRecordDocument] = []
async for item in OldLotteryData.find_many(sort={"date": "ASC", "ranking": "ASC"}):
async for item in OldLotteryData.find_many(
{}, sort={"date": "ASC", "ranking": "ASC"}
):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))

Expand Down
2 changes: 1 addition & 1 deletion migrate_06_jpep.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def main() -> None:
logger.info(f"旧集合数据量:{old_data_count}")

data_to_save: List[FTNTradeOrderDocument] = []
async for item in OldJPEPFTNMacket.find_many(sort={"fetch_time": "ASC"}):
async for item in OldJPEPFTNMacket.find_many({}, sort={"fetch_time": "ASC"}):
await insert_or_update_user(item)
data_to_save.append(await convert_item(item))

Expand Down
20 changes: 6 additions & 14 deletions models/jianshu/article_earning_ranking_record.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import ClassVar, List, Optional
from typing import Optional

from jkit.msgspec_constraints import (
ArticleSlug,
Expand All @@ -8,28 +8,22 @@
PositiveInt,
UserSlug,
)
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Field, Index

from utils.db import JIANSHU_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
FIELD_OBJECT_CONFIG,
Document,
Field,
)


class ArticleField(Field, **FIELD_OBJECT_CONFIG):
class ArticleField(Field, **MODEL_CONFIG):
slug: Optional[ArticleSlug]
title: Optional[NonEmptyStr]


class EarningField(Field, **FIELD_OBJECT_CONFIG):
class EarningField(Field, **MODEL_CONFIG):
to_author: PositiveFloat
to_voter: PositiveFloat


class ArticleEarningRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class ArticleEarningRankingRecordDocument(Document, **MODEL_CONFIG):
date: datetime
ranking: PositiveInt

Expand All @@ -39,6 +33,4 @@ class ArticleEarningRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JIANSHU_DB.article_earning_ranking_records
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["date", "ranking"], unique=True),
]
indexes = (Index(keys=("date", "ranking"), unique=True),)
18 changes: 5 additions & 13 deletions models/jianshu/assets_ranking_record.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
from datetime import datetime
from typing import ClassVar, List, Optional
from typing import Optional

from jkit.msgspec_constraints import (
NonNegativeFloat,
PositiveFloat,
PositiveInt,
UserSlug,
)
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Field, Index

from utils.db import JIANSHU_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
FIELD_OBJECT_CONFIG,
Document,
Field,
)


class AmountField(Field, **FIELD_OBJECT_CONFIG):
class AmountField(Field, **MODEL_CONFIG):
fp: Optional[NonNegativeFloat]
ftn: Optional[NonNegativeFloat]
assets: Optional[PositiveFloat]


class AssetsRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class AssetsRankingRecordDocument(Document, **MODEL_CONFIG):
date: datetime
ranking: PositiveInt

Expand All @@ -33,6 +27,4 @@ class AssetsRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JIANSHU_DB.assets_ranking_records
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["date", "ranking"], unique=True),
]
indexes = (Index(keys=("date", "ranking"), unique=True),)
13 changes: 3 additions & 10 deletions models/jianshu/daily_update_ranking_record.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
from datetime import datetime
from typing import ClassVar, List

from jkit.msgspec_constraints import (
PositiveInt,
UserSlug,
)
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Index

from utils.db import JIANSHU_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
Document,
)


class DailyUpdateRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class DailyUpdateRankingRecordDocument(Document, **MODEL_CONFIG):
date: datetime
ranking: PositiveInt
days: PositiveInt
Expand All @@ -23,6 +18,4 @@ class DailyUpdateRankingRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JIANSHU_DB.daily_update_ranking_records
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["date", "userSlug"], unique=True),
]
indexes = (Index(keys=("date", "userSlug"), unique=True),)
15 changes: 4 additions & 11 deletions models/jianshu/lottery_win_record.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
from datetime import datetime
from typing import ClassVar, List

from jkit.msgspec_constraints import (
NonEmptyStr,
PositiveInt,
UserSlug,
)
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Index

from utils.db import JIANSHU_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
Document,
)


class LotteryWinRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class LotteryWinRecordDocument(Document, **MODEL_CONFIG):
id: PositiveInt
time: datetime
award_name: NonEmptyStr
Expand All @@ -24,13 +19,11 @@ class LotteryWinRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JIANSHU_DB.lottery_win_records
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["id"], unique=True),
]
indexes = (Index(keys=("id",), unique=True),)

@classmethod
async def get_latest_record_id(cls) -> int:
latest_data = await LotteryWinRecordDocument.find_one(sort={"id": "DESC"})
latest_data = await LotteryWinRecordDocument.find_one({}, sort={"id": "DESC"})
if not latest_data:
return 0

Expand Down
13 changes: 3 additions & 10 deletions models/jianshu/lp_recommend_article_record.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from typing import ClassVar, List

from jkit.msgspec_constraints import (
ArticleSlug,
Expand All @@ -10,16 +9,12 @@
UserSlug,
)
from msgspec import field
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Index

from utils.db import JIANSHU_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
Document,
)


class LPRecommendedArticleRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class LPRecommendedArticleRecordDocument(Document, **MODEL_CONFIG):
date: datetime
id: PositiveInt
slug: ArticleSlug
Expand All @@ -40,9 +35,7 @@ class LPRecommendedArticleRecordDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JIANSHU_DB.lp_recommended_article_records
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["date", "slug"], unique=True),
]
indexes = (Index(("date", "slug"), unique=True),)

@classmethod
async def is_record_exist(cls, slug: str) -> bool:
Expand Down
15 changes: 7 additions & 8 deletions models/jianshu/user.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from datetime import datetime
from enum import Enum
from typing import Any, ClassVar, Dict, List, Optional
from typing import Any, Dict, List, Optional

from jkit.msgspec_constraints import PositiveInt, UserName, UserSlug, UserUploadedUrl
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Index

from utils.db import JIANSHU_DB
from utils.document_model import Document


class JianshuUserStatus(Enum):
NORMAL = "NORMAL"
INACCESSABLE = "INACCESSIBLE"


class JianshuUserDocument(Document):
class JianshuUserDocument(Document, **MODEL_CONFIG):
slug: UserSlug
status: JianshuUserStatus
updated_at: datetime
Expand All @@ -25,10 +24,10 @@ class JianshuUserDocument(Document):

class Meta: # type: ignore
collection = JIANSHU_DB.users
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["slug"], unique=True),
IndexModel(["updatedAt"]),
]
indexes = (
Index(keys=("slug",), unique=True),
Index(keys=("updatedAt",)),
)

@classmethod
async def is_record_exist(cls, slug: str) -> bool:
Expand Down
14 changes: 4 additions & 10 deletions models/jpep/credit_history.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
from datetime import datetime
from typing import ClassVar, List, Optional
from typing import Optional

from jkit.msgspec_constraints import NonNegativeInt, PositiveInt
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Index

from utils.db import JPEP_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
Document,
)


class CreditHistoryDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class CreditHistoryDocument(Document, **MODEL_CONFIG):
time: datetime
user_id: PositiveInt
value: NonNegativeInt

class Meta: # type: ignore
collection = JPEP_DB.credit_history
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["time", "userId"], unique=True),
]
indexes = (Index(keys=("time", "userId"), unique=True),)

@classmethod
async def get_latest_value(cls, user_id: int) -> Optional[int]:
Expand Down
18 changes: 5 additions & 13 deletions models/jpep/ftn_trade_order.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
from datetime import datetime
from typing import ClassVar, List, Literal
from typing import Literal

from jkit.msgspec_constraints import (
NonNegativeInt,
PositiveFloat,
PositiveInt,
)
from pymongo import IndexModel
from sshared.mongo import MODEL_CONFIG, Document, Field, Index

from utils.db import JPEP_DB
from utils.document_model import (
DOCUMENT_OBJECT_CONFIG,
FIELD_OBJECT_CONFIG,
Document,
Field,
)


class AmountField(Field, **FIELD_OBJECT_CONFIG):
class AmountField(Field, **MODEL_CONFIG):
total: PositiveInt
traded: NonNegativeInt
tradable: NonNegativeInt
minimum_trade: PositiveInt


class FTNTradeOrderDocument(Document, **DOCUMENT_OBJECT_CONFIG):
class FTNTradeOrderDocument(Document, **MODEL_CONFIG):
fetch_time: datetime
id: PositiveInt
published_at: datetime
Expand All @@ -37,6 +31,4 @@ class FTNTradeOrderDocument(Document, **DOCUMENT_OBJECT_CONFIG):

class Meta: # type: ignore
collection = JPEP_DB.ftn_trade_orders
indexes: ClassVar[List[IndexModel]] = [
IndexModel(["fetchTime", "id"], unique=True),
]
indexes = (Index(keys=("fetchTime", "id"), unique=True),)
Loading

0 comments on commit 3d06c2a

Please sign in to comment.