Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature RM#66980:log reader #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import com.axelor.contact.service.HelloServiceImpl;
import com.axelor.sale.service.AccessSaleQuickMenu;
import com.axelor.sale.service.HelloServiceSaleImpl;
import com.axelor.sale.service.LogReaderService;
import com.axelor.sale.service.LogReaderServiceImpl;

public class SaleModule extends AxelorModule {

@Override
protected void configure() {
bind(HelloServiceImpl.class).to(HelloServiceSaleImpl.class);
bind(AccessContactQuickMenu.class).to(AccessSaleQuickMenu.class);
bind(LogReaderService.class).to(LogReaderServiceImpl.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2005-2022 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.axelor.sale.service;

import java.time.LocalDateTime;
import java.util.List;

public interface LogReaderService {

List<String> readLogReaderFilter(
String path, int limit, int offset, LocalDateTime startDateTime, LocalDateTime endDateTime);

public int countTotalLines(
String path, int limit, int offset, LocalDateTime startDateTime, LocalDateTime endDateTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2005-2022 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.axelor.sale.service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class LogReaderServiceImpl implements LogReaderService {

@Override
public List<String> readLogReaderFilter(
String path, int limit, int offset, LocalDateTime startDateTime, LocalDateTime endDateTime) {
List<String> filteredLogEntries = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(path))) {

if (startDateTime != null && endDateTime != null) {
Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
filteredLogEntries =
br.lines()
.filter(
logLine -> {
Matcher matcher = pattern.matcher(logLine);
if (matcher.find()) {
String dateTimeStr = matcher.group();
LocalDateTime logDateTime =
LocalDateTime.parse(
dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return logDateTime.isAfter(startDateTime)
&& logDateTime.isBefore(endDateTime);
}
return false;
})
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
} else {
filteredLogEntries = br.lines().skip(offset).limit(limit).collect(Collectors.toList());
}

} catch (IOException e) {
e.printStackTrace();
}
return filteredLogEntries;
}

@Override
public int countTotalLines(
String path, int limit, int offset, LocalDateTime startDateTime, LocalDateTime endDateTime) {

int count = 0;
List<String> filteredLogEntries = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
if (startDateTime != null && endDateTime != null) {

Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
filteredLogEntries =
br.lines()
.filter(
logLine -> {
Matcher matcher = pattern.matcher(logLine);
if (matcher.find()) {
String dateTimeStr = matcher.group();
LocalDateTime logDateTime =
LocalDateTime.parse(
dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return logDateTime.isAfter(startDateTime)
&& logDateTime.isBefore(endDateTime);
}
return false;
})
.collect(Collectors.toList());

count = filteredLogEntries.size();

} else {
filteredLogEntries = br.lines().collect(Collectors.toList());
count = filteredLogEntries.size();
}

} catch (Exception e) {
e.printStackTrace();
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2005-2022 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.axelor.sale.web;

import com.axelor.db.JpaSupport;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.axelor.sale.db.LogReader;
import com.axelor.sale.db.repo.LogReaderRepository;
import com.axelor.sale.service.LogReaderService;
import java.util.List;
import java.util.stream.Collectors;

public class LogReaderController extends JpaSupport {

public void setFileLog(ActionRequest request, ActionResponse response) {
LogReader logReader = request.getContext().asType(LogReader.class);

if (logReader.getId() == null) {
logReader = Beans.get(LogReaderRepository.class).find(logReader.getId());
}
LogReaderService logReaderService = Beans.get(LogReaderService.class);
String path = "/home/axelor/Documents/console.log";

List<String> readLogReaderFilter =
logReaderService.readLogReaderFilter(
path,
logReader.getNoOfLineToLoad(),
logReader.getOffSet(),
logReader.getStartDate(),
logReader.getEndDate());

int totalLines =
logReaderService.countTotalLines(
path,
logReader.getNoOfLineToLoad(),
logReader.getOffSet(),
logReader.getStartDate(),
logReader.getEndDate());

String collect = readLogReaderFilter.stream().collect(Collectors.joining("\n"));
String log =
(logReader.getLog() == null && logReader.getOffSet() == null)
? readLogReaderFilter.stream().collect(Collectors.joining("\n"))
: logReader.getLog();

response.setValue("log", (log+ collect+"\n"));
response.setValue("offSet", logReader.getOffSet() + logReader.getNoOfLineToLoad());

if ((logReader.getOffSet() + logReader.getNoOfLineToLoad()) > totalLines) {
response.setValue("offSet", 0);
response.setNotify("No more log available");
}
}
}
16 changes: 16 additions & 0 deletions modules/demo-sale/src/main/resources/domains/LogReader.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<domain-models xmlns="http://axelor.com/xml/ns/domain-models"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://axelor.com/xml/ns/domain-models https://axelor.com/xml/ns/domain-models/domain-models_6.1.xsd">

<module name="sale" package="com.axelor.sale.db"/>

<entity name="LogReader" cacheable="true">
<datetime name="startDate" title="Start Date"/>
<datetime name="endDate" title="End Date"/>
<integer name="noOfLineToLoad" title="Number of line to load" min="10"/>
<string name="log" title="Log" readonly="true" large="true"/>
<integer name="offSet" default="0"/>
</entity>

</domain-models>
3 changes: 2 additions & 1 deletion modules/demo-sale/src/main/resources/views/Currency.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<object-views xmlns="http://axelor.com/xml/ns/object-views"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://axelor.com/xml/ns/object-views https://axelor.com/xml/ns/object-views/object-views_6.1.xsd">

<grid name="currency-grid" title="Currencies" model="com.axelor.sale.db.Currency">
Expand Down
44 changes: 44 additions & 0 deletions modules/demo-sale/src/main/resources/views/LogReader.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://axelor.com/xml/ns/object-views https://axelor.com/xml/ns/object-views/object-views_6.1.xsd">

<grid name="log-reader-grid" title="Log Reader" model="com.axelor.sale.db.LogReader">
<field name="startDate" />
<field name="endDate" />
</grid>

<form name="log-reader-form" title="Log Reader"
model="com.axelor.sale.db.LogReader" width="large" onLoad="action-record-log-default-value" >
<panel name="mainPanel">
<field name="startDate" onChange="action-record-log-default-value" />
<field name="endDate" onChange="action-record-log-default-value" />
<field name="noOfLineToLoad" colSpan="3"/>
<button name="loadBtn" title="Load" colSpan="3" onClick="save,action-log-reader-check-onClick,save" />
<button name="clearBtn" title="Clear" colSpan="3" onClick="action-record-log-default-value"/>
<field name="offSet" hidden="true"/>
</panel>
<panel name="logGroupPanel" title="Log">
<field name="log" colSpan="12" widget="code-editor" showTitle="false"/>
</panel>
</form>

<action-method name="action-log-batch-data-load">
<call class="com.axelor.sale.web.LogReaderController" method="setFileLog"/>
</action-method>

<action-record name="action-record-log-default-value" model="com.axelor.sale.db.LogReader">
<field name="offSet" expr="0"/>
<field name="log" expr=" "/>
</action-record>

<action-validate name="action-check-log-dates">
<info message="Log date should be proper ." if="startDate &gt; endDate"/>
</action-validate>

<action-group name="action-log-reader-check-onClick">
<action name="action-log-batch-data-load"/>
<action name="action-check-log-dates"/>
</action-group>

</object-views>
11 changes: 11 additions & 0 deletions modules/demo-sale/src/main/resources/views/Menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
<view type="search" name="sale-order-search"/>
</action-view>

<menuitem name="menu-logreader" parent="menu-sales"
title="Log Reader"
action="log.reader"/>

<action-view name="log.reader" model="com.axelor.sale.db.LogReader" title="Log Reader">
<view type="grid"/>
<view type="form"/>
</action-view>



<menuitem name="menu-sales-config" parent="menu-sales" title="Configuration"/>

<menuitem name="menu-sales-product-category" parent="menu-sales-config"
Expand Down
16 changes: 8 additions & 8 deletions modules/demo-sale/src/main/resources/views/Order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@
<![CDATA[
<div>
<div class="span6">
<strong>{{ name }}</strong>
</div>
<div class="span6">
<span class="tag-select pull-right" style="border: 0;">
<strong>{{ name }}</strong>
</div>
<div class="span6">
<span class="tag-select pull-right" style="border: 0;">
<span class="label label-primary blue" ng-if="record.status == 'DRAFT'"><span x-translate>Draft</span></span>
<span class="label label-primary yellow" ng-if="record.status == 'OPEN'"><span x-translate>Open</span></span>
<span class="label label-primary red" ng-if="record.status == 'CANCELED'"><span x-translate>Canceled</span></span>
<span class="label label-primary green" ng-if="record.status == 'CLOSED'"><span x-translate>Closed</span></span>
</span>
</div>
</div>
<div>
</div>
</div>
<div>
<div class="span6">
<p><i class="fa fa-users" aria-hidden="true"></i>&nbsp;&nbsp;{{customer.fullName}}</p>
<p><i class="fa fa-clock-o" aria-hidden="true"></i>&nbsp;&nbsp;{{$fmt('orderDate')}}</p>
Expand All @@ -114,7 +114,7 @@
<br/>
<p><strong class="pull-right"><span x-translate="">ATI</span>:&nbsp;{{$fmt('totalAmount')}} {{$fmt('currency.symbol')}}</strong></p>
</div>
</div>
</div>
]]>
</template>
</cards>
Expand Down
Loading