Skip to content

Commit

Permalink
Large directory rework, added electron-log
Browse files Browse the repository at this point in the history
  • Loading branch information
coltoneshaw committed Sep 16, 2021
1 parent bc63d4b commit 97f2116
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 43 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"webpack": "webpack",
"dev": "concurrently --kill-others \"npm run react:dev\" \"npm run electron:dev\"",
"start": "npm run build && electron dist/main.js",
"rebuild": "./node_modules/.bin/electron-rebuild -f -w better-sqlite3"
"rebuild": "./node_modules/.bin/electron-rebuild -f -w better-sqlite3",
"wpanddev": "webpack && concurrently --kill-others \"npm run react:dev\" \"npm run electron:dev\""
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -83,6 +84,7 @@
"date-fns-tz": "^1.1.6",
"dot-prop": "^6.0.1",
"electron-fetch": "^1.7.4",
"electron-log": "^4.4.1",
"electron-notarize": "^1.1.1",
"electron-store": "^8.0.0",
"material-ui-popup-state": "^1.9.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'
import crypto from "crypto";
import fetch from 'electron-fetch'
import fetch from 'electron-fetch';
const log = require('electron-log');


const API_URL = 'https://api.3commas.io'

Expand Down Expand Up @@ -50,7 +52,7 @@ class threeCommasAPI {

return await response.json()
} catch (e) {
console.log(e);
log.error(e);
return false
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/app/Features/3Commas/API/api.ts → src/main/3Commas/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const threeCommasAPI = require('./3commaslib')
const threeCommasAPI = require('./3commaslib');
const log = require('electron-log');
import { Type_API_bots, Type_Deals_API, Type_MarketOrders} from '@/types/3Commas'

import { getProfileConfig, setProfileConfig, getProfileConfigAll } from '@/utils/config';
import { getProfileConfig, setProfileConfig, getProfileConfigAll } from '@/main/Config/config';

import {
calc_dealHours,
Expand Down Expand Up @@ -39,7 +40,7 @@ const threeCapi = (profileData?: Type_Profile, key?: string, secret?: string, mo


if (key == null || secret == null || mode == null) {
console.error('missing API keys or mode')
log.error('missing API keys or mode')
return false
}

Expand Down Expand Up @@ -248,7 +249,7 @@ async function getDealsThatAreUpdated(perSyncOffset: number, profileData:Type_Pr
newLastSyncTime = new Date(response[0].updated_at).getTime()
}

console.info({
log.debug({
'responseArrayLength': responseArray.length,
'currentResponse': response.length,
offset,
Expand All @@ -266,7 +267,7 @@ async function getDealsThatAreUpdated(perSyncOffset: number, profileData:Type_Pr

}

console.log('Response data Length: ' + responseArray.length)
log.info('Response data Length: ' + responseArray.length)

// updating the last sync time if it's actually changed.
if (lastSyncTime != newLastSyncTime) { setProfileConfig('syncStatus.deals.lastSyncTime', newLastSyncTime, profileData.id) }
Expand Down Expand Up @@ -356,7 +357,7 @@ async function getAccountDetail(profileData:Type_Profile) {
const accountIDs = profileData.statSettings.reservedFunds.filter(a => a.is_enabled).map(a => a.id)

for (let account of accountData.filter((a:any) => accountIDs.includes(a.id))) {
// console.log('syncing the account ', account.id)
// log.info('syncing the account ', account.id)

// this loads the account balances from the exchange to 3C ensuring the numbers are updated
await api.accountLoadBalances(account.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { update, run, query } from '@/app/Features/Database/database';
import { update, run, query } from '@/main/Database/database';
const { bots, getAccountDetail, deals, getAccountSummary } = require('./api');
const { getProfileConfigAll } = require('@/utils/config')
import { findAndNotifyNewDeals } from '@/electron/Notifications/notifications'
const log = require('electron-log');

const { getProfileConfigAll } = require('@/main/Config/config')
import { findAndNotifyNewDeals } from '@/main/Notifications/notifications'


import { Type_Deals_API, Type_Query_Accounts, Type_API_bots, Type_UpdateFunction } from '@/types/3Commas'
import { Type_Profile } from '@/types/config'
Expand All @@ -17,17 +20,19 @@ async function updateAPI(type: string, options: Type_UpdateFunction, profileData
profileData = getProfileConfigAll()
}

log.debug(profileData)

await deals(options.offset, type, profileData)
.then((data: Type_Deals_API[]) => {

// if notifications need to be enabled for the fullSync then the type below needs to be updated.
if (type === 'autoSync' && options.notifications && options.time != undefined) findAndNotifyNewDeals(data, options.time, options.summary)
update('deals', data, profileData.id)
// console.log(data)
// log.info(data)
})

// if (type !== 'autoSync' || options.syncCount === 20) {
// console.log('updating the accounts!')
// log.info('updating the accounts!')
await getAccountData(profileData)
// }

Expand Down Expand Up @@ -92,7 +97,7 @@ async function getAndStoreBotData(profileData: Type_Profile) {

})
} catch (error) {
console.error('error getting bot data', error)
log.error('error getting bot data', error)
}

}
Expand Down
21 changes: 11 additions & 10 deletions src/utils/config.ts → src/main/Config/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TconfigValues, Type_Profile } from "@/types/config";
const log = require('electron-log');

const Store = require('electron-store');
const { run } = require('@/app/Features/Database/database')
const { run } = require('@/main/Database/database')
import { v4 as uuidv4 } from 'uuid';
import { defaultConfig } from '@/utils/defaultConfig';

Expand All @@ -13,7 +14,7 @@ const migrateCurrencyToArray = (store: any) => {

const migrationToProfiles = (config:any) => {
if(config.get('general.version') === 'v0.5.0') {
console.debug('looks like this is already on the latest version.')
log.debug('looks like this is already on the latest version.')
return false
}
const id = uuidv4()
Expand All @@ -40,8 +41,8 @@ const migrationToProfiles = (config:any) => {
run(`UPDATE deals SET profile_id='${id}' WHERE profile_id IS NULL`)
})
} catch (e) {
console.error(e)
console.error('error migrating to v0.5 ')
log.error(e)
log.error('error migrating to v0.5 ')
}

}
Expand All @@ -51,31 +52,31 @@ const migrationToProfiles = (config:any) => {
const config = new Store({
migrations: {
// '0.0.3': ( store: any )=>{
// console.info('migrating the config store to 0.0.2-RC1')
// log.info('migrating the config store to 0.0.2-RC1')
// store.set('statSettings.account_id', []);
// migrateCurrencyToArray(store)
// },
// '0.0.4': ( store: any )=>{
// console.info('migrating the config store to 0.0.4')
// console.log('adding a reserved funds array.')
// log.info('migrating the config store to 0.0.4')
// log.log('adding a reserved funds array.')
// store.set('statSettings.reservedFunds', []);
// },
// '0.1.0': ( store: any )=>{
// console.info('migrating the config store to 0.1.0')
// log.info('migrating the config store to 0.1.0')
// run('drop table bots;')
// store.set('general.updated', true)
// },
// '0.1.1': ( store: any )=>{
// store.set('general.updated', true)
// },
'<=0.2.0': () => {
console.log('running the v0.2 migration!!!!!!!!!!!!!!!!!!!!')
log.log('running the v0.2 migration!!!!!!!!!!!!!!!!!!!!')
// removing the bots that have been synced so they can be resynced and a new column added
run('ALTER TABLE bots ADD COLUMN hide boolean;')
run("delete from deals where status in ('failed', 'cancelled') ")
},
'v0.5.0': (store: any) => {
console.info('migrating the config store to 0.5.0')
log.info('migrating the config store to 0.5.0')

migrationToProfiles(store)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { app } from "electron";
import { config } from "@/utils/config";
import { config } from "@/main/Config/config";
const log = require('electron-log');

import path from "path";
import Database from 'better-sqlite3';

Expand Down Expand Up @@ -226,7 +228,7 @@ function normalizeData(data: any) {
async function update(table: string, data: any[], profileId:string) {

if (data.length == 0) {
console.log('no data to write')
log.log('no data to write')
return false
}

Expand Down Expand Up @@ -259,7 +261,7 @@ async function update(table: string, data: any[], profileId:string) {
async function upsert(table: string, data: any[], id: string, updateColumn: string) {

if (data.length == 0) {
console.log('no data to write')
log.log('no data to write')
return false
}

Expand Down Expand Up @@ -313,9 +315,9 @@ async function deleteAllData(profileID?: string) {
await run(`DELETE FROM bots;'`)
await run(`DELETE FROM accountData;`)
await run(`DELETE FROM deals;'`)
console.info('deleting all database info.')
log.info('deleting all database info.')
}
console.info('deleting all database info.')
log.info('deleting all database info.')
await run(`DELETE
FROM bots
WHERE profile_id = '${profileID}'`)
Expand All @@ -325,7 +327,7 @@ async function deleteAllData(profileID?: string) {
await run(`DELETE
FROM deals
WHERE profile_id = '${profileID}'`)
console.info('database info deleted.')
log.info('database info deleted.')

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Notification } from 'electron';
import { Type_Deals_API } from '@/types/3Commas'

import { parseNumber } from '@/utils/number_formatting'
import { getProfileConfig } from '@/utils/config'
import { getProfileConfig } from '@/main/Config/config'

import { convertMiliseconds } from '@/utils/helperFunctions'

Expand Down
14 changes: 8 additions & 6 deletions src/main.ts → src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {app, BrowserWindow, ipcMain, shell, Menu} from 'electron';
import {config} from './utils/config';
import {config} from '@/main/Config/config';
// import isDev from 'electron-is-dev'; // New Import
const log = require('electron-log');


const path = require("path");
const isDev = !app.isPackaged;

import { menu } from './electron/menu';
import { menu } from './menu';

const { update, query, checkOrMakeTables, run, deleteAllData, upsert } = require( '@/app/Features/Database/database');
const { update, query, checkOrMakeTables, run, deleteAllData, upsert } = require( '@/main/Database/database');

let win;

Expand Down Expand Up @@ -114,7 +116,7 @@ ipcMain.handle('database-deleteAll', (e, profileID: string) => {
});

ipcMain.handle('database-checkOrMakeTables', () => {
console.log('attempting to check if tables exist yet.')
log.log('attempting to check if tables exist yet.')
checkOrMakeTables()
});

Expand All @@ -125,7 +127,7 @@ ipcMain.handle('database-checkOrMakeTables', () => {
*
*/

const { updateAPI, getAndStoreBotData, getAccountSummary } = require('@/app/Features/3Commas/API/index');
const { updateAPI, getAndStoreBotData, getAccountSummary } = require('@/main/3Commas/index')
import {Type_Profile} from '@/types/config'

ipcMain.handle('api-updateData', async (event, type, options, profileData?:Type_Profile) => {
Expand Down Expand Up @@ -156,7 +158,7 @@ ipcMain.handle('binance-getCoins', async (event) => {
return await fetchCoinPricesBinance()
});

import { fetchVersions } from './app/Features/UpdateBanner/UpdateApiFetch';
import { fetchVersions } from '../app/Features/UpdateBanner/UpdateApiFetch';

ipcMain.handle('pm-versions', async (event) => {
return await fetchVersions()
Expand Down
6 changes: 3 additions & 3 deletions src/electron/menu.ts → src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { app, Menu, dialog, BrowserWindow } = require('electron')

const isMac = process.platform === 'darwin'

const {deleteAllData} = require('@/app/Features/Database/database');
const {setDefaultConfig} = require('@/utils/config')
const {deleteAllData} = require('@/main/Database/database');
const {setDefaultConfig} = require('@/main/Config/config')

const {win} = require('@/main')
const {win} = require('@/main/main')


const template = [
Expand Down
4 changes: 2 additions & 2 deletions webpack.electron.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('path');

module.exports = {
externals: {'better-sqlite3': 'commonjs2 better-sqlite3', '3commas-api-node': 'commonjs2 3commas-api-node' },
externals: {'better-sqlite3': 'commonjs2 better-sqlite3'},
// Build Mode
mode: 'development',
// Electron Entrypoint
entry: './src/main.ts',
entry: './src/main/main.ts',
target: 'electron-main',
resolve: {
alias: {
Expand Down

0 comments on commit 97f2116

Please sign in to comment.