/** * @param Request $request * @return JsonResponse */ public function logAction(Request $request) { if ($request->request->has('log')) { $data = $request->request->get('log'); $log = new Log(); $log->setTopic($data['topic'])->setValue($data['value']); $this->entityManager->persist($log); $this->entityManager->flush($log); } return JsonResponse::create(); }
/** * Persist logs to database on a defined period basis */ private function persistLogs() { if (time() - $this->startTime > self::PERSISTENCE_PERIOD) { foreach ($this->logs as $topic => $data) { $log = new Log(); $log->setTopic($topic); if (is_numeric($data[0])) { $log->setValue(round(array_sum($data) / count($data), 2)); } else { $countValues = array_count_values($data); arsort($countValues); $log->setValue(key($countValues)); $countValues = []; } $this->getEntityManager()->persist($log); } $this->flush(); $this->logs = []; $this->startTime = time(); } }
/** * @param integer $id * @return array */ public function batchLogsAction($id) { $logs = $this->logRepository->findForGraphAndBatchId($id); $phases = $this->phaseRepository->findForBatchId($id); $mappedPhases = []; /** @var Phase $phase */ foreach ($phases as $phase) { $value = sprintf('%s: %s', $phase->getName(), $phase->getValue()); if ($phase->getType() === Phase::CONTROL_TEMP || $phase->getType() === Phase::REACH_TEMP) { $value .= ' °C'; } else { $value .= ' gram'; } $mappedPhases[$phase->getId()] = $value; } $data = []; $previousLog = new Log(); /** @var Log $log */ foreach ($logs as $key => $log) { if ($log->getCreatedAt()->format('YmdHi') - $previousLog->getCreatedAt()->format('YmdHi') < 2 && $previousLog->getTopic() == $log->getTopic()) { continue; } $item = ['type' => $log->getType(), 'topic' => $log->getTopic(), 'time' => $log->getCreatedAt()->format('U') * 1000, 'value' => $log->getValue()]; if ($log->getTopic() === Log::TOPIC_PUMP_SET_STATE) { $item['value'] = 'Pomp: ' . ($log->getValue() === 'on' ? 'aan' : 'uit'); } elseif ($log->getTopic() === 'phase.start') { if (isset($mappedPhases[$log->getValue()])) { $item['value'] = $mappedPhases[$log->getValue()]; if ($previousLog->getTopic() == $log->getTopic() && $previousLog->getValue() != $log->getValue()) { $log->setValue($previousLog->getValue() . ', ' . $log->getValue()); continue; } } else { // must be set?! probably bad data! continue; } } $data[] = $item; $previousLog = clone $log; } return JsonResponse::create(['data' => $data]); }
/** * @param string $identifier * @param string $value * @param null $batch */ private function log($identifier, $value, $batch = null) { $log = new Log(); $log->setType(Log::TYPE_S88)->setTopic($identifier)->setValue($value)->setBatch($batch); $this->entityManager->persist($log); $this->entityManager->flush(); }