Example #1
0
 def parse_report_row(self, row, user, total=False):
     return ReportRow(
         row[0],  # unit_name
         row[1].lower().strip(),  # violation
         int(row[2]) if row[2] else 1,  # violation_count
         self.duration_cache.get(row[0], 0)
         if total else parse_timedelta(row[3]).total_seconds(),  # duration
         parse_float(row[4], default=.0),  # fine
         parse_float(row[5], default=.0),  # rating
         local_to_utc_time(
             parse_wialon_report_datetime(
                 row[6]['t'] if isinstance(row[6], dict) else row[6]
             ), user.timezone
         ),  # from_dt
         local_to_utc_time(
             parse_wialon_report_datetime(
                 row[7]['t'] if isinstance(row[7], dict) else row[7]
             ), user.timezone
         ),  # to_dt
         self.mileage_cache.get(row[0], .0)
         if total else parse_float(row[9], default=.0)  # mileage_corrected
     )
Example #2
0
    def get_report_data(self):
        self.get_report_data_tables()

        self.request_dt_from, self.request_dt_to = get_period(
            self.input_data['date_begin'], self.input_data['date_end'])

        cleanup_and_request_report(self.request.user,
                                   self.report_template_id,
                                   self.sess_id,
                                   item_id=self.unit_id)

        try:
            r = exec_report(self.request.user,
                            self.report_template_id,
                            self.sess_id,
                            self.request_dt_from,
                            self.request_dt_to,
                            object_id=self.unit_id)
        except ReportException as e:
            raise WialonException(
                'Не удалось получить в Wialon отчет о поездках: %s' % e)

        for table_index, table_info in enumerate(r['reportResult']['tables']):
            if table_info['name'] not in self.report_data:
                continue

            try:
                rows = get_report_rows(self.sess_id,
                                       table_index,
                                       table_info['rows'],
                                       level=1)

                if table_info['name'] != 'unit_sensors_tracing':
                    self.report_data[table_info['name']] = rows
                else:
                    for row in rows:
                        if isinstance(row['c'][0], dict):
                            key = row['c'][0]['v']
                        else:
                            key = parse_wialon_report_datetime(row['c'][0])
                            key = int(
                                local_to_utc_time(
                                    key,
                                    self.request.user.timezone).timestamp())
                        if key and row['c'][1]:
                            self.fuel_data[key] = parse_float(
                                row['c'][1]) or .0

            except ReportException as e:
                raise WialonException(
                    'Не удалось получить в Wialon отчет о поездках: %s' % e)
Example #3
0
    def get_last_data(self, unit_name):
        data = self.last_data.get(unit_name)
        if data and len(data) > 1:
            dt, place = data[0], data[2]

            if isinstance(dt, dict):
                dt = datetime.datetime.utcfromtimestamp(dt['v'])
                dt = utc_to_local_time(dt, self.user.timezone)
            else:
                dt = parse_wialon_report_datetime(dt)

            if isinstance(place, dict) and 't' in place:
                place = place['t']

            return dt, place

        return None, None
Example #4
0
 def __init__(self, dt_from, dt_to, *args, **kwargs):
     tz = kwargs.pop('tz')
     self.dt_from = local_to_utc_time(parse_wialon_report_datetime(dt_from),
                                      tz)
     self.dt_to = local_to_utc_time(parse_wialon_report_datetime(dt_to), tz)
