-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
234 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from asyncio import run as asyncio_run | ||
|
||
from sshared.logging import Logger | ||
|
||
from models.jpep.credit_record import CreditRecord | ||
from utils.mongo import JPEP_DB | ||
|
||
OLD_COLLECTION = JPEP_DB.credit_history | ||
logger = Logger() | ||
|
||
|
||
async def main() -> None: | ||
await CreditRecord.init() | ||
|
||
logger.info("开始执行数据迁移") | ||
async for item in OLD_COLLECTION.find().sort({"time": 1, "userId": 1}): | ||
await CreditRecord( | ||
time=item["time"], user_id=item["userId"], credit=item["value"] | ||
).create() | ||
|
||
logger.debug(f"已迁移 {item['time']} - {item['userId']}") | ||
|
||
logger.info("数据迁移完成") | ||
|
||
|
||
asyncio_run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from asyncio import run as asyncio_run | ||
|
||
from sshared.logging import Logger | ||
|
||
from models.jpep.user import User | ||
from utils.mongo import JPEP_DB | ||
|
||
OLD_COLLECTION = JPEP_DB.users | ||
logger = Logger() | ||
|
||
|
||
async def main() -> None: | ||
await User.init() | ||
|
||
logger.info("开始执行数据迁移") | ||
async for item in OLD_COLLECTION.find().sort({"id": 1}): | ||
await User( | ||
id=item["id"], | ||
update_time=item["updatedAt"], | ||
name=item["name"], | ||
hashed_name=item["hashedName"], | ||
avatar_url=item["avatarUrl"], | ||
).create() | ||
|
||
logger.debug(f"已迁移 {item['id']}") | ||
|
||
logger.info("数据迁移完成") | ||
|
||
|
||
asyncio_run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from models.jianshu.article_earning_ranking_record import ( | ||
ArticleEarningRankingRecord, | ||
) | ||
from models.jianshu.daily_update_ranking_record import DailyUpdateRankingRecord | ||
from models.jianshu.user import User as JianshuUser | ||
from models.jianshu.user_assets_ranking_record import UserAssetsRankingRecord | ||
from models.jpep.credit_record import CreditRecord | ||
from models.jpep.ftn_trade_order import FTNTradeOrderDocument | ||
from models.jpep.user import User as JPEPUser | ||
|
||
|
||
async def init_db() -> None: | ||
await ArticleEarningRankingRecord.init() | ||
await DailyUpdateRankingRecord.init() | ||
await JianshuUser.init() | ||
await UserAssetsRankingRecord.init() | ||
await CreditRecord.init() | ||
await FTNTradeOrderDocument.ensure_indexes() | ||
await JPEPUser.init() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from sshared.postgres import Table | ||
from sshared.strict_struct import ( | ||
NonNegativeInt, | ||
PositiveInt, | ||
) | ||
|
||
from utils.postgres import get_jpep_conn | ||
|
||
|
||
class CreditRecord(Table, frozen=True): | ||
time: datetime | ||
user_id: PositiveInt | ||
credit: NonNegativeInt | ||
|
||
@classmethod | ||
async def _create_table(cls) -> None: | ||
conn = await get_jpep_conn() | ||
await conn.execute( | ||
""" | ||
CREATE TABLE IF NOT EXISTS credit_records ( | ||
time TIMESTAMP NOT NULL, | ||
user_id INTEGER NOT NULL, | ||
credit INTEGER NOT NULL, | ||
CONSTRAINT pk_credit_records_time_user_id PRIMARY KEY (time, user_id) | ||
); | ||
""" | ||
) | ||
|
||
async def create(self) -> None: | ||
self.validate() | ||
|
||
conn = await get_jpep_conn() | ||
await conn.execute( | ||
"INSERT INTO credit_records (time, user_id, credit) VALUES " | ||
"(%s, %s, %s);", | ||
(self.time, self.user_id, self.credit), | ||
) | ||
|
||
@classmethod | ||
async def get_latest_credit(cls, user_id: int) -> Optional[int]: | ||
conn = await get_jpep_conn() | ||
cursor = await conn.execute( | ||
"SELECT credit FROM credit_records WHERE user_id = %s " | ||
"ORDER BY time DESC LIMIT 1;", | ||
(user_id,), | ||
) | ||
|
||
data = await cursor.fetchone() | ||
if not data: | ||
return None | ||
|
||
return data[0] |
Oops, something went wrong.