Select to view content in your preferred language

Best way to monitor for new features

879
2
Jump to solution
12-27-2020 03:16 AM
lightpro
Regular Contributor

Hi, I'm looking for any tips on the best way to monitor an AGOL feature service for newly added features. Basically I have a public feature service that I want to monitor for when new features are added. I want to run some follow on scripts on new features only.

Right now, I think my best bet is to just set up a pythonanywhere script that runs every hour and checks the "created date" field for any features created in the last hour. Another option might be to create a custom "tracking field" that is NULL by default, then I create a script to check for features where this field is null, then when my script is finished I update this field to be not null. 

Are these my basic options?

1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

@lightpro 

I reckon the following script can help.

This is from @JakeSkinner which sends email when a feature is added to a hosted feature service.

 

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

# Variables
username = 'jskinner_CountySandbox'        # AGOL Username
password = '******'    # AGOL Password

URL = 'https://services1.arcgis.com/vdNDkVykv9vEWFX4/arcgis/rest/services/address_errors/FeatureServer/0/qu...'       # Feature Service URL
uniqueID = 'OBJECTID'           # i.e. OBJECTID
dateField = 'CreationDate'      # Date field to query
hoursValue = 1                  # Number of hours to check when a feature was added

fromEmail = 'test@esri.com' # Email sender
toEmail = 'test@esri.com'   # Email receiver
smtpServer = 'smtp.gis.com'    # SMPT Server Name
portNumber = 25                 # SMTP Server port

# Create empty list for uniqueIDs
oidList = []

# Generate AGOL token
try:
    print('Generating Token')
    tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
    params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
    req = urllib2.Request(tokenURL, urllib.urlencode(params))
    response = urllib2.urlopen(req)
    data = json.load(response)
    token = data['token']
except:
    token = ''

# Query service and check if created_date time is within the last hour
params = {'f': 'pjson', 'where': "1=1", 'outfields' : '{0}, {1}'.format(uniqueID, dateField), 'returnGeometry' : 'false', 'token' : token}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
    createDate = feat['attributes'][dateField]
    createDate = int(str(createDate)[0:-3])
    t = datetime.datetime.now() - timedelta(hours=hoursValue)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes'][uniqueID])

print(oidList)

# Email Info
FROM = fromEmail
TO = [toEmail]
SUBJECT = 'New Features Added'
TEXT = "Features with {0}s {1} were added.".format(uniqueID, oidList)

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# If new features exist, send email
if len(oidList) > 0:
    smtpObj = smtplib.SMTP(host=smtpServer, port=portNumber)
    smtpObj.sendmail(FROM, TO, message)
    print "Successfully sent email"
    smtpObj.quit()

 

 

The other option is using webhooks

You can use a webhook for feature service view (tile, scene, etc.), to receive notification for updates. 

https://www.esri.com/arcgis-blog/products/arcgis-online/sharing-collaboration/how-to-create-a-hosted...

 

I hope these give you some ideas.

Cheers

Mehdi

======================================================================

Please give a like if helpful and Accept as Solution if it's answered your query.

 

View solution in original post

2 Replies
MehdiPira1
Esri Contributor

@lightpro 

I reckon the following script can help.

This is from @JakeSkinner which sends email when a feature is added to a hosted feature service.

 

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

# Variables
username = 'jskinner_CountySandbox'        # AGOL Username
password = '******'    # AGOL Password

URL = 'https://services1.arcgis.com/vdNDkVykv9vEWFX4/arcgis/rest/services/address_errors/FeatureServer/0/qu...'       # Feature Service URL
uniqueID = 'OBJECTID'           # i.e. OBJECTID
dateField = 'CreationDate'      # Date field to query
hoursValue = 1                  # Number of hours to check when a feature was added

fromEmail = 'test@esri.com' # Email sender
toEmail = 'test@esri.com'   # Email receiver
smtpServer = 'smtp.gis.com'    # SMPT Server Name
portNumber = 25                 # SMTP Server port

# Create empty list for uniqueIDs
oidList = []

# Generate AGOL token
try:
    print('Generating Token')
    tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
    params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
    req = urllib2.Request(tokenURL, urllib.urlencode(params))
    response = urllib2.urlopen(req)
    data = json.load(response)
    token = data['token']
except:
    token = ''

# Query service and check if created_date time is within the last hour
params = {'f': 'pjson', 'where': "1=1", 'outfields' : '{0}, {1}'.format(uniqueID, dateField), 'returnGeometry' : 'false', 'token' : token}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
    createDate = feat['attributes'][dateField]
    createDate = int(str(createDate)[0:-3])
    t = datetime.datetime.now() - timedelta(hours=hoursValue)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes'][uniqueID])

print(oidList)

# Email Info
FROM = fromEmail
TO = [toEmail]
SUBJECT = 'New Features Added'
TEXT = "Features with {0}s {1} were added.".format(uniqueID, oidList)

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# If new features exist, send email
if len(oidList) > 0:
    smtpObj = smtplib.SMTP(host=smtpServer, port=portNumber)
    smtpObj.sendmail(FROM, TO, message)
    print "Successfully sent email"
    smtpObj.quit()

 

 

The other option is using webhooks

You can use a webhook for feature service view (tile, scene, etc.), to receive notification for updates. 

https://www.esri.com/arcgis-blog/products/arcgis-online/sharing-collaboration/how-to-create-a-hosted...

 

I hope these give you some ideas.

Cheers

Mehdi

======================================================================

Please give a like if helpful and Accept as Solution if it's answered your query.

 

lightpro
Regular Contributor

Excellent, this is great thanks!

0 Kudos