Przeglądaj źródła

add basic tracer function.

oldj 4 lat temu
rodzic
commit
4de108519f

+ 2 - 0
src/main/actions/cmd/tryToRun.ts

@@ -43,4 +43,6 @@ export default async () => {
       await cfgdb.collection.cmd_history.delete(item => item._id === all[i]._id)
     }
   }
+
+  global.tracer.add(`cmd:${result.success ? 1 : 0}`)
 }

+ 8 - 6
src/main/actions/hosts/setSystemHosts.ts

@@ -43,7 +43,7 @@ const addHistory = async (content: string) => {
   await swhdb.collection.history.insert({
     id: uuid4(),
     content,
-    add_time_ms: (new Date()).getTime()
+    add_time_ms: (new Date()).getTime(),
   })
 
   let history_limit = await configGet('history_limit')
@@ -67,7 +67,7 @@ const writeWithSudo = (sys_hosts_path: string, content: string): Promise<IWriteR
   let cmd = [
     `echo '${sudo_pswd}' | sudo -S chmod 777 ${sys_hosts_path}`
     , `cat "${tmp_fn}" > ${sys_hosts_path}`
-    , `echo '${sudo_pswd}' | sudo -S chmod 644 ${sys_hosts_path}`
+    , `echo '${sudo_pswd}' | sudo -S chmod 644 ${sys_hosts_path}`,
     // , 'rm -rf ' + tmp_fn
   ].join(' && ')
 
@@ -86,7 +86,7 @@ const writeWithSudo = (sys_hosts_path: string, content: string): Promise<IWriteR
       console.log('success.')
 
       result = {
-        success: true
+        success: true,
       }
     } else {
       console.log('fail!')
@@ -94,7 +94,7 @@ const writeWithSudo = (sys_hosts_path: string, content: string): Promise<IWriteR
 
       result = {
         success: false,
-        message: stderr
+        message: stderr,
       }
     }
 
@@ -138,7 +138,7 @@ const write = async (content: string, options?: IHostsWriteOptions): Promise<IWr
 
     return {
       success: false,
-      code: 'no_access'
+      code: 'no_access',
     }
   }
 
@@ -154,7 +154,7 @@ const write = async (content: string, options?: IHostsWriteOptions): Promise<IWr
     return {
       success: false,
       code,
-      message: e.message
+      message: e.message,
     }
   }
 
@@ -180,6 +180,8 @@ const setSystemHosts = async (content: string, options?: IHostsWriteOptions): Pr
     await tryToRun()
   }
 
+  global.tracer.add(`w:${success ? 1 : 0}`)
+
   return result
 }
 

+ 3 - 0
src/main/libs/cron.ts

@@ -62,6 +62,9 @@ const check = async () => {
 
   checkServer()
     .catch(e => console.error(e))
+
+  global.tracer.emit()
+    .catch(e => console.error(e))
 }
 
 export const start = () => {

+ 33 - 0
src/main/libs/tracer.ts

@@ -0,0 +1,33 @@
+import { configGet } from '@main/actions'
+import { GET } from '@main/libs/request'
+import { server_url } from '@root/common/constants'
+
+class Tracer {
+  data: string[]
+
+  constructor () {
+    this.data = []
+  }
+
+  add (action: string) {
+    this.data.push(action)
+  }
+
+  async emit () {
+    if (this.data.length === 0) return
+
+    let send_usage_data = await configGet('send_usage_data')
+    if (send_usage_data) {
+      console.log('send usage data...')
+      await GET(`${server_url}/api/tick/`, {
+        sid: global.session_id,
+        t: 1,
+        a: this.data.join(','),
+      })
+    }
+
+    this.data = []
+  }
+}
+
+export default Tracer

+ 4 - 1
src/main/main.ts

@@ -3,11 +3,12 @@ import '@main/core/agent'
 import * as message from '@main/core/message'
 import '@main/core/popupMenu'
 import '@main/data'
+import * as http_api from '@main/http'
 import * as cron from '@main/libs/cron'
 import getIndex from '@main/libs/getIndex'
 import isDev from '@main/libs/isDev'
 import { makeMainMenu } from '@main/libs/menu'
-import * as http_api from '@main/http'
+import Tracer from '@main/libs/tracer'
 import '@main/tray'
 import version from '@root/version.json'
 import { app, BrowserWindow } from 'electron'
@@ -113,6 +114,8 @@ const onActive = async () => {
   win?.show()
 }
 
+global.tracer = new Tracer()
+
 app.on('ready', async () => {
   console.log(`VERSION: ${version.join('.')}`)
   global.session_id = uuid4()

+ 2 - 0
src/main/types.d.ts

@@ -4,6 +4,7 @@
  * @homepage: https://oldj.net
  */
 
+import Tracer from '@main/libs/tracer'
 import SwhDb from 'potdb'
 import { BrowserWindow } from 'electron'
 import * as actions from './actions'
@@ -30,6 +31,7 @@ declare global {
       session_id: string; // A random value, refreshed every time the app starts, used to identify different startup sessions.
       main_win: BrowserWindow;
       last_path?: string; // the last path opened by SwitchHosts
+      tracer: Tracer;
     }
   }
 }