Example #5
0
    def report_post_processing(self, unit_info):
        job_date_begin = utc_to_local_time(
            self.job.date_begin.replace(tzinfo=None),
            self.request.user.timezone)
        job_date_end = utc_to_local_time(
            self.job.date_end.replace(tzinfo=None), self.request.user.timezone)

        for point in self.ride_points:
            point['time_in'] = utc_to_local_time(
                datetime.datetime.utcfromtimestamp(point['time_in']),
                self.request.user.timezone)
            point['time_out'] = utc_to_local_time(
                datetime.datetime.utcfromtimestamp(point['time_out']),
                self.request.user.timezone)

            if point['time_in'] >= job_date_begin and point[
                    'time_out'] <= job_date_end:
                point['job_id'] = self.job.pk

        for row in self.report_data['unit_thefts']:
            volume = parse_float(row['c'][2])

            if volume > .0 and row['c'][1]:
                dt = utc_to_local_time(
                    parse_wialon_report_datetime(
                        row['c'][1]['t'] if isinstance(row['c'][1], dict
                                                       ) else row['c'][1]),
                    self.request.user.timezone)

                for point in self.ride_points:
                    if point['time_in'] <= dt <= point['time_out']:
                        point['params']['fuelDrain'] += volume
                        break

        for row in self.report_data['unit_fillings']:
            volume = parse_float(row['c'][1])

            if volume > .0:
                dt = utc_to_local_time(
                    parse_wialon_report_datetime(
                        row['c'][0]['t'] if isinstance(row['c'][0], dict
                                                       ) else row['c'][0]),
                    self.request.user.timezone)

                for point in self.ride_points:
                    if point['time_in'] <= dt <= point['time_out']:
                        point['params']['fuelRefill'] += volume
                        break

        # рассчитываем моточасы пропорционально интервалам
        for row in self.report_data['unit_engine_hours']:
            time_from = utc_to_local_time(
                parse_wialon_report_datetime(row['c'][0]['t'] if isinstance(
                    row['c'][0], dict) else row['c'][0]),
                self.request.user.timezone)

            time_until_value = row['c'][1]['t'] \
                if isinstance(row['c'][1], dict) else row['c'][1]

            if 'unknown' in time_until_value.lower():
                time_until = utc_to_local_time(self.input_data['date_end'],
                                               self.request.user.timezone)
            else:
                time_until = utc_to_local_time(
                    parse_wialon_report_datetime(time_until_value),
                    self.request.user.timezone)

            for point in self.ride_points:
                if point['time_in'] > time_until:
                    # дальнейшие строки точно не совпадут (виалон все сортирует по дате)
                    break

                # если интервал точки меньше даты начала моточасов, значит еще не дошли
                if point['time_out'] < time_from:
                    continue

                delta = min(time_until, point['time_out']) - max(
                    time_from, point['time_in'])
                # не пересекаются:
                if delta.total_seconds() <= 0:
                    continue

                point['params']['motoHours'] += delta.total_seconds()

        for row in self.report_data['unit_chronology']:
            row_data = row['c']
            if not isinstance(row_data[0], str):
                send_trigger_email(
                    'В хронологии первое поле отчета не строка!',
                    extra_data={
                        'POST': self.request.body,
                        'row_data': row_data,
                        'user': self.request.user
                    })
                continue

            time_from = utc_to_local_time(
                parse_wialon_report_datetime(row_data[1]['t'] if isinstance(
                    row_data[1], dict) else row_data[1]),
                self.request.user.timezone)

            time_until_value = row_data[2]['t'] \
                if isinstance(row_data[2], dict) else row_data[2]

            if 'unknown' in time_until_value.lower():
                time_until = self.input_data['date_end']
            else:
                time_until = utc_to_local_time(
                    parse_wialon_report_datetime(time_until_value),
                    self.request.user.timezone)

            for point in self.ride_points:
                if point['time_in'] > time_until:
                    # дальнейшие строки точно не совпадут (виалон все сортирует по дате)
                    break

                # если интервал точки меньше даты начала хронологии, значит еще не дошли
                if point['time_out'] < time_from:
                    continue

                delta = min(time_until, point['time_out']) - max(
                    time_from, point['time_in'])
                # не пересекаются:
                if delta.total_seconds() < 0:
                    continue

                if row_data[0].lower() in ('поездка', 'trip'):
                    point['params']['moveMinutes'] += delta.total_seconds()

        # рассчитываем время работы крановой установки пропорционально интервалам
        # (только лишь ради кэша, необходимости в расчетах нет)
        for row in self.report_data['unit_digital_sensors']:
            time_from = utc_to_local_time(
                parse_wialon_report_datetime(row['c'][0]['t'] if isinstance(
                    row['c'][0], dict) else row['c'][0]),
                self.request.user.timezone)

            time_until_value = row['c'][1]['t'] \
                if isinstance(row['c'][1], dict) else row['c'][1]

            if 'unknown' in time_until_value.lower():
                time_until = utc_to_local_time(self.input_data['date_end'],
                                               self.request.user.timezone)
            else:
                time_until = utc_to_local_time(
                    parse_wialon_report_datetime(time_until_value),
                    self.request.user.timezone)

            for point in self.ride_points:
                if point['time_in'] > time_until:
                    # дальнейшие строки точно не совпадут (виалон все сортирует по дате)
                    break

                # если интервал точки меньше даты начала моточасов, значит еще не дошли
                if point['time_out'] < time_from:
                    continue

                delta = min(time_until, point['time_out']) - max(
                    time_from, point['time_in'])
                # не пересекаются:
                if delta.total_seconds() <= 0:
                    continue

                point['params']['GPMTime'] += delta.total_seconds()

        for point in self.ride_points:
            point['params']['stopMinutes'] = (
                point['time_out'] - point['time_in']
            ).total_seconds() - point['params']['moveMinutes']
