<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Query for Users that have not logged in for 40 days in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232408#M8015</link>
    <description>&lt;P&gt;I've seen the ESRI staff present just this use case several times when demoing the API.&lt;BR /&gt;&lt;A href="https://uc2022.esri.com/flow/esri/22uc/uc-2022-ps/page/proceedings" target="_blank"&gt;2022 User Conference - Proceedings (esri.com)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This notebook looks like it has some guidance:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Esri/arcgis-python-api/tree/conference_talks/talks/uc2021/Administering%20Your%20GIS%20Organizations%20Using%20ArcGIS%20API%20for%20Python" target="_blank"&gt;https://github.com/Esri/arcgis-python-api/tree/conference_talks/talks/uc2021/Administering%20Your%20GIS%20Organizations%20Using%20ArcGIS%20API%20for%20Python&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2022 20:00:36 GMT</pubDate>
    <dc:creator>DavidAnderson_1701</dc:creator>
    <dc:date>2022-11-16T20:00:36Z</dc:date>
    <item>
      <title>Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232362#M8014</link>
      <description>&lt;P&gt;I am trying to see how to query our ARCGIS Online account to get a list of users and their Last Login Date.&amp;nbsp; From there I want to grab the users that have not logged in say for 40 days.&amp;nbsp; And then create a new list and do something with it like email them etc.&lt;/P&gt;&lt;P&gt;I can do something like this to get a full count of the users but don't know how to query for the Users and their Login Date....&lt;/P&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
import arcgis 

gis = GIS("https://xxx.maps.arcgis.com", "usernamne", "passaword") # set first arguement to your Portal or AGOL
users = arcgis.gis.UserManager(gis)

# get the total number of users in your AGOL account provided you have administrative priveledges
totalUsers = users.counts('user_type', as_df=False)[0]['count']
print(totalUsers)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 18:54:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232362#M8014</guid>
      <dc:creator>kapalczynski</dc:creator>
      <dc:date>2022-11-16T18:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232408#M8015</link>
      <description>&lt;P&gt;I've seen the ESRI staff present just this use case several times when demoing the API.&lt;BR /&gt;&lt;A href="https://uc2022.esri.com/flow/esri/22uc/uc-2022-ps/page/proceedings" target="_blank"&gt;2022 User Conference - Proceedings (esri.com)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This notebook looks like it has some guidance:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Esri/arcgis-python-api/tree/conference_talks/talks/uc2021/Administering%20Your%20GIS%20Organizations%20Using%20ArcGIS%20API%20for%20Python" target="_blank"&gt;https://github.com/Esri/arcgis-python-api/tree/conference_talks/talks/uc2021/Administering%20Your%20GIS%20Organizations%20Using%20ArcGIS%20API%20for%20Python&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 20:00:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232408#M8015</guid>
      <dc:creator>DavidAnderson_1701</dc:creator>
      <dc:date>2022-11-16T20:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232411#M8016</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;time.localtime(user.lastLogin/1000)

#though, I like this better as it looks like a "normal" date format

time.strftime('%m/%d/%Y', time.localtime(user.lastLogin/1000))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Found that &lt;A href="https://support.esri.com/en/technical-article/000022604" target="_self"&gt;here&lt;/A&gt;.&amp;nbsp; Might help.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 20:14:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232411#M8016</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-11-16T20:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232432#M8018</link>
      <description>&lt;P&gt;Since this reminded me that I was going to do a logon inventory myself, I modified the snippet from&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/97885"&gt;@JRhodes&lt;/a&gt;&amp;nbsp;in &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/what-is-the-best-way-to-get-wildcard-title/td-p/1231799" target="_self"&gt;this post&lt;/A&gt; to just write the username and last login date to a csv file.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
import csv
import time

agol_username = 'AdminUser'                       # change
agol_password = 'userPassword'                    # change
output_csv = r'C:\path\to\out\file\users.csv'     # change
search_user = '*'                                 # change to query individual user

gis = GIS('https://arcgis.com', agol_username, agol_password)

user_list = gis.users.search(query=search_user, max_users=10000)

with open(output_csv, 'w', encoding='utf-8') as file:
    csvfile = csv.writer(file, delimiter=',', lineterminator='\n')
    csvfile.writerow(["Name",                         # these are the headers; modify according to whatever properties you want in your report
					"Title",
                      "LastLogOn"
                    ])  
    
    for item in user_list:
        if item.lastLogin != -1:
           csvfile.writerow([item.username,          # modify according to whatever properties you want in your report
                            time.strftime('%m/%d/%Y', time.localtime(item.lastLogin/1000))
                            ])  &lt;/LI-CODE&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 20:40:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232432#M8018</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-11-16T20:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232529#M8021</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/15530"&gt;@RhettZufelt&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;time.localtime(item.lastLogin/1000))&lt;/PRE&gt;&lt;P&gt;What is the /1000 how do I equate that to days or hours&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where can I get a list of properties that I can use to return values?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or does this look back to who logged in within the last Day?&lt;/P&gt;&lt;P&gt;if item.lastLogin != -1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just looking how to modify the code to look for users that have not logged in in the last 20 days, 40 days etc...&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 23:51:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232529#M8021</guid>
      <dc:creator>kapalczynski</dc:creator>
      <dc:date>2022-11-16T23:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232552#M8022</link>
      <description>&lt;P&gt;Trying this but getting str vs Date error&lt;/P&gt;&lt;P&gt;ERROR:&lt;/P&gt;&lt;P&gt;if actualDate &amp;lt; lastFewDays:&lt;BR /&gt;TypeError: '&amp;lt;' not supported between instances of 'str' and 'datetime.datetime'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;gis = GIS('https://vdot.maps.arcgis.com', agol_username, agol_password)
