Пример #1
0
    def run(self, options):
        logs.begin(
            saveLog=stampedAPI._logsDB.saveLog,
            saveStat=stampedAPI._statsDB.addStat,
            nodeName=stampedAPI.node_name
        )
        logs.async_request('alerts')

        lock = os.path.join(base, 'alerts.lock')
        if os.path.exists(lock):
            logs.warning('Locked - aborting')
            return
        
        try:
            open(lock, 'w').close()
            self.buildAlerts(limit=options.limit, noop=options.noop)
            self.buildInvitations(limit=options.limit, noop=options.noop)
            if len(self._emailQueue) > 0:
                self.sendEmails(noop=options.noop)
            if len(self._pushQueue) > 0:
                self.sendPush(noop=options.noop)
            self.cleanupPush(noop=options.noop)

        except Exception as e:
            logs.warning('Exception: %s' % e)
            logs.warning(utils.getFormattedException())
            logs.error(500)

        finally:
            os.remove(lock)
            try:
                logs.save()
            except Exception:
                print '\n\n\nWARNING: UNABLE TO SAVE LOGS\n\n\n'
Пример #2
0
def plot(x, idxs, label, idx, path):
    colors = Neon.color12()
    loglib.dark()
    c = colors[idx % 12]
    loglib.plot(x, inds=idxs, label=str(idx), c=c.norm)
    loglib.godsword()
    loglib.save(path + label + '.png')
    plt.close()
Пример #3
0
def plots(x, label, idx, path, split):
    colors = Neon.color12()
    loglib.dark()
    for idx, item in enumerate(x.items()):
        annID, val = item
        c = colors[idx % 12]
        idxs, val = compress(val, split)
        loglib.plot(val, inds=idxs, label=str(annID), c=c.norm)
    loglib.godsword()
    loglib.save(path + label + '.png')
    plt.close()
Пример #4
0
def passwordReset(request, **kwargs):
    token  = kwargs.pop('token', None)

    errorMsg = 'An error occurred. Please try again.'

    try:
        # Verify token is valid
        authUserId = stampedAuth.verifyPasswordResetToken(token)

        # Check if a form exists with data
        if request.method == 'POST':
            # User submitted the form
            data = request.POST

            # Verify passwords match and len > 0
            if not data['password']:
                logs.warning("Invalid password: %s" % data['password'])
                raise StampedInvalidPasswordError("Invalid password")

            if data['password'] != data['confirm']:
                logs.warning("Password match failed")
                raise StampedInvalidPasswordError("Password match failed")
            
            # Store password            
            stampedAuth.updatePassword(authUserId, data['password'])

            # Return success
            response = HttpResponseRedirect('/settings/password/success')

        else:
            # Display 'change password' form
            account = stampedAPI.getAccount(authUserId)
            params = {
                'token': token,
                'form': True,
                }
            response = render_to_response('password-reset.html', params)

        return response

    except Exception as e:
        logs.begin(
            addLog=stampedAPI._logsDB.addLog, 
            saveLog=stampedAPI._logsDB.saveLog,
            saveStat=stampedAPI._statsDB.addStat,
            requestData=request,
        )
        logs.request(request)
        logs.warning("500 Error: %s" % e)
        logs.error(500)
        logs.save()

        return render_to_response('password-reset.html', {'error': errorMsg})
Пример #5
0
def passwordForgot(request):

    errorMsg = 'An error occurred. Please try again.'

    try:
        # Check if a form exists with data
        if request.method == 'POST':
            # User submitted the form
            data = request.POST

            # Validate email address
            email = str(data['forgotemail']).lower().strip()
            if not utils.validate_email(email):
                msg = "Invalid format for email address"
                logs.warning(msg)
                raise StampedInvalidEmailError("Invalid email address")
            
            # Verify account exists
            try:
                user = stampedAPI.checkAccount(email)
            except:
                ### TODO: Display appropriate error message
                errorMsg = 'No account information was found for that email address.'
                raise StampedHTTPError(404, msg="Email address not found", kind='invalid_input')

            # Send email
            stampedAuth.forgotPassword(email)

            # Return success
            response = HttpResponseRedirect('/settings/password/sent')

        else:
            # Display 'submit email' form
            response = render_to_response('password-forgot.html', None)

        return response

    except Exception as e:
        logs.begin(
            addLog=stampedAPI._logsDB.addLog, 
            saveLog=stampedAPI._logsDB.saveLog,
            saveStat=stampedAPI._statsDB.addStat,
            requestData=request,
        )
        logs.request(request)
        logs.warning("500 Error: %s" % e)
        logs.error(500)
        logs.save()

        return render_to_response('password-forgot.html', {'error': errorMsg})

    return True
Пример #6
0
def alertSettings(request, **kwargs):
    try:
        # Check token
        tokenId     = request.GET.get('token', None)
        authUserId  = stampedAuth.verifyEmailAlertToken(tokenId)

        # Display 'change settings' form
        account = stampedAPI.getAccount(authUserId)

        alerts  = HTTPAccountAlerts().importSchema(account)
        user    = HTTPUser().importSchema(account)

        image_url = user['image_url'].replace('.jpg', '-31x31.jpg')
        stamp_url = 'http://static.stamped.com/logos/%s-%s-credit-18x18.png' % \
                    (user.color_primary, user.color_secondary)

        settings = {
            'email_alert_credit':   alerts.email_alert_credit,
            'email_alert_like':     alerts.email_alert_like,
            'email_alert_fav':      alerts.email_alert_fav,
            'email_alert_mention':  alerts.email_alert_mention,
            'email_alert_comment':  alerts.email_alert_comment,
            'email_alert_reply':    alerts.email_alert_reply,
            'email_alert_follow':   alerts.email_alert_follow,
        }

        params = {
            'screen_name':      user.screen_name,
            'name':             user.name,
            'image_url':        image_url,
            'stamp_url':        stamp_url,
            'action_token':     tokenId,
            'json_settings':    json.dumps(settings, sort_keys=True)
        }
        
        return render_to_response('notifications.html', params)

    except Exception as e:
        logs.begin(
            addLog=stampedAPI._logsDB.addLog, 
            saveLog=stampedAPI._logsDB.saveLog,
            saveStat=stampedAPI._statsDB.addStat,
            requestData=request,
        )
        logs.request(request)
        logs.warning("500 Error: %s" % e)
        logs.error(500)
        logs.save()

        ### TODO: CHANGE URL
        return render_to_response('password-reset.html', {'error': 'FAIL'})
Пример #7
0
def agents():
   exps = list(experiments.exps.keys())
   loglib.dark()
   colors = Neon.color12()
   maxVal = 0
   for idx, exp in enumerate(exps):
      name, log = exp
      c = colors[idx]
      loglib.plot(log['lifespan'], name, c.norm)
      maxVal = max(maxVal, np.max(log['lifespan']))
   loglib.limits(ylims=[0, 50*(1+maxVal//50)])
   loglib.godsword()
   loglib.save(logDir+'/agents.png')
   plt.close()
Пример #8
0
def joints(exps):
   print('Joints...')
   keys   = reversed('return lifespan value value_loss pg_loss entropy grad_mean grad_std grad_min grad_max'.split())
   colors = Neon.color12()
   for key in keys:
      loglib.dark()
      maxVal = 0
      for idx, dat in enumerate(exps):
         name, _, log = dat
         loglib.plot(log[key], name, colors[idx].norm, lw=3)
         maxVal = max(maxVal, np.max(log[key]))
      loglib.limits(ylims=[0, 50*(1+maxVal//50)])
      loglib.godsword()
      loglib.save(logDir+'joint/'+key)
      plt.close()
Пример #9
0
def alertSettingsUpdate(request, **kwargs):
    try:
        # Check token
        tokenId     = request.POST.get('token', None)
        authUserId  = stampedAuth.verifyEmailAlertToken(tokenId)

        def _checkBool(v):
            if v in ['True', 'true', '1', 1, True]:
                return True
            return False

        # Get settings
        alerts = {
            'email_alert_credit':   _checkBool(request.POST.get('email_alert_credit', False)),
            'email_alert_like':     _checkBool(request.POST.get('email_alert_like', False)),
            'email_alert_fav':      _checkBool(request.POST.get('email_alert_fav', False)),
            'email_alert_mention':  _checkBool(request.POST.get('email_alert_mention', False)),
            'email_alert_comment':  _checkBool(request.POST.get('email_alert_comment', False)),
            'email_alert_reply':    _checkBool(request.POST.get('email_alert_reply', False)),
            'email_alert_follow':   _checkBool(request.POST.get('email_alert_follow', False)),
        }

        stampedAPI.updateAlerts(authUserId, alerts)
        
        params = {}
        params.setdefault('content_type', 'text/javascript; charset=UTF-8')
        params.setdefault('mimetype', 'application/json')
        
        output_json = json.dumps(alerts, sort_keys=True)
        output = HttpResponse(output_json, **params)
        
        return output
    
    except Exception as e:
        logs.begin(
            addLog=stampedAPI._logsDB.addLog, 
            saveLog=stampedAPI._logsDB.saveLog,
            saveStat=stampedAPI._statsDB.addStat,
            requestData=request,
        )
        logs.request(request)
        logs.warning("500 Error: %s" % e)
        logs.error(500)
        logs.save()
        
        return HttpResponse("internal server error", status=500)
Пример #10
0
    def wrapper(worker, job):
        try:
            k = job.task
            logs.begin(saveLog=api._logsDB.saveLog, saveStat=api._statsDB.addStat, nodeName=api.node_name)
            logs.async_request(k)
            v = functions[k]
            data = pickle.loads(job.data)
            logs.info("%s: %s: %s" % (k, v, data))
            v(k, data)
        except Exception as e:
            logs.error(str(e))
        finally:
            try:
                logs.save()
            except Exception:
                print "Unable to save logs"
                import traceback

                traceback.print_exc()
                logs.warning(traceback.format_exc())
        return ""
Пример #11
0
def gen_plot(log, keys, savename, train=True):
   loglib.dark()

   if len(keys) > 12:
      colors = Color256.colors
   else:
      colors = Neon.color12()

   pops = []
   for i, key in enumerate(keys):
      c = colors[i]

      if not train:
         log[key] = np.cumsum(np.array(log[key])) / (1+np.arange(len(log[key])))

      if i == 0:
         loglib.plot(log[key], key, (1.0, 0, 0))
      else:
         loglib.plot(log[key], key, c.norm)
   loglib.godsword()
   loglib.save(savename)
   plt.close()
Пример #12
0
 def wrapper(task_id, key, data, **kwargs):
     try:
         logs.begin(saveLog=api._logsDB.saveLog,
                    saveStat=api._statsDB.addStat,
                    nodeName=api.node_name)
         logs.async_request(key)
         logs.info("Request %s: %s: %s: %s" % (task_id, key, data, kwargs))
         handler(task_id, key, data, **kwargs)
         logs.info("Finished with request %s" % (task_id,))
     except Exception as e:
         logs.error("Failed request %s" % (task_id,))
         logs.report()
         _warningEmail('%s - %s failed (%s)' % (api.node_name, key, datetime.utcnow().isoformat()))
     finally:
         logs.info('Saving request log for request %s' % (task_id,))
         try:
             logs.save()
         except Exception:
             print 'Unable to save logs'
             import traceback
             traceback.print_exc()
             logs.warning(traceback.format_exc())
Пример #13
0
def invoke(request, *args, **kwargs):
    """ 
        wrapper to invoke a stamped api function in an asynchronous context 
        which adds logging and exception handling.
    """
    
    taskId = kwargs.pop('taskId', None)

    try:
        stampedAPI = getStampedAPI()
        func = "%sAsync" % utils.getFuncName(1)
        
        if not request.is_eager:
            logs.begin(
                saveLog=stampedAPI._logsDB.saveLog,
                saveStat=stampedAPI._statsDB.addStat,
                nodeName=stampedAPI.node_name,
            )
            
            logs.async_request(func, *args, **kwargs)
        
        logs.info("%s %s %s (is_eager=%s, hostname=%s, task_id=%s)" % 
                  (func, args, kwargs, request.is_eager, request.hostname, request.id))

        getattr(stampedAPI, func)(*args, **kwargs)

    except Exception as e:
        logs.error(str(e))
        raise
    finally:
        try:
            if taskId is not None:
                stampedAPI._asyncTasksDB.removeTask(taskId)
            if not request.is_eager:
                logs.save()
        except:
            pass
Пример #14
0
def individual(log, label, npop, logDir='resource/data/exps/', train=True):

   if train:
      split = 'train'
   else:
      split = 'test'

   savedir = osp.join(logDir, label, split)
   if not osp.exists(savedir):
      os.makedirs(savedir)

   if len(log['return']) > 0:
      loglib.dark()
      keys   = reversed('return lifespan value value_loss pg_loss entropy grad_mean grad_std grad_min grad_max'.split())
      colors = Neon.color12()
      fName = 'frag.png'
      for idx, key in enumerate(keys):
         if idx == 0:
            c = colors[idx]
            loglib.plot(log[key], key, (1.0, 0, 0))
         else:
            c = colors[idx]
            loglib.plot(log[key], key, c.norm)
      maxLife = np.max(log['return'])
      loglib.limits(ylims=[0, 50*(1+maxLife//50)])
      loglib.godsword()
      savepath = osp.join(logDir, label, split, fName)
      loglib.save(savepath)
      print(savepath)
      plt.close()

   # Construct population specific code
   pop_mean_keys = ['lifespan{}_mean'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_mean.png')
   gen_plot(log, pop_mean_keys, savefile, train=train)

   # Per population movement probability
   pop_move_keys = ['pop{}_move'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_move.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # Attack probability plots
   pop_move_keys = ['pop{}_range'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_range.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_melee'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_melee.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_mage'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_mage.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # Movement tile entropy
   pop_move_keys = ['pop{}_entropy'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_move_entropy.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # Population attack probabilities when action is selected
   pop_move_keys = ['pop{}_melee_logit'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_melee_logit.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_range_logit'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_range_logit.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_mage_logit'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_mage_logit.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # Sum up all the logits to check if they actually sum to zero
   for i in range(npop):
      logit_sum = np.array(log['pop{}_melee_logit'.format(i)]) + np.array(log['pop{}_range_logit'.format(i)]) + np.array(log['pop{}_mage_logit'.format(i)])
      log['pop{}_sum_logit'.format(i)] = logit_sum

   pop_move_keys = ['pop{}_sum_logit'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_sum_logit.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # Tile exploration statistics
   pop_move_keys = ['pop{}_grass_tiles'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_grass_tiles.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_forest_tiles'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_forest_tiles.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   pop_move_keys = ['pop{}_forest_tiles_depleted'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_forest_depleted.png')
   gen_plot(log, pop_move_keys, savefile, train=train)

   # pop_move_keys = ['pop{}_forest_tiles_other'.format(i) for i in range(npop)]
   # savefile = osp.join(logDir, label, 'pop_forest_tiles_other.png')
   # gen_plot(log, pop_move_keys, savefile, train=train)

   for i in range(npop):
      forest_tiles = np.array(log['pop{}_forest_tiles'.format(i)])
      other_tiles = np.array(log['pop{}_grass_tiles'.format(i)]) + np.array(log['pop{}_forest_tiles_depleted'.format(i)]) + forest_tiles
      forage_percent = forest_tiles / other_tiles
      log['pop{}_forage_success'.format(i)] = forage_percent

   pop_move_keys = ['pop{}_forage_success'.format(i) for i in range(npop)]
   savefile = osp.join(logDir, label, split, 'pop_forage_success.png')
   gen_plot(log, pop_move_keys, savefile, train=train)
Пример #15
0
                #logs.error(response.status_code)
                #return response
            
            except Exception as e:
                logs.warning("500 Error: %s" % e)
                logs.warning(utils.getFormattedException())
                utils.printException()
                
                logs.error(500)
                raise # invoke django's default 500 handler
                #response = HttpResponse("internal server error", status=500)
                #return response
            
            finally:
                try:
                    logs.save()
                except:
                    pass

        return _wrapper
    return decorator

def stamped_render(request, template, context, **kwargs):
    # augment template context with global django / stamped settings
    kwargs['context_instance'] = kwargs.get('context_instance', RequestContext(request))
    
    preload = kwargs.pop('preload', None)
    context = get_stamped_context(context, preload)
    
    #utils.log(pprint.pformat(context))
    return render_to_response(template, context, **kwargs)
Пример #16
0
        def wrapper(request, *args, **kwargs):
            try:
                origin = None
                
                try:
                    origin = request.META['HTTP_ORIGIN']
                    valid_origin = origin
                    assert valid_origin in VALID_ORIGINS
                except Exception:
                    valid_origin = None
                
                def _add_cors_headers(response):
                    response['Access-Control-Allow-Origin']  = valid_origin
                    response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
                    response['Access-Control-Max-Age']       = 1000
                    response['Access-Control-Allow-Headers'] = '*'
                    return response
                
                # allow API to gracefully handle cross-domain requests from trusted origins
                if request.method == 'OPTIONS' and valid_origin is not None:
                    return _add_cors_headers(HttpResponse())
                
                logs.begin(saveLog=stampedAPI._logsDB.saveLog,
                           saveStat=stampedAPI._statsDB.addStat,
                           requestData=request,
                           nodeName=stampedAPI.node_name)
                logs.info("%s %s" % (request.method, request.path))

                if valid_origin is None:
                    if origin is not None:
                        logs.warning("Invalid origin: %s" % origin)
                    else:
                        logs.debug("Origin not included")
                
                params = {}

                params['authUserId'], params['authClientId'] = checkOAuthWithRequest(request, requires_auth)
                params['client_id'] = checkClient(request, required=requires_client)
                
                if parse_request:
                    parse_kwargs = parse_request_kwargs or {}

                    if upload is not None:
                        if http_schema is None:
                            raise Exception("ERROR: handleHTTPRequest requires http_schema if upload is provided")
                        
                        params['http_schema']   = parseFileUpload(http_schema(), request, upload, **parse_kwargs)
                    elif http_schema is not None:
                        params['http_schema']   = parseRequest(http_schema(), request, **parse_kwargs)
                    else:
                        params['http_schema']   = parseRequest(None, request, **parse_kwargs)
                    
                    if conversion is not None:
                        params['schema']        = conversion(params['http_schema'])
                    elif http_schema is not None:
                        params['data']          = params['http_schema'].dataExport()

                if 'PATH_INFO' in request.META:
                    params['uri'] = request.META['PATH_INFO'].lower()
                
                kwargs.update(params)
                ret = fn(request, *args, **kwargs)
                logs.info("End request: Success")
                
                if valid_origin and isinstance(ret, HttpResponse):
                    _add_cors_headers(ret)
                
                return ret

            except Exception as e:
                return handleStampedExceptions(e, exceptions)
            finally:
                try:
                    logs.save()
                except Exception:
                    print 'Unable to save logs'
                    import traceback
                    traceback.print_exc()
                    logs.warning(traceback.format_exc())