Example #1
0
	def setApplication(self, application_key, json):
		session = models.getSession()
		application = session.query(models.Application).filter_by(key=application_key).first()
		if application:
			models.updateObjectWithJSON(application, json, ['key'])
			session.commit()
		return application
Example #2
0
	def getApplicationSecret(self, application_key):
		session = models.getSession()
		application = session.query(models.Application).filter_by(key = application_key).first()
	
		if application != None:
			return application.secret
		return None
Example #3
0
	def updateGood(self, good_key, json):
		session = models.getSession()
		good = session.query(models.Good).filter_by(key=good_key).first()
		if good:
			models.updateObjectWithJSON(good, json, ['key', 'application_key'])
			session.commit()
		return good
Example #4
0
	def exportFile(self, application, fromDate, toDate, file):
		session = models.getSession()
		q = session.query(models.Events).filter(models.Events.application_key==application, models.Events.created>=fromDate, models.Events.created<toDate)
		
		for row in q:	
			file.write(extras.toJSON(row.as_dict())+'\n')
			file.flush()
Example #5
0
	def addPrices(self, application_key, engine, data, path):
		session = models.getSession()
		prices = models.Prices(application_key = application_key, engine=engine, data=data, path=path)
		pricing.PricingEngine.validate(prices)
		session.add(prices)
		session.commit()
		return prices
Example #6
0
	def setABTest(self, application_key, json):
		session = models.getSession()

		abtest = self.getABTest(application_key)

		if abtest:

			newABTest = models.ABTest()
			newABTest.application_key = abtest.application_key
			newABTest.countryWhiteList = abtest.countryWhiteList
			newABTest.countryBlackList = abtest.countryBlackList
			newABTest.modulus = abtest.modulus
			newABTest.modulusLimit = abtest.modulusLimit
			newABTest.groupAPrices_key = abtest.groupAPrices_key
			newABTest.groupBPrices_key = abtest.groupBPrices_key

			# Check price foreign keys manually
			for prices in ['groupAPrices_key', 'groupBPrices_key']:
				if prices in json and json[prices] != None:
					count = session.query(models.Prices).join(models.Application).filter(models.Application.key == newABTest.application_key).filter(models.Prices.key==json[prices]).count()
					if count == 1:
						pass
					else:
						del json[prices]
			
			models.updateObjectWithJSON(newABTest, json, ['key'])
			session.add(newABTest)
			session.commit()

			return newABTest 
Example #7
0
    def getGood(self, key):
        session = models.getSession()
        good = session.query(models.Good).filter_by(key=key).first()

        cacheObject(good)
        session.close()

        return good
Example #8
0
    def deleteGood(self, good_key):
        session = models.getSession()
        res = session.query(models.Good).filter_by(key=good_key).delete()
        session.commit()

        session.close()

        return res
Example #9
0
    def getPrice(self, prices_key):
        session = models.getSession()
        price = session.query(models.Prices).filter_by(key=prices_key).first()

        cacheObject(price)
        session.close()

        return price
Example #10
0
    def getApplicationExists(self, application_key):
        session = models.getSession()
        app = session.query(
            models.Application).filter_by(key=application_key).count()

        session.close()

        return app
Example #11
0
	def setMetrics(self, application_key, json):
		session = models.getSession()

		metrics = session.query(models.Metrics).join(models.Application).filter(models.Application.key == application_key).first()
		if metrics:
			models.updateObjectWithJSON(metrics, json, ['key'])
			session.commit()
		return metrics 
Example #12
0
	def setCurrency(self, application_key, json):
		session = models.getSession()
		currencies = session.query(models.Currencies).join(models.Application).filter_by(key=application_key).first()

		if currencies:
			models.updateObjectWithJSON(currencies, json, ['key'])
			session.commit()
		return currencies
Example #13
0
    def getGoods(self, application_key):
        session = models.getSession()
        goods = session.query(models.Good).join(models.Application).filter(
            models.Application.key == application_key).all()

        cacheObjects(goods)
        session.close()

        return goods
Example #14
0
    def getPrices(self, application_key):
        session = models.getSession()
        prices = session.query(models.Prices).join(models.Application).filter(
            models.Application.key == application_key).all()

        cacheObjects(prices)
        session.close()

        return prices