Example #6
0
    def report_post_processing(self, job_info):
        for race in job_info['races']:
            race['date_start'] = utc_to_local_time(
                datetime.datetime.utcfromtimestamp(race['date_start']),
                self.request.user.timezone
            )
            race['date_end'] = utc_to_local_time(
                datetime.datetime.utcfromtimestamp(race['date_end']),
                self.request.user.timezone
            )

            for point in race['points']:
                point['time_in'] = utc_to_local_time(
                    datetime.datetime.utcfromtimestamp(point['time_in']),
                    self.request.user.timezone
                )
                point['time_out'] = utc_to_local_time(
                    datetime.datetime.utcfromtimestamp(point['time_out']),
                    self.request.user.timezone
                )

        for row in self.report_data['unit_chronology']:
            row_data = row['c']

            try:
                if row_data[0].lower() not in ('поездка', 'trip'):
                    continue

            except AttributeError as e:
                send_trigger_email(
                    'Ошибка в работе интеграции Wialon', extra_data={
                        'Exception': str(e),
                        'Traceback': traceback.format_exc(),
                        'data': row_data,
                        'POST': self.request.body,
                        'user': self.request.user
                    }
                )

            time_from = utc_to_local_time(
                parse_wialon_report_datetime(
                    row_data[1]['t']
                    if isinstance(row_data[1], dict)
                    else row_data[1]
                ),
                self.request.user.timezone
            )

            time_until_value = row_data[2]['t'] \
                if isinstance(row_data[2], dict) else row_data[2]

            if 'unknown' in time_until_value.lower():
                time_until = self.input_data['date_end']
            else:
                time_until = utc_to_local_time(
                    parse_wialon_report_datetime(time_until_value),
                    self.request.user.timezone
                )

            for race in job_info['races']:
                for point in race['points']:
                    if point['time_in'] > time_until:
                        # дальнейшие строки точно не совпадут (виалон все сортирует по дате)
                        break

                    # если интервал точки меньше даты начала моточасов, значит еще не дошли
                    if point['time_out'] < time_from:
                        continue

                    delta = min(time_until, point['time_out']) - max(time_from, point['time_in'])
                    # не пересекаются:
                    if delta.total_seconds() < 0:
                        continue

                    point['params']['moveTime'] += delta.total_seconds()
Example #7
0
 def __init__(self, dt, volume, *args, **kwargs):
     tz = kwargs.pop('tz')
     self.dt = local_to_utc_time(parse_wialon_report_datetime(dt), tz)
     self.volume = parse_float(volume)
Example #8
0
 def __init__(self, geozone, dt_from, dt_to, *args, **kwargs):
     tz = kwargs.pop('tz')
     self.geozone = 'SPACE' if '---' in geozone else geozone.strip()
     self.dt_from = local_to_utc_time(parse_wialon_report_datetime(dt_from),
                                      tz)
     self.dt_to = local_to_utc_time(parse_wialon_report_datetime(dt_to), tz)
