This commit is contained in:
felinae98
2022-08-10 21:24:58 +08:00
parent ea928bf3fc
commit 014ec92ff9
9 changed files with 65 additions and 22 deletions
+3 -1
View File
@@ -11,7 +11,9 @@ export const store = configureStore({
[subscribeApi.reducerPath]: subscribeApi.reducer, [subscribeApi.reducerPath]: subscribeApi.reducer,
[weightApi.reducerPath]: weightApi.reducer, [weightApi.reducerPath]: weightApi.reducer,
}, },
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(subscribeApi.middleware), middleware: (getDefaultMiddleware) => getDefaultMiddleware()
.concat(subscribeApi.middleware)
.concat(weightApi.middleware),
}); });
export type AppDispatch = typeof store.dispatch; export type AppDispatch = typeof store.dispatch;
@@ -1,9 +1,28 @@
import React from 'react'; import React from 'react';
import { useGetWeightQuery } from './weightConfigSlice'; import { WeightConfig } from '../../utils/type';
import { useGetWeightQuery, useUpdateWeightMutation } from './weightConfigSlice';
export default function WeightManager() { export default function WeightManager() {
const { data: weight } = useGetWeightQuery(); const { data: weight } = useGetWeightQuery();
const [updateWeight] = useUpdateWeightMutation();
const doUpdate = () => {
const weightConfig: WeightConfig = {
default: 20,
time_config: [
{
start_time: '01:00',
end_time: '02:00',
weight: 50,
},
],
};
updateWeight({ weight: weightConfig, platform_name: 'weibo', target: '' });
};
return ( return (
<>
<div>{weight && JSON.stringify(weight)}</div> <div>{weight && JSON.stringify(weight)}</div>
<button type="button" onClick={doUpdate}> 123</button>
</>
); );
} }
@@ -1,4 +1,5 @@
import { createApi } from '@reduxjs/toolkit/query/react'; import { createApi } from '@reduxjs/toolkit/query/react';
import { PlatformWeightConfigResp, StatusResp } from '../../utils/type';
import baseQueryWithAuth from '../auth/authQuery'; import baseQueryWithAuth from '../auth/authQuery';
export const weightApi = createApi({ export const weightApi = createApi({
@@ -6,13 +7,22 @@ export const weightApi = createApi({
baseQuery: baseQueryWithAuth, baseQuery: baseQueryWithAuth,
tagTypes: ['Weight'], tagTypes: ['Weight'],
endpoints: (builder) => ({ endpoints: (builder) => ({
getWeight: builder.query<any, void>({ getWeight: builder.query<PlatformWeightConfigResp, void>({
query: () => '/weight', query: () => '/weight',
providesTags: ['Weight'], providesTags: ['Weight'],
}), }),
updateWeight: builder.mutation<StatusResp,
Pick<PlatformWeightConfigResp, 'platform_name' | 'target' | 'weight' >>({
query: ({ platform_name: platformName, target, weight }) => ({
method: 'PUT',
url: `/weight?platform_name=${platformName}&target=${target}`,
body: weight,
}),
invalidatesTags: ['Weight'],
}),
}), }),
}); });
export const { export const {
useGetWeightQuery, useGetWeightQuery, useUpdateWeightMutation,
} = weightApi; } = weightApi;
+12
View File
@@ -58,3 +58,15 @@ export interface TimeWeightConfig {
end_time: string; end_time: string;
weight: number; weight: number;
} }
export interface WeightConfig {
default: number;
time_config: TimeWeightConfig[];
}
export interface PlatformWeightConfigResp {
target: string;
target_name: string;
platform_name: string;
weight: WeightConfig;
}
@@ -136,9 +136,9 @@ def register_router_fastapi(driver: Driver, socketio):
async def _get_weight_config(): async def _get_weight_config():
return await get_weight_config() return await get_weight_config()
@app.patch(WEIGHT_URL, dependencies=[Depends(check_is_superuser)]) @app.put(WEIGHT_URL, dependencies=[Depends(check_is_superuser)])
async def _update_weight_config(platformName: str, target: str, req: WeightConfig): async def _update_weight_config(platform_name: str, target: str, req: WeightConfig):
return await update_weigth_config(platformName, target, req) return await update_weigth_config(platform_name, target, req)
app.mount(URL_BASE, SinglePageApplication(directory=static_path), name="bison") app.mount(URL_BASE, SinglePageApplication(directory=static_path), name="bison")
+1 -2
View File
@@ -1,11 +1,10 @@
from pathlib import Path from pathlib import Path
import nonebot
from alembic.config import Config from alembic.config import Config
from alembic.runtime.environment import EnvironmentContext from alembic.runtime.environment import EnvironmentContext
from alembic.script.base import ScriptDirectory from alembic.script.base import ScriptDirectory
from nonebot.log import logger from nonebot.log import logger
from nonebot_plugin_datastore import PluginData, create_session, db from nonebot_plugin_datastore import PluginData, db
from nonebot_plugin_datastore.db import get_engine from nonebot_plugin_datastore.db import get_engine
from sqlalchemy.engine.base import Connection from sqlalchemy.engine.base import Connection
from sqlalchemy.ext.asyncio.session import AsyncSession from sqlalchemy.ext.asyncio.session import AsyncSession
@@ -195,7 +195,10 @@ class DBConfig:
raise NoSuchTargetException() raise NoSuchTargetException()
target_id = targetObj.id target_id = targetObj.id
targetObj.default_schedule_weight = conf.default targetObj.default_schedule_weight = conf.default
delete(ScheduleTimeWeight).where(ScheduleTimeWeight.target_id == target_id) delete_statement = delete(ScheduleTimeWeight).where(
ScheduleTimeWeight.target_id == target_id
)
await sess.execute(delete_statement)
for time_conf in conf.time_config: for time_conf in conf.time_config:
new_conf = ScheduleTimeWeight( new_conf = ScheduleTimeWeight(
start_time=time_conf.start_time, start_time=time_conf.start_time,
@@ -271,6 +274,7 @@ class DBConfig:
res[platform_name][target.target] = PlatformWeightConfigResp( res[platform_name][target.target] = PlatformWeightConfigResp(
target=T_Target(target.target), target=T_Target(target.target),
target_name=target.target_name, target_name=target.target_name,
platform_name=platform_name,
weight=WeightConfig( weight=WeightConfig(
default=target.default_schedule_weight, time_config=[] default=target.default_schedule_weight, time_config=[]
), ),
+1 -3
View File
@@ -1,9 +1,7 @@
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import Column, ForeignKey, UniqueConstraint from sqlalchemy.sql.schema import Column, ForeignKey, UniqueConstraint
from sqlalchemy.sql.sqltypes import JSON, DateTime, Integer, String, Time from sqlalchemy.sql.sqltypes import JSON, Integer, String, Time
Base = declarative_base() Base = declarative_base()
+7 -8
View File
@@ -1,6 +1,8 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import time from datetime import time
from typing import Any, Callable, Literal, NamedTuple, NewType from typing import Any, Literal, NamedTuple, NewType
from pydantic import BaseModel
RawPost = NewType("RawPost", Any) RawPost = NewType("RawPost", Any)
Target = NewType("Target", str) Target = NewType("Target", str)
@@ -27,22 +29,19 @@ class UserSubInfo(NamedTuple):
tags: list[Tag] tags: list[Tag]
@dataclass class TimeWeightConfig(BaseModel):
class TimeWeightConfig:
start_time: time start_time: time
end_time: time end_time: time
weight: int weight: int
@dataclass class WeightConfig(BaseModel):
class WeightConfig:
default: int default: int
time_config: list[TimeWeightConfig] time_config: list[TimeWeightConfig]
@dataclass class PlatformWeightConfigResp(BaseModel):
class PlatformWeightConfigResp:
target: Target target: Target
target_name: str target_name: str
platform_name: str
weight: WeightConfig weight: WeightConfig