Skip to content

Commit

Permalink
feat: hybrid db setup
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Oct 27, 2023
1 parent 5cc9c08 commit dadb121
Show file tree
Hide file tree
Showing 31 changed files with 346 additions and 362 deletions.
2 changes: 1 addition & 1 deletion client/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export interface Config {
start_lat: number
start_lon: number
tile_server: string
scanner_type: string
scanner_type: 'rdm' | 'unown' | 'hybrid'
logged_in: boolean
dangerous: boolean
}
Expand Down
10 changes: 5 additions & 5 deletions client/src/components/buttons/SaveToScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default function SaveToScanner({ fc, ...rest }: Props) {
const [loading, setLoading] = React.useState(false)
return (
<Button
disabled={useStatic.getState().scannerType === 'rdm' || loading}
onClick={() => {
disabled={useStatic.getState().scannerType !== 'unown' || loading}
onClick={async () => {
setLoading(true)
return save('/api/v1/geofence/save-scanner', fc)
.then(() => getScannerCache())
.then(() => setLoading(false))
await save('/api/v1/geofence/save-scanner', fc)
await getScannerCache()
return setLoading(false)
}}
{...rest}
>
Expand Down
27 changes: 10 additions & 17 deletions client/src/components/dialogs/import/AssignStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ const AssignStep = React.forwardRef<
}),
[nameProp, routeMode],
)

const modes =
scannerType === 'unown'
? routeMode
? UNOWN_ROUTES
: UNOWN_FENCES
: routeMode
? RDM_ROUTES
: RDM_FENCES
return (
<Grid2 container ref={ref} sx={{ width: '100%' }}>
<Grid2 xs={1} mt={1} />
Expand Down Expand Up @@ -183,14 +190,7 @@ const AssignStep = React.forwardRef<
}}
>
<MenuItem value="" />
{(scannerType === 'rdm'
? routeMode
? RDM_ROUTES
: RDM_FENCES
: routeMode
? UNOWN_ROUTES
: UNOWN_FENCES
).map((mode) => (
{modes.map((mode) => (
<MenuItem key={mode} value={mode}>
{mode}
</MenuItem>
Expand Down Expand Up @@ -425,14 +425,7 @@ const AssignStep = React.forwardRef<
}}
>
<MenuItem value="" />
{(scannerType === 'rdm'
? routeMode
? RDM_ROUTES
: RDM_FENCES
: routeMode
? UNOWN_ROUTES
: UNOWN_FENCES
).map((instanceType) => (
{modes.map((instanceType) => (
<MenuItem key={instanceType} value={instanceType}>
{instanceType}
</MenuItem>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/dialogs/import/ImportStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const ImportStep = React.forwardRef<
)
}}
controlled
filters={scannerType === 'rdm' ? RDM_FENCES : UNOWN_FENCES}
filters={scannerType === 'unown' ? UNOWN_FENCES : RDM_FENCES}
initialState={geojson.features
.filter((feat) => feat.properties?.__scanner)
.map((feat) => feat.id as KojiKey)}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/drawer/Routing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default function RoutingTab() {
<Toggle
field="save_to_scanner"
label="Save to Scanner Db"
disabled={scannerType === 'rdm'}
disabled={scannerType !== 'unown'}
/>
<Toggle field="skipRendering" />
<ListItemButton
Expand Down
3 changes: 2 additions & 1 deletion client/src/hooks/useStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
FeatureCollection,
StoreNoFn,
KojiTileServer,
Config,
} from '@assets/types'
import { collectionToObject } from '@services/utils'
import { ALL_FENCES, ALL_ROUTES } from '@assets/constants'
Expand Down Expand Up @@ -34,7 +35,7 @@ export interface UseStatic {
tileServers: KojiTileServer[]
kojiRoutes: { name: string; id: number; type: string }[]
scannerRoutes: { name: string; id: number; type: string }[]
scannerType: string
scannerType: Config['scanner_type']
dangerous: boolean
geojson: FeatureCollection
layerEditing: {
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/geofence/GeofenceFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export function GeofenceFilter() {
</FilterList>
<FilterList label="Mode" icon={<AutoModeIcon />}>
{[
...(scannerType === 'rdm' ? RDM_FENCES : UNOWN_FENCES),
'unset',
...(scannerType === 'unown' ? UNOWN_FENCES : RDM_FENCES),
].map((mode) => (
<FilterListItem key={mode} label={mode} value={{ mode }} />
))}
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/geofence/GeofenceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function GeofenceForm() {
<TextInput source="name" fullWidth required />
<SelectInput
source="mode"
choices={(scannerType === 'rdm' ? RDM_FENCES : UNOWN_FENCES).map(
choices={(scannerType === 'unown' ? UNOWN_FENCES : RDM_FENCES).map(
(mode, i) => ({ id: i, mode }),
)}
optionText="mode"
Expand Down
12 changes: 6 additions & 6 deletions client/src/pages/admin/project/ProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export default function ProjectForm() {
source="api_endpoint"
fullWidth
helperText={
scannerType === 'rdm'
? 'Hint! For RDM use this format: http://{host_ip}:{port}/api/set_data?reload_instances=true'
: 'http://{host_ip}:{port}/reload'
scannerType === 'unown'
? 'Hint! For Unown use this format: http://{host_ip}:{port}/reload'
: 'Hint! For RDM use this format: http://{host_ip}:{port}/api/set_data?reload_instances=true'
}
sx={{ my: 2 }}
/>
<TextInput
source="api_key"
fullWidth
helperText={
scannerType === 'rdm'
? 'Hint! For RDM use this format: {username}:{password}'
: ''
scannerType === 'unown'
? 'Hint! For Unown use this format: {header_name}:{api_key}'
: 'Hint! For RDM use this format: {username}:{password}'
}
sx={{ my: 2 }}
/>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/route/RouteFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function RouteFilter() {
<FilterLiveSearch />
<FilterList label="Mode" icon={<AutoModeIcon />}>
{[
...(scannerType === 'rdm' ? RDM_ROUTES : UNOWN_ROUTES),
...(scannerType === 'unown' ? UNOWN_ROUTES : RDM_ROUTES),
'unset',
].map((mode) => (
<FilterListItem key={mode} label={mode} value={{ mode }} />
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/route/RouteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function RouteForm() {
<TextInput source="description" fullWidth />
<SelectInput
source="mode"
choices={(scannerType === 'rdm' ? RDM_ROUTES : UNOWN_ROUTES).map(
choices={(scannerType === 'unown' ? UNOWN_ROUTES : RDM_ROUTES).map(
(mode, i) => ({ id: i, mode }),
)}
optionText="mode"
Expand Down
6 changes: 3 additions & 3 deletions client/src/pages/map/popups/Point.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export function PointPopup({ id, lat, lon, type: geoType, dbRef }: Props) {
updateProperty(feature.geometry.type, feature.id, '__mode', mode)
}
>
{(useStatic.getState().scannerType === 'rdm'
? RDM_ROUTES
: UNOWN_ROUTES
{(useStatic.getState().scannerType === 'unown'
? UNOWN_ROUTES
: RDM_ROUTES
).map((t) => (
<MenuItem key={t} value={t}>
{t}
Expand Down
6 changes: 3 additions & 3 deletions client/src/pages/map/popups/Polygon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ export function PolygonPopup({
updateProperty(feature.geometry.type, feature.id, '__mode', mode)
}
>
{(useStatic.getState().scannerType === 'rdm'
? RDM_FENCES
: UNOWN_FENCES
{(useStatic.getState().scannerType === 'unown'
? UNOWN_FENCES
: RDM_FENCES
).map((t) => (
<MenuItem key={t} value={t}>
{t}
Expand Down
4 changes: 2 additions & 2 deletions client/src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ export function getRouteType(category: Category): KojiRouteModes {
const { scannerType } = useStatic.getState()
switch (category) {
case 'gym':
return scannerType === 'rdm' ? 'circle_smart_raid' : 'circle_raid'
return scannerType === 'unown' ? 'circle_raid' : 'circle_smart_raid'
case 'pokestop':
return 'circle_quest'
default:
return scannerType === 'rdm' ? 'circle_smart_pokemon' : 'circle_pokemon'
return scannerType === 'unown' ? 'circle_pokemon' : 'circle_smart_pokemon'
}
}

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
environment:
# Required
SCANNER_DB_URL: 'mysql://rdm_user:rdm_password@host:3306/rdm_database'
# UNOWN_DB_URL: 'mysql://rdm_user:rdm_password@host:3306/flygon_database'
# CONTROLLER_DB_URL: 'mysql://rdm_user:rdm_password@host:3306/flygon_database'
KOJI_DB_URL: 'mysql://koji_user:[email protected]:3306/koji_database'
KOJI_SECRET: 'your_super_secret'
# Optional
Expand Down
2 changes: 1 addition & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Required
SCANNER_DB_URL='mysql://rdm_user:[email protected]:3306/rdm_database'
# UNOWN_DB_URL='mysql://unown_user:[email protected]:3306/flygon_database'
# CONTROLLER_DB_URL='mysql://unown_user:[email protected]:3306/flygon_database'
KOJI_DB_URL='mysql://koji_user:[email protected]:3306/koji_database'
KOJI_SECRET='your_super_secret_password'

Expand Down
12 changes: 2 additions & 10 deletions server/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod utils;
pub async fn start() -> io::Result<()> {
let databases = model::utils::get_database_struct().await;

match Migrator::up(&databases.koji_db, None).await {
match Migrator::up(&databases.koji, None).await {
Ok(_) => log::info!("Migrations successful"),
Err(err) => log::error!("Migration Error {:?}", err),
};
Expand All @@ -53,18 +53,10 @@ pub async fn start() -> io::Result<()> {
)
.unwrap();

let scanner_type = if databases.unown_db.is_none() {
"rdm"
} else {
"unown"
}
.to_string();

App::new()
.app_data(web::Data::new(databases.clone()))
.app_data(web::Data::new(scanner_type))
.app_data(web::Data::new(client))
// increase max payload size to 20MB
// increase max payload size to 50MB
.app_data(web::JsonConfig::default().limit(1024 * 1024 * 50))
.wrap(middleware::Logger::new("%s | %r - %b bytes in %D ms (%a)"))
.wrap(middleware::Compress::default())
Expand Down
76 changes: 38 additions & 38 deletions server/api/src/private/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ async fn paginate(
let resource = path.into_inner();

let paginated_results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::paginate(&db.koji_db, parsed).await,
"project" => db::project::Query::paginate(&db.koji_db, parsed).await,
"property" => db::property::Query::paginate(&db.koji_db, parsed).await,
"route" => db::route::Query::paginate(&db.koji_db, parsed).await,
"tileserver" => db::tile_server::Query::paginate(&db.koji_db, parsed).await,
"geofence" => db::geofence::Query::paginate(&db.koji, parsed).await,
"project" => db::project::Query::paginate(&db.koji, parsed).await,
"property" => db::property::Query::paginate(&db.koji, parsed).await,
"route" => db::route::Query::paginate(&db.koji, parsed).await,
"tileserver" => db::tile_server::Query::paginate(&db.koji, parsed).await,
_ => Err(DbErr::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -50,8 +50,8 @@ async fn parent_list(
let resource = path.into_inner();

let results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::unique_parents(&db.koji_db).await,
"route" => db::route::Query::unique_geofence(&db.koji_db).await,
"geofence" => db::geofence::Query::unique_parents(&db.koji).await,
"route" => db::route::Query::unique_geofence(&db.koji).await,
_ => Err(ModelError::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -73,11 +73,11 @@ async fn get_all(
let resource = path.into_inner();

let results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::get_json_cache(&db.koji_db).await,
"project" => db::project::Query::get_json_cache(&db.koji_db).await,
"property" => db::property::Query::get_json_cache(&db.koji_db).await,
"route" => db::route::Query::get_json_cache(&db.koji_db).await,
"tileserver" => db::tile_server::Query::get_json_cache(&db.koji_db).await,
"geofence" => db::geofence::Query::get_json_cache(&db.koji).await,
"project" => db::project::Query::get_json_cache(&db.koji).await,
"property" => db::property::Query::get_json_cache(&db.koji).await,
"route" => db::route::Query::get_json_cache(&db.koji).await,
"tileserver" => db::tile_server::Query::get_json_cache(&db.koji).await,
_ => Err(DbErr::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -99,11 +99,11 @@ async fn get_one(
let (resource, id) = path.into_inner();

let result = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::get_one_json_with_related(&db.koji_db, id).await,
"project" => db::project::Query::get_one_json_with_related(&db.koji_db, id).await,
"property" => db::property::Query::get_one_json(&db.koji_db, id).await,
"route" => db::route::Query::get_one_json(&db.koji_db, id).await,
"tileserver" => db::tile_server::Query::get_one_json(&db.koji_db, id).await,
"geofence" => db::geofence::Query::get_one_json_with_related(&db.koji, id).await,
"project" => db::project::Query::get_one_json_with_related(&db.koji, id).await,
"property" => db::property::Query::get_one_json(&db.koji, id).await,
"route" => db::route::Query::get_one_json(&db.koji, id).await,
"tileserver" => db::tile_server::Query::get_one_json(&db.koji, id).await,
_ => Err(ModelError::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -127,11 +127,11 @@ async fn create(
let resource = path.into_inner();

let result = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::upsert_json_return(&db.koji_db, 0, payload).await,
"project" => db::project::Query::upsert_json_return(&db.koji_db, 0, payload).await,
"property" => db::property::Query::upsert_json_return(&db.koji_db, 0, payload).await,
"route" => db::route::Query::upsert_json_return(&db.koji_db, 0, payload).await,
"tileserver" => db::tile_server::Query::upsert_json_return(&db.koji_db, 0, payload).await,
"geofence" => db::geofence::Query::upsert_json_return(&db.koji, 0, payload).await,
"project" => db::project::Query::upsert_json_return(&db.koji, 0, payload).await,
"property" => db::property::Query::upsert_json_return(&db.koji, 0, payload).await,
"route" => db::route::Query::upsert_json_return(&db.koji, 0, payload).await,
"tileserver" => db::tile_server::Query::upsert_json_return(&db.koji, 0, payload).await,
_ => Err(ModelError::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -155,11 +155,11 @@ async fn update(
let payload = payload.into_inner();

let result = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::upsert_json_return(&db.koji_db, id, payload).await,
"project" => db::project::Query::upsert_json_return(&db.koji_db, id, payload).await,
"property" => db::property::Query::upsert_json_return(&db.koji_db, id, payload).await,
"route" => db::route::Query::upsert_json_return(&db.koji_db, id, payload).await,
"tileserver" => db::tile_server::Query::upsert_json_return(&db.koji_db, id, payload).await,
"geofence" => db::geofence::Query::upsert_json_return(&db.koji, id, payload).await,
"project" => db::project::Query::upsert_json_return(&db.koji, id, payload).await,
"property" => db::property::Query::upsert_json_return(&db.koji, id, payload).await,
"route" => db::route::Query::upsert_json_return(&db.koji, id, payload).await,
"tileserver" => db::tile_server::Query::upsert_json_return(&db.koji, id, payload).await,
_ => Err(ModelError::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -181,11 +181,11 @@ async fn remove(
let (resource, id) = path.into_inner();

let result = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::delete(&db.koji_db, id).await,
"project" => db::project::Query::delete(&db.koji_db, id).await,
"property" => db::property::Query::delete(&db.koji_db, id).await,
"route" => db::route::Query::delete(&db.koji_db, id).await,
"tileserver" => db::tile_server::Query::delete(&db.koji_db, id).await,
"geofence" => db::geofence::Query::delete(&db.koji, id).await,
"project" => db::project::Query::delete(&db.koji, id).await,
"property" => db::property::Query::delete(&db.koji, id).await,
"route" => db::route::Query::delete(&db.koji, id).await,
"tileserver" => db::tile_server::Query::delete(&db.koji, id).await,
_ => Err(DbErr::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand All @@ -211,7 +211,7 @@ async fn assign(
let (resource, property, id) = path.into_inner();

let results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::assign(&db.koji_db, id, property, payload).await,
"geofence" => db::geofence::Query::assign(&db.koji, id, property, payload).await,
// "project" => db::project::Query::search(&db.koji_db, search.query).await,
// "property" => db::property::Query::search(&db.koji_db, search.query).await,
// "route" => db::route::Query::search(&db.koji_db, search.query).await,
Expand All @@ -238,11 +238,11 @@ async fn search(
let resource = path.into_inner();

let results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::search(&db.koji_db, search.query).await,
"project" => db::project::Query::search(&db.koji_db, search.query).await,
"property" => db::property::Query::search(&db.koji_db, search.query).await,
"route" => db::route::Query::search(&db.koji_db, search.query).await,
"tileserver" => db::tile_server::Query::search(&db.koji_db, search.query).await,
"geofence" => db::geofence::Query::search(&db.koji, search.query).await,
"project" => db::project::Query::search(&db.koji, search.query).await,
"property" => db::property::Query::search(&db.koji, search.query).await,
"route" => db::route::Query::search(&db.koji, search.query).await,
"tileserver" => db::tile_server::Query::search(&db.koji, search.query).await,
_ => Err(DbErr::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;
Expand Down
Loading

0 comments on commit dadb121

Please sign in to comment.