Example #1
0
    def sync_to_server(self):
        """Sync tables that have the 'sunc' column available"""

        data_len = 0
        num_tabs = 0
        sync_success = False
        for table_name in self.tables:
            table = self.tables[table_name]
            if "sunc" not in table.c:
                logging.debug("Not syncing table '%s' - no 'sunc' column" % table_name)
                continue
            query = table.select(table.c.sunc == 0)
            ex = query.execute()
            results = ex.fetchall()
            data_len += len(results)
            if results:
                num_tabs += 1
            for data in self.chunker(results, self.SYNC_LIMIT):
                result_as_dict = [dict(e) for e in data]
                data_to_upload = {"table": table_name,
                                           "data": result_as_dict}
                data_to_upload = objs_to_json(data_to_upload)
                sync_result = self.web_upload(data_to_upload)
                if not sync_result:
                    logging.error("Unable to upload %d rows from table '%s'. Moving to next table (check logs for details). " % (len(data), table_name))
                    break
                else:
                    sync_success = True
                    if self.flush_local_data_after_sync:
                        table.delete().execute()
                    else:
                        table.update(values={table.c.sunc:1}).execute()

        if data_len > 0 and self.verbose > 0 and sync_success:
            logging.info("Snoopy successfully %s%s%s %s%d%s elements over %s%d%s tables." % (GR,"sunc",G,GR,data_len,G,GR,num_tabs,G))
Example #2
0
def pull():
    global tables
    global metadata
    metadata.reflect()
    all_data = []
    for table in tables:
        if "sunc" in table.c:
            table.metadata = metadata
            query = table.select()
            ex = query.execute()
            results = ex.fetchall()
            if results:
                result_as_dict = [dict(e) for e in results]
                data_to_return = {"table": table.name,
                                               "data": result_as_dict}
                data_to_return = json.loads(objs_to_json(data_to_return)) # A bit backward, ne
                all_data.append(data_to_return)

    #return type(json.dumps(str(all_data))
    return json.dumps(all_data)
Example #3
0
def pull():
    global tables
    global metadata
    metadata.reflect()
    all_data = []
    for table in tables:
        if "sunc" in table.c:
            table.metadata = metadata
            query = table.select()
            ex = query.execute()
            results = ex.fetchall()
            if results:
                result_as_dict = [dict(e) for e in results]
                data_to_return = {"table": table.name, "data": result_as_dict}
                data_to_return = json.loads(
                    objs_to_json(data_to_return))  # A bit backward, ne
                all_data.append(data_to_return)

    #return type(json.dumps(str(all_data))
    return json.dumps(all_data)