def to_list(self, _columns): """similar to to_list including dataId resolver. """ assert isinstance(_columns, Columns) l = [] for column in _columns: assert isinstance(column, Column) if column.isDataField(): data_id = getattr(self, column.getId()) if data_id is None: l.append(None) continue assert isinstance(data_id, int) data = Data.getByDataId(data_id) assert isinstance(data, Data) assert isinstance(data.field, unicode) l.append(data.field) continue if column.isDataString(): data_id = getattr(self, column.getId()) if data_id is None: l.append(None) continue assert isinstance(data_id, int) data = Data.getByDataId(data_id) assert isinstance(data, Data) assert isinstance(data.string, unicode) l.append(data.string) continue l.append(getattr(self, column.getId())) return l
def register(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() request = jrequest.request assert isinstance(request, Request) if len(request.params) < 3: raise InvalidParams( "method=register&x=y&z=w&moduleId=mid&sensorId=sid&sensorName=mysensor where x is product field, y is product string, z is serial field, is serial string" ) assert request.params.items()[0][0] == "method" assert request.params.items()[0][1] == "register" (product_name_field, product_name_string) = request.params.items()[1] (serial_number_field, serial_number_string) = request.params.items()[2] assert isinstance(product_name_field, str) product_name_field = unicode(product_name_field) assert isinstance(product_name_field, unicode) assert isinstance(serial_number_field, str) serial_number_field = unicode(serial_number_field) assert isinstance(serial_number_string, unicode) try: product_name_data = Data.prepare(product_name_field, product_name_string) serial_number_data = Data.prepare(serial_number_field, serial_number_string) module_id_string = request.get("moduleId") assert len(module_id_string) > 0 sensor_id_string = request.get("sensorId") assert len(sensor_id_string) > 0 module_id_data = Data.prepare("moduleId", request.get("moduleId")) sensor_id_data = Data.prepare("sensorId", request.get("sensorId")) existing_sensor = Sensor.getByProductNameSerialNumberModuleIdSensorId( product_name_data, serial_number_data, module_id_data, sensor_id_data) raise EntityExists( "Equipment", { product_name_field: product_name_string, serial_number_field: serial_number_string, "moduleId": module_id_string, "sensorId": sensor_id_string }) except EntityNotFound: pass odenki_user = OdenkiUser.loadFromSession() assert isinstance(odenki_user, OdenkiUser) sensor_name = request.get("sensorName") if len(sensor_name) == 0: sensor_name = unicode(sensor_name) sensor_name_data = Data.prepare("sensorName", sensor_name) sensor = Sensor.create(product_name_data, serial_number_data, module_id_data, sensor_id_data, sensor_name_data, odenki_user) jresponse.addResult(sensor)
def testSucceeded(self): keys = Data.fetchByField("a") self.assertEqual(len(keys), 0) response = self.testapp.get("/post?a=bb&c=dd&a=x") self.assertEqual(json.loads(response.body), [{"dataId": 4, "field": "a", "string": "bb"}, {"dataId": 5, "field": "a", "string": "x"}, {"dataId": 6, "field": "c", "string": "dd"}]) keys = Data.fetchByField("a") self.assertEqual(len(keys), 2) self.assertTrue(keys[0].get().field == "a") self.assertTrue(keys[1].get().field == "a") self.assertTrue(keys[0].get().string in ["bb", "x"]) self.assertTrue(keys[1].get().string in ["bb", "x"])
def GET(self, jrequest, jresponse): LIMIT = 100 assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: start = int(jrequest.getValue("start")[0]) end = int(jrequest.getValue("end")[0]) except: jresponse.setError(JsonRpcError.INVALID_PARAMS, "start and end are mandatory") return query = Data.queryRange(start, end) keys = query.fetch(keys_only=True, limit=LIMIT) for key in keys: data = key.get() assert isinstance(data, Data) query_for_duplicated_data = data.queryDuplication() assert isinstance(query_for_duplicated_data, ndb.Query) keys_for_duplicated_data = query_for_duplicated_data.fetch( keys_only=True) if len(keys_for_duplicated_data) <= 1: continue jresponse.addResult([ data.dataId, data.field, data.string, getCanonicalData(key).get().dataId ]) jresponse.setExtraValue("limit", LIMIT)
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() product_name = jrequest.getPathInfo(3) serial_number = jrequest.getPathInfo(4) module_id = jrequest.getPathInfo(5) field = jrequest.getPathInfo(6) product_name_data = Data.getByFieldAndString("productName", product_name) serial_number_data = Data.getByFieldAndString("serialNumber", serial_number) module_id_data = Data.getByFieldAndString("moduleId", module_id) relays = Relays(product_name, serial_number, module_id) jresponse.addResult(relays)
def PUT(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) query_string = urlparse(self.request.url)[4] debug(query_string) if len(query_string) > 0: keys = Data.putParams(query_string) self.response.out.write("keys = %s" % keys) return
def PUT(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) query_string = urlparse(self.request.url)[4] #debug(query_string) if len(query_string) > 0: keys = Data.putParams(query_string) self.response.out.write("keys = %s" % keys) return
def testSucceeded(self): d = Data.prepare("f1", "s1") data_id = d.dataId self.assertEqual(data_id, 1) response = self.testapp.get("/api/Data/dataId/%s" % data_id) j = simplejson.loads(response.body) #print(j) self.assertEqual(j["result"][0]["field"], "f1") self.assertEqual(j["result"][0]["string"], "s1") self.assertEqual(j["result"][0]["dataId"], 1)
def testSucceeded(self): d = Data.prepare("f1", "s1") data_id = d.dataId self.assertEqual(data_id, 1) response = self.testapp.get("/api/Data/dataId/%s" % data_id) j = simplejson.loads(response.body) # print(j) self.assertEqual(j["result"][0]["field"], "f1") self.assertEqual(j["result"][0]["string"], "s1") self.assertEqual(j["result"][0]["dataId"], 1)
def _getDataListByArduinoId(arduino_id): arduino_id = unicode(arduino_id) MEMCACHE_KEY = "kjasnbargasenanviajfiafjjoi" + arduino_id client = Client() data_list = client.get(MEMCACHE_KEY) if not isinstance(data_list, list): query = Data.queryByFieldAndString("arduinoid", arduino_id) data_list = query.fetch(keys_only=True) client.set(MEMCACHE_KEY, data_list) return data_list
def setUp(self): import webtest from google.appengine.ext import webapp #app = webapp2.WSGIApplication([map]) #from lib.gae import run_wsgi_app #app = run_wsgi_app(UrlMap.UrlMap) wsgi_application = webapp.WSGIApplication(post.paths, debug=True) self.testapp = webtest.TestApp(wsgi_application) from google.appengine.ext import testbed self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.data1 = Data.prepare("f1", "s1") self.data1 = Data.prepare("f1", "s11") self.data2 = Data.prepare("f2", "s2")
def testFailed(self): d = Data.prepare("f1", "s1") data_id = d.dataId self.assertEqual(data_id, 1) response = self.testapp.get("/api/Data/dataId/%s" % (data_id + 1), status=500) # print(response.body) json_object = simplejson.loads(response.body) self.assertEqual(json_object["error"]["code"], -32099) self.assertIsNone(json_object["error"]["data"]) self.assertIsNone(json_object["error"]["name"])
def testFailed(self): d = Data.prepare("f1", "s1") data_id = d.dataId self.assertEqual(data_id, 1) response = self.testapp.get("/api/Data/dataId/%s" % (data_id + 1), status=500) #print(response.body) json_object = simplejson.loads(response.body) self.assertEqual(json_object["error"]["code"], -32099) self.assertIsNone(json_object["error"]["data"]) self.assertIsNone(json_object["error"]["name"])
def eliminateUnusedData(self): query = Data.queryRange(self.currentDataId, self.end) for data_key in query.iter(keys_only=True): data = data_key.get() if data: self.currentDataId = data_key.get().dataId metadata_query = Metadata.queryByData(data_key) metadata_key = metadata_query.get(keys_only=True) if metadata_key: continue #info("deleting data_key=%s (data_id = %s)" % (data_key, data_key.get().dataId)) self.nDeleted += 1 data_key.delete()
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: arduinoid = unicode(jrequest.getPathInfo(3)) debug("arduinoid = %s" % arduinoid) data_keys = Data.fetchByFieldAndString("arduinoid", arduinoid) debug("data_keys = %s" % data_keys) except Exception, e: jresponse.setErrorInvalidParameter(e) return
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: data_id = int(jrequest.getPathInfo(4)) data_key = DataNdb.getByDataId(data_id) if data_key is None: raise RuntimeError("Data entity with dataId %s was not found" % data_id) assert isinstance(data_key, ndb.Key) except Exception, e: jresponse.setErrorInvalidParameter(e) return
def testSucceeded(self): keys = Data.fetchByField("a") self.assertEqual(len(keys), 0) response = self.testapp.get("/post?a=bb&c=dd&a=x") self.assertEqual(json.loads(response.body), [{ "dataId": 4, "field": "a", "string": "bb" }, { "dataId": 5, "field": "a", "string": "x" }, { "dataId": 6, "field": "c", "string": "dd" }]) keys = Data.fetchByField("a") self.assertEqual(len(keys), 2) self.assertTrue(keys[0].get().field == "a") self.assertTrue(keys[1].get().field == "a") self.assertTrue(keys[0].get().string in ["bb", "x"]) self.assertTrue(keys[1].get().string in ["bb", "x"])
def register(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() request = jrequest.request assert isinstance(request, Request) if len(request.params) < 3: raise InvalidParams("method=register&x=y&z=w&moduleId=mid&sensorId=sid&sensorName=mysensor where x is product field, y is product string, z is serial field, is serial string") assert request.params.items()[0][0] == "method" assert request.params.items()[0][1] == "register" (product_name_field, product_name_string) = request.params.items()[1] (serial_number_field, serial_number_string) = request.params.items()[2] assert isinstance(product_name_field, str) product_name_field = unicode(product_name_field) assert isinstance(product_name_field, unicode) assert isinstance(serial_number_field, str) serial_number_field = unicode(serial_number_field) assert isinstance(serial_number_string, unicode) try: product_name_data = Data.prepare(product_name_field, product_name_string) serial_number_data = Data.prepare(serial_number_field, serial_number_string) module_id_string = request.get("moduleId") assert len(module_id_string) > 0 sensor_id_string = request.get("sensorId") assert len(sensor_id_string) > 0 module_id_data = Data.prepare("moduleId", request.get("moduleId")) sensor_id_data = Data.prepare("sensorId", request.get("sensorId")) existing_sensor = Sensor.getByProductNameSerialNumberModuleIdSensorId(product_name_data, serial_number_data, module_id_data, sensor_id_data) raise EntityExists("Equipment", {product_name_field:product_name_string, serial_number_field: serial_number_string, "moduleId":module_id_string, "sensorId":sensor_id_string}) except EntityNotFound: pass odenki_user = OdenkiUser.loadFromSession() assert isinstance(odenki_user, OdenkiUser) sensor_name = request.get("sensorName") if len(sensor_name) == 0: sensor_name = unicode(sensor_name) sensor_name_data = Data.prepare("sensorName", sensor_name) sensor = Sensor.create(product_name_data, serial_number_data, module_id_data, sensor_id_data, sensor_name_data, odenki_user) jresponse.addResult(sensor)
class Field(JsonRpcDispatcher): def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: field = unicode(jrequest.getPathInfo(3)) except Exception, e: jresponse.setErrorInvalidParameter(e) return data_keys = Data.fetchByField(field) jresponse.setResult([]) for data_key in data_keys: data = data_key.get() jresponse.addResult(data)
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: data_id = int(jrequest.getPathInfo()[3]) except: return data_key = Data.getByDataId(data_id) if data_key is None: jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN, "dataId %s not found" % data_id) return data = data_key.get() assert isinstance(data, Data) jresponse.addResult(data) jresponse.setExtraValue("key_id", data.key.id())
def GET(self, jrequest, jresponse): assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() path_info = self.request.path_info.split("/") start = int(path_info[3]) end = int(path_info[4]) query = Data.queryRange(start, end) #query = query.filter(Data.dataId >= start) #query = query.filter(Data.dataId <= end) keys = query.fetch(keys_only=True) for key in keys: entity = key.get() assert isinstance(entity, Data) jresponse.addResult(entity)
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: limit = int(jrequest.getValue("limit")) except: limit = 100 jresponse.setExtraValue("limit", limit) query = Data.queryRecent() keys = query.fetch(keys_only=True, limit=limit) for k in keys: assert isinstance(k, ndb.Key) e = k.get() assert isinstance(e, Data) jresponse.addResult(e)
class FieldAndString(JsonRpcDispatcher): def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: field = unicode(jrequest.getPathInfo(3)) string = unicode(jrequest.getPathInfo(4)) except Exception, e: jresponse.setErrorInvalidParameter(e) return data_keys = Data.fetchByFieldAndString(field, string) if data_keys is None or len(data_keys) == 0: raise EntityNotFound() for data_key in data_keys: data = data_key.get() jresponse.addResult(data)
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: data_id_string = jrequest.getPathInfo(4) data_id = int(data_id_string) except: return try: data = Data.getByDataId(data_id) except: jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN, "dataId %s not found" % data_id) return #data = data_key.get() assert isinstance(data, Data) jresponse.addResult(data) jresponse.setExtraValue("key_id", data.key.id())
def GET(self, jrequest, jresponse): LIMIT = 100 assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: start = int(jrequest.getValue("start")[0]) end = int(jrequest.getValue("end")[0]) except: jresponse.setError(JsonRpcError.INVALID_PARAMS, "start and end are mandatory") return query = Data.queryRange(start, end) keys = query.fetch(keys_only=True, limit=LIMIT) for key in keys: data = key.get() assert isinstance(data, Data) query_for_duplicated_data = data.queryDuplication() assert isinstance(query_for_duplicated_data, ndb.Query) keys_for_duplicated_data = query_for_duplicated_data.fetch(keys_only=True) if len(keys_for_duplicated_data) <= 1: continue jresponse.addResult([data.dataId, data.field, data.string, getCanonicalData(key).get().dataId]) jresponse.setExtraValue("limit", LIMIT)
class _OneDay(JsonRpcDispatcher): def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: arduino_id = unicode(jrequest.getPathInfo(3)) year = int(jrequest.getPathInfo(4)) month = int(jrequest.getPathInfo(5)) day = int(jrequest.getPathInfo(6)) hour = int(jrequest.getPathInfo(7)) except Exception, e: jresponse.setError( JsonRpcError.INVALID_REQUEST, "Try /record/Arduino/<arduinoid>/<year>/<month>/<day> %s." % e.message()) return try: data_keys = Data.fetchByFieldAndString("arduinoid", arduino_id) except Exception, e: jresponse.setErrorInvalidParameter(e) return
def tearDown(self): keys = Data.query().fetch(keys_only=True) for key in keys: key.delete_async() self.testbed.deactivate()
def delete_data(start, end): query = DataNdb.queryRange(start, end) keys = query.fetch(keys_only=True) for key in keys: key.delete()