def insertC2CMoisPartnerRegionAdm(self, monthString): prevMonthString = Utils.getPrevMonthString(monthString) prevMonthData = self.getMonthValues(prevMonthString) monthData = self.getMonthValues(monthString) for c2c in monthData: for prevC2C in prevMonthData: if (prevC2C.partnerId == c2c.partnerId and prevC2C.regionAdm == c2c.regionAdm): c2c.c2cM1 = prevC2C.c2c c2c.evolM1 = c2c.c2c - prevC2C.c2c sessionFactory = SessionFactory() session = sessionFactory.Session() try: print(time.strftime("%d/%m/%Y %H:%M:%S"), "Début insertion ") session.add_all(monthData) session.commit() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertion ") print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertions avec succès") except Exception as ex: self.view_traceback() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Echec de l'insertion, annulation des modifications") session.rollback() finally: session.close() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Session close")
def loadC2CDayLocally(self, fileLocation, day, mappingZebra): """ Cette fonction prend en paramètre une date de jour formatée en chaine de caractère, Lit le fichier de configuration pour récupérer le repertoire de dépot des fichiers et lit pour le jour correspondant les données du c2s et les met dans une liste qu'elle renvoie ensuite """ c2cList = dict() resultList = [] with open(fileLocation, "r") as csvfile: reader = csv.reader(csvfile, delimiter=";", quotechar='"') #on ne tient pas compte de la première ligne next(reader) for row in reader: tmpElt = DDC2CJourSTKP(row) if tmpElt.stkpMsisdn not in c2cList: c2cList[tmpElt.stkpMsisdn] = tmpElt else: previousElt = c2cList[tmpElt.stkpMsisdn] previousElt.c2c = previousElt.c2c + tmpElt.c2c c2cList[tmpElt.stkpMsisdn] = previousElt print(time.strftime("%d/%m/%Y %H:%M:%S"), "Début de la complétion totale ...") #semaine = self.getWeekNumberFromDate(day, self._DATE_FORMAT) semaine = Utils.getWeekNumberFromDate(day, self._DATE_FORMAT) for stkpMsisdn, stkp in c2cList.items(): stkp.jour = day stkp.sem = semaine resultList.append(self.getMappingZebraInfos(stkp, mappingZebra)) print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin de la complétion totale ...") return resultList
def extract_pricing(self, field_data, field_name): pricing_obj = [] if field_data is None: return [] for price_key in field_data: price_obj = field_data[price_key] if isinstance(price_obj, dict): type_price = price_obj.get(field_name, '0') price = Utils.get_price(type_price) if price != 0: pricing_obj.append({"region": price_key, "price": price}) else: price = Utils.get_price(price_obj) if price != 0: pricing_obj.append({"region": price_key, "price": price}) return pricing_obj
def getAllStkpForWeek(self, weekString): """ Cette fonction prend en paramètres une semaine au format aaaass exemple 201735 Génère l'ensemble des jours de la semaine en question au format dd/mm/aaaa Elle crée ensuite une liste contenant les informations de la stkb sans c2s """ #jours = self.getAllWeekDays(weekString, self._DATE_FORMAT) jours = Utils.getAllWeekDays(weekString, self._DATE_FORMAT) for jour in jours: print(jour) sessionFactory = SessionFactory() session = sessionFactory.Session() stkpsWeek = self.getC2CJourSTKPEntriesByDate(jours, self._DATE_FORMAT, session) return stkpsWeek
def insertC2CSTKADay(self, day): sessionFactory = SessionFactory() session = sessionFactory.Session() stkps = session.query(DDC2CJourSTKP).filter_by(jour = day) resultat = dict() sem = Utils.getWeekNumberFromDate(day, self._DATE_FORMAT) for stkp in stkps: if(resultat.get(stkp.stkaMsisdn) == None): tmp = DDC2CJourSTKA(stkp) tmp.jour = day tmp.sem = sem tmp.stkaMsisdn = stkp.stkaMsisdn resultat[stkp.stkaMsisdn] = tmp connection = sessionFactory.getConnection() result = connection.execute("select sum(c2c) c2c, stka_msisdn from dd_c2c_jour_stkp where jour= '"+day+"' group by stka_msisdn") for row in result: stkaMsisdn = row['stka_msisdn'] tmpC2c = row['c2c'] resultat[stkaMsisdn].c2c = tmpC2c connection.close() toAdd = [] for res, val in resultat.items(): toAdd.append(val) try: print(time.strftime("%d/%m/%Y %H:%M:%S"), "Début insertion ") session.add_all(toAdd) session.commit() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertion ") print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertions avec succès") except Exception as ex: self.view_traceback() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Echec de l'insertion, annulation des modifications") session.rollback() finally: session.close() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Session close")
def getC2CSTKAWeek(self, weekString): """ Cette fonction prend en paramètre une semaine et calcule pour chaque stka en BD son C2C pour cette semaine """ weekDays = Utils.getAllWeekDays(weekString, self._DATE_FORMAT) resultat = dict() sessionFactory = SessionFactory() session = sessionFactory.Session() print(time.strftime("%d/%m/%Y %H:%M:%S"), ">>>>>>>>>> Début du parcours des jours pour la semaine ", weekString) for day in weekDays: stkasDay = session.query(DDC2CJourSTKA).filter_by(jour = day) print(time.strftime("%d/%m/%Y %H:%M:%S"),">>>>>>>>>>>>>>>>>>> Début du parcours pour le jour ", day ) for stka in stkasDay: if(resultat.get(stka.stkaMsisdn) == None): resultat[stka.stkaMsisdn] = DDC2CSemSTKA(stka) else: resultat[stka.stkaMsisdn].c2c = resultat[stka.stkaMsisdn].c2c + stka.c2c print(time.strftime("%d/%m/%Y %H:%M:%S"),">>>>>>>>>>>>>>>>>>> Fin du parcours pour le jour ", day ) return resultat
def insertNewWeek(self, weekString): print( time.strftime("%d/%m/%Y %H:%M:%S"), "Début calculs préalables à l'insertion d'une nouvelle semaine c2s_week_stkb", weekString) prevWeekString = Utils.getPrevWeekString(weekString) print( time.strftime("%d/%m/%Y %H:%M:%S"), "Fin des calculs préalables à l'insertion d'une nouvelle semaine c2s_week_stkb", weekString) print(time.strftime("%d/%m/%Y %H:%M:%S"), "Chargement des données pour la semaine précédente") stkbsPreviousWeek = self.getC2SWeek(prevWeekString) print(time.strftime("%d/%m/%Y %H:%M:%S"), "Chargement des données pour la semaine en cours") stkbsWeek = self.getAllStkbForWeek(weekString) print(time.strftime("%d/%m/%Y %H:%M:%S"), "Début de la complétion des lignes") stkbsToAdd = [] for stkb in stkbsWeek: print(time.strftime("%d/%m/%Y %H:%M:%S"), ">>>> Début de la complétion pour", stkb) stkbPrev = stkbsPreviousWeek.get(stkb) if stkbPrev != None: stkbsWeek[stkb].c2sMoins1 = stkbPrev.c2s stkbsWeek[ stkb].evolutionS1 = stkbsWeek[stkb].c2s - stkbPrev.c2s stkbsToAdd.append(stkbsWeek[stkb]) print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin de la complétion des lignes") # à ce niveau ont a toutes les données sur l'évolution et on peut donce enregistrer la nouvelle semaine sessionFactory = SessionFactory() session = sessionFactory.Session() try: print(time.strftime("%d/%m/%Y %H:%M:%S"), "Début insertion ") session.add_all(stkbsToAdd) session.commit() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertion ") print(time.strftime("%d/%m/%Y %H:%M:%S"), "Fin insertions avec succès") except Exception as ex: self.view_traceback() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Echec de l'insertion, annulation des modifications") session.rollback() finally: session.close() print(time.strftime("%d/%m/%Y %H:%M:%S"), "Session close")
def post(self): form = FindCloudsForm.from_json(request.json) if form.validate() is False: return {"errors": form.errors}, 400 collections = mongo.get_collections() filters = form.data['filters'] labels = form.data['labels'] limit = form.data['limit'] price_label = '' items = [] for collection_name in collections: last_data = list(mongo.find_last(collection_name))[0] fields = form.data['select'].get(collection_name, []) del last_data['_id'] for machine_key in last_data: machine = last_data[machine_key] price_configurations = [] general_configurations = {'type': machine_key, 'cloud': collection_name} """ Montando o objeto """ for index, field_list in enumerate(fields): field_split = str(field_list).split('.') field = field_split[0] field_data = machine.get(field, None) if 'pricing' in field: price_configurations = self.extract_pricing(field_data, field_split[1]) price_label = labels[index] else: general_configurations[labels[index]] = field_data items = items + [{**general_configurations, price_label: {**x}} for x in price_configurations] # Ordenando a lista pelos valores items.sort(key=lambda elem: elem[price_label]['price']) """ Realizando a filtragem dos resultados """ for filter_obj in filters: list_filtered = [] for item in items: field_filter = filter_obj["field"] comparator = filter_obj["comparator"] value_filter = filter_obj["value"] field_data = "'" + str(item.get(field_filter, '')) + "'" # Removendo . caso seja um número decimal if str(value_filter).replace('.', '').isnumeric(): field_data = Utils.only_numbers(field_data) else: # Se não for um número, adicionar " para considerar como string na hora do eval value_filter = '"' + value_filter + '"' if eval("{} {} {}".format(field_data, comparator, value_filter)) is True: list_filtered.append(item) items = list_filtered if limit is not None: items = items[:limit] return items