Merge branch 'feature/query-stats' into feature/CTLE-507-custom-ct-live-packing-slips

This commit is contained in:
2023-06-26 12:09:52 -05:00
43 changed files with 3563 additions and 68 deletions

View File

@ -28,13 +28,21 @@ import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.actions.async.AsyncRecordPipeLoop;
import com.kingsrook.qqq.backend.core.actions.reporting.RecordPipe;
import com.kingsrook.qqq.backend.core.actions.tables.helpers.QueryStatManager;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
import com.kingsrook.qqq.backend.core.model.querystats.QueryStat;
import com.kingsrook.qqq.backend.core.model.querystats.QueryStatMetaDataProvider;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.tables.QQQTablesMetaDataProvider;
import com.kingsrook.qqq.backend.core.modules.backend.implementations.mock.MockQueryAction;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
@ -390,6 +398,59 @@ class QueryActionTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testQueryManager() throws QException
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// add tables for QueryStats, and turn them on in the memory backend, then start the query-stat manager //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
QInstance qInstance = QContext.getQInstance();
qInstance.getBackend(TestUtils.MEMORY_BACKEND_NAME).withCapability(Capability.QUERY_STATS);
new QQQTablesMetaDataProvider().defineAll(qInstance, TestUtils.MEMORY_BACKEND_NAME, TestUtils.MEMORY_BACKEND_NAME, null);
new QueryStatMetaDataProvider().defineAll(qInstance, TestUtils.MEMORY_BACKEND_NAME, null);
QueryStatManager.getInstance().start(QContext.getQInstance(), QSession::new);
/////////////////////////////////////////////////////////////////////////////////
// insert some order "trees", then query them, so some stats will get recorded //
/////////////////////////////////////////////////////////////////////////////////
insert2OrdersWith3Lines3LineExtrinsicsAnd4OrderExtrinsicAssociations();
QueryInput queryInput = new QueryInput();
queryInput.setTableName(TestUtils.TABLE_NAME_ORDER);
queryInput.setIncludeAssociations(true);
queryInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderBy("id")));
QContext.pushAction(queryInput);
QueryOutput queryOutput = new QueryAction().execute(queryInput);
////////////////////////////////////////////////////////////
// run the stat manager (so we don't have to wait for it) //
////////////////////////////////////////////////////////////
QueryStatManager.getInstance().storeStatsNow();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// stat manager expects to be ran in a thread, where it needs to clear context, so reset context after it //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
QContext.init(qInstance, new QSession());
////////////////////////////////////////////////
// query to see that some stats were inserted //
////////////////////////////////////////////////
queryInput = new QueryInput();
queryInput.setTableName(QueryStat.TABLE_NAME);
QContext.pushAction(queryInput);
queryOutput = new QueryAction().execute(queryInput);
///////////////////////////////////////////////////////////////////////////////////
// selecting all of those associations should have caused (at least?) 4 queries. //
// this is the most basic test here, but we'll take it. //
///////////////////////////////////////////////////////////////////////////////////
assertThat(queryOutput.getRecords().size()).isGreaterThanOrEqualTo(4);
}
/*******************************************************************************
**
*******************************************************************************/

View File

@ -30,8 +30,10 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/*******************************************************************************
@ -224,6 +226,53 @@ class QMetaDataVariableInterpreterTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testGetBooleanFromPropertyOrEnvironment()
{
QMetaDataVariableInterpreter interpreter = new QMetaDataVariableInterpreter();
//////////////////////////////////////////////////////////
// if neither prop nor env is set, get back the default //
//////////////////////////////////////////////////////////
assertFalse(interpreter.getBooleanFromPropertyOrEnvironment("notSet", "NOT_SET", false));
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("notSet", "NOT_SET", true));
/////////////////////////////////////////////
// unrecognized values are same as not set //
/////////////////////////////////////////////
System.setProperty("unrecognized", "asdf");
interpreter.setEnvironmentOverrides(Map.of("UNRECOGNIZED", "1234"));
assertFalse(interpreter.getBooleanFromPropertyOrEnvironment("unrecognized", "UNRECOGNIZED", false));
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("unrecognized", "UNRECOGNIZED", true));
/////////////////////////////////
// if only prop is set, get it //
/////////////////////////////////
assertFalse(interpreter.getBooleanFromPropertyOrEnvironment("foo.enabled", "FOO_ENABLED", false));
System.setProperty("foo.enabled", "true");
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("foo.enabled", "FOO_ENABLED", false));
////////////////////////////////
// if only env is set, get it //
////////////////////////////////
assertFalse(interpreter.getBooleanFromPropertyOrEnvironment("bar.enabled", "BAR_ENABLED", false));
interpreter.setEnvironmentOverrides(Map.of("BAR_ENABLED", "true"));
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("bar.enabled", "BAR_ENABLED", false));
///////////////////////////////////
// if both are set, get the prop //
///////////////////////////////////
System.setProperty("baz.enabled", "true");
interpreter.setEnvironmentOverrides(Map.of("BAZ_ENABLED", "false"));
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("baz.enabled", "BAZ_ENABLED", true));
assertTrue(interpreter.getBooleanFromPropertyOrEnvironment("baz.enabled", "BAZ_ENABLED", false));
}
/*******************************************************************************
**
*******************************************************************************/

