Providing context for T419002¶
This notebook exists to provide extra context to an external researcher, answering questions asked in the above-linked task.
License statement¶
Copyright 2026 Martin Urbanec, Wikimedia Foundation (murbanec@wikimedia.org)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
In [1]:
from wmfdata import spark, mariadb
import pandas as pd
You are using Wmfdata v2.3.0, but v2.4.0 is available. To update, run `pip install --upgrade git+https://github.com/wikimedia/wmfdata-python.git@release`. To see the changes, refer to https://github.com/wikimedia/wmfdata-python/blob/release/CHANGELOG.md.
In [2]:
def convert_up_property(val):
if val == '0':
return 'disabled'
elif pd.isna(val):
return 'enabled'
else:
return 'unknown'
In [3]:
dfMentees = mariadb.run('''
SELECT *
FROM growthexperiments_mentor_mentee
WHERE gemm_mentor_role = 'primary'
''', 'enwiki', use_x1=True)
dfMentees.set_index('gemm_mentee_id', inplace=True)
dfMentees.head()
/home/urbanecm/.conda/envs/2024-06-16T13.49.54_urbanecm/lib/python3.10/site-packages/pandas/io/sql.py:761: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy warnings.warn(
Out[3]:
| gemm_mentor_role | gemm_mentor_id | gemm_mentee_is_active | |
|---|---|---|---|
| gemm_mentee_id | |||
| 14588380 | primary | 32 | 0 |
| 24437066 | primary | 32 | 0 |
| 41821693 | primary | 32 | 0 |
| 41866921 | primary | 32 | 0 |
| 42206763 | primary | 32 | 0 |
In [4]:
dfAccs = spark.run('''
SELECT
dt,
event.userId,
event.userName,
event.isSelfMade
FROM event_sanitized.serversideaccountcreation
WHERE
year = 2021
AND month = 12
AND wiki = 'enwiki'
LIMIT 500
''')
dfAccs.head(5)
SPARK_HOME: /usr/lib/spark3 Using Hadoop client lib jars at 3.2.0, provided by Spark. PYSPARK_PYTHON=/opt/conda-analytics/bin/python3
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
26/03/09 13:36:24 WARN SparkConf: Note that spark.local.dir will be overridden by the value set by the cluster manager (via SPARK_LOCAL_DIRS in mesos/standalone/kubernetes and LOCAL_DIRS in YARN).
26/03/09 13:36:25 WARN Utils: Service 'sparkDriver' could not bind on port 12000. Attempting port 12001.
26/03/09 13:36:25 WARN Utils: Service 'sparkDriver' could not bind on port 12001. Attempting port 12002.
26/03/09 13:36:25 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attempting port 4041.
26/03/09 13:36:25 WARN Utils: Service 'SparkUI' could not bind on port 4041. Attempting port 4042.
26/03/09 13:36:32 WARN Utils: Service 'org.apache.spark.network.netty.NettyBlockTransferService' could not bind on port 13000. Attempting port 13001.
26/03/09 13:36:32 WARN Utils: Service 'org.apache.spark.network.netty.NettyBlockTransferService' could not bind on port 13001. Attempting port 13002.
26/03/09 13:36:35 WARN HiveExternalCatalog: The table schema given by Hive metastore(struct<dt:string,event:struct<campaign:string,displayMobile:boolean,isSelfMade:boolean,returnTo:string,returnToQuery:string,token:string,userBuckets:string,userId:bigint,userName:string,isApi:boolean,isStable:string,sul3Enabled:boolean,hCaptchaScore:double>,recvfrom:string,revision:bigint,schema:string,seqid:bigint,useragent:struct<browser_family:string,browser_major:string,browser_minor:string,device_family:string,is_bot:boolean,is_mediawiki:boolean,os_family:string,os_major:string,os_minor:string,wmf_app_version:string>,uuid:string,webhost:string,wiki:string,ip:string,geocoded_data:map<string,string>,topic:string,is_wmf_domain:boolean,_schema:string,client_dt:string,http:struct<protocol:string,method:string,status_code:bigint,has_cookies:boolean,request_headers:map<string,string>,response_headers:map<string,string>,client_ip:string>,meta:struct<uri:string,request_id:string,id:string,dt:string,domain:string,stream:string>,user_agent_map:map<string,string>,normalized_host:struct<project_class:string,project:string,qualifiers:array<string>,tld:string,project_family:string>,year:bigint,month:bigint,day:bigint,hour:bigint>) is different from the schema when this table was created by Spark SQL(struct<dt:string,event:struct<campaign:string,displayMobile:boolean,isSelfMade:boolean,returnTo:string,returnToQuery:string,token:string,userBuckets:string,userId:bigint,userName:string,isApi:boolean,isStable:string>,recvfrom:string,revision:bigint,schema:string,seqid:bigint,useragent:struct<browser_family:string,browser_major:string,browser_minor:string,device_family:string,is_bot:boolean,is_mediawiki:boolean,os_family:string,os_major:string,os_minor:string,wmf_app_version:string>,uuid:string,webhost:string,wiki:string,ip:string,geocoded_data:map<string,string>,topic:string,is_wmf_domain:boolean,_schema:string,client_dt:string,http:struct<protocol:string,method:string,status_code:bigint,has_cookies:boolean,request_headers:map<string,string>,response_headers:map<string,string>,client_ip:string>,meta:struct<uri:string,request_id:string,id:string,dt:string,domain:string,stream:string>,user_agent_map:map<string,string>,normalized_host:struct<project_class:string,project:string,qualifiers:array<string>,tld:string,project_family:string>,year:bigint,month:bigint,day:bigint,hour:bigint>). We have to fall back to the table schema from Hive metastore which is not case preserving.
26/03/09 13:36:36 WARN HiveExternalCatalog: The table schema given by Hive metastore(struct<dt:string,event:struct<campaign:string,displayMobile:boolean,isSelfMade:boolean,returnTo:string,returnToQuery:string,token:string,userBuckets:string,userId:bigint,userName:string,isApi:boolean,isStable:string,sul3Enabled:boolean,hCaptchaScore:double>,recvfrom:string,revision:bigint,schema:string,seqid:bigint,useragent:struct<browser_family:string,browser_major:string,browser_minor:string,device_family:string,is_bot:boolean,is_mediawiki:boolean,os_family:string,os_major:string,os_minor:string,wmf_app_version:string>,uuid:string,webhost:string,wiki:string,ip:string,geocoded_data:map<string,string>,topic:string,is_wmf_domain:boolean,_schema:string,client_dt:string,http:struct<protocol:string,method:string,status_code:bigint,has_cookies:boolean,request_headers:map<string,string>,response_headers:map<string,string>,client_ip:string>,meta:struct<uri:string,request_id:string,id:string,dt:string,domain:string,stream:string>,user_agent_map:map<string,string>,normalized_host:struct<project_class:string,project:string,qualifiers:array<string>,tld:string,project_family:string>,year:bigint,month:bigint,day:bigint,hour:bigint>) is different from the schema when this table was created by Spark SQL(struct<dt:string,event:struct<campaign:string,displayMobile:boolean,isSelfMade:boolean,returnTo:string,returnToQuery:string,token:string,userBuckets:string,userId:bigint,userName:string,isApi:boolean,isStable:string>,recvfrom:string,revision:bigint,schema:string,seqid:bigint,useragent:struct<browser_family:string,browser_major:string,browser_minor:string,device_family:string,is_bot:boolean,is_mediawiki:boolean,os_family:string,os_major:string,os_minor:string,wmf_app_version:string>,uuid:string,webhost:string,wiki:string,ip:string,geocoded_data:map<string,string>,topic:string,is_wmf_domain:boolean,_schema:string,client_dt:string,http:struct<protocol:string,method:string,status_code:bigint,has_cookies:boolean,request_headers:map<string,string>,response_headers:map<string,string>,client_ip:string>,meta:struct<uri:string,request_id:string,id:string,dt:string,domain:string,stream:string>,user_agent_map:map<string,string>,normalized_host:struct<project_class:string,project:string,qualifiers:array<string>,tld:string,project_family:string>,year:bigint,month:bigint,day:bigint,hour:bigint>). We have to fall back to the table schema from Hive metastore which is not case preserving.
26/03/09 13:36:36 WARN SessionState: METASTORE_FILTER_HOOK will be ignored, since hive.security.authorization.manager is set to instance of HiveAuthorizerFactory.
Out[4]:
| dt | userId | userName | isSelfMade | |
|---|---|---|---|---|
| 0 | 2021-12-22T19:06:35.681Z | 43069486 | Riggedy | True |
| 1 | 2021-12-22T19:02:42.937Z | 43069478 | Sami9600 | True |
| 2 | 2021-12-22T19:55:19.012Z | 43069663 | MrMatchkick | True |
| 3 | 2021-12-22T19:23:20.041Z | 43069546 | Kototramwaj | True |
| 4 | 2021-12-22T19:25:21.844Z | 43069554 | Xugabear⁷ | True |
In [5]:
dfPrefLimited = mariadb.run('''
SELECT *
FROM user_properties
WHERE
up_property='growthexperiments-homepage-mentorship-enabled'
AND up_user IN ({users})
'''.format(users=', '.join(list(dfAccs.userId.apply(str)))), 'enwiki')
dfPrefLimited.set_index('up_user', inplace=True)
/home/urbanecm/.conda/envs/2024-06-16T13.49.54_urbanecm/lib/python3.10/site-packages/pandas/io/sql.py:761: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy warnings.warn(
In [6]:
dfTmp = dfAccs.join(dfMentees, 'userId').join(dfPrefLimited, 'userId')
dfTmp['mentorship_property'] = dfTmp.up_value.apply(convert_up_property)
dfTmp['has_mentee'] = dfTmp.gemm_mentor_id.notna()
In [7]:
dfTmp[['has_mentee', 'userId']].groupby('has_mentee').count()
Out[7]:
| userId | |
|---|---|
| has_mentee | |
| True | 500 |
In [ ]: