def connect_to_dev_server_by_remote_api(cfg, shell): if hasattr(cfg, "gae_sdk_path") and cfg.gae_sdk_path: sdk_path = cfg.gae_sdk_path else: sdk_path = "/usr/local/google_appengine" # For transaction os.environ['SERVER_SOFTWARE'] = 'Development (devshell remote-api)/1.0' # For TaskQueue os.environ['HTTP_HOST'] = 'localhost' sys.path.append(sdk_path) import dev_appserver dev_appserver.fix_sys_path() if hasattr(cfg, "project_path") and cfg.project_path: path = os.path.abspath(os.path.expanduser(cfg.project_path)) sys.path.insert(0, path) if hasattr(cfg, "remote_api_path") and cfg.remote_api_path: remote_entry = cfg.remote_api_path else: remote_entry = "/_ah/remote_api" if hasattr(cfg, "port") and cfg.port: port = cfg.port else: port = "8080" from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApiForOAuth("localhost:" + port, remote_entry, secure=False) start_shell(shell, globals(), locals())
def main(key, url): print "Configuring GAE Remote API on {} to import {}".format(url, key) if 'localhost' in url: remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', local_auth_func, url) else: remote_api_stub.ConfigureRemoteApiForOAuth(url, '/_ah/remote_api') print "Loading data from The Blue Alliance requires an API key" print "Please go to https://thebluealliance.com/account and generate a read API key" apiv3_key = raw_input("Enter your API key: ") global AUTH_TOKEN AUTH_TOKEN = apiv3_key if Match.validate_key_name(key): match_data = fetch_match(key) store_match(match_data) elif Event.validate_key_name(key): update_event(key) elif Team.validate_key_name(key): team_data = fetch_team(key) store_team(team_data) elif key.isdigit(): event_keys = [ event['key'] for event in fetch_endpoint('events/{}'.format(key)) ] for event in event_keys: update_event(event) else: print "Unknown key :("
def _boot_app_engine(project_id): # # Boot the App Engine environment # # This makes the GCS work! # http://stackoverflow.com/questions/22646641/google-cloud-storage-client-with-remote-api-on-local-client os.environ["SERVER_SOFTWARE"] = "" os.environ["HTTP_HOST"] = project_id + '.appspot.com' # Always use UTC here so entities timestamps get updated with UTC os.environ['TZ'] = 'UTC' time.tzset() from google.appengine.ext.remote_api import remote_api_stub try: remote_api_stub.ConfigureRemoteApiForOAuth(os.environ["HTTP_HOST"], '/_ah/remote_api') except urllib2.HTTPError as e: click.echo("ConfigureRemoteApiForOAuth failure ({})".format(e)) click.echo('') click.echo("To authenticate run:") click.echo(" gcloud auth application-default login") return os.environ['SERVER_SOFTWARE'] = 'remote_api_shell'
def main(project_id): print('Logging into app engine') remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(project_id), '/_ah/remote_api') print('Logged in') from google.appengine.api import app_identity print app_identity.get_application_id()
def main(project_id): remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(project_id), '/_ah/remote_api') # List the first 10 keys in the datastore. keys = ndb.Query().fetch(10, keys_only=True) for key in keys: print(key)
def connect(): os.environ[ 'GOOGLE_APPLICATION_CREDENTIALS'] = conf.GOOGLE_APPLICATION_CREDENTIALS with open(conf.GOOGLE_APPLICATION_CREDENTIALS) as fp: project_id = json.load(fp)['project_id'] # You will get UnicodeDecodeError if servername is a unicode string. servername = str('%s.appspot.com' % project_id) remote_api_stub.ConfigureRemoteApiForOAuth(servername, '/_ah/remote_api')
def main(project_id): remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(project_id), '/_ah/remote_api') # List the first 10 keys in the datastore. # can also use ndb. ... # keys = ndb.Query().fetch(10, kind=Packet, keys_only=True) keys = Packet.query().fetch(10) #, keys_only=True) for key in keys: print(key.rrInterval)
def process_item(self, item, spider): from data.gcloud.video_model import Video remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(settings['GOOGLE_APPID']), '/_ah/remote_api') # Video().insert_video(title=title,content=content,picture=picture, category=category, url=url, country=country) Video().insert_video(**item) return item
def main(): try: remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(app_id), '/_ah/remote_api') convert(os.environ['PICKLE_FILE']) except: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr) exit(1)
def remote_api_shell(servername, appid, path, secure, rpc_server_factory, oauth2=False): """Actually run the remote_api_shell.""" if oauth2: remote_api_stub.ConfigureRemoteApiForOAuth(servername, path, secure=secure, app_id=appid) else: remote_api_stub.ConfigureRemoteApi( appid, path, auth_func, servername=servername, save_cookies=True, secure=secure, rpc_server_factory=rpc_server_factory) remote_api_stub.MaybeInvokeAuthentication() os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0' if not appid: appid = os.environ['APPLICATION_ID'] sys.ps1 = '%s> ' % appid if readline is not None: readline.parse_and_bind('tab: complete') atexit.register(lambda: readline.write_history_file(HISTORY_PATH)) if os.path.exists(HISTORY_PATH): readline.read_history_file(HISTORY_PATH) if '' not in sys.path: sys.path.insert(0, '') preimported_locals = { 'memcache': memcache, 'urlfetch': urlfetch, 'users': users, 'db': db, 'ndb': ndb, } code.interact(banner=BANNER, local=preimported_locals)
def configure_remote_api(hostport, path, secure, server_type): if server_type == 'appengine': remote_api_stub.ConfigureRemoteApiForOAuth(hostport, path, secure=secure, auth_tries=1) elif server_type == 'local': # It seems local dev_appserver doesn't support remote API with OAuth. remote_api_stub.ConfigureRemoteApi(None, path, lambda: ('*****@*****.**', None), hostport, secure=secure) else: raise Exception('Unknown --server_type: %r' % server_type)
def EnableRemoteApi(app_id='findit-for-me'): # pragma: no cover """Enable appengine services through remote API. Args: app_id (str): The appengine ID without '.appspot.com', eg. findit-for-me. """ if hasattr(EnableRemoteApi, app_id): return SetTimeoutForUrlOperations() remote_api_stub.ConfigureRemoteApiForOAuth('%s.appspot.com' % app_id, '/_ah/remote_api', secure=True, save_cookies=True) setattr(EnableRemoteApi, app_id, True)
def main(key, url): print "Configuring GAE Remote API on {} to import {}".format(url, key) if 'localhost' in url: remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', local_auth_func, url) else: remote_api_stub.ConfigureRemoteApiForOAuth(url, '/_ah/remote_api') print "Loading data from The Blue Alliance requires an API key" print "Please go to https://thebluealliance.com/account and generate a read API key" apiv3_key = raw_input("Enter your API key: ") global AUTH_TOKEN AUTH_TOKEN = apiv3_key if Match.validate_key_name(key): match_data = fetch_match(key) store_match(match_data) elif Event.validate_key_name(key): event_data = fetch_event(key) event = store_event(event_data) event_teams = fetch_event_detail(key, 'teams') teams = map(store_team, event_teams) map(lambda t: store_eventteam(t, event), teams) event_matches = fetch_event_detail(key, 'matches') map(store_match, event_matches) event_rankings = fetch_event_detail(key, 'rankings') store_eventdetail(event, 'rankings2', event_rankings['rankings'] if event_rankings else []) event_alliances = fetch_event_detail(key, 'alliances') store_eventdetail(event, 'alliance_selections', event_alliances) event_awards = fetch_event_detail(key, 'awards') map(lambda t: store_award(t, event), event_awards) elif Team.validate_key_name(key): team_data = fetch_team(key) store_team(team_data) else: print "Unknown key :("
def flush_memcache(app_name): """Flushes memcache for the server. Args: app_name: str. The name of the GCloud project. Returns: bool. True if memcache is flushed successfully, false otherwise. """ import dev_appserver dev_appserver.fix_sys_path() from google.appengine.ext.remote_api import remote_api_stub from google.appengine.api import memcache remote_api_stub.ConfigureRemoteApiForOAuth('%s.appspot.com' % app_name, '/_ah/remote_api') return memcache.flush_all()
def connect_to_pro_server_by_remote_api(cfg, shell): if hasattr(cfg, "project_id") and cfg.project_id: project_id = cfg.project_id else: click.echo("[Error] Please set project_id in your config file") return if hasattr(cfg, "gae_sdk_path") and cfg.gae_sdk_path: sdk_path = cfg.gae_sdk_path else: sdk_path = "/usr/local/google_appengine" os.environ['SERVER_SOFTWARE'] = 'Development (devshell remote-api)/1.0' os.environ['HTTP_HOST'] = 'localhost' os.environ['GAE_SDK_ROOT'] = sdk_path python_path = subprocess.check_output("which python", shell=True) os.environ['PYTHONPATH'] = os.environ["GAE_SDK_ROOT"] + ':' + python_path try: import dev_appserver dev_appserver.fix_sys_path() except ImportError: click.echo( 'Please make sure the App Engine SDK is in your PYTHONPATH.') raise if hasattr(cfg, "project_path") and cfg.project_path: path = os.path.abspath(os.path.expanduser(cfg.project_path)) sys.path.insert(0, path) if hasattr(cfg, "remote_api_path") and cfg.remote_api_path: remote_entry = cfg.remote_api_path else: remote_entry = "/_ah/remote_api" from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(project_id), remote_entry) click.secho("\nYou are connecting to the production server, be careful!\n", bold=True, fg="red") start_shell(shell, globals(), locals())
def setup_remote(): from google.appengine.tools.devappserver2.devappserver2 import PARSER args = [APP_YAML_FILE, ] args.extend(sys.argv[1:]) options = PARSER.parse_args(args) from google.appengine.tools.devappserver2.devappserver2 import application_configuration configuration = application_configuration.ApplicationConfiguration(options.config_paths, options.app_id) mc = configuration.modules[0] host = '-dot-'.join((mc.major_version, mc.module_name, mc.application_external_name)) + '.appspot.com' if 'HTTP_HOST' not in os.environ: os.environ['HTTP_HOST'] = host os.environ['APPLICATION_ID'] = configuration.app_id from google.appengine.ext.remote_api import remote_api_stub os.environ['SERVER_SOFTWARE'] = 'Development (remote_api)/1.0' remote_api_stub.ConfigureRemoteApiForOAuth(host, '/_ah/remote_api')
def init(keyfile_path, app_id, remote_api_endpoint=DEFAULT_REMOTE_API_ENDPOINT): scopes = remote_api_stub._OAUTH_SCOPES creds = ServiceAccountCredentials.from_json_keyfile_name(keyfile_path, scopes=scopes) oauth2_parameters = appengine_rpc_httplib2.HttpRpcServerOAuth2.OAuth2Parameters( access_token=None, client_id=None, client_secret=None, scope=scopes, refresh_token=None, credential_file=None, credentials=creds) app_url = '{0}.appspot.com'.format(app_id) return remote_api_stub.ConfigureRemoteApiForOAuth( servername=app_url, path=remote_api_endpoint, oauth2_parameters=oauth2_parameters)
def list(self, request): # debug test data # startdatetime = datetime.datetime(2017, 04, 10, 00, 00) # enddatetime = datetime.datetime(2017, 04, 16, 00, 00) # tracker_number = None # 4532071844 # veh_registration = None # 'NotSet' # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle ID from the query string try: vehicle_id = request.query_params['vehicle'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle = Vehicle.objects.get(pk=vehicle_id) veh_registration = vehicle.registration vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) t_key = ndb.Key('tracker', int(tracker_number)) print "timesheet tracker key built" timesheets = [] # Should pull out user configured timezone and working week? # Split time period into working days # Should consider users / vehicle timezone currentdaystart = startdatetime.replace(hour=0, minute=0, second=0, microsecond=0) #endday = enddatetime.date() dayperiod = datetime.timedelta(hours=24) while currentdaystart < enddatetime: # Get all tracker points for this day points = trackerPoint.query_tracePoints( t_key, currentdaystart, currentdaystart + dayperiod).fetch(MAX_POINTS_PER_DAY) print "Retrived points for %s" % (currentdaystart.isoformat()) # Process timesheet day # We consider first key on as start of day - may be other IO inputs that some users would like considering? # If the first point in the day is some sort of driving point then consider start of activity to be midnight? # Last key off as end of day daystartpoint = None dayendpoint = None daydrivestart = None daydrivingduration = datetime.timedelta() for point in points: # debug print event code #print "%s: ec %d" % (point.updateDateTime, point.eventCode) # look for 1st key on if (daystartpoint is None) and (point.eventCode == 4): daystartpoint = point # mark possible final key off if (daystartpoint is not None) and (point.eventCode == 5): dayendpoint = point # find individual trip fragments and sum driving time if point.eventCode == 4: daydrivestart = point if (daydrivestart is not None) and (point.eventCode == 5): daydrivingduration = daydrivingduration + ( point.updateDateTime - daydrivestart.updateDateTime) daydrivestart = None if (daystartpoint is not None) and (dayendpoint is not None): print "start %s" % (daystartpoint.updateDateTime.isoformat()) print "end %s" % (dayendpoint.updateDateTime.isoformat()) daystart_location = reverseGeocode( daystartpoint.lat / 10000000.0, daystartpoint.lon / 10000000.0) dayend_location = reverseGeocode(dayendpoint.lat / 10000000.0, dayendpoint.lon / 10000000.0) timesheet = self.processTimeSheet(vehicle_id, veh_registration, currentdaystart.date(), daystartpoint, daystart_location, dayendpoint, dayend_location, daydrivingduration) timesheets.append(timesheet) else: timesheet = TimeSheetRow( vehicleId=vehicle_id, vehicleName=veh_registration, date=currentdaystart.date().isoformat(), driverId='0', driveName='Unknown', startDateTime=currentdaystart, startLocationId=0, startLocationName='None', endDateTime=currentdaystart, endLocationId=0, endLocationName='None', duration=datetime.timedelta(), distance=0, durationDriving=datetime.timedelta()) timesheets.append(timesheet) print "no activity for this day" currentdaystart = currentdaystart + dayperiod serializer = serializers.TimeSheetRowSerializer(instance=timesheets, many=True) return Response(serializer.data)
def list(self, request): print(' ### user: {0} ###').format(request.user) print(' &&& auth: {0} &&&').format(request.auth) # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle ID from the query string try: vehicle_id = request.query_params['vehicle'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle = Vehicle.objects.get(pk=vehicle_id) veh_registration = vehicle.registration vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) except IndexError: tracker_number = 0 # no trackers attached to vehicle, so just look for a fake one try: t_key = ndb.Key('tracker', int(tracker_number)) #print "tracker key built" # Get all tracker points for this period points = trackerPoint.query_tracePoints( t_key, startdatetime, enddatetime).fetch(MAX_POINTS_PER_DAY) except ValueError: # Catch bad keys for trackers that don't exist points = [] # fix up points # Can't store the lat/lon float result as model field is an int! # Could stiore in a new field and only serialize that, but convert in presentaion for now. #for point in points: # point.lat = point.lat / 10000000.0 # point.lon = point.lon / 10000000.0 # point.speed = (point.speed * 36.0) / 1000.0; # convert from cm/second to km/hour. !! JWF Sends as 'int' possible loss of precision !! # fix up missing accum0 from 3 wire trackers? # also fill in GPS ODO # print "fixing up accum0" for point in points: point.accum0 = point.getODO() #if not hasattr(point, 'accum0'): # point.accum0 = 0 serializer = serializers.TrackerPointSerializer(instance=points, many=True) return Response(serializer.data)
def list(self, request): #print(' ### user: {0} ###').format(request.user) #print(' &&& auth: {0} &&&').format(request.auth) # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae #print('NDB configured') groups = request.user.vehicle_groups.all().select_related( 'customer').prefetch_related('vehicles').defer( 'vehicles__trackers', 'vehicles__customer') # look up the latest tracker points for group in groups: print(group) for vehicle in group.vehicles.all(): print(vehicle.registration) try: vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker except IndexError: tracker_number = 0 # no trackers attached to vehicle, so just look for a fake one #print(tracker_number) try: t_key = ndb.Key('tracker', int(tracker_number)) points = trackerPoint.query_trackerPoint(t_key).fetch(1) except ValueError: # 0 tracker number doesn't exist so datastore will complain about the key points = [] if len(points) > 0: print(points[0]) vehicle.driverLabel = "No Driver ID" vehicle.lat = points[0].lat / 10000000.0 vehicle.lon = points[0].lon / 10000000.0 vehicle.dateTime = points[0].updateDateTime vehicle.eventCode = points[0].eventCode vehicle.speed = (points[0].speed * 36.0) / 1000.0 # convert from cm/second to km/hour. !! JWF Sends as 'int' possible loss of precision !! # round((vehicle.trackPoint.speed*36) / 1600); vehicle.heading = points[0].heading location = reverseGeocode(points[0].lat / 10000000.0, points[0].lon / 10000000.0) vehicle.locationName = location['formatted_address'] vehicle.locationId = location['place_id'] print points[0].getODO() vehicle.odo = points[0].getODO() / 1000.0 vehicle.messageStatus = 0 #try: # vehicle.odo = points[0].accum0 / 1000.0; # convert from meters to km #except Exception as e: # vehicle.odo = 0; # print "tracker point has no accum0" #round(points[0].accum0 / 1600) # convert from meters to miles else: vehicle.driverLabel = "No Driver ID" vehicle.lat = 0 vehicle.lon = 0 vehicle.dateTime = 0 vehicle.eventCode = 0 vehicle.speed = 0 vehicle.heading = 0 vehicle.locationName = 'None' vehicle.locationId = '0' vehicle.odo = 0 vehicle.messageStatus = 0 serializer = serializers.TrackVehicleGroupSerializer(instance=groups, many=True) #print(repr(serializer)) #print(serializer.data) return Response(serializer.data) """
def CMDshell(parser, args): """Opens interactive remote shell with app's GAE environment. Connects to a specific version of a specific module (an active version of 'default' module by default). The app must have 'remote_api: on' builtin enabled in app.yaml. Always uses password based authentication. """ parser.allow_positional_args = True parser.add_option('-H', '--host', help='Only necessary if not hosted on .appspot.com') parser.add_option('--local', action='store_true', help='Operates locally on an empty dev instance') app, options, args = parser.parse_args(args) module = 'default' version = None if len(args) == 2: module, version = args elif len(args) == 1: module = args[0] elif args: parser.error('Unknown args: %s' % args) if module not in app.modules: parser.error('No such module: %s' % module) if not options.host and not options.local: prefixes = filter(None, (version, module, app.app_id)) options.host = '%s.appspot.com' % '-dot-'.join(prefixes) # Ensure remote_api is initialized and GAE sys.path is set. gae_sdk_utils.setup_env(app.app_dir, app.app_id, version, module, remote_api=True) if options.host: # Open the connection. from google.appengine.ext.remote_api import remote_api_stub try: print('If asked to login, run:\n') print( 'gcloud auth application-default login ' '--scopes=https://www.googleapis.com/auth/appengine.apis,' 'https://www.googleapis.com/auth/userinfo.email\n') remote_api_stub.ConfigureRemoteApiForOAuth(options.host, '/_ah/remote_api') except urllib2.URLError: print >> sys.stderr, 'Failed to access %s' % options.host return 1 remote_api_stub.MaybeInvokeAuthentication() def register_sys_path(*path): abs_path = os.path.abspath(os.path.join(*path)) if os.path.isdir(abs_path) and not abs_path in sys.path: sys.path.insert(0, abs_path) # Simplify imports of app modules (with dependencies). This code is optimized # for layout of apps that use 'components'. register_sys_path(app.app_dir) register_sys_path(app.app_dir, 'third_party') register_sys_path(app.app_dir, 'components', 'third_party') # Import some common modules into interactive console namespace. def setup_context(): # pylint: disable=unused-variable from google.appengine.api import app_identity from google.appengine.api import memcache from google.appengine.api import urlfetch from google.appengine.ext import ndb return locals().copy() context = setup_context() # Fancy readline support. if readline is not None: readline.parse_and_bind('tab: complete') history_file = os.path.expanduser('~/.config/gae_tool/remote_api_%s' % app.app_id) if not os.path.exists(os.path.dirname(history_file)): os.makedirs(os.path.dirname(history_file)) atexit.register(lambda: readline.write_history_file(history_file)) if os.path.exists(history_file): readline.read_history_file(history_file) prompt = [ 'App Engine interactive console for "%s".' % app.app_id, 'Available symbols:', ] prompt.extend(sorted(' %s' % symbol for symbol in context)) code.interact('\n'.join(prompt), None, context) return 0
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\yaml\lib') if 'google' in sys.modules: del sys.modules['google'] import dev_appserver dev_appserver.fix_sys_path() except ImportError: print('Please make sure the App Engine SDK is in your PYTHONPATH.') raise from google.appengine.ext import ndb from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format('demo11-1147'), '/_ah/remote_api') # List the first 10 keys in the datastore. keys = ndb.Query().fetch(10, keys_only=True) for key in keys: print(key) # # if __name__ == '__main__': # parser = argparse.ArgumentParser( # description=__doc__, # formatter_class=argparse.RawDescriptionHelpFormatter) # parser.add_argument('project_id', help='demo11-1147') #
def main(project_id): print('Logging into app engine') remote_api_stub.ConfigureRemoteApiForOAuth( '{}.appspot.com'.format(project_id), '/_ah/remote_api') print('Logged in') global_conf = GlobalConfig.get_by_id(config.global_config_id) if not global_conf: GlobalConfig.new() from google.appengine.api import memcache memcache.flush_all() print 'Flushing all memcache' memcache.flush_all() global_conf = tt_logging.LoggingConfig.get_by_id(config.logging_config_id) if not global_conf: tt_logging.LoggingConfig.new() global_conf = GlobalConfig.get_by_id(config.global_config_id) if not global_conf: GlobalConfig.new() wordshare = Group.query(Group.name == 'world') if wordshare.get() is None: wordshare = Group(id='world', name='World Share', description='World Share') wordshare.active = True wordshare.put() print 'Checking for super users' dragonchain = Organization.get_by_id('DragonChain') if dragonchain is None: dragonchain = Organization(id='DragonChain', name='DragonChain', description='DragonChain', domain='dragonchain.com', owner='Joe Roets', webpage='https://dragonchain.com/', point_of_contact='Joe Roets', email='*****@*****.**', phone='+1 111 111 1111', account_type='super_account') dragonchain.groups.append(wordshare.key) dragonchain.put() super_admin = Group.get_by_id('super_admin') if super_admin is None: super_admin = Group(id='super_admin', name='Super Administrator', description='Super Administrator', organization=dragonchain.key) super_admin.active = True super_admin.put() org = Group.query(Group.name == 'DragonChain') if org.get() is None: org = Group(key=Group.create_key(), name='DragonChain', description='DragonChain organization group', organization=dragonchain.key) org.active = True org.organization = dragonchain.key dragonchain.groups.append(org.key) dragonchain.org_group = org.key org.put() tech = Group.query(Group.name == 'Tech') if tech.get() is None: tech = Group(key=Group.create_key(), name='Tech', description='Tech', organization=dragonchain.key) tech.active = True tech.organization = dragonchain.key dragonchain.groups.append(tech.key) tech.put() legal = Group.query(Group.name == 'Legal') if legal.get() is None: legal = Group(key=Group.create_key(), name='Legal', description='Legal', organization=dragonchain.key) legal.active = True legal.organization = dragonchain.key dragonchain.groups.append(legal.key) legal.put() finance = Group.query(Group.name == 'Finance') if finance.get() is None: finance = Group(key=Group.create_key(), name='Finance', description='Finance', organization=dragonchain.key) finance.active = True finance.organization = dragonchain.key dragonchain.hidden_groups.append(finance.key) finance.put() marketing = Group.query(Group.name == 'Marketing') if marketing.get() is None: marketing = Group(key=Group.create_key(), name='Marketing', description='Marketing', organization=dragonchain.key) marketing.active = True marketing.organization = dragonchain.key dragonchain.groups.append(marketing.key) marketing.put() topsec = Group.query(Group.name == 'Top Secret') if topsec.get() is None: topsec = Group(key=Group.create_key(), name='Top Secret', description='Top Secret', organization=dragonchain.key) topsec.active = True topsec.organization = dragonchain.key dragonchain.hidden_groups.append(topsec.key) topsec.put() secret = Group.query(Group.name == 'Secret') if secret.get() is None: secret = Group(key=Group.create_key(), name='Secret', description='Secret', organization=dragonchain.key) secret.active = True secret.organization = dragonchain.key dragonchain.hidden_groups.append(secret.key) secret.put() confid = Group.query(Group.name == 'Confidential') if confid.get() is None: confid = Group(key=Group.create_key(), name='Confidential', description='Confidential', organization=dragonchain.key) confid.active = True tech.organization = dragonchain.key dragonchain.groups.append(confid.key) confid.put() amiller_su = User.get_by_id('amiller_su') if amiller_su is None: print 'Super user amiller not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Andrew', 'last_name': 'Miller', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) amiller_su = User.get_by_id('amiller_su') amiller_su.organization = dragonchain.key amiller_su.require_password_change = True amiller_su.groups = [super_admin.key] amiller_su.put() dragonchain.admins.append(amiller_su.key) baxter_su = User.get_by_id('baxter_su') if baxter_su is None: print 'Super user joe not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Baxter', 'last_name': 'Finch', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) baxter_su = User.get_by_id('baxter_su') baxter_su.groups.append(super_admin.key) baxter_su.organization = dragonchain.key baxter_su.require_password_change = True baxter_su.put() dragonchain.admins.append(baxter_su.key) joe_su = User.get_by_id('joe_su') if joe_su is None: print 'Super user joe not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Joe', 'last_name': 'Roets', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) joe_su = User.get_by_id('joe_su') joe_su.groups.append(super_admin.key) joe_su.organization = dragonchain.key joe_su.require_password_change = True joe_su.put() dragonchain.admins.append(joe_su.key) amiller = User.get_by_id('amiller') if amiller is None: print 'User amiller not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Andrew', 'last_name': 'Miller', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) amiller = User.get_by_id('amiller') amiller.organization = dragonchain.key amiller.require_password_change = True amiller.groups.append(tech.key) amiller.groups.append(legal.key) amiller.put() joe = User.get_by_id('joe') if joe is None: print 'User joe not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Joe', 'last_name': 'Roets', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) joe = User.get_by_id('joe') joe.groups.append(tech.key) joe.groups.append(legal.key) joe.organization = dragonchain.key joe.require_password_change = True joe.put() baxter = User.get_by_id('baxter') if baxter is None: print 'User baxter not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'Baxter', 'last_name': 'Finch', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) baxter = User.get_by_id('baxter') baxter.groups.append(tech.key) baxter.groups.append(legal.key) baxter.organization = dragonchain.key baxter.require_password_change = True baxter.put() bob = User.get_by_id('bob') if bob is None: print 'User bob not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'bob', 'last_name': 'bob', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) bob = User.get_by_id('bob') bob.organization = dragonchain.key bob.require_password_change = True bob.groups.append(tech.key) bob.groups.append(legal.key) bob.put() tom = User.get_by_id('tom') if tom is None: print 'User tom not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'tom', 'last_name': 'tom', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) tom = User.get_by_id('tom') tom.organization = dragonchain.key tom.require_password_change = True tom.groups.append(tech.key) tom.groups.append(legal.key) tom.put() sam = User.get_by_id('sam') if sam is None: print 'User sam not found, creating him now' User.new( { 'username': '******', 'password': '******', 'first_name': 'sam', 'last_name': 'sam', 'email': '*****@*****.**', 'phone_numbers': { 'main': '304-123-1234' } }, verify_email=False, organization=dragonchain, worldshare_group=Group.get_worldshare_key()) sam = User.get_by_id('sam') sam.organization = dragonchain.key sam.require_password_change = True sam.groups.append(tech.key) sam.groups.append(secret.key) sam.put() dragonchain.put()
def list(self, request): # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle Group ID from the query string try: vehicle_group_id = request.query_params['vehicle_group'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle_group = VehicleGroup.objects.get(pk=vehicle_group_id) except VehicleGroup.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group) # In future pull these weights from a config weight_ldw = 17.0 weight_hw = 16.0 weight_ucw = 17.0 weight_fcw = 17.0 weight_pdz = 17.0 weight_s = 16.0 ldw_score_baseline_low = 2.0 hw_score_baseline_low = 20.0 ucw_score_baseline_low = 2.0 fcw_score_baseline_low = 2.0 pdz_score_baseline_low = 2.0 s_score_baseline_low = 2.0 ldw_score_baseline_high = 100.0 hw_score_baseline_high = 300.0 ucw_score_baseline_high = 100.0 fcw_score_baseline_high = 100.0 pdz_score_baseline_high = 100.0 s_score_baseline_high = 100.0 safety_rows = [] average_score = 0.0 total_distance = 0.0 average_ldw_per_1000 = 0.0 total_ldw = 0 average_hw_per_1000 = 0.0 total_hw = 0 average_ucw_per_1000 = 0.0 total_ucw = 0 average_fcw_per_1000 = 0.0 total_fcw = 0 average_pdz_per_1000 = 0.0 total_pdz = 0 average_s_per_1000 = 0.0 total_s = 0 for vehicle in vehicle_group.vehicles.all(): #print(vehicle.registration) vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker #print(tracker_number) t_key = ndb.Key('tracker', int(tracker_number)) points = [] points = trackerPoint.query_tracePoints( t_key, startdatetime, enddatetime).filter( trackerPoint.eventCode.IN( [4, 5, 151, 152, 153, 154, 155, 156, 157])).fetch(MAX_POINTS_PER_DAY) start_odo = 0.0 end_odo = 0.0 ldw_count = 0 ldw_per_1000 = 0.0 hw_count = 0 hw_per_1000 = 0.0 ucw_count = 0 ucw_per_1000 = 0.0 fcw_count = 0 fcw_per_1000 = 0.0 pdz_count = 0 pdz_per_1000 = 0.0 s_count = 0 s_per_1000 = 0.0 score = 0.0 distance = 0.0 if len(points) > 0: for point in points: #print(point) # Look for 1st key on and record ODO if (point.eventCode == 4): if (start_odo == 0): start_odo = point.getODO() #if (point.accum0 != 0): # Use vehicle ODO if it exists # start_odo = point.accum0 #else: # Otherwise use GPS ODO (accum1 where supported) # start_odo = point.accum1 # Look for final key off and record ODO elif (point.eventCode == 5): end_odo = point.getODO() #if (point.accum0 != 0): # Use vehicle ODO if it exists # end_odo = point.accum0 #else: # Otherwise use GPS ODO (accum1 where supported) # end_odo = point.accum1 # Deal with Mobileye activation points elif (point.eventCode == 151): # LDW left ldw_count += 1 elif (point.eventCode == 152): # LDW right ldw_count += 1 elif (point.eventCode == 153): # HW1 hw_count += 1 elif (point.eventCode == 154): # TSR s_count += 1 elif (point.eventCode == 155): # UFCW ucw_count += 1 elif (point.eventCode == 156): # FCW+PCW fcw_count += 1 elif (point.eventCode == 157): # PDZ pdz_count += 1 # Calculate distance if (start_odo != 0) and (end_odo != 0): if (end_odo > start_odo): distance = end_odo - start_odo # in meters # Convert distance to km distance = distance / 1000.0 # Calculate activations per_1000's if (distance > 0): scale = 1000.0 / distance ldw_per_1000 = ldw_count * scale hw_per_1000 = hw_count * scale s_per_1000 = s_count * scale ucw_per_1000 = ucw_count * scale fcw_per_1000 = fcw_count * scale pdz_per_1000 = pdz_count * scale # Calculate scores score += calcScoreComponent(ldw_per_1000, ldw_score_baseline_low, ldw_score_baseline_high, weight_ldw) score += calcScoreComponent(hw_per_1000, hw_score_baseline_low, hw_score_baseline_high, weight_hw) score += calcScoreComponent(s_per_1000, s_score_baseline_low, s_score_baseline_high, weight_s) score += calcScoreComponent(ucw_per_1000, ucw_score_baseline_low, ucw_score_baseline_high, weight_ucw) score += calcScoreComponent(fcw_per_1000, fcw_score_baseline_low, fcw_score_baseline_high, weight_fcw) score += calcScoreComponent(pdz_per_1000, pdz_score_baseline_low, pdz_score_baseline_high, weight_pdz) safety_rows.append( BehaviourSafetyMobileEyeVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=score, distance=distance, ldwPerThousand=ldw_per_1000, ldwCount=ldw_count, hwPerThousand=hw_per_1000, hwCount=hw_count, ucwPerThousand=ucw_per_1000, ucwCount=ucw_count, fcwPerThousand=fcw_per_1000, fcwCount=fcw_count, pdzPerThousand=pdz_per_1000, pdzCount=pdz_count, sPerThousand=s_per_1000, sCount=s_count, )) average_score += score total_distance += distance total_ldw += ldw_count total_hw += hw_count total_ucw += ucw_count total_fcw += fcw_count total_pdz += pdz_count total_s += s_count # Calculate totals if len(safety_rows) > 0: average_score = average_score / len(safety_rows) # Calculate ABC per_1000's if (total_distance > 0): scale = 1000.0 / total_distance average_ldw_per_1000 = total_ldw * scale average_hw_per_1000 = total_hw * scale average_ucw_per_1000 = total_ucw * scale average_fcw_per_1000 = total_fcw * scale average_pdz_per_1000 = total_pdz * scale average_s_per_1000 = total_s * scale safety = BehaviourSafetyMobileEyeVehicle( vehicleGroupId=vehicle_group_id, vehicleGroupName=vehicle_group.name, startDateTime=startdatetime, endDateTime=enddatetime, weightLDW=weight_ldw, weightHW=weight_hw, weightUCW=weight_ucw, weightFCW=weight_fcw, weightPDZ=weight_pdz, weightS=weight_s, averageScore=average_score, totalDistance=total_distance, averageLDWPerThousand=average_ldw_per_1000, totalLDWCount=total_ldw, averageHWPerThousand=average_hw_per_1000, totalHWCount=total_hw, averageUCWPerThousand=average_ucw_per_1000, totalUCWCount=total_ucw, averageFCWPerThousand=average_fcw_per_1000, totalFCWCount=total_fcw, averagePDZPerThousand=average_pdz_per_1000, totalPDZCount=total_pdz, averageSPerThousand=average_s_per_1000, totalSCount=total_s, ) safety.rows = safety_rows serializer = serializers.BehaviourSafetyMobileEyeVehicleSerializer( instance=safety) return Response(serializer.data)
def list(self, request): # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle Group ID from the query string try: vehicle_group_id = request.query_params['vehicle_group'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle_group = VehicleGroup.objects.get(pk=vehicle_group_id) #veh_registration = vehicle.registration #vts = vehicle.trackers.all().order_by('-installed'); #tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group) safety_rows = [] for vehicle in vehicle_group.vehicles.all(): print(vehicle.registration) vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker print(tracker_number) t_key = ndb.Key('tracker', int(tracker_number)) #points = trackerPoint.query_trackerPoint(t_key).fetch(1) safety_rows.append( BehaviourSafetyMobileEyeVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=86.3, distance=459.3, ldwPerThousand=12, ldwCount=6, hwPerThousand=12, hwCount=6, ucwPerThousand=12, ucwCount=6, fcwPerThousand=12, fcwCount=6, pdzPerThousand=12, pdzCount=6, sPerThousand=10, sCount=10, )) safety_rows.append( BehaviourSafetyMobileEyeVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=83.3, distance=225.3112, ldwPerThousand=13, ldwCount=6, hwPerThousand=16.22, hwCount=6, ucwPerThousand=16, ucwCount=6, fcwPerThousand=13, fcwCount=6, pdzPerThousand=19.333, pdzCount=12, sPerThousand=10, sCount=10, )) safety_rows.append( BehaviourSafetyMobileEyeVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=70.3, distance=459.3, ldwPerThousand=16, ldwCount=8, hwPerThousand=19, hwCount=6, ucwPerThousand=21, ucwCount=9, fcwPerThousand=12, fcwCount=6, pdzPerThousand=20, pdzCount=6, sPerThousand=10, sCount=10, )) safety = BehaviourSafetyMobileEyeVehicle( vehicleGroupId=vehicle_group_id, vehicleGroupName=vehicle_group.name, startDateTime=startdatetime, endDateTime=enddatetime, weightLDW=17.0, weightHW=16.0, weightUCW=16.0, weightFCW=16.0, weightPDZ=16.0, weightS=16.0, averageScore=45.8, totalDistance=234567.6, averageLDWPerThousand=34.6, totalLDWCount=57, averageHWPerThousand=45.34433, totalHWCount=89, averageUCWPerThousand=34.833, totalUCWCount=90, averageFCWPerThousand=23.4, totalFCWCount=234, averagePDZPerThousand=89.5, totalPDZCount=56, averageSPerThousand=56.2, totalSCount=9898, ) safety.rows = safety_rows serializer = serializers.BehaviourSafetyMobileEyeVehicleSerializer( instance=safety) return Response(serializer.data)
def list(self, request): # debug test data # startdatetime = datetime.datetime(2017, 04, 10, 00, 00) # enddatetime = datetime.datetime(2017, 04, 16, 00, 00) # tracker_number = None # 4532071844 # veh_registration = None # 'NotSet' # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle ID from the query string try: vehicle_id = request.query_params['vehicle'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle = Vehicle.objects.get(pk=vehicle_id) veh_registration = vehicle.registration vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) t_key = ndb.Key('tracker', int(tracker_number)) print "tracker key built" # Set up 'trip' vars tripCount = 0 # no trips trips = [] tripStartPoint = None # trip not yet started tripPossibleEndPoint = None # Set up 'stops' vars stopCount = 0 stops = [] # Get all tracker points for this period points = trackerPoint.query_tracePoints( t_key, startdatetime, enddatetime).fetch(MAX_POINTS_PER_DAY) print "Retrived points" for point in points: # debug print event code # print "%s: ec %d" % (point.updateDateTime, point.eventCode) # 'Key Off' and no trip started if (point.eventCode == 5) and (tripStartPoint is None): print "Started on a partial trip" # 'Key On' and no start of trip yet recorded # This will only happen on the 1st trip we find, all others the start # point is found as part of the trip end discovery process. if (point.eventCode == 4) and (tripStartPoint is None): tripStartPoint = point tripStartPoint_location = reverseGeocode( tripStartPoint.lat / 10000000.0, tripStartPoint.lon / 10000000.0) # 'Key Off' and trip has started if (point.eventCode == 5) and (tripStartPoint is not None): tripPossibleEndPoint = point # 'Key On' and trip possible end found if (point.eventCode == 4) and (tripPossibleEndPoint is not None): if ((point.updateDateTime - tripPossibleEndPoint.updateDateTime) < datetime.timedelta(seconds=30)): # short stop, possible start stop ign, ignore tripPossibleEndPoint = None else: # We've found the start of a genuine new trip, so we can # close off the old trip we had. # We've found a real trip (previous possible end point) # Create a new point that is the average of the possible end # and new start locations, then just do one reverse geocode # look up for only this point. # TODO Could make better choices here based on sat count and HDOP tripPossibleEndPoint.lat = (tripPossibleEndPoint.lat + point.lat) / 2 tripPossibleEndPoint.lon = (tripPossibleEndPoint.lon + point.lon) / 2 tripPossibleEndPoint_location = reverseGeocode( tripPossibleEndPoint.lat / 10000000.0, tripPossibleEndPoint.lon / 10000000.0) trip = self.processTrip(vehicle_id, veh_registration, tripCount, tripStartPoint, tripStartPoint_location, tripPossibleEndPoint, tripPossibleEndPoint_location) # add trip to list tripCount += 1 trips.append(trip) stop = self.processStop(stopCount, tripPossibleEndPoint, tripPossibleEndPoint_location, point) # add stop to list stopCount += 1 stops.append(stop) # Mark start of new trip, use the averaged location data we've already looked up tripStartPoint = point tripStartPoint.lat = tripPossibleEndPoint.lat tripStartPoint.lon = tripPossibleEndPoint.lon tripStartPoint_location = tripPossibleEndPoint_location tripPossibleEndPoint = None if tripPossibleEndPoint is not None: # process the final trip that had no following key on to help identify it tripPossibleEndPoint_location = reverseGeocode( tripPossibleEndPoint.lat / 10000000.0, tripPossibleEndPoint.lon / 10000000.0) trip = self.processTrip(vehicle_id, veh_registration, tripCount, tripStartPoint, tripStartPoint_location, tripPossibleEndPoint, tripPossibleEndPoint_location) # add trip to list tripCount += 1 trips.append(trip) print "Processed trips" tripsStops = TripsStops(vehicleId=vehicle_id, vehicleName=veh_registration) tripsStops.trips = trips tripsStops.stops = stops serializer = serializers.TripsStopsSerializer( instance=tripsStops) #TripRowSerializer(instance=trips, many=True) return Response(serializer.data)
def main(args): # type: (argparse.Namespace) -> None try: import dev_appserver dev_appserver.fix_sys_path() except ImportError as e: print('ERROR: %s\n' % (e)) print('Please provide --sdk-root, or make sure App Engine SDK is' ' in your PYTHONPATH') import google.appengine.ext.ndb as ndb from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApiForOAuth(args.server_uri, '/_ah/remote_api', secure=args.secure) class Token(ndb.Model): Secret = ndb.StringProperty() class TestRun(ndb.Model): BrowserName = ndb.StringProperty() BrowserVersion = ndb.StringProperty() OSName = ndb.StringProperty() OSVersion = ndb.StringProperty() Revision = ndb.StringProperty() ResultsURL = ndb.StringProperty() CreatedAt = ndb.DateProperty(auto_now_add=True) # Create empty Token 'upload-token' secret = Token(id='upload-token', Secret='') secret.put() logging.info('Added Token \'upload-token\' with empty secret.') # Add some runs. path = 'http://localhost:8080/static/24278ab617/%s' test_runs = [ TestRun(id='dev-testrun-chrome-63', BrowserName='chrome', BrowserVersion='63.0', OSName='linux', OSVersion='3.16', Revision='24278ab617', FullRevisionHash='24278ab61781de72ed363b866ae6b50b86822b27', ResultsURL=path % 'chrome-63.0-linux-summary.json.gz'), TestRun(id='dev-testrun-edge-15', BrowserName='edge', BrowserVersion='15', OSName='windows', OSVersion='10', Revision='24278ab617', FullRevisionHash='24278ab61781de72ed363b866ae6b50b86822b27', ResultsURL=path % 'edge-15-windows-10-sauce-summary.json.gz'), TestRun(id='dev-testrun-firefox-57', BrowserName='firefox', BrowserVersion='57.0', OSName='linux', OSVersion='*', Revision='24278ab617', FullRevisionHash='24278ab61781de72ed363b866ae6b50b86822b27', ResultsURL=path % 'firefox-57.0-linux-summary.json.gz'), TestRun(id='dev-testrun-safari-10', BrowserName='safari', BrowserVersion='10', OSName='macos', OSVersion='10.12', Revision='24278ab617', FullRevisionHash='24278ab61781de72ed363b866ae6b50b86822b27', ResultsURL=path % 'safari-10-macos-10.12-sauce-summary.json.gz'), ] # type: List[TestRun] for test_run in test_runs: test_run.put() logging.info('Added TestRun %s' % test_run.key.id()) # Also whatever the latest TestRun are. logging.debug('Added TestRun %s' % test_run.key.id()) copier = ProdRunCopier(logging.getLogger()) copier.copy_prod_run('latest')
def list(self, request): # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle Group ID from the query string try: vehicle_group_id = request.query_params['vehicle_group'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle_group = VehicleGroup.objects.get(pk=vehicle_group_id) except VehicleGroup.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) # In future pull these weights from a config weight_a = 20.0 weight_b = 20.0 weight_c = 20.0 weight_s = 20.0 weight_i = 20.0 a_score_baseline_low = 2.0 b_score_baseline_low = 1.0 c_score_baseline_low = 1.0 s_score_baseline_low = 1.0 i_score_baseline_low = 1.0 a_score_baseline_high = 100.0 b_score_baseline_high = 20.0 c_score_baseline_high = 20.0 s_score_baseline_high = 20.0 # percent i_score_baseline_high = 20.0 # percent safety_rows = [] average_score = 0.0 total_distance = 0.0 average_a_per_1000 = 0.0 total_a = 0 average_b_per_1000 = 0.0 total_b = 0 average_c_per_1000 = 0.0 total_c = 0 total_s = 0.0 total_i = datetime.timedelta() for vehicle in vehicle_group.vehicles.all(): #print(vehicle.registration) vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker #print(tracker_number) t_key = ndb.Key('tracker', int(tracker_number)) points = [] points = trackerPoint.query_tracePoints( t_key, startdatetime, enddatetime).filter( trackerPoint.eventCode.IN([4, 5, 40, 41, 42])).fetch(MAX_POINTS_PER_DAY) start_odo = 0.0 end_odo = 0.0 a_count = 0 a_per_1000 = 0.0 b_count = 0 b_per_1000 = 0.0 c_count = 0 c_per_1000 = 0.0 s_distance = 0.0 idle_duration = datetime.timedelta() score = 0.0 distance = 0.0 keyon_time = None duration = datetime.timedelta() # TODO must add duration, idle and speed if len(points) > 0: for point in points: # Look for 1st key on and record ODO # Also record time at each keyon so we can calc driving duration if (point.eventCode == 4): keyon_time = point.updateDateTime if (start_odo == 0): start_odo = point.getODO() # ODO in meters # Look for final key off and record ODO # Also record idle duration for each trip # Also record speeding distnace for trip elif (point.eventCode == 5): end_odo = point.getODO() # ODO in meters idle_duration += datetime.timedelta( seconds=point.getIdleDurationSeconds()) s_distance += point.getSpeedingDistance() if keyon_time is not None: duration += (point.updateDateTime - keyon_time) keyon_time = None # Deal with ABC points elif (point.eventCode == 40): a_count += 1 elif (point.eventCode == 41): b_count += 1 elif (point.eventCode == 42): c_count += 1 # Calculate distance (in meters) if (start_odo != 0) and (end_odo != 0): if (end_odo > start_odo): distance = end_odo - start_odo # in meters # Convert distances to km distance = distance / 1000.0 s_distance = s_distance / 1000.0 # Calculate ABC per_1000's if (distance > 0): scale = 1000.0 / distance a_per_1000 = a_count * scale b_per_1000 = b_count * scale c_per_1000 = c_count * scale # Calculate scores. Eaach component can add up to it's 'weight' to the score score += calcScoreComponent(a_per_1000, a_score_baseline_low, a_score_baseline_high, weight_a) score += calcScoreComponent(b_per_1000, b_score_baseline_low, b_score_baseline_high, weight_b) score += calcScoreComponent(c_per_1000, c_score_baseline_low, b_score_baseline_high, weight_c) if (distance > 0): score += calcScoreComponent((s_distance / distance), s_score_baseline_low, s_score_baseline_high, weight_s) else: score += weight_s if (duration.total_seconds() > 0): score += calcScoreComponent( (idle_duration.total_seconds() / duration.total_seconds()), i_score_baseline_low, i_score_baseline_high, weight_i) else: score += weight_i safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=score, distance=distance, duration=duration, aPerThousand=a_per_1000, aCount=a_count, bPerThousand=b_per_1000, bCount=b_count, cPerThousand=c_per_1000, cCount=c_count, speedingDistance=s_distance, idleDuration=idle_duration, )) average_score += score total_distance += distance total_a += a_count total_b += b_count total_c += c_count total_s += s_distance total_i += idle_duration # Calculate totals if len(safety_rows) > 0: average_score = average_score / len(safety_rows) # Calculate ABC per_1000's if (total_distance > 0): scale = 1000.0 / total_distance average_a_per_1000 = total_a * scale average_b_per_1000 = total_b * scale average_c_per_1000 = total_c * scale safety = BehaviourSafetyABCVehicle( vehicleGroupId=vehicle_group_id, vehicleGroupName=vehicle_group.name, startDateTime=startdatetime, endDateTime=enddatetime, weightA=weight_a, weightB=weight_b, weightC=weight_c, weightS=weight_s, weightI=weight_i, averageScore=average_score, totalDistance=total_distance, averageAPerThousand=average_a_per_1000, totalACount=total_a, averageBPerThousand=average_b_per_1000, totalBCount=total_b, averageCPerThousand=average_c_per_1000, totalCCount=total_c, totalSpeedingDistance=total_s, totalIdleDuration=total_i, ) safety.rows = safety_rows serializer = serializers.BehaviourSafetyABCVehicleSerializer( instance=safety) return Response(serializer.data)
def post(self, request, *args, **kwargs): #def get(self, request, format=None): # Verify user has access to this vehicle # Verify user is loggedin print "post" print(request.data) # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae print "NDB config" try: vehicle_id = request.query_params['vehicle'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_id) try: vehicle = Vehicle.objects.get(pk=vehicle_id) # veh_registration = vehicle.registration vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) # Create outbound message object for listener tm = TrackerOutboundMessage() time_int = calendar.timegm(datetime.datetime.utcnow().timetuple()) tm.key = ndb.Key('trackerOutbound', int(tracker_number), TrackerOutboundMessage, time_int) #datetime.datetime.utcnow().isoformat()) tm.action = 3 # PEG action tm.state = 0 # active tm.userKey = None tm.intParameterList = [8, 1, 0] # Set output, output 1, delayed execution tm.stringParameterList = [] tm.creationDateTime = datetime.datetime.utcnow() td = datetime.timedelta(hours=8) tm.timeoutDateTime = datetime.datetime.utcnow() + td #tm.parent = tracker_key tm.put() # create a new vehicle message entity to track message history #vm_key = ndb.Key(, ) vm = VehicleMessage() vm.key = ndb.Key( 'vehicleMessageBase', int(tracker_number), VehicleMessage, datetime.datetime.utcnow().isoformat( )) # parent should be based on vehicle ID not tracker ID? vm.vehicleKey = None # vk old ndb links vm.customerKey = None # v.customerKey old ndb links vm.currentTracker = int(tracker_number) vm.deliveredDateTime = None vm.acceptedDateTime = None vm.messageStatus = 1 # queued to send vm.put() return Response(status=status.HTTP_200_OK)
def list(self, request): # Only require this for local dev! # Only require this once! if os.environ['APPLICATION_ID'].startswith('dev'): local_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch') remote_api_stub.ConfigureRemoteApiForOAuth( 'datalive-staging.appspot.com', '/_ah/remote_api') apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_stub) print "Remote NDB configured" else: pass # we're uploaded on gae # Get the vehicle Group ID from the query string try: vehicle_group_id = request.query_params['vehicle_group'] except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group_id) # Get dates from query string try: startdatestring = request.query_params['start'] startdatetime = datetime.datetime.strptime(startdatestring, DATE_FORMAT_RFC_3339) enddatestring = request.query_params['end'] enddatetime = datetime.datetime.strptime(enddatestring, DATE_FORMAT_RFC_3339) print(startdatetime) print(enddatetime) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) try: vehicle_group = VehicleGroup.objects.get(pk=vehicle_group_id) #veh_registration = vehicle.registration #vts = vehicle.trackers.all().order_by('-installed'); #tracker_number = vts[0].tracker except Vehicle.DoesNotExist: return Response(status=status.HTTP_400_BAD_REQUEST) except exceptions.MultipleObjectsReturned: return Response(status=status.HTTP_400_BAD_REQUEST) print(vehicle_group) safety_rows = [] for vehicle in vehicle_group.vehicles.all(): print(vehicle.registration) vts = vehicle.trackers.all().order_by('-installed') tracker_number = vts[0].tracker print(tracker_number) t_key = ndb.Key('tracker', int(tracker_number)) #points = trackerPoint.query_trackerPoint(t_key).fetch(1) print(safety_rows) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=86.3, distance=459.3, duration=datetime.timedelta(), aPerThousand=12, aCount=6, bPerThousand=12, bCount=6, cPerThousand=12, cCount=6, speedingDistance=120, idleDuration=datetime.timedelta(), )) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=74.3, distance=387.3, duration=datetime.timedelta(), aPerThousand=15, aCount=8, bPerThousand=12, bCount=6, cPerThousand=16, cCount=7, speedingDistance=150, idleDuration=datetime.timedelta(), )) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=74.3, distance=387.3, duration=datetime.timedelta(), aPerThousand=15, aCount=8, bPerThousand=12, bCount=6, cPerThousand=16, cCount=7, speedingDistance=150, idleDuration=datetime.timedelta(), )) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=73.1, distance=329.2, duration=datetime.timedelta(), aPerThousand=18, aCount=7, bPerThousand=19, bCount=9, cPerThousand=16, cCount=4, speedingDistance=150, idleDuration=datetime.timedelta(), )) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=72.1, distance=319.2, duration=datetime.timedelta(), aPerThousand=13, aCount=6, bPerThousand=21, bCount=9, cPerThousand=14, cCount=4, speedingDistance=110, idleDuration=datetime.timedelta(), )) safety_rows.append( BehaviourSafetyABCVehicleRow( vehicleId=vehicle.id, vehicleName=vehicle.registration, score=68.1, distance=319.2, duration=datetime.timedelta(), aPerThousand=19, aCount=12, bPerThousand=21, bCount=9, cPerThousand=14, cCount=4, speedingDistance=180, idleDuration=datetime.timedelta(), )) safety = BehaviourSafetyABCVehicle( vehicleGroupId=vehicle_group_id, vehicleGroupName=vehicle_group.name, startDateTime=startdatetime, endDateTime=enddatetime, weightA=20.0, weightB=20.0, weightC=20.0, weightS=20.0, weightI=20.0, averageScore=50.0, totalDistance=23564.7, averageAPerThousand=12.6, totalACount=197, averageBPerThousand=2.7, totalBCount=56, averageCPerThousand=56.4, totalCCount=56, totalSpeedingDistance=4503.7, totalIdleDuration=datetime.timedelta(), ) safety.rows = safety_rows serializer = serializers.BehaviourSafetyABCVehicleSerializer( instance=safety) return Response(serializer.data)