Example #15
0
	def getABTest(self, application_key):
		session = models.getSession()

		abtests = session.query(models.ABTest).join(models.Application).filter(models.Application.key == application_key).order_by(models.ABTest.id.desc()).limit(1).all()
		
		if len(abtests) == 1:
			return abtests[0]

		return None
Example #16
0
    def deleteApplication(self, application_key):
        session = models.getSession()
        res = session.query(
            models.Application).filter_by(key=application_key).delete()
        session.commit()

        session.close()

        return res
Example #17
0
    def getCurrency(self, application_key):
        session = models.getSession()
        currency = session.query(models.Currencies).join(
            models.Application).filter_by(key=application_key).first()

        cacheObject(currency)
        session.close()

        return currency
Example #18
0
    def getApplications(self):
        session = models.getSession()
        applications = session.query(models.Application).all()

        cacheObjects(
            applications,
            ['currencies', 'abtest', 'events', 'metrics', 'goods', 'prices'])
        session.close()

        return applications
Example #19
0
    def getApplicationWithName(self, name):
        session = models.getSession()
        app = session.query(models.Application).filter_by(name=name).first()

        cacheObject(
            app,
            ['currencies', 'abtest', 'events', 'metrics', 'goods', 'prices'])
        session.close()

        return app
Example #20
0
    def getMetrics(self, application_key):
        session = models.getSession()
        metrics = session.query(models.Metrics).join(
            models.Application).filter(
                models.Application.key == application_key).first()
        if metrics:
            cacheObject(metrics)
        session.close()

        return metrics
Example #21
0
    def exportFile(self, application, fromDate, toDate, file):
        session = models.getSession()
        q = session.query(models.Events).filter(
            models.Events.application_key == application,
            models.Events.created >= fromDate,
            models.Events.created < toDate,
        )

        for row in q:
            file.write(extras.toJSON(row.as_dict()) + "\n")
            file.flush()
Example #22
0
    def getApplicationSecret(self, application_key):
        session = models.getSession()
        application = session.query(
            models.Application).filter_by(key=application_key).first()

        if application != None:
            session.close()
            return application.secret

        session.close()
        return None
Example #23
0
    def updateGood(self, good_key, json):
        session = models.getSession()
        good = session.query(models.Good).filter_by(key=good_key).first()
        if good:
            models.updateObjectWithJSON(good, json, ['key', 'application_key'])
            session.commit()

        cacheObject(good)
        session.close()

        return good
Example #24
0
	def addGood(self, application_key, name):
		session = models.getSession()
		

		application = session.query(models.Application).filter_by(key = application_key).first()
	
		good = models.Good(name=name, application_key=application.key)
		
		session.add(good)
		session.commit()

		return good
Example #25
0
	def exportStream(self, application, fromDate, toDate, file = None):

		session = models.getSession()
		
		q = session.query(models.Events).filter(models.Events.application_key==application, models.Events.created>=fromDate, models.Events.created<toDate)
		
		
		for row in q:
			if file == None:
				yield extras.toJSON(row.as_dict())+'\n'
			else:
				file.write(extras.toJSON(row.as_dict())+'\n')
Example #26
0
    def setApplication(self, application_key, json):
        session = models.getSession()
        application = session.query(
            models.Application).filter_by(key=application_key).first()
        if application:
            models.updateObjectWithJSON(application, json, ['key'])
            session.commit()

        cacheObject(application)
        session.close()

        return application
Example #27
0
    def setCurrency(self, application_key, json):
        session = models.getSession()
        currencies = session.query(models.Currencies).join(
            models.Application).filter_by(key=application_key).first()

        if currencies:
            models.updateObjectWithJSON(currencies, json, ['key'])
            session.commit()

        cacheObject(currencies)
        session.close()

        return currencies
Example #28
0
    def addPrices(self, application_key, engine, data, path):
        session = models.getSession()
        prices = models.Prices(application_key=application_key,
                               engine=engine,
                               data=data,
                               path=path)
        pricing.PricingEngine.validate(prices)
        session.add(prices)
        session.commit()

        cacheObject(prices)
        session.close()

        return prices