Example #9
0
    def update_last_sensor_data(self, report_row, attempt=0):

        date_slice_from = attempt * LAST_SIGNAL_STEP
        date_slice_to = (attempt + 1) * LAST_SIGNAL_STEP

        if date_slice_to > LAST_SIGNAL_UNTIL:
            return report_row

        now = local_date_to = utc_to_local_time(utcnow(), self.user.timezone)

        if date_slice_from:
            local_date_to = now - datetime.timedelta(days=date_slice_from)

        local_date_from = now - datetime.timedelta(days=date_slice_to)
        dt_from, dt_to = get_period(local_date_from, local_date_to,
                                    self.user.timezone)

        print('Пробуем период поиска последнего сигнала %s - %s (попытка %s)' %
              (local_date_from, local_date_to, attempt + 1))
        cleanup_and_request_report(self.user, self.sensors_template_id,
                                   self.sess_id)
        r = exec_report(self.user,
                        self.sensors_template_id,
                        self.sess_id,
                        dt_from,
                        dt_to,
                        object_id=report_row['unit_id'])

        for table_index, table_info in enumerate(r['reportResult']['tables']):
            label = table_info['label'].split('(')[0].strip()

            if table_info[
                    'name'] != 'unit_sensors_tracing' or label != report_row[
                        'sensor']:
                continue

            if table_info['rows'] == 0:
                return self.update_last_sensor_data(report_row,
                                                    attempt=attempt + 1)

            rows = get_report_rows(self.sess_id, table_index, rows=1, level=1)

            if not rows:
                return self.update_last_sensor_data(report_row,
                                                    attempt=attempt + 1)

            dt, place = rows[0]['c'][2], rows[0]['c'][4]

            if isinstance(dt, dict):
                dt = datetime.datetime.utcfromtimestamp(dt['v'])
                dt = utc_to_local_time(dt, self.user.timezone)
            else:
                dt = parse_wialon_report_datetime(dt)

            if isinstance(place, dict) and 't' in place:
                place = place['t']

            if place:
                report_row['place'] = place

            if dt:
                report_row['dt'] = dt
                report_row[
                    'sum_broken_work_time'] = self.get_sum_broken_work_time(
                        report_row['unit_id'],
                        local_to_utc_time(dt, self.user.timezone),
                        report_row['job_date_end'])
            return report_row

        return self.update_last_sensor_data(report_row, attempt=attempt + 1)
