synapse/cmd.sh
author Peter Gervai <grin@grin.hu>
Fri, 21 Jan 2022 10:10:19 +0100
changeset 2 c3f49b58aff6
child 3 99e6c1cdae3b
permissions -rwxr-xr-x
Add synapse/cmd.sh: query db for various user statistics - last registrations - user events - user event contents - active daily users
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/bin/bash
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
#$Id: cmd.sh,v 7c8632f2b6be 2022/01/21 09:05:10 grin $
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
#
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
# (c)Peter Gervai, 2020-21
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
# Licensed under GPLv3+ and CC_By-Sa-4.0-int
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
#
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
#CMD='sudo -u postgres psql synapse -tAc'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
CMD='psql -h 10.1.1.257 -U synapse synapse -c'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
MODE="$1"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
ARG="$2"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
if [ "x$MODE" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
	echo "Usage: $0 <mode> <args> ...."
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
	echo " modes: user @someuser:grin.hu       - user stats"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
	echo "        userevents @someuser:grin.hu - user events"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
	echo "        active1                      - last 1 day active users"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
	echo "        lastreg [<n>]	           - last <n> (25) registrations"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
egy_arg() {
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
	local s=$1
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
	if [ "x$ARG" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
		echo "Missing arg ${s}!"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
		echo "$0 $MODE $s"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
		exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
	fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
}
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
if [ "$MODE" = "user" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
	egy_arg 'user'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
	echo "=== User data ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
	$CMD "SELECT display_name FROM user_directory WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
	$CMD "SELECT medium, address, unts(validated_at) AS validated_at, unts(added_at) AS added_at FROM user_threepids WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
	$CMD "SELECT joined_rooms FROM user_stats_current WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
 	echo "=== Logins ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
 	$CMD "SELECT user_id,device_id,ip,unts(last_seen) AS last_seen,user_agent FROM user_ips WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
 	$CMD "SELECT device_id, user_agent, unts(timestamp) FROM user_daily_visits WHERE user_id='$ARG' ORDER BY 1 ASC, 3 DESC LIMIT 10"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
 	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
 	echo "=== Shared private rooms ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
 	$CMD "select * from users_who_share_private_rooms where user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
 	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
 	echo "=== Rooms the user has been ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
 	$CMD "SELECT rm.room_id, rm.event_id, rm.sender, rm.membership, 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
       rss.canonical_alias, rss.join_rules, rss.encryption, 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
       unts(e.origin_server_ts), rss.name
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
 FROM room_memberships  rm
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
 JOIN room_stats_state rss USING(room_id) 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
 JOIN events e USING(event_id)
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
 WHERE user_id='$ARG'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
 ORDER BY e.origin_server_ts" | more
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
if [ "$MODE" = "userevents" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
        egy_arg 'user'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
        $CMD "SELECT * FROM events WHERE sender='@sztg:grin.hu';
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
SELECT stream_ordering,type,e.room_id,content,sender,unts(origin_server_ts) AS orig_s,json 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
 FROM events e LEFT JOIN event_json j USING(event_id) 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
 WHERE sender='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
if [ "$MODE" = "active1" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
	$CMD "SELECT user_id,device_id,ip,unts(MAX(last_seen)) AS last_seen,user_agent FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
		 GROUP BY 1,2,3,5
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
		 ORDER BY last_seen DESC"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
if [ "$MODE" = "lastreg" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
	if [ "x$ARG" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
		ARG=25
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
	fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
	$CMD "SELECT name,unts(creation_ts*1000) AS created,is_guest,deactivated,shadow_banned,
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
		device_id,ip,unts(last_seen) AS last_seen, user_agent
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
		FROM users LEFT JOIN user_ips ON(name=user_id)
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
		ORDER BY 2 DESC 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
		LIMIT $ARG"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
if [ "$MODE" = "active1u" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
	$CMD "SELECT DISTINCT user_id FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
		 ORDER BY 1"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
	$CMD "SELECT DISTINCT user_id,device_id,ip FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
		 ORDER BY 1"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106