From 7448ef682abb690cad69bb570ff42c662d5f7cbb Mon Sep 17 00:00:00 2001
From: Vishal <64505169+vishalxl@users.noreply.github.com>
Date: Mon, 14 Nov 2022 15:38:43 +0530
Subject: [PATCH] tweaked some constants to get fewer events

and added fn to print info about event types
---
 bin/nostr_console.dart | 16 ++++++++++++---
 lib/console_ui.dart    | 11 +++++++----
 lib/settings.dart      |  6 +++---
 lib/tree_ds.dart       | 44 +++++++++++++++++++++++++++++++++++++-----
 4 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart
index 2ed2cca..a3dbfcc 100644
--- a/bin/nostr_console.dart
+++ b/bin/nostr_console.dart
@@ -37,8 +37,11 @@ void printVersion() {
 
 Future<void> main(List<String> arguments) async {
     Logger.root.level = Level.ALL; // defaults to Level.INFO
+    DateTime appStartTime = DateTime.now();
+    print("app start time: $appStartTime");
     Logger.root.onRecord.listen((record) {
-      print('${record.level.name}: ${record.time}: ${record.message}');
+      //print(record.time.runtimeType);
+      print('${record.level.name}: ${record.time.difference(appStartTime)}: ${record.message}');
     });
       
     final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(pubkeyArg, abbr:"p")..addOption(prikeyArg, abbr:"k")
@@ -274,6 +277,7 @@ Future<void> main(List<String> arguments) async {
       // if more than 1000 posts have already been read from the file, then don't get too many day's events. Only for last 3 days. 
       if(numFilePosts > 1000) {
         daysToGetEventsFor = 3;
+        gDefaultNumWaitSeconds = gDefaultNumWaitSeconds ~/3;
       }
 
       getUserEvents(gListRelayUrls1, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor * 100));
@@ -285,7 +289,7 @@ Future<void> main(List<String> arguments) async {
       // TODO  get all 40 events, and then get all #e for them ( responses to them)
     
       stdout.write('Waiting for user posts to come in.....');
-      Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds), () {
+      Future.delayed( Duration(milliseconds: gDefaultNumWaitSeconds), () {
 
         initialEvents.addAll(getRecievedEvents());
         clearEvents();
@@ -311,11 +315,14 @@ Future<void> main(List<String> arguments) async {
         getContactFeed(gListRelayUrls1, contacts, gLimitPerSubscription, getSecondsDaysAgo(2 * daysToGetEventsFor));
 
         // calculate top mentioned ptags, and then get the events for those users
+        //log.info('calling getpTags');
         List<String> pTags = getpTags(initialEvents, gMaxPtagsToGet);
+        //log.info('after getpTags\n');
         getMultiUserEvents(gListRelayUrls1, pTags, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor));
         
+        
         stdout.write('Waiting for feed to come in..............');
-        Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
+        Future.delayed(Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
 
             initialEvents.addAll(getRecievedEvents());
             clearEvents();
@@ -324,7 +331,10 @@ Future<void> main(List<String> arguments) async {
             if( gDebug > 0) log.info("Received ptag events events.");
 
             // Creat tree from all events read form file
+            //log.info("going to call getTree.");
             Store node = getTree(initialEvents);
+            //node.printEventInfo();
+            //log.info("after getTree returned.");
             gStore = node;
             
             clearEvents();
diff --git a/lib/console_ui.dart b/lib/console_ui.dart
index eb4d722..94e7e55 100644
--- a/lib/console_ui.dart
+++ b/lib/console_ui.dart
@@ -622,9 +622,12 @@ Future<void> otherOptionsMenuUi(Store node) async {
         print("\n");
         relays.printInfo();
         print("\n");
-        printUnderlined("Posts and User");
-        print("Total number of kind-1 posts:  ${node.count()}");
-        print("Total number of all events:    ${node.allChildEventsMap.length}");
+        printUnderlined("Event and User Info");
+        //print("Total number of kind-1 posts:  ${node.count()}");
+
+        print("\nEvent distribution by event kind:\n");
+        node.printEventInfo();
+        print("\nTotal number of all events:    ${node.allChildEventsMap.length}");
         print("Total number of user profiles: ${gKindONames.length}\n");
         printUnderlined("Logged in user Info");
         if( userPrivateKey.length == 64) {
@@ -1183,7 +1186,7 @@ void showInitialNotifications(Store node) {
 Future<void> mainMenuUi(Store node) async {
    
     clearScreen();
-
+    //log.info("in main menu");
     //Show only notifications
     showInitialNotifications(node);
 
diff --git a/lib/settings.dart b/lib/settings.dart
index e9183b1..22689f0 100644
--- a/lib/settings.dart
+++ b/lib/settings.dart
@@ -9,19 +9,19 @@ final log = Logger('ExampleLogger');
 // for debugging
 String gCheckEventId = "fg ee810ea73072af056cceaa6d051b4fcce60739247f7bcc752e72fa5defb64f09"; 
 
-const int gDefaultNumWaitSeconds = 2000; // is used in main()
+int gDefaultNumWaitSeconds = 3000; // is used in main()
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// file related settings 
 const String gDefaultEventsFilename = "all_nostr_events.txt";
 String       gEventsFilename        = ""; // is set in arguments, and if set, then file is read from and written to
 bool         gDontWriteOldEvents    = true;
-const int gDontSaveBeforeDays       = 100; // dont save events older than this many days if gDontWriteOldEvents flag is true
+const int gDontSaveBeforeDays       = 20; // dont save events older than this many days if gDontWriteOldEvents flag is true
 const int gDeletePostsOlderThanDays = 20;
 bool         gOverWriteFile         = false; // overwrite the file, and don't just append. Will write all events in memory. 
 
 const int gDontAddToStoreBeforeDays = 60; // events older than this are not added to the Store of all events
 
-const int gDaysToGetEventsFor       = 20; // when getting events, this is the since field (unless a fully formed request is given in command line)
+const int gDaysToGetEventsFor       = 10; // when getting events, this is the since field (unless a fully formed request is given in command line)
 const int gLimitPerSubscription     = 6000;
 
  // don't show notifications for events that are older than 5 days and come when program is running
diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart
index 49917ba..ef325ad 100644
--- a/lib/tree_ds.dart
+++ b/lib/tree_ds.dart
@@ -905,6 +905,8 @@ class Store {
       }
     }); // going over tempChildEventsMap and adding children to their parent's .children list
 
+    //log.info("in middle of fromJson");
+
     tempChildEventsMap.forEach((newEventId, tree) {
       int eKind = tree.event.eventData.kind;
      if( eKind == 142 || eKind == 140 || eKind == 141) {
@@ -988,7 +990,7 @@ class Store {
         return;
       }
 
-      // expand mentions ( and translate if flag is set) and then add event to main event map
+      // expand mentions ( and translate if flag is set) and then add event to main event map; 142 events are expanded later
       if( newEvent.eventData.kind != 142) 
         newEvent.eventData.translateAndExpandMentions(directRooms, allChildEventsMap); // this also handles dm decryption for kind 4 messages, for kind 1 will do translation/expansion; 
 
@@ -1623,10 +1625,14 @@ class Store {
           }
         }
 
-        // only write if its not too old
+        // only write if its not too old ( except in case of user logged in)
         if( gDontWriteOldEvents) {
-          if( tree.event.eventData.createdAt <  getSecondsDaysAgo(gDontSaveBeforeDays)) {
-            continue;
+          if(  tree.event.eventData.createdAt <  getSecondsDaysAgo(gDontSaveBeforeDays) ) {
+            if( tree.event.eventData.pubkey != userPublicKey ) {
+              if( !(tree.event.eventData.kind == 4 && isValidDirectMessage(tree.event.eventData)))
+                continue;
+            
+            }
           }
         }
 
@@ -2030,6 +2036,34 @@ class Store {
     return;
   }
 
+  void printEventInfo() {
+    Map<int, int> eventCounterMap = {} ;
+
+    List<int> kindCounted = [0, 1, 3, 4, 5, 6, 7, 40, 41, 42, 140, 141, 142];
+    for( var k in kindCounted ) {
+      eventCounterMap[k] = 0;
+    }
+
+    for(var t in allChildEventsMap.values) {
+      EventData e = t.event.eventData;
+      eventCounterMap[e.kind] = eventCounterMap[e.kind]??0 + 1;
+      if( eventCounterMap.containsKey(e.kind)) {
+        //print("added one more for ${e.kind}");
+        eventCounterMap[e.kind] = eventCounterMap[e.kind]! + 1;
+        //print("eventCounterMap[e.kind] = ${eventCounterMap[e.kind]}");
+      } else {
+        //print("added first for ${e.kind}");
+        eventCounterMap[e.kind] = 0;
+      }
+
+    }
+
+    printUnderlined("kind       count");
+    for( var k in kindCounted) {
+      print("${k.toString().padRight(5)}      ${eventCounterMap[k]}");
+    }
+  }
+
 } //================================================================================================================================ end Store
 
 int ascendingTimeTree(Tree a, Tree b) {
@@ -2105,7 +2139,7 @@ Store getTree(Set<Event> events) {
     Store node = Store.fromEvents(events);
     //log.info("After calling fromEvents with ${node.allChildEventsMap.length} events in its internal store");
 
-    // translate and expand mentions for all
+    // translate and expand mentions for all ( both take 0.5 sec for 20k events)
     events.where((element) => element.eventData.kind != 142).forEach( (event) =>   event.eventData.translateAndExpandMentions(node.directRooms, node.allChildEventsMap));;
     events.where((element) => element.eventData.kind == 142).forEach( (event) =>   event.eventData.translateAndExpand14x(node.directRooms, node.encryptedChannels, node.allChildEventsMap));;
     if( gDebug > 0) log.info("expand mentions finished.");