Skip to content

Commit

Permalink
reduced events fetched
Browse files Browse the repository at this point in the history
.. to about 30k for my own username.

This reduces connection which seems to improve relay response.

added file user.dart with mostly old code.
  • Loading branch information
vishalxl committed Dec 23, 2022
1 parent f8070e4 commit 91fa45a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 62 deletions.
25 changes: 16 additions & 9 deletions bin/nostr_console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:nostr_console/relays.dart';
import 'package:nostr_console/console_ui.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:nostr_console/user.dart';

import 'package:args/args.dart';
import 'package:logging/logging.dart';

Expand Down Expand Up @@ -322,8 +324,8 @@ Future<void> main(List<String> arguments) async {
usersFetched = usersFetched.union(gDefaultFollows);

// get group and meta info events
getKindEvents([40, 41], gListRelayUrls1, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents));
getKindEvents([42], gListRelayUrls1, 3 * limitPerSubscription, getSecondsDaysAgo(2));
getKindEvents([40, 41], gListRelayUrls1, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents));
getKindEvents([42], gListRelayUrls1, 3 * limitPerSubscription, getSecondsDaysAgo(4));

getMultiUserEvents(gListRelayUrls1, usersFetched, 4 * limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), {0,3});

Expand Down Expand Up @@ -352,21 +354,27 @@ Future<void> main(List<String> arguments) async {
contacts.add(contact.id);
});
contacts = contacts.difference(usersFetched); // remove already fetched users from this list

getContactFeed(gListRelayUrls1, contacts, 3 * gLimitPerSubscription, getSecondsDaysAgo( limitOthersEvents));
usersFetched = usersFetched.union(contacts);
} else {
print("Could not find your contact list.");
}
}

// calculate top mentioned ptags, and then get the events for those users
Set<String> pTags = getpTags(initialEvents, gMaxPtagsToGet);
// fetch extra events for people who don't have too large a follow list
if( usersFetched.length < gMaxPtagsToGet * 2 ) {
// calculate top mentioned ptags, and then get the events for those users
Set<String> pTags = getpTags(initialEvents, gMaxPtagsToGet);
pTags = pTags.difference(usersFetched);

pTags = pTags.difference(usersFetched);
getMultiUserEvents(gListRelayUrls1, pTags, 4 * gLimitPerSubscription, getSecondsDaysAgo(limitOthersEvents));
usersFetched = usersFetched.union(pTags);
getMultiUserEvents(gListRelayUrls1, pTags, 4 * gLimitPerSubscription, getSecondsDaysAgo(limitOthersEvents));
usersFetched = usersFetched.union(pTags);
}

//print("total users fetched: ${usersFetched.length}");
// get events from channels of user
Set<String> userChannels = getUserChannels(initialEvents, userPublicKey);
//getMentionEvents(gListRelayUrls1, userChannels, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), "#e");

stdout.write('Waiting for feed to come in..............');
Future.delayed(Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
Expand All @@ -381,7 +389,6 @@ Future<void> main(List<String> arguments) async {
Store node = getTree(initialEvents);
gStore = node;


clearEvents();
mainMenuUi(node);
});
Expand Down
1 change: 1 addition & 0 deletions lib/console_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:nostr_console/tree_ds.dart';
import 'package:nostr_console/relays.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:nostr_console/user.dart';
import 'package:bip340/bip340.dart';

Future<void> processAnyIncomingEvents(Store node, [bool printNotifications = true]) async {
Expand Down
48 changes: 0 additions & 48 deletions lib/event_ds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -998,54 +998,6 @@ class Event {
}
}

void addToHistogram(Map<String, int> histogram, List<String> pTags) {
Set tempPtags = {};
pTags.retainWhere((x) => tempPtags.add(x));

for(int i = 0; i < pTags.length; i++ ) {
String pTag = pTags[i];
if( histogram.containsKey(pTag)) {
int? val = histogram[pTag];
if( val != null) {
histogram[pTag] = ++val;
} else {
}
} else {
histogram[pTag] = 1;
}
}
//return histogram;
}

