Exemplo n.º 1
0
    def get_simulation_csv(self, simulation):
        user = self.getCurrentUser()
        simulation_model = Simulation()
        summary_stats = simulation_model.get_summary_stats(simulation, user)

        # The values of summary stats will typically be nested dicts, now we flatten them
        summary_stats = {
            time: flatten_dict(data)
            for time, data in summary_stats.items()
        }
        # move it to a list and sort by time
        summary_stats = [(time, data) for time, data in summary_stats.items()]
        summary_stats.sort(key=lambda x: float(x[0]))

        # write a csv to memory
        with io.StringIO() as sio:
            csvwriter = csv.writer(sio, dialect='excel')
            if len(summary_stats) > 0:
                # header
                csvwriter.writerow(
                    ["time", *[label for label, value in summary_stats[0][1]]])

                for time, data in summary_stats:
                    csvwriter.writerow(
                        [time, *[value for label, value in data]])

            rest.setResponseHeader('Content-Type', 'text/csv')
            return sio.getvalue()
Exemplo n.º 2
0
 def get_simulation_json(self, simulation):
     user = self.getCurrentUser()
     simulation_model = Simulation()
     return simulation_model.get_summary_stats(simulation, user)