-2

I am literally BRAND NEW to python and in my current role it is not a hardcore requirement, but knowing my way around a script will make life much easier. I am working in an ArcGIS Server environment, for context. I am gutting old scripts and piecing them together to do what I want. Right now, my script writes a text file compiling a list of services and their average draw times, and sends me an email every single time it detects an average draw time of more than 0 seconds for a service. (Ultimately this will be moved to 2 seconds but I'm using 0 to ensure it is workign correctly first). What I really want it to do is send one email with the listing of the draw times instead of x amount of individual emails. I will attach below the part that I think is relevant.

# Open text file and write header line 
summaryFile = open(filePath, "w") 
header = "Now,Service,Number of hits,Average seconds per draw\n"
summaryFile.write(header)

# Read through dictionary and write totals into file
for key in hitDict:

    #print hitDict

    # Calculate average elapsed time
    totalDraws = hitDict[key][0]
    totalElapsed = hitDict[key][1]
    avgElapsed = 0

    if totalDraws > 0:
        avgElapsed = (1.0 * (totalElapsed / totalDraws)) #Elapsed time divided by hits


     #Send email when invalid password is found in logs.                                            #NEW
    if avgElapsed > 0:
                #Define Email Parameters
                #counter = 1 + counter
                #if counter > 5:
                    EmailBody = "High avg draw time noted, check services."
                    EmailSubject = "High avg draw times observed in " + item["source"]

                    for address in Receivers:
                        EmailAttachments(address, From, EmailSubject, EmailBody)

                    print "High Average Draw Time Observed"#.format(timeStamp, machine)


    # Construct and write the comma-separated line
    line = timeStamp +"," + key + "," + str(totalDraws) + "," + str(avgElapsed) + "\n"

    fields = ["TIME", "SERVICE", "HITS", "AVGSECPERDRAW"]
    insCur.insertRow(( timeStamp, key, str(totalDraws), str(avgElapsed)))

    #serverName = item["machine"]
    #print serverName

    summaryFile.write(line)

summaryFile.close()
return 
5
  • 1
    What is your question? Commented Apr 23, 2014 at 15:42
  • I need to know what the code looks like to add the comma-separated lines into the email body, and email it only once with all the lines.
    – Strider
    Commented Apr 23, 2014 at 16:40
  • Maybe a better way of asking this would be: how do I write lines to an array(?) and then add this array as the Email body? And where do I nestle the EmailBody/EmailSubject so that it emails only once, at the end of the script running?
    – Strider
    Commented Apr 23, 2014 at 18:57
  • And wouldn't any service, no matter how fast take more than 0 seconds to draw? ESRI's marketing might be good, but never do they claim 0 seconds reponse time. Commented Apr 24, 2014 at 4:38
  • Yes, they would always take more than 0 seconds. I was using this just to test the script, to ensure I always got feedback.
    – Strider
    Commented Apr 25, 2014 at 13:23

2 Answers 2

2

Open the file using summaryFile = open(filePath, "a") instead, so the logs are appended to the file instead of overwritten.

Then just set the script up so it only emails you once at the end of the day and removes the log, so it's fresh the next day.

1
  • The piece that emails me once at the end of the day, and fills the body of the email with the log, is exactly the part I need help with. What does this code look like? Or does anyone have a link to information describing this? Remember, I have no background with this at all. I'm stumbling through code in pre-written scripts, creating essentially a Python Frankenstein script from different pieces from here and there.
    – Strider
    Commented Apr 23, 2014 at 16:39
0

how do I write lines to an array(?) and then add this array as the Email body? And where do I nestle the EmailBody/EmailSubject so that it emails only once, at the end of the script running?

Lets assume you have a list (Python does not have a native Array type) of messages (text strings that you want to email). This first thing to do is join them together.

You could loop on the items in the list

messages = ['messages1','messages2','messages3']
email_body = ""
for message in messages:
    email_body = email_body + "\n" + message

but the more pythonic way would be to use the join method on string

email_body = "\n".join(messages)

After you have created the body of the email, then you would email it (using the method in your example)

# set the address, from, and subject
EmailAttachments(address, from, subject, email_body)

Your example loops on the keys in the hitdict dictionary, and then calls EmailAttacments for each key. You will need to loop over hitdict to create your list of messages.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.