import requests import traceback import datetime import time from elasticsearch import helpers, Elasticsearch auth_token ='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8va2Fycm9zdGVjaC5pbyIsInN1YiI6ImRjYjA5YmQzLTFmZjAtNGY5ZC05ZGYzLWNjMzk5YWJmYzgwZiIsInBlcnNvbklkIjpudWxsLCJpYXQiOjE1MzE4MTk5MDgsImV4cCI6MTU2MzM1NjAwNywiYXVkIjoiMDg0NGM3NzY1MzBhYzRkYTA3ZmRiYmUzZTU4OGVlNjQyYTEzNjU3ZSIsInNjb3BlIjpbIio6RWR1bG9nOkFkbWluIiwiKjpLYXJyb3M6QWRtaW4iXSwiYXV0aG9yaXphdGlvbiI6eyJncm91cHMiOlt7Imdyb3VwTmFtZSI6IkVkdWxvZyIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IkRldmVsb3BlciIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IkthcnJvcyIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IlNjaG9vbCIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IlBhcmVudCIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IlRlbmFudCIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfSx7Imdyb3VwTmFtZSI6IkRyaXZlciIsInJvbGVzIjpbIlN5c3RlbSBBZG1pbiJdfV19fQ.OC_G1MsYmrbUKXgqGC8PRa5KUnL070DKCnTJMK-wzXo' hed = {'Authorization': 'Bearer ' + auth_token, 'ApiKey': '0844c776530ac4da07fdbbe3e588ee642a13657e'} def migrateStudentAccessRequests(): data = {} page = 0 size = 100 while True: print("page: %s, size: %s") %(page, size) GET_TENANT_URL = 'https://localhost:6543/api/v2.0old/tenants/search?page=%i&size=%i' %(page, size) CREATE_TENANT_URL = "https://localhost:6543/api/v2.0/tenants" response = requests.get(GET_TENANT_URL, json=data, headers=hed) tenantResponse = response.json() if not tenantResponse["content"]["data"]: break if not tenantResponse["serviceCode"] and tenantResponse["content"]["data"]: for tenant in tenantResponse["content"]["data"]: try: print('createing tenant with id: ' + tenant['id']) createTenantResponse = requests.post(CREATE_TENANT_URL, json=tenant, headers=hed) if createTenantResponse.status_code != 201 and createTenantResponse.status_code != 200: print('create tenant failed with status_code: ' + str(createTenantResponse.status_code)) else: print('create tenant successed') except: print('An error occured when create tenant with id: ' + tenant['id']) traceback.print_exc() page += 1 def migrateSchoolsViaTenantService(): page = 0 size = 10000 data = {} data["size"] = 10000 GET_SCHOOL_URL = 'https://localhost:6543/api/v2.0old/schools/search?page=%i&size=%i' %(page, size) CREATE_SCHOOL_URL = "https://localhost:6543/api/v1/schools" print("start time") print(datetime.datetime.now()) es = Elasticsearch('https://vpc-p01ase1dev-vppzojfv77kazi26fdojnrst6e.ap-southeast-1.es.amazonaws.com') while True: data["page"] = page GET_SCHOOL_URL = 'https://localhost:6543/api/v2.0old/schools/search?page=%i&size=%i' %(page, 10000) schoolResponse = requests.post(GET_SCHOOL_URL, json=data, headers=hed).json() if schoolResponse["serviceCode"] or not schoolResponse["schools"]: break for school in schoolResponse["schools"]: try: #print('createing school with id: ' + school['id']) createSchoolResponse = requests.post(CREATE_SCHOOL_URL, json=school, headers=hed) if createSchoolResponse.status_code != 201 and createSchoolResponse.status_code != 200: print('create school failed with status_code: ' + str(createSchoolResponse.status_code)) #else: # print('create school successed') except: print('An error occured when create school with id: ' + school['id']) traceback.print_exc() page += 1 print("end time") print(datetime.datetime.now()) def transformSchoolAddress(school): if 'address' in school and school['address']: address = school['address'] addresskeys = ['address1', 'address2', 'aptNo', 'country', 'city', 'state', 'postalCode'] for key in addresskeys: if key in address and address[key]: school[key] = address[key] else: school[key] = None del school['address'] def transformSchoolDistrict(school): if 'districtId' in school and school['districtId'] and str(school['districtId']) == '': school['districtId'] = None if 'districtName' in school and school['districtName'] and str(school['districtName']) == '': school['districtId'] = None def transformSchoolToSchoolIndex(school): school['_id'] = school['id'] if( school['latitude'] and school['longitude']): school['location'] = ({'lat': school['latitude'], 'lon': school['longitude']}) else: school['location'] = None del school['latitude'] del school['longitude'] if str(school['tenantId']) == "": school['tenantId'] = None if str(school['tenantName']) == "": school['tenantName'] = None currentTime = int(round(time.time() * 1000)) if (not 'createdDatetime' in school) or (not school['createdDatetime']): school['createdDatetime'] = currentTime if (not 'updatedDatetime' in school) or (not school['updatedDatetime']): school['updatedDatetime'] = currentTime transformSchoolAddress(school) transformSchoolDistrict(school) def migrateSchoolsDirectToElasticSearch(): page = 0 size = 10000 data = {} data["size"] = 10000 GET_SCHOOL_URL = 'https://localhost:6543/api/v2.0old/schools/search?page=%i&size=%i' %(page, size) CREATE_SCHOOL_URL = "https://localhost:6543/api/v1/schools" print("start time") print(datetime.datetime.now()) es = Elasticsearch('https://vpc-p01ase1dev-vppzojfv77kazi26fdojnrst6e.ap-southeast-1.es.amazonaws.com') while True: data["page"] = page GET_SCHOOL_URL = 'https://localhost:6543/api/v2.0old/schools/search?page=%i&size=%i' %(page, 10000) schoolResponse = requests.post(GET_SCHOOL_URL, json=data, headers=hed).json() schools = schoolResponse["schools"] if schoolResponse["serviceCode"] or not schools: break try: for school in schools: transformSchoolToSchoolIndex(school) helpers.bulk(es, schools, index='school', doc_type='doc') except: print('An error occured when bulk create school') traceback.print_exc() page += 1 print("end time") print(datetime.datetime.now()) migrateStudentAccessRequests() # migrateSchoolsDirectToElasticSearch()
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more