user_list = gis.users.search(query=search_user, max_users=5)

for item in user_list:
    lastFewDays = datetime.now() + timedelta(days=-30)
    print(lastFewDays)
    actualDate = time.strftime('%m/%d/%Y', time.localtime(item.lastLogin/1000))
    print(actualDate)
    
    if actualDate &amp;gt; lastFewDays:&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 01:10:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232552#M8022</guid>
      <dc:creator>kapalczynski</dc:creator>
      <dc:date>2022-11-17T01:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232553#M8023</link>
      <description>&lt;P&gt;Apperantly lastLogin returns UNIX style date format in miliseconds.&amp;nbsp; the val/1000 converts it to seconds so the time functions work properly.&lt;/P&gt;&lt;P&gt;That is why I used this one in the updated script that I posted, as it give MM/DD/YY.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;time.strftime('%m/%d/%Y', time.localtime(user.lastLogin/1000)&lt;/LI-CODE&gt;&lt;P&gt;If a user has never logged on, it will return -1 and will throw an error.&amp;nbsp; so, that line just tells it to skip that user if they have never logged on and continue running the script rather than error out.&lt;/P&gt;&lt;P&gt;I am not good at finding documentation for these types of things (or, the docs barely exist) so I just tested it to see what properties were availalbe for users.&lt;/P&gt;&lt;P&gt;Basically, I used the piece of code you have above (in IDLE) to get a list of the users:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;users = arcgis.gis.UserManager(gis)&lt;/LI-CODE&gt;&lt;P&gt;then I just grabbed a single user from the list (this grabs the first user):&lt;/P&gt;&lt;LI-CODE lang="c"&gt;user = users[0]&lt;/LI-CODE&gt;&lt;P&gt;Then, I use the IDLE code suggestions (hold windows key and press spacebar after the . ) to find the available properties, and scrolled down until I saw lastLogin.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RhettZufelt_0-1668645893260.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/56171i57FD4DF746A90B37/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RhettZufelt_0-1668645893260.png" alt="RhettZufelt_0-1668645893260.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Once I knew the property I was after was called lastLogin, I did Google search for "ESRI lastLogin" to find info/examples of how to use it.&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 01:21:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232553#M8023</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-11-17T01:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232555#M8024</link>
      <description>&lt;P&gt;not where I can test at the moment, but your actualDate will be a string and datetime.now will be a datetime object.&lt;/P&gt;&lt;P&gt;will need to convert the string time to a datetime object, then the math should work.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 01:41:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232555#M8024</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-11-17T01:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Query for Users that have not logged in for 40 days</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232666#M8026</link>
      <description>&lt;P&gt;Awesome I got it with this... Maybe a smoother way to do this?&lt;/P&gt;&lt;P&gt;Thanks for steering he correct direction!!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def findUsers():
    counter = 0
    with open(output_csv, 'w', encoding='utf-8') as file:
        csvfile = csv.writer(file, delimiter=',', lineterminator='\n')
        csvfile.writerow(["Name", 
                          "LastLogOn",
                          "Email",
                          "Days"
                        ])

        for item in user_list:
            lastThirtyDays = datetime.now() + timedelta(days=-numberOfDays)
            lastLogintime = item.lastLogin/1000
            lastLoginTimeConvert = (datetime.utcfromtimestamp(lastLogintime).strftime('%Y-%m-%d %H:%M:%S'))

            format = '%Y-%m-%d %H:%M:%S'
            lastLoginDate = datetime.strptime(lastLoginTimeConvert, format)

            diffdays = lastThirtyDays - lastLoginDate
            diffDays2 = diffdays.days
            
            if lastLoginDate &amp;lt; lastThirtyDays:
                if diffDays2 &amp;gt; 35:
                    print(item.username + " logged in " + str(diffDays2) + " days ago - " + item.email)
                    counter += 1
                    csvfile.writerow([item.username,    
                                lastLoginTimeConvert,
                                item.email,
                                diffDays2
                                ])
    return counter&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 17:59:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/query-for-users-that-have-not-logged-in-for-40/m-p/1232666#M8026</guid>
      <dc:creator>kapalczynski</dc:creator>
      <dc:date>2022-11-17T17:59:49Z</dc:date>
    </item>
  </channel>
</rss>

