Skip to content

Commit

Permalink
Initial build with moving storage to a database per profile
Browse files Browse the repository at this point in the history
  • Loading branch information
coltoneshaw committed Dec 1, 2021
1 parent 3f30d3c commit 35bc92e
Show file tree
Hide file tree
Showing 23 changed files with 384 additions and 355 deletions.
2 changes: 1 addition & 1 deletion src/app/Components/Charts/Line/PairByDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getPairList = async (updatePairs: Function, updatePairFilters: Function, w

// selecting the pair data and sorting by profit for easier viewing.

window.ThreeCPM.Repository.Database.query(`select pair, sum(actual_profit) as total_profit from deals WHERE ${wheres} and profile_id = '${currentProfileID}' and from_currency in (${currencyString}) and account_id in (${accountIdString}) group by pair order by total_profit desc;`)
window.ThreeCPM.Repository.Database.query(currentProfileID, `select pair, sum(actual_profit) as total_profit from deals WHERE ${wheres} and profile_id = '${currentProfileID}' and from_currency in (${currencyString}) and account_id in (${accountIdString}) group by pair order by total_profit desc;`)
.then((result: { pair: string }[]) => {
updatePairs(result.map(pair => ({ pair: pair.pair, opacity: 1 })))
let storedPairs = getStorageItem(storageItem.charts.pairByDateFilter)
Expand Down
2 changes: 1 addition & 1 deletion src/app/Features/3Commas/DataQueries/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getAccountDataFunction = async (profileData: Type_Profile) => {
and currency_code IN ( ${currencyString} )
and profile_id = '${currentProfileID}';
`
let accountData: Type_Query_Accounts[] | [] = await window.ThreeCPM.Repository.Database.query(query)
let accountData: Type_Query_Accounts[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, query)

// removed this since it seems redundant to the above query
// .then((data: Type_Query_Accounts[]) => data.filter(row => defaultCurrency.includes(row.currency_code)))
Expand Down
4 changes: 2 additions & 2 deletions src/app/Features/3Commas/DataQueries/bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const fetchBotPerformanceMetrics = async (profileData: Type_Profile, oDate?: Dat
GROUP BY
bot_id;`

let databaseQuery: fetchBotPerformanceMetrics[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: fetchBotPerformanceMetrics[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, queryString);

if (databaseQuery == null || databaseQuery.length > 0) {
return databaseQuery
Expand All @@ -71,7 +71,7 @@ const botQuery = async (currentProfile: Type_Profile): Promise<Type_Query_bots[]
and from_currency in (${currencyString})
and (account_id in (${accountIdString}) OR origin = 'custom')`

let databaseQuery: Type_Query_bots[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: Type_Query_bots[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, queryString);

if (databaseQuery != null) return databaseQuery
return []
Expand Down
8 changes: 4 additions & 4 deletions src/app/Features/3Commas/DataQueries/deals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const fetchDealDataFunction = async (profileData: Type_Profile) => {
ORDER BY
closed_at asc;`

let dataArray: fetchDealDataFunctionQuery[] | [] = await window.ThreeCPM.Repository.Database.query(query)
let dataArray: fetchDealDataFunctionQuery[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, query)

// if no data return blank array.
if (dataArray == null || dataArray.length === 0) {
Expand Down Expand Up @@ -132,7 +132,7 @@ const fetchPerformanceDataFunction = async (profileData: Type_Profile, oDate?: D
performance_id;`


let databaseQuery: fetchPerformanceData[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: fetchPerformanceData[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, queryString);

if (databaseQuery == null || databaseQuery.length > 0) {
const totalProfitSummary = databaseQuery
Expand Down Expand Up @@ -170,7 +170,7 @@ const getActiveDealsFunction = async (profileData: Type_Profile) => {
and currency in (${currencyString} )
and profile_id = '${currentProfileID}'
`
let activeDeals: Type_ActiveDeals[] | [] = await window.ThreeCPM.Repository.Database.query(query)
let activeDeals: Type_ActiveDeals[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, query)


if (activeDeals != undefined && activeDeals.length > 0) {
Expand Down Expand Up @@ -228,7 +228,7 @@ const fetchSoData = async (currentProfile: Type_Profile, oDate?: DateRange) => {
group by
completed_safety_orders_count;`

const data : fetchSoData[] | [] = await window.ThreeCPM.Repository.Database.query(query)
const data : fetchSoData[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, query)

if(!data || data.length == 0){
return []
Expand Down
4 changes: 2 additions & 2 deletions src/app/Features/3Commas/DataQueries/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const fetchPairPerformanceMetrics = async (profileData: Type_Profile, oDate?: Da
GROUP BY
pair;`

let databaseQuery: fetchPairPerformanceMetrics[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: fetchPairPerformanceMetrics[] | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, queryString);

if (databaseQuery == null || databaseQuery.length > 0) {
return databaseQuery
Expand Down Expand Up @@ -82,7 +82,7 @@ const getSelectPairDataByDate = async (profileData: Type_Profile, pairs: string[
`


let pairData: Array<Type_Pair_By_Date> | [] = await window.ThreeCPM.Repository.Database.query(query);
let pairData: Array<Type_Pair_By_Date> | [] = await window.ThreeCPM.Repository.Database.query(currentProfileID, query);


let currentDate = moment(date.from).clone();
Expand Down
10 changes: 5 additions & 5 deletions src/app/Pages/BotPlanner/BotPlanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ const BotPlannerPage = () => {
const saveCustomDeals = async () => {
const customBots = localBotData.filter(bot => bot.origin === 'custom')

if (customBots.length > 0) window.ThreeCPM.Repository.Database.update('bots', customBots)
if (customBots.length > 0) window.ThreeCPM.Repository.Database.update(currentProfile.id, 'bots', customBots)


// Deciding what bots to delete if they're included in the local bot data or not.
// Needs to have the logic thought through again. There seems to be reduncant calls here.
const customBotIds = customBots.map(bot => bot.id);
if (customBotIds.length === 0) {
window.ThreeCPM.Repository.Database.run(`DELETE from bots where origin = 'custom' AND profile_id = '${currentProfile.id}'`)
window.ThreeCPM.Repository.Database.run(currentProfile.id, `DELETE from bots where origin = 'custom' AND profile_id = '${currentProfile.id}'`)
} else {
window.ThreeCPM.Repository.Database.query(`select * from bots where origin = 'custom' AND profile_id = '${currentProfile.id}';`)
window.ThreeCPM.Repository.Database.query(currentProfile.id, `select * from bots where origin = 'custom' AND profile_id = '${currentProfile.id}';`)
.then((table: Type_Query_bots[]) => {
for (let row of table) {
if (!customBotIds.includes(row.id)) {
window.ThreeCPM.Repository.Database.run(`DELETE from bots where id = '${row.id}' AND profile_id = '${currentProfile.id}'`)
window.ThreeCPM.Repository.Database.run(currentProfile.id, `DELETE from bots where id = '${row.id}' AND profile_id = '${currentProfile.id}'`)
}
}
});
}
window.ThreeCPM.Repository.Database.upsert('bots', localBotData, 'id', 'hide')
window.ThreeCPM.Repository.Database.upsert(currentProfile.id, 'bots', localBotData, 'id', 'hide')
}

return (
Expand Down
12 changes: 6 additions & 6 deletions src/app/Pages/DailyStats/Components/3Commas/dailyDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const queryDealByPairByDay = async (profileData: Type_Profile, utcDateRange: utc
pair;`


let databaseQuery: queryDealByPairByDayQuery[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: queryDealByPairByDayQuery[] | [] = await window.ThreeCPM.Repository.Database.query(profileData.id, queryString);

if (!databaseQuery || databaseQuery.length > 0) {
const totalProfitSummary = databaseQuery
Expand Down Expand Up @@ -79,7 +79,7 @@ const queryDealByBotByDay = async (profileData: Type_Profile, utcDateRange: utcD
bot_id;`


let databaseQuery: botQueryDealByDayQuery[] | [] = await window.ThreeCPM.Repository.Database.query(queryString);
let databaseQuery: botQueryDealByDayQuery[] | [] = await window.ThreeCPM.Repository.Database.query(profileData.id, queryString);

if (!databaseQuery || databaseQuery.length > 0) {
const totalProfitSummary = databaseQuery
Expand Down Expand Up @@ -119,7 +119,7 @@ const getHistoricalProfits = async (profitArray: Type_Profit[] | [], filters: fi
and closed_at_iso_string BETWEEN ${utcStart - daysInMilli.thirty} and ${utcStart}
and profile_id = '${currentProfileID}';`

let sixtyDayProfit = await window.ThreeCPM.Repository.Database.query(sixtyQuery);
let sixtyDayProfit = await window.ThreeCPM.Repository.Database.query(currentProfileID, sixtyQuery);
const totalDays = profitArray.length

const currentWeek = profitArray
Expand Down Expand Up @@ -163,7 +163,7 @@ const getTotalProfit = async (profileData: Type_Profile, filters: filters): Prom
and currency in (${filters.currency} )
and profile_id = '${profileData.id}';`

const total = await window.ThreeCPM.Repository.Database.query(totalProfit);
const total = await window.ThreeCPM.Repository.Database.query(profileData.id, totalProfit);
return total[0].final_profit
}

Expand Down Expand Up @@ -191,7 +191,7 @@ const queryProfitDataByDay = async (profileData: Type_Profile, utcDateRange: utc
ORDER BY
closed_at asc;`

let dataArray: fetchDealDataFunctionQuery[] | [] = await window.ThreeCPM.Repository.Database.query(query)
let dataArray: fetchDealDataFunctionQuery[] | [] = await window.ThreeCPM.Repository.Database.query(profileData.id, query)

// if no data return blank array.
if (dataArray == null || dataArray.length === 0) {
Expand Down Expand Up @@ -281,7 +281,7 @@ const getActiveDealsFunction = async (profileData: Type_Profile, filters: filter
and currency in (${filters.currency} )
and profile_id = '${profileData.id}'
`
let activeDeals: Type_ActiveDeals[] | [] = await window.ThreeCPM.Repository.Database.query(query)
let activeDeals: Type_ActiveDeals[] | [] = await window.ThreeCPM.Repository.Database.query(profileData.id, query)


if (activeDeals != undefined && activeDeals.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/Pages/Settings/Components/SaveDeleteButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { useAppSelector, useAppDispatch } from '@/app/redux/hooks';
import { updateConfig, deleteProfileByIdGlobal, storeConfigInFile, updateNestedCurrentProfile } from '@/app/redux/configActions'
import { useAppSelector } from '@/app/redux/hooks';
import { updateConfig, deleteProfileByIdGlobal, updateNestedCurrentProfile } from '@/app/redux/configActions'
import { syncNewProfileData } from '@/app/redux/threeCommas/Actions'
import { configPaths } from '@/app/redux/configSlice';

Expand Down
3 changes: 3 additions & 0 deletions src/app/Repositories/Impl/electron/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default class ElectronConfigRepository extends BaseElectronRepository imp
get = (value: 'all' | string) => {
return this.mainPreload.config.get(value)
}
profile = (type: 'create' | 'delete', config: typeof defaultConfig, profileId: string) => {
return this.mainPreload.config.profile(type, config, profileId)
}
getProfile = (value: string, profileId: string) => {
return this.mainPreload.config.getProfile(value, profileId)
}
Expand Down
12 changes: 6 additions & 6 deletions src/app/Repositories/Impl/electron/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { DBRepository } from '@/app/Repositories/interfaces';
import { tableNames } from "@/types/preload";

export default class ElectronDBRepository extends BaseElectronRepository implements DBRepository {
query = async (queryString:string) => await this.mainPreload.database.query(queryString);
update = (table:tableNames, data:object[]) => {
query = async (profileId:string, queryString:string) => await this.mainPreload.database.query(profileId, queryString);
update = (profileId:string, table:tableNames, data:object[]) => {
if(!data || data.length === 0){
console.log('no data to update');
return
}
this.mainPreload.database.update(table, data);
this.mainPreload.database.update(profileId, table, data);
}
upsert = (table:tableNames, data:any[], id:string, updateColumn:string) => {
upsert = (profileId:string, table:tableNames, data:any[], id:string, updateColumn:string) => {
if(!data || data.length === 0){
console.log('no data to update');
return
}
this.mainPreload.database.upsert(table, data, id, updateColumn);
this.mainPreload.database.upsert(profileId, table, data, id, updateColumn);
}
run = (query:string) => this.mainPreload.database.run(query);
run = (profileId:string, query:string) => this.mainPreload.database.run(profileId, query);
deleteAllData = async (profileID?: string) => await this.mainPreload.database.deleteAllData(profileID);
}
1 change: 1 addition & 0 deletions src/app/Repositories/interfaces/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { config } from "@/types/preload";

export default interface ConfigRepository {
get: config['get'];
profile: config['profile'];
getProfile: config['getProfile']
reset: config['reset']
set: config['set']
Expand Down
7 changes: 0 additions & 7 deletions src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { HashRouter } from 'react-router-dom'

import { MainWindow } from "@/app/Pages/Index"

// import { ConfigProvider } from './Context/Config';
import { useThemeProvidor } from './Context/ThemeEngine';

import UpdateBanner from './Features/UpdateBanner/UpdateBanner';
Expand All @@ -29,13 +28,11 @@ const App = () => {
useEffect(() => {
console.log('updating the config here')
updateConfig();

}, [dispatch]);

useLayoutEffect(() => {
// if(updated) return
if(currentProfile.id == profile.id) return

if(currentProfile && currentProfile?.statSettings?.reservedFunds.filter(a => a.is_enabled).length > 0) {
updateAllDataQuery(currentProfile, 'fullSync');
console.log('Changing to a new profile')
Expand All @@ -53,10 +50,6 @@ const App = () => {
<Sidebar />
<MainWindow />
</div>




</HashRouter>
)
}
Expand Down
13 changes: 7 additions & 6 deletions src/app/redux/configSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const configSlice = createSlice({
state.config = action.payload
},
setCurrentProfile: (state, action) => {
console.trace(action)
state.currentProfile = action.payload
},
setCurrentProfileById: (state, action) => {
Expand All @@ -59,12 +60,12 @@ export const configSlice = createSlice({
state.currentProfile = { ...newConfig.profiles[profileId] }
state.config = { ...newConfig, current: profileId }
},
storeCurrentProfile: state => {
const newConfig = { ...state.config }
const currentProfile = state.currentProfile
newConfig.profiles[currentProfile.id] = currentProfile
state.config = newConfig
},
// storeCurrentProfile: state => {
// const newConfig = { ...state.config }
// const currentProfile = state.currentProfile
// newConfig.profiles[currentProfile.id] = currentProfile
// state.config = newConfig
// },
updateLastSyncTime: (state, action) => {
const { data } = action.payload
let newProfile = Object.assign({}, { ...state.currentProfile })
Expand Down
23 changes: 12 additions & 11 deletions src/app/redux/threeCommas/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const fetchAndStoreBotData = async (currentProfile: Type_Profile, update: boolea
if (update && result.length > 0) dispatch_setBotData(result)
const inactiveBotFunds = result.filter(b => b.is_enabled === 1).map(r => r.enabled_inactive_funds)
// pull enabled_inactive_funds from the bots and add it to metrics.
dispatch_setMetricsData({ inactiveBotFunds: (inactiveBotFunds.length > 0) ? inactiveBotFunds.reduce((sum, funds) => sum + funds) : 0})
dispatch_setMetricsData({ inactiveBotFunds: (inactiveBotFunds.length > 0) ? inactiveBotFunds.reduce((sum, funds) => sum + funds) : 0 })

})
} catch (error) {
Expand Down Expand Up @@ -112,7 +112,7 @@ const fetchAndStorePerformanceData = async (profileData: Type_Profile) => {
console.log('getting SO performance metrics')
dispatch_setPerformanceData({ safety_order: data })
}))

await Promise.all([pair_bot(), bot(), pair(), so()])
}

Expand Down Expand Up @@ -243,12 +243,12 @@ const updateAllData = async (offset: number = 1000, profileData: Type_Profile, t
})
.then((lastSyncTime) => {
store.dispatch(setSyncData({
syncCount:(type === 'autoSync') ? options.syncCount + 1 : 0,
syncCount: (type === 'autoSync') ? options.syncCount + 1 : 0,
// don't override syncOptions.time in case of a fullSync
// because there might be a concurrent autoSync running
time: (type === 'autoSync') ? originalTime + 15000 : originalTime
}))
store.dispatch(updateLastSyncTime({data: lastSyncTime }))
store.dispatch(updateLastSyncTime({ data: lastSyncTime }))
})

} catch (error) {
Expand All @@ -271,12 +271,13 @@ const syncNewProfileData = async (offset: number = 1000) => {

const options = { syncCount: 0, summary: false, notifications: false, time: 0, offset }
let success;


try {
await window.ThreeCPM.Repository.Config.bulk(store.getState().config.config)
await updateThreeCData('newProfile', options, profileData)
.then(async () => updateAllDataQuery(profileData, 'fullSync'))
await window.ThreeCPM.Repository.Config.profile('create', store.getState().config.config, profileData.id)
.then(async () => {
await updateThreeCData('newProfile', options, profileData)
.then(async () => await updateAllDataQuery(profileData, 'fullSync'))
})

success = true;
} catch (error) {
console.error(error)
Expand Down Expand Up @@ -314,7 +315,7 @@ const refreshFunction = (method: string, offset?: number) => {
// updating the refresh function here to 15000 instead of 50. The update bar doesn't work anymore
const refreshRate = 15000

if(!store.getState().threeCommas.autoRefresh) return
if (!store.getState().threeCommas.autoRefresh) return
switch (method) {
case 'stop':
store.dispatch(setAutoRefresh(false))
Expand All @@ -327,7 +328,7 @@ const refreshFunction = (method: string, offset?: number) => {
return
}

if(!store.getState().threeCommas.autoRefresh) return
if (!store.getState().threeCommas.autoRefresh) return
updateAllData(offset, profileData, 'autoSync', undefined)
.then(() => {
refreshFunction('run', offset)
Expand Down
Loading

0 comments on commit 35bc92e

Please sign in to comment.