Example #10
0
    def get_context_data(self, **kwargs):
        kwargs = super(DischargeView, self).get_context_data(**kwargs)
        form = kwargs['form']
        report_data = None

        sess_id = self.request.session.get('sid')
        if not sess_id:
            raise ReportException(WIALON_NOT_LOGINED)

        try:
            units_list = get_units(sess_id, extra_fields=True)
        except WialonException as e:
            raise ReportException(str(e))

        kwargs['units'] = units_list

        if self.request.POST:
            if form.is_valid():
                report_data = OrderedDict()

                self.user = User.objects.filter(is_active=True)\
                    .filter(wialon_username=self.request.session.get('user')).first()

                if not self.user:
                    raise ReportException(WIALON_USER_NOT_FOUND)

                normal_ratio = 1 + (
                    form.cleaned_data['overspanding_percentage'] / 100)

                units_dict = OrderedDict((u['id'], u) for u in units_list)
                selected_unit = form.cleaned_data.get('unit')

                if selected_unit and selected_unit in units_dict:
                    units_dict = {selected_unit: units_dict[selected_unit]}

                jobs_count = len(units_dict)
                print('Всего ТС: %s' % jobs_count)

                dt_from_utc = local_to_utc_time(form.cleaned_data['dt_from'],
                                                self.user.timezone)
                dt_to_utc = local_to_utc_time(
                    form.cleaned_data['dt_to'].replace(hour=23,
                                                       minute=59,
                                                       second=59),
                    self.user.timezone)

                ura_user = self.user.ura_user if self.user.ura_user_id else self.user
                jobs = Job.objects.filter(user=ura_user,
                                          date_begin__lt=dt_to_utc,
                                          date_end__gt=dt_from_utc).order_by(
                                              'date_begin', 'date_end')

                jobs_cache = defaultdict(list)
                for job in jobs:
                    try:
                        jobs_cache[int(job.unit_id)].append(job)
                    except ValueError:
                        pass

                template_id = get_wialon_report_template_id(
                    'discharge_individual', self.user, sess_id)
                device_fields = defaultdict(lambda: {'extras': .0, 'idle': .0})

                i = 0
                for unit_id, unit in units_dict.items():
                    i += 1
                    unit_name = unit['name']
                    print('%s/%s) %s' % (i, jobs_count, unit_name))

                    # норматив потребления доп.оборудования, л / час
                    extras_values = [
                        x['v'] for x in unit.get('fields', [])
                        if x.get('n') == 'механизм'
                    ]
                    # норматив потребления на холостом ходу, л / час
                    idle_values = [
                        x['v'] for x in unit.get('fields', [])
                        if x.get('n') == 'хх'
                    ]

                    if extras_values:
                        try:
                            device_fields[unit_name]['extras'] = float(
                                extras_values[0])
                        except ValueError:
                            pass

                    if idle_values:
                        try:
                            device_fields[unit_name]['idle'] = float(
                                idle_values[0])
                        except ValueError:
                            pass

                    if unit_id not in report_data:
                        report_data[unit_id] = self.get_new_grouping()

                    report_row = report_data[unit_id]
                    report_row['unit_name'] = unit_name
                    report_row['unit_number'] = unit.get('number', '')
                    report_row['vehicle_type'] = unit.get('vehicle_type', '')

                    unit_jobs = jobs_cache.get(unit_id)
                    if not unit_jobs:
                        report_row['periods'].append(
                            self.get_new_period(dt_from_utc, dt_to_utc))
                    else:
                        if unit_jobs[0].date_begin > dt_from_utc:
                            # если начало периода не попадает на смену
                            report_row['periods'].append(
                                self.get_new_period(dt_from_utc,
                                                    unit_jobs[0].date_begin))

                        previous_job = None
                        for unit_job in unit_jobs:
                            # если между сменами есть перерыв, то тоже добавляем период
                            if previous_job and unit_job.date_begin > previous_job.date_end:
                                report_row['periods'].append(
                                    self.get_new_period(
                                        previous_job.date_end,
                                        unit_job.date_begin))

                            report_row['periods'].append(
                                self.get_new_period(unit_job.date_begin,
                                                    unit_job.date_end,
                                                    unit_job))

                            previous_job = unit_job

                        if unit_jobs[-1].date_end < dt_to_utc:
                            # если смена закончилась до конца периода
                            report_row['periods'].append(
                                self.get_new_period(unit_jobs[-1].date_end,
                                                    dt_to_utc))

                    # получим полный диапазон запроса
                    dt_from = int(
                        time.mktime(
                            report_row['periods'][0]['dt_from'].timetuple()))
                    dt_to = int(
                        time.mktime(
                            report_row['periods'][-1]['dt_to'].timetuple()))

                    cleanup_and_request_report(self.user, template_id, sess_id)

                    r = exec_report(self.user,
                                    template_id,
                                    sess_id,
                                    dt_from,
                                    dt_to,
                                    object_id=unit_id)

                    wialon_report_rows = {}
                    for table_index, table_info in enumerate(
                            r['reportResult']['tables']):
                        rows = get_report_rows(sess_id,
                                               table_index,
                                               table_info['rows'],
                                               level=2 if table_info['name']
                                               == 'unit_thefts' else 1)

                        wialon_report_rows[table_info['name']] = [
                            row['c'] for row in rows
                        ]

                    for period in report_row['periods']:
                        for row in wialon_report_rows.get('unit_trips', []):
                            row_dt_from, row_dt_to = self.parse_wialon_report_datetime(
                                row)
                            if row_dt_to is None:
                                row_dt_to = period['dt_to']

                            if period['dt_from'] < row_dt_from and period[
                                    'dt_to'] > row_dt_to:
                                delta = (min(row_dt_to, period['dt_to']) -
                                         max(row_dt_from, period['dt_from'])
                                         ).total_seconds()
                                if not delta:
                                    print('empty trip period')
                                    continue

                                trip_ratio = 1
                                total_delta = (row_dt_to -
                                               row_dt_from).total_seconds()
                                if total_delta > 0:
                                    trip_ratio = delta / total_delta
                                period['mileage'] += parse_float(
                                    row[3]) * trip_ratio
                                period['move_hours'] += parse_timedelta(row[4]).total_seconds()\
                                    * trip_ratio

                        for row in wialon_report_rows.get(
                                'unit_digital_sensors', []):
                            row_dt_from, row_dt_to = self.parse_wialon_report_datetime(
                                row)
                            if row_dt_to is None:
                                row_dt_to = period['dt_to']

                            if period['dt_from'] < row_dt_from and period[
                                    'dt_to'] > row_dt_to:
                                delta = min(row_dt_to, period['dt_to']) - \
                                        max(row_dt_from, period['dt_from'])
                                period[
                                    'extra_device_hours'] += delta.total_seconds(
                                    )

                        for row in wialon_report_rows.get('unit_thefts', []):
                            dt = parse_wialon_report_datetime(
                                row[1]['t'] if isinstance(row[1], dict
                                                          ) else row[1])
                            utc_dt = local_to_utc_time(dt, self.user.timezone)
                            if period['dt_from'] <= utc_dt <= period['dt_to']:

                                place = row[0]['t'] if isinstance(
                                    row[0], dict) else (row[0] or '')
                                if place and not period['discharge']['place']:
                                    period['discharge']['place'] = place

                                if not period['discharge']['dt']:
                                    period['discharge']['dt'] = dt

                                try:
                                    volume = float(
                                        row[2].split(' ')[0] if row[2] else .0)
                                except ValueError:
                                    volume = .0

                                period['discharge']['volume'] += volume
                                self.stats['discharge_total'] += volume
                                self.stats['overspanding_count'] += 1

                                period['details'].append({
                                    'place': place,
                                    'dt': dt,
                                    'volume': volume
                                })

                        extras_value = device_fields.get(unit_name,
                                                         {}).get('extras', .0)
                        idle_value = device_fields.get(unit_name,
                                                       {}).get('idle', .0)

                        for row in wialon_report_rows.get(
                                'unit_engine_hours', []):
                            row_dt_from, row_dt_to = self.parse_wialon_report_datetime(
                                row)
                            if row_dt_to is None:
                                row_dt_to = period['dt_to']

                            if period['dt_from'] < row_dt_from and period[
                                    'dt_to'] > row_dt_to:
                                delta = (min(row_dt_to, period['dt_to']) -
                                         max(row_dt_from, period['dt_from'])
                                         ).total_seconds()

                                if not delta:
                                    print('empty motohours period')
                                    continue

                                total_delta = (row_dt_to -
                                               row_dt_from).total_seconds()

                                period['moto_hours'] += delta
                                # доля моточасов в периоде и общих моточасов в строке
                                # данный множитель учтем в расчетах потребления
                                moto_ratio = 1
                                if total_delta > 0:
                                    moto_ratio = delta / total_delta

                                try:
                                    period['fact_dut'] += (float(
                                        parse_float(row[3])) if row[3] else
                                                           .0) * moto_ratio

                                except ValueError:
                                    pass

                                try:
                                    period['fact_mileage'] += (float(
                                        parse_float(row[4])) if row[4] else
                                                               .0) * moto_ratio

                                except ValueError:
                                    pass

                                try:
                                    period['idle_hours'] += (parse_timedelta(
                                        row[6]).total_seconds() if row[6] else
                                                             .0) * moto_ratio

                                except ValueError:
                                    pass

                        if idle_value:
                            period['fact_motohours'] = max(
                                period['moto_hours'] - period['move_hours'] -
                                period['extra_device_hours'],
                                .0) / 3600.0 * idle_value

                        if extras_value:
                            period['fact_extra_device'] = \
                                period['extra_device_hours'] / 3600.0 * extras_value

                        total_facts = period['fact_extra_device'] \
                            + period['fact_motohours'] \
                            + period['fact_mileage']

                        if total_facts:
                            ratio = period['fact_dut'] / total_facts
                            if ratio >= normal_ratio:
                                overspanding = period['fact_dut'] \
                                   - total_facts

                                period['overspanding'] = overspanding
                                self.stats[
                                    'overspanding_total'] += overspanding

                        period['dt_from'] = utc_to_local_time(
                            period['dt_from'], self.user.timezone)
                        period['dt_to'] = utc_to_local_time(
                            period['dt_to'], self.user.timezone)

                if report_data:
                    for k, v in report_data.items():
                        v['periods'] = [
                            p for p in v['periods']
                            if p['discharge']['volume'] > .0
                            or p['overspanding'] > 0
                        ]

                    report_data = OrderedDict(
                        (k, v) for k, v in report_data.items() if v['periods'])

        kwargs.update(enumerate=enumerate,
                      report_data=report_data,
                      today=datetime.date.today(),
                      stats=self.stats)

        return kwargs
