Rich-text Reply

Query Results of All Experiments using Python Client API

SumeetDesai 09-10-15
Accepted Solution

Query Results of All Experiments using Python Client API

I am using Python -Django Framework for displaying the results of all experiments using the Python Client API. I am getting some errors when the for loop with i index goes to 2nd iteration. Note that, if I take out the code that queries the results, I am able to successfully query how many experiments I am running within the current project. I can provide more details such as token, project, experiment as necessary.

I am unable to understand what is wrong with the current code when I want to display results. Can someone help me with this?

 

Thanks in advance,

Sumeet

 

def Optimizely(request):

   client = optimizely.Client('xxxxxxxx')

   project = client.Projects.get(11111111)

   listExpts = project.experiments()#get all experiments in the project  

   print "Number of Expts",len(listExpts)

   exptDetails = [ ]

   for i in range(len(listExpts)):      

       experiment = listExpts[i]

       exp = experiment.__dict__

       #print i, exp      

      

       for j in range(2):

           results = experiment.results()[j]  

           result = results.__dict__

           resultGoal = result['goal_name']

           resultVisitors = result['visitors']

           resultConversion = result['conversions']

           resultStatus = exp['status']

           resultConfidence = result['confidence']

           print exp['id'],resultGoal,resultVisitors,resultConversion,resultStatus,resultConfidence

       expID = exp['id']

       expDesc = exp['description']

       expCreateDt = exp['created']

       d1 = expCreateDt[:10]

       expLastDt = exp['last_modified']

       d2 = expLastDt[:10]

       exptDetails.append([expID, expDesc,d1,d2,resultGoal,resultStatus,resultVisitors,resultConversion,resultConfidence])

      

 

   context_dict = {}

   context_dict['exptDetails'] = mark_safe(json.dumps(exptDetails))

   return render(request, 'Optimizely.html',context_dict)

 

OptimizelyError at /webanalytics/Optimizely/

Internal Server Error

Request Method:

GET

Request URL:

http://127.0.0.1:8000/webanalytics/Optimizely/

Django Version:

1.7.4

Exception Type:

OptimizelyError

Exception Value:

Internal Server Error

Exception Location:

C:\Users\sdesai.LOE\Envs\27_17\lib\site-packages\optimizely\client.py in parse_response, line 69

Python Executable:

C:\Users\sdesai.LOE\Envs\27_17\Scripts\python.exe

Python Version:

2.7.8

Re: Query Results of All Experiments using Python Client API

[ Edited ]

Hi @SumeetDesai

 

Thanks for posting in Optiverse! We have a known bug with our REST API where if you hit our /stats endpoint for an experiment that has no results (ie. has never been run) it returns a 500 internal server error.

 

As a result, when you are leveraging our python client and looping over all the experiments in your project and trying to fetch results for experiments that have no results, the client raises an error and exits your loop. 

 

I did some testing on my end and found that if I added a try/except block when the results were being fetched, everything seemed to run smoothly and only the experiments with results were processed. (see code below)

 

Current:

 

  for j in range(2):
           results = experiment.results()[j]  
           result = results.__dict__
           resultGoal = result['goal_name']
           resultVisitors = result['visitors']
           ....

Updated:

 

for j in range(2):
    try:
        results = experiment.results()[j]
    except optimizely.error.OptimizelyError:
        pass

    result = results.__dict__
    resultGoal = result['goal_name']
    resultVisitors = result['visitors']
    ....

Does this resolve the issue on your end? Please let me know if I can provide any additional assistance or clarification.

 

Thanks,

Becca

SumeetDesai 09-15-15
 

Re: Query Results of All Experiments using Python Client API

Perfect, I actually ended up with the same solution (I just used different error handler). Thanks for the response thought.
Best,
Sumeet