View File

@ -23,10 +23,14 @@ package com.kingsrook.qqq.backend.core.model.data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.data.testentities.Item;
import com.kingsrook.qqq.backend.core.model.data.testentities.ItemWithPrimitives;
import com.kingsrook.qqq.backend.core.model.data.testentities.LineItem;
import com.kingsrook.qqq.backend.core.model.data.testentities.Order;
import com.kingsrook.qqq.backend.core.model.metadata.fields.DisplayFormat;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
@ -34,6 +38,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -304,7 +309,103 @@ class QRecordEntityTest extends BaseTest
assertEquals(QFieldType.STRING, qTableMetaData.getField("sku").getType());
assertEquals(QFieldType.INTEGER, qTableMetaData.getField("quantity").getType());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testOrderWithAssociationsToQRecord() throws QException
{
Order order = new Order();
order.setOrderNo("ORD001");
order.setLineItems(List.of(
new LineItem().withSku("ABC").withQuantity(1),
new LineItem().withSku("DEF").withQuantity(2)
));
QRecord qRecord = order.toQRecord();
assertEquals("ORD001", qRecord.getValueString("orderNo"));
List<QRecord> lineItems = qRecord.getAssociatedRecords().get("lineItems");
assertNotNull(lineItems);
assertEquals(2, lineItems.size());
assertEquals("ABC", lineItems.get(0).getValueString("sku"));
assertEquals(1, lineItems.get(0).getValueInteger("quantity"));
assertEquals("DEF", lineItems.get(1).getValueString("sku"));
assertEquals(2, lineItems.get(1).getValueInteger("quantity"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testOrderWithoutAssociationsToQRecord() throws QException
{
Order order = new Order();
order.setOrderNo("ORD001");
order.setLineItems(null);
QRecord qRecord = order.toQRecord();
assertEquals("ORD001", qRecord.getValueString("orderNo"));
List<QRecord> lineItems = qRecord.getAssociatedRecords().get("lineItems");
assertNull(lineItems);
order.setLineItems(new ArrayList<>());
qRecord = order.toQRecord();
lineItems = qRecord.getAssociatedRecords().get("lineItems");
assertNotNull(lineItems);
assertEquals(0, lineItems.size());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testQRecordWithAssociationsToOrder() throws QException
{
QRecord qRecord = new QRecord()
.withValue("orderNo", "ORD002")
.withAssociatedRecords("lineItems", List.of(
new QRecord().withValue("sku", "AB12").withValue("quantity", 42),
new QRecord().withValue("sku", "XY89").withValue("quantity", 47)
));
Order order = qRecord.toEntity(Order.class);
assertEquals("ORD002", order.getOrderNo());
assertEquals(2, order.getLineItems().size());
assertEquals("AB12", order.getLineItems().get(0).getSku());
assertEquals(42, order.getLineItems().get(0).getQuantity());
assertEquals("XY89", order.getLineItems().get(1).getSku());
assertEquals(47, order.getLineItems().get(1).getQuantity());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testQRecordWithoutAssociationsToOrder() throws QException
{
QRecord qRecord = new QRecord().withValue("orderNo", "ORD002");
Order order = qRecord.toEntity(Order.class);
assertEquals("ORD002", order.getOrderNo());
assertNull(order.getLineItems());
qRecord.withAssociatedRecords("lineItems", null);
order = qRecord.toEntity(Order.class);
assertNull(order.getLineItems());
qRecord.withAssociatedRecords("lineItems", new ArrayList<>());
order = qRecord.toEntity(Order.class);
assertNotNull(order.getLineItems());
assertEquals(0, order.getLineItems().size());
}
}

View File

@ -43,6 +43,7 @@ public class Item extends QRecordEntity
@QField(isEditable = false, displayFormat = DisplayFormat.COMMAS)
private Integer quantity;
@QField()
private BigDecimal price;
@QField(backendName = "is_featured")

View File

@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.model.data.testentities;
import java.math.BigDecimal;
import com.kingsrook.qqq.backend.core.model.data.QField;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
@ -31,11 +32,20 @@ import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
*******************************************************************************/
public class ItemWithPrimitives extends QRecordEntity
{
private String sku;
private String description;
private int quantity;
@QField()
private String sku;
@QField()
private String description;
@QField()
private int quantity;
@QField()
private BigDecimal price;
private boolean featured;
@QField()
private boolean featured;

View File

@ -0,0 +1,102 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* 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.kingsrook.qqq.backend.core.model.data.testentities;
import com.kingsrook.qqq.backend.core.model.data.QField;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
/*******************************************************************************
** Sample of an entity that can be converted to & from a QRecord
*******************************************************************************/
public class LineItem extends QRecordEntity
{
@QField()
private String sku;
@QField()
private Integer quantity;
/*******************************************************************************
** Getter for sku
*******************************************************************************/
public String getSku()
{
return (this.sku);
}
/*******************************************************************************
** Setter for sku
*******************************************************************************/
public void setSku(String sku)
{
this.sku = sku;
}
/*******************************************************************************
** Fluent setter for sku
*******************************************************************************/
public LineItem withSku(String sku)
{
this.sku = sku;
return (this);
}
/*******************************************************************************
** Getter for quantity
*******************************************************************************/
public Integer getQuantity()
{
return (this.quantity);
}
/*******************************************************************************
** Setter for quantity
*******************************************************************************/
public void setQuantity(Integer quantity)
{
this.quantity = quantity;
}
/*******************************************************************************
** Fluent setter for quantity
*******************************************************************************/
public LineItem withQuantity(Integer quantity)
{
this.quantity = quantity;
return (this);
}
}

View File

@ -0,0 +1,104 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* 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.kingsrook.qqq.backend.core.model.data.testentities;
import java.util.List;
import com.kingsrook.qqq.backend.core.model.data.QAssociation;
import com.kingsrook.qqq.backend.core.model.data.QField;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
/*******************************************************************************
** Sample of an entity that can be converted to & from a QRecord
*******************************************************************************/
public class Order extends QRecordEntity
{
@QField()
private String orderNo;
@QAssociation(name = "lineItems")
private List<LineItem> lineItems;
/*******************************************************************************
** Getter for orderNo
*******************************************************************************/
public String getOrderNo()
{
return (this.orderNo);
}
/*******************************************************************************
** Setter for orderNo
*******************************************************************************/
public void setOrderNo(String orderNo)
{
this.orderNo = orderNo;
}
/*******************************************************************************
** Fluent setter for orderNo
*******************************************************************************/
public Order withOrderNo(String orderNo)
{
this.orderNo = orderNo;
return (this);
}
/*******************************************************************************
** Getter for lineItems
*******************************************************************************/
public List<LineItem> getLineItems()
{
return (this.lineItems);
}
/*******************************************************************************
** Setter for lineItems
*******************************************************************************/
public void setLineItems(List<LineItem> lineItems)
{
this.lineItems = lineItems;
}
/*******************************************************************************
** Fluent setter for lineItems
*******************************************************************************/
public Order withLineItems(List<LineItem> lineItems)
{
this.lineItems = lineItems;
return (this);
}
}

View File

@ -69,6 +69,16 @@ class QTableMetaDataTest extends BaseTest
// table:false & backend:false = false
assertFalse(new QTableMetaData().withoutCapability(capability).isCapabilityEnabled(new QBackendMetaData().withoutCapability(capability), capability));
// backend false, but then true = true
assertTrue(new QTableMetaData().isCapabilityEnabled(new QBackendMetaData().withoutCapability(capability).withCapability(capability), capability));
// backend true, but then false = false
assertFalse(new QTableMetaData().isCapabilityEnabled(new QBackendMetaData().withCapability(capability).withoutCapability(capability), capability));
// table true, but then false = true
assertFalse(new QTableMetaData().withCapability(capability).withoutCapability(capability).isCapabilityEnabled(new QBackendMetaData(), capability));
}
}