Example #11
0
 def __init__(self, last_message, last_coords, place, *args, **kwargs):
     tz = kwargs.pop('tz')
     self.last_message = local_to_utc_time(parse_wialon_report_datetime(last_message), tz)
     self.last_coords = local_to_utc_time(parse_wialon_report_datetime(last_coords), tz)
     self.address = parse_address(place)
     self.coords = parse_coords(place)
Example #12
0
    def get_context_data(self, **kwargs):
        kwargs = super(DrivingStyleView, self).get_context_data(**kwargs)
        self.form = kwargs['form']
        kwargs['today'] = datetime.date.today()
        errors = []

        sess_id = self.request.session.get('sid')
        if not sess_id:
            raise ReportException(WIALON_NOT_LOGINED)

        try:
            units_list = get_units(sess_id, extra_fields=True)
        except WialonException as e:
            raise ReportException(str(e))

        kwargs['units'] = units_list

        if self.request.POST:
            report_data = None

            if self.form.is_valid():
                report_data = OrderedDict()

                self.user = User.objects.filter(is_active=True) \
                    .filter(wialon_username=self.request.session.get('user')).first()

                if not self.user:
                    raise ReportException(WIALON_USER_NOT_FOUND)

                units_dict = OrderedDict((u['id'], u) for u in units_list)
                selected_unit = self.form.cleaned_data.get('unit')

                if selected_unit and selected_unit in units_dict:
                    units_dict = {selected_unit: units_dict[selected_unit]}

                dt_from_utc = local_to_utc_time(
                    self.form.cleaned_data['dt_from'], self.user.timezone)
                dt_to_utc = local_to_utc_time(
                    self.form.cleaned_data['dt_to'].replace(second=59),
                    self.user.timezone)

                ura_user = self.user.ura_user if self.user.ura_user_id else self.user
                jobs = Job.objects.filter(user=ura_user,
                                          date_begin__lt=dt_to_utc,
                                          date_end__gt=dt_from_utc).order_by(
                                              'date_begin', 'date_end')

                jobs_cache = defaultdict(list)
                for job in jobs:
                    try:
                        jobs_cache[int(job.unit_id)].append(job)
                    except ValueError:
                        pass

                template_id = get_wialon_report_template_id(
                    'driving_style_individual', self.user, sess_id)

                jobs_count = len(units_dict)
                print('Всего ТС: %s' % jobs_count)

                mobile_vehicle_types = set()
                if self.user.wialon_mobile_vehicle_types:
                    mobile_vehicle_types = set(
                        x.strip()
                        for x in self.user.wialon_mobile_vehicle_types.lower(
                        ).split(','))

                i = 0
                for unit_id, unit in units_dict.items():
                    i += 1
                    unit_name = unit['name']
                    print('%s/%s) %s' % (i, jobs_count, unit_name))

                    vehicle_type = unit.get('vehicle_type', '').lower().strip()

                    if not vehicle_type or (mobile_vehicle_types
                                            and vehicle_type
                                            not in mobile_vehicle_types):
                        print('%s) Skip vehicle type "%s" of item %s' %
                              (i, vehicle_type, unit_name))
                        continue

                    if unit_id not in report_data:
                        report_data[unit_id] = self.get_new_grouping()

                    report_row = report_data[unit_id]
                    report_row['unit_name'] = unit_name
                    report_row['unit_number'] = unit.get('number', '')

                    unit_jobs = jobs_cache.get(unit_id)
                    if not unit_jobs:
                        report_row['periods'].append(
                            self.get_new_period(dt_from_utc, dt_to_utc))
                    else:
                        if unit_jobs[0].date_begin > dt_from_utc:
                            # если начало периода не попадает на смену
                            report_row['periods'].append(
                                self.get_new_period(dt_from_utc,
                                                    unit_jobs[0].date_begin))

                        previous_job = None
                        for unit_job in unit_jobs:
                            # если между сменами есть перерыв, то тоже добавляем период
                            if previous_job and unit_job.date_begin > previous_job.date_end:
                                report_row['periods'].append(
                                    self.get_new_period(
                                        previous_job.date_end,
                                        unit_job.date_begin))

                            report_row['periods'].append(
                                self.get_new_period(unit_job.date_begin,
                                                    unit_job.date_end,
                                                    unit_job))

                            previous_job = unit_job

                        if unit_jobs[-1].date_end < dt_to_utc:
                            # если смена закончилась до конца периода
                            report_row['periods'].append(
                                self.get_new_period(unit_jobs[-1].date_end,
                                                    dt_to_utc))

                    # получим полный диапазон запроса
                    dt_from = int(
                        time.mktime(
                            report_row['periods'][0]['dt_from'].timetuple()))
                    dt_to = int(
                        time.mktime(
                            report_row['periods'][-1]['dt_to'].timetuple()))

                    cleanup_and_request_report(self.user, template_id, sess_id)
                    r = exec_report(self.user,
                                    template_id,
                                    sess_id,
                                    dt_from,
                                    dt_to,
                                    object_id=unit_id)

                    try:
                        wialon_report_rows = {}
                        for table_index, table_info in enumerate(
                                r['reportResult']['tables']):
                            wialon_report_rows[
                                table_info['name']] = get_report_rows(
                                    sess_id,
                                    table_index,
                                    table_info['rows'],
                                    level=1)
                    except ReportException as e:
                        print('%s) Skip vehicle %s due to error' %
                              (i, unit_name))
                        errors.append(
                            ('%s %s' % (report_row['unit_name'],
                                        report_row['unit_number']), str(e)))
                        continue

                    for period in report_row['periods']:
                        for row in wialon_report_rows.get('unit_trips', []):
                            row_dt_from, row_dt_to = self.parse_wialon_report_datetime(
                                row['c'])
                            if row_dt_to is None:
                                row_dt_to = period['dt_to']

                            # дальнейшие строки точно не совпадут (виалон все сортирует по дате):
                            if row_dt_from > period['dt_to']:
                                break

                            # если конец поездки меньше даты начала периода, значит еще не дошли
                            if row_dt_to < period['dt_from']:
                                continue

                            delta = (min(row_dt_to, period['dt_to']) -
                                     max(row_dt_from,
                                         period['dt_from'])).total_seconds()

                            if delta <= 0:
                                print('empty trips period')
                                continue

                            period['total_time'] += delta

                        if period['total_time']:
                            for row in wialon_report_rows.get(
                                    'unit_ecodriving', []):
                                if period['t_from'] < row['t2'] and period[
                                        't_to'] > row['t1']:
                                    detail_data = {
                                        'speed': {
                                            'count': 0,
                                            'seconds': .0
                                        },
                                        'lights': {
                                            'count': 0,
                                            'seconds': .0
                                        },
                                        'belt': {
                                            'count': 0,
                                            'seconds': .0
                                        },
                                        'devices': {
                                            'count': 0,
                                            'seconds': .0
                                        },
                                        'dt_from': '',
                                        'dt_to': ''
                                    }
                                    violation = row['c'][1].lower(
                                    ) if row['c'][1] else ''
                                    if 'свет' in violation or 'фар' in violation:
                                        viol_key = 'lights'
                                    elif 'скорост' in violation or 'превышен' in violation:
                                        viol_key = 'speed'
                                    elif 'ремн' in violation or 'ремен' in violation:
                                        viol_key = 'belt'
                                    elif 'кму' in violation:
                                        viol_key = 'devices'
                                    else:
                                        viol_key = ''

                                    if viol_key:
                                        detail_data[
                                            'dt_from'] = parse_wialon_report_datetime(
                                                row['c'][2]['t'] if isinstance(
                                                    row['c'][2], dict
                                                ) else row['c'][2])
                                        detail_data[
                                            'dt_to'] = parse_wialon_report_datetime(
                                                row['c'][3]['t'] if isinstance(
                                                    row['c'][3], dict
                                                ) else row['c'][3])

                                        delta = min(row['t2'], period['t_to']) - \
                                            max(row['t1'], period['t_from'])
                                        detail_data[viol_key][
                                            'seconds'] = delta

                                        if self.form.cleaned_data[
                                                'include_details']:
                                            period['details'].append(
                                                detail_data)
                                        period['facts'][viol_key]['count'] += 1
                                        period['facts'][viol_key][
                                            'seconds'] += delta

                            for viol_key in ('speed', 'lights', 'belt',
                                             'devices'):
                                percentage = min(
                                    period['facts'][viol_key]['seconds'] /
                                    period['total_time'], 1.0) * 100
                                period['percentage'][viol_key] = percentage
                                period['rating'] -= percentage

                        period['dt_from'] = utc_to_local_time(
                            period['dt_from'], self.user.timezone)
                        period['dt_to'] = utc_to_local_time(
                            period['dt_to'], self.user.timezone)
                        period['rating'] = max(period['rating'], .0)

                for k, v in report_data.items():
                    v['periods'] = list(
                        filter(lambda p: p['total_time'], v.get('periods',
                                                                [])))

                report_data = OrderedDict(
                    (k, v) for k, v in report_data.items() if v['periods'])

            if errors:
                messages.error(self.request,
                               '<br>'.join(['%s: %s' % x for x in errors]))
                kwargs.update(messages=get_messages(self.request) or [])

            kwargs.update(report_data=report_data,
                          render_background=self.render_background,
                          enumerate=enumerate)

        return kwargs