From 419cb6a572d31534cf767fefda9d5c7f544411d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Moreau?= <cem.moreau@gmail.com>
Date: Sun, 10 Jun 2018 11:43:26 +0200
Subject: [PATCH] [enh] pilot Monitoring annotations using constants

---
 app/lib/debug/MonitorLokiExecutionTime.ts | 29 +++++++++++++----------
 app/lib/debug/MonitorSQLExecutionTime.ts  | 27 +++++++++++----------
 app/lib/other_constants.ts                |  5 +++-
 3 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/app/lib/debug/MonitorLokiExecutionTime.ts b/app/lib/debug/MonitorLokiExecutionTime.ts
index ca4f15412..0491951af 100644
--- a/app/lib/debug/MonitorLokiExecutionTime.ts
+++ b/app/lib/debug/MonitorLokiExecutionTime.ts
@@ -13,26 +13,29 @@
 
 import {NewLogger} from "../logger"
 import {getMicrosecondsTime} from "../../ProcessCpuProfiler"
+import {OtherConstants} from "../other_constants"
 
 const theLogger = NewLogger()
 
 export const MonitorLokiExecutionTime = function (dumpFirstParam = false) {
   return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
-    const original = descriptor.value
-    if (original.__proto__.constructor.name === "AsyncFunction") {
-      descriptor.value = async function (...args:any[]) {
-        const that :any = this
-        const now = getMicrosecondsTime()
-        const result = await original.apply(this, args)
-        if (dumpFirstParam) {
-          theLogger.trace('[loki][%s][%s] => %sµs', that.collectionName, propertyKey, (getMicrosecondsTime() - now), args && args[0])
-        } else {
-          theLogger.trace('[loki][%s][%s] => %sµs', that.collectionName, propertyKey, (getMicrosecondsTime() - now))
+    if (OtherConstants.ENABLE_LOKI_MONITORING) {
+      const original = descriptor.value
+      if (original.__proto__.constructor.name === "AsyncFunction") {
+        descriptor.value = async function (...args:any[]) {
+          const that :any = this
+          const now = getMicrosecondsTime()
+          const result = await original.apply(this, args)
+          if (dumpFirstParam) {
+            theLogger.trace('[loki][%s][%s] => %sµs', that.collectionName, propertyKey, (getMicrosecondsTime() - now), args && args[0])
+          } else {
+            theLogger.trace('[loki][%s][%s] => %sµs', that.collectionName, propertyKey, (getMicrosecondsTime() - now))
+          }
+          return result
         }
-        return result
+      } else {
+        throw Error("Monitoring a Loki synchronous function is not allowed.")
       }
-    } else {
-      throw Error("Monitoring a Loki synchronous function is not allowed.")
     }
   }
 }
\ No newline at end of file
diff --git a/app/lib/debug/MonitorSQLExecutionTime.ts b/app/lib/debug/MonitorSQLExecutionTime.ts
index 3f2dff366..65724c527 100644
--- a/app/lib/debug/MonitorSQLExecutionTime.ts
+++ b/app/lib/debug/MonitorSQLExecutionTime.ts
@@ -13,24 +13,27 @@
 
 import {getDurationInMicroSeconds, getMicrosecondsTime} from "../../ProcessCpuProfiler"
 import {NewLogger} from "../logger"
+import {OtherConstants} from "../other_constants"
 
 const theLogger = NewLogger()
 
 export const MonitorSQLExecutionTime = function () {
   return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
-    const original = descriptor.value
-    if (original.__proto__.constructor.name === "AsyncFunction") {
-      descriptor.value = async function (...args:any[]) {
-        const start = getMicrosecondsTime()
-        const sql: string = args[0]
-        const params: any[] = args[1]
-        const entities: any[] = await original.apply(this, args)
-        const duration = getDurationInMicroSeconds(start)
-        theLogger.trace('[sqlite][query] %s %s %sµs', sql, JSON.stringify(params || []), duration)
-        return entities
+    if (OtherConstants.ENABLE_SQL_MONITORING) {
+      const original = descriptor.value
+      if (original.__proto__.constructor.name === "AsyncFunction") {
+        descriptor.value = async function (...args: any[]) {
+          const start = getMicrosecondsTime()
+          const sql: string = args[0]
+          const params: any[] = args[1]
+          const entities: any[] = await original.apply(this, args)
+          const duration = getDurationInMicroSeconds(start)
+          theLogger.trace('[sqlite][query] %s %s %sµs', sql, JSON.stringify(params || []), duration)
+          return entities
+        }
+      } else {
+        throw Error("Monitoring an SQL synchronous function is not allowed.")
       }
-    } else {
-      throw Error("Monitoring an SQL synchronous function is not allowed.")
     }
   }
 }
\ No newline at end of file
diff --git a/app/lib/other_constants.ts b/app/lib/other_constants.ts
index d10528e99..e7b7b364b 100644
--- a/app/lib/other_constants.ts
+++ b/app/lib/other_constants.ts
@@ -20,5 +20,8 @@ export const OtherConstants = {
     SWITCHED: 'switched',
     HEAD_CHANGED: 'newHEAD',
     RESOLUTION_DONE: 'resolution_done'
-  }
+  },
+
+  ENABLE_LOKI_MONITORING: false,
+  ENABLE_SQL_MONITORING: false,
 }
\ No newline at end of file
-- 
GitLab