Example #29
0
    def setMetrics(self, application_key, json):
        session = models.getSession()

        metrics = session.query(models.Metrics).join(
            models.Application).filter(
                models.Application.key == application_key).first()
        if metrics:
            models.updateObjectWithJSON(metrics, json, ['key'])
            session.commit()
            cacheObject(metrics)

        session.close()

        return metrics
Example #30
0
    def addGood(self, application_key, name):
        session = models.getSession()

        application = session.query(
            models.Application).filter_by(key=application_key).first()

        good = models.Good(name=name, application_key=application.key)

        session.add(good)
        session.commit()

        cacheObject(good)
        session.close()

        return good
Example #31
0
    def exportStream(self, application, fromDate, toDate, file=None):

        session = models.getSession()

        q = session.query(models.Events).filter(
            models.Events.application_key == application,
            models.Events.created >= fromDate,
            models.Events.created < toDate,
        )

        for row in q:
            if file == None:
                yield extras.toJSON(row.as_dict()) + "\n"
            else:
                file.write(extras.toJSON(row.as_dict()) + "\n")
Example #32
0
    def getABTest(self, application_key):
        session = models.getSession()

        abtests = session.query(models.ABTest).join(models.Application).filter(
            models.Application.key == application_key).order_by(
                models.ABTest.id.desc()).limit(1).all()

        if len(abtests) == 1:

            cacheObject(abtests[0])
            session.close()

            return abtests[0]

        session.close()
        return None
Example #33
0
	def addApplication(self, name):	
		session = models.getSession()
		
		application = models.Application(name=name)
		
		currencies = models.Currencies(application=application)
		abtest = models.ABTest(application=application)
		metrics = models.Metrics(application=application)

		session.add(application)
		session.add(currencies)
		session.add(abtest)
		session.add(metrics)
		session.commit()
		
		return application
Example #34
0
	def deletePrices(self, prices_key):
		session = models.getSession()

		prices = session.query(models.Prices).filter_by(key = prices_key).first()
		prices.data = None
		prices.path = None
		prices.deleted = True

		abtest = self.getABTest(prices.application_key)
		if abtest.groupAPrices_key == prices_key and abtest.groupBPrices_key != prices_key:
			self.setABTest(prices.application_key, {'groupAPrices_key': None})
		elif abtest.groupAPrices_key != prices_key and abtest.groupBPrices_key == prices_key:
			self.setABTest(prices.application_key, {'groupBPrices_key': None})
		elif abtest.groupAPrices_key == prices_key and abtest.groupBPrices_key == prices_key:
			self.setABTest(prices.application_key, {'groupAPrices_key': None, 'groupBPrices_key': None})

		session.commit()
		return True
Example #35
0
    def addApplication(self, name):
        session = models.getSession()

        application = models.Application(name=name)

        currencies = models.Currencies(application=application)
        abtest = models.ABTest(application=application)
        metrics = models.Metrics(application=application)

        session.add(application)
        session.add(currencies)
        session.add(abtest)
        session.add(metrics)
        session.commit()

        cacheObject(application)
        session.close()

        return application
Example #36
0
	def processThreadUpdate(self, filenames):
		session = models.getSession()		

		eventsCount = 0
		events = []

		for filename in filenames:
			if 'json' in filename:
				
				try:
					data = self.storage.load(filename)
					data = json.loads(data)
				except:
					monitor.getMonitor().count('AnalyticsUpdateBadParse')
					try:
						self.storage.delete(filename)
					except:
						monitor.getMonitor().count('AnalyticsUpdateBadDelete')
					continue

				if self.getApplication(data['liftpass-application']) != None:
					currentEvents = self.processUpdate(data)
					eventsCount += len(currentEvents)
					events.extend(currentEvents)

				self.storage.delete(filename)


			if len(events) > 1000:
				print('Saving: %s saved %d (%d so far)'%(multiprocessing.current_process(), len(events), eventsCount))
				session.bulk_insert_mappings(models.Events, events)
				events = []
				
		session.bulk_insert_mappings(models.Events, events)
		session.commit()

		return eventsCount