// return the numMostFrequent number of most frequent p tags ( user pubkeys) in the given events
Set<String> getpTags(Set<Event> events, int numMostFrequent) {
List<HistogramEntry> listHistogram = [];
Map<String, int> histogramMap = {};
for(var event in events) {
addToHistogram(histogramMap, event.eventData.pTags);
}

histogramMap.forEach((key, value) {listHistogram.add(HistogramEntry(key, value));/* print("added to list of histogramEntry $key $value"); */});
listHistogram.sort(HistogramEntry.histogramSorter);
List<String> ptags = [];
for( int i = 0; i < listHistogram.length && i < numMostFrequent; i++ ) {
ptags.add(listHistogram[i].str);
}

return ptags.toSet();
}

// From the list of events provided, lookup the lastst contact information for the given user/pubkey
Event? getContactEvent(String pubkey) {

// get the latest kind 3 event for the user, which lists his 'follows' list
if( gKindONames.containsKey(pubkey)) {
Event? e = (gKindONames[pubkey]?.latestContactEvent)??null;
return e;
}

return null;
}

// for the user userPubkey, returns the relay of its contact contactPubkey
String getRelayOfUser(String userPubkey, String contactPubkey) {
Expand Down
6 changes: 2 additions & 4 deletions lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ const int gLimitPerSubscription = 10000;
const int gDontHighlightEventsOlderThan = 4;

int gDefaultNumWaitSeconds = 12000; // is used in main()
const int gMaxAuthorsInOneRequest = 200; // number of author requests to send in one request
const int gMaxPtagsToGet = 200; // maximum number of p tags that are taken from the comments of feed ( the top most, most frequent)
const int gMaxAuthorsInOneRequest = 300; // number of author requests to send in one request
const int gMaxPtagsToGet = 100; // maximum number of p tags that are taken from the comments of feed ( the top most, most frequent)

// global counters of total events read or processed
int numFileEvents = 0, numFilePosts = 0, numUserPosts = 0, numFeedPosts = 0, numOtherPosts = 0;

//String defaultServerUrl = 'wss://relay.damus.io';
//const String nostrRelayUnther = 'wss://nostr-relay.untethr.me'; not working
String defaultServerUrl = "wss://relay.damus.io";
const String relayNostrInfo = 'wss://relay.nostr.info';

Expand Down
3 changes: 2 additions & 1 deletion lib/tree_ds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'dart:io';
import 'dart:convert';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/relays.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/user.dart';
import 'dart:math'; // for Point


Expand Down
59 changes: 59 additions & 0 deletions lib/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

import 'dart:io';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/utils.dart';

// From the list of events provided, lookup the lastst contact information for the given user/pubkey
Event? getContactEvent(String pubkey) {

// get the latest kind 3 event for the user, which lists his 'follows' list
if( gKindONames.containsKey(pubkey)) {
Event? e = (gKindONames[pubkey]?.latestContactEvent)??null;
return e;
}

return null;
}

Set<String> getUserChannels(Set<Event> userEvents, String userPublicKey) {
Set<String> userChannels = {};

return userChannels;
}

void addToHistogram(Map<String, int> histogram, List<String> pTags) {
Set tempPtags = {};
pTags.retainWhere((x) => tempPtags.add(x));

for(int i = 0; i < pTags.length; i++ ) {
String pTag = pTags[i];
if( histogram.containsKey(pTag)) {
int? val = histogram[pTag];
if( val != null) {
histogram[pTag] = ++val;
} else {
}
} else {
histogram[pTag] = 1;
}
}
//return histogram;
}

// return the numMostFrequent number of most frequent p tags ( user pubkeys) in the given events
Set<String> getpTags(Set<Event> events, int numMostFrequent) {
List<HistogramEntry> listHistogram = [];
Map<String, int> histogramMap = {};
for(var event in events) {
addToHistogram(histogramMap, event.eventData.pTags);
}

histogramMap.forEach((key, value) {listHistogram.add(HistogramEntry(key, value));/* print("added to list of histogramEntry $key $value"); */});
listHistogram.sort(HistogramEntry.histogramSorter);
List<String> ptags = [];
for( int i = 0; i < listHistogram.length && i < numMostFrequent; i++ ) {
ptags.add(listHistogram[i].str);
}

return ptags.toSet();
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ homepage: https://github.com/vishalxl/nostr_console
# increased channel fetches from 2 days from half a day

# reduced items fetched. 23/12
# reduced items more evening 23/12

environment:
sdk: '>=2.17.3 <3.0.0'
Expand Down

0 comments on commit 91fa45a

Please sign in to comment.