Example #37
0
    def setABTest(self, application_key, json):
        session = models.getSession()

        abtest = self.getABTest(application_key)

        if abtest:

            newABTest = models.ABTest()
            newABTest.application_key = abtest.application_key
            newABTest.countryWhiteList = abtest.countryWhiteList
            newABTest.countryBlackList = abtest.countryBlackList
            newABTest.modulus = abtest.modulus
            newABTest.modulusLimit = abtest.modulusLimit
            newABTest.groupAPrices_key = abtest.groupAPrices_key
            newABTest.groupBPrices_key = abtest.groupBPrices_key

            # Check price foreign keys manually
            for prices in ['groupAPrices_key', 'groupBPrices_key']:
                if prices in json and json[prices] != None:
                    count = session.query(models.Prices).join(
                        models.Application).filter(
                            models.Application.key == newABTest.application_key
                        ).filter(models.Prices.key == json[prices]).count()
                    if count == 1:
                        pass
                    else:
                        del json[prices]

            models.updateObjectWithJSON(newABTest, json, ['key'])
            session.add(newABTest)
            session.commit()

            cacheObject(newABTest)

        session.close()

        return newABTest
Example #38
0
    def deletePrices(self, prices_key):
        session = models.getSession()

        prices = session.query(models.Prices).filter_by(key=prices_key).first()
        prices.data = None
        prices.path = None
        prices.deleted = True

        abtest = self.getABTest(prices.application_key)
        if abtest.groupAPrices_key == prices_key and abtest.groupBPrices_key != prices_key:
            self.setABTest(prices.application_key, {'groupAPrices_key': None})
        elif abtest.groupAPrices_key != prices_key and abtest.groupBPrices_key == prices_key:
            self.setABTest(prices.application_key, {'groupBPrices_key': None})
        elif abtest.groupAPrices_key == prices_key and abtest.groupBPrices_key == prices_key:
            self.setABTest(prices.application_key, {
                'groupAPrices_key': None,
                'groupBPrices_key': None
            })

        session.commit()

        session.close()

        return True
Example #39
0
    def processThreadUpdate(self, filenames):
        session = models.getSession()

        eventsCount = 0
        events = []

        for filename in filenames:
            if "json" in filename:

                try:
                    data = self.storage.load(filename)
                    data = json.loads(data)
                except:
                    monitor.getMonitor().count("AnalyticsUpdateBadParse")
                    try:
                        self.storage.delete(filename)
                    except:
                        monitor.getMonitor().count("AnalyticsUpdateBadDelete")
                    continue

                if self.getApplication(data["liftpass-application"]) != None:
                    currentEvents = self.processUpdate(data)
                    eventsCount += len(currentEvents)
                    events.extend(currentEvents)

                self.storage.delete(filename)

            if len(events) > 1000:
                print("Saving: %s saved %d (%d so far)" % (multiprocessing.current_process(), len(events), eventsCount))
                session.bulk_insert_mappings(models.Events, events)
                events = []

        session.bulk_insert_mappings(models.Events, events)
        session.commit()

        return eventsCount
Example #40
0
	def getCurrency(self, application_key):
		session = models.getSession()
		return session.query(models.Currencies).join(models.Application).filter_by(key=application_key).first()
Example #41
0
	def getApplication(self, application_key):
		return models.getSession().query(models.Application).filter_by(key=application_key).first()
Example #42
0
	def getApplications(self):
		return models.getSession().query(models.Application).all()
Example #43
0
	def getMetrics(self, application_key):
		session = models.getSession()
		return session.query(models.Metrics).join(models.Application).filter(models.Application.key == application_key).first()
Example #44
0
	def deleteApplication(self, application_key):
		session = models.getSession()
		res = session.query(models.Application).filter_by(key=application_key).delete()
		session.commit()
		return res
Example #45
0
	def deleteGood(self, good_key):
		session = models.getSession()
		res = session.query(models.Good).filter_by(key=good_key).delete()
		session.commit()
		return res
Example #46
0
	def getGood(self, key):
		session = models.getSession()
		return session.query(models.Good).filter_by(key = key).first()
Example #47
0
	def getGoods(self, application_key):
		session = models.getSession()
		return session.query(models.Good).join(models.Application).filter(models.Application.key == application_key).all()
Example #48
0
	def getPrice(self, prices_key):
		session = models.getSession()
		return session.query(models.Prices).filter_by(key = prices_key).first()