Skip to content
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
23 changes: 23 additions & 0 deletions api/cueSearch/migrations/0006_auto_20220224_0933.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.5 on 2022-02-24 09:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dataset', '0001_initial'),
('cueSearch', '0005_searchcardtemplate_connectiontype'),
]

operations = [
migrations.RemoveField(
model_name='searchcardtemplate',
name='connectionType',
),
migrations.AddField(
model_name='searchcardtemplate',
name='connectionType',
field=models.ManyToManyField(blank=True, null=True, to='dataset.ConnectionType'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.5 on 2022-02-24 10:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dataset', '0001_initial'),
('cueSearch', '0006_auto_20220224_0933'),
]

operations = [
migrations.AlterField(
model_name='searchcardtemplate',
name='connectionType',
field=models.ManyToManyField(blank=True, to='dataset.ConnectionType'),
),
]
5 changes: 2 additions & 3 deletions api/cueSearch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ def __str__(self):
class SearchCardTemplate(models.Model):
RENDER_TYPE_TABLE = "table"
RENDER_TYPE_LINE = "line"
connectionType = models.ForeignKey(
ConnectionType, on_delete=models.SET_NULL, null=True, blank=True
)

connectionType = models.ManyToManyField(ConnectionType, blank=True) #on_delete=models.SET_NULL
templateName = models.TextField(null=True, blank=True)
title = models.TextField(null=True, blank=True)
bodyText = models.TextField(null=True, blank=True)
Expand Down
15 changes: 3 additions & 12 deletions api/cueSearch/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import serializers
from dataset.models import ConnectionType, Dataset
from cueSearch.models import GlobalDimension, SearchCardTemplate

from dataset.serializers import ConnectionTypeSerializer

class AllDimensionsSerializer(serializers.ModelSerializer):
"""
Expand Down Expand Up @@ -81,18 +81,9 @@ class Meta:
class SearchCardTemplateSerializer(serializers.ModelSerializer):
"""Serializers for get Search card template"""

connectionTypeName = serializers.SerializerMethodField()
connectionTypeId = serializers.SerializerMethodField()

def get_connectionTypeName(self, obj):
name = ""
if obj.connectionType:
name = obj.connectionType.name
return name
connectionTypeName = ConnectionTypeSerializer(many=True, read_only=True)
connectionTypeId = ConnectionTypeSerializer(many=True, read_only=True)

def get_connectionTypeId(self, obj):
if obj.connectionType:
return obj.connectionType.id

class Meta:
model = SearchCardTemplate
Expand Down
66 changes: 39 additions & 27 deletions api/cueSearch/services/cardTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

logger = logging.getLogger(__name__)


class CardTemplates:
"""
Service to create, read, update & delete operation on Search card template
Expand All @@ -22,27 +21,30 @@ def createCardTemplate(payload: dict):
Create search card template
"""
try:
res = ApiResponse("Error occurred while creating search card template")
connectionTypeId = int(payload.get("connectionTypeId", 1))
connectionType = ConnectionType.objects.get(id=connectionTypeId)
res = ApiResponse("Error occur while creating search card template")
renderType = payload.get("renderType", "table")
templateName = payload.get("templateName", "")
title = payload.get("title", "")
bodyText = payload.get("bodyText", "")
sql = payload.get("sql", "")

cardTemplateObj = SearchCardTemplate.objects.create(
templateName=templateName,
title=title,
bodyText=bodyText,
sql=sql,
renderType=renderType,
connectionType=connectionType,
)

connectionTypeId = list(payload.get("connectionTypeId"))
for id in connectionTypeId:
value = ConnectionType.objects.get(id=id)
cardTemplateObj.connectionType.add(value)

res.update(True, "Search card template created successfully")
except Exception as ex:
logging.error("Error %s", str(ex))
res.update(False, "Exception occurred while creating templates")
res.update(False, "Exception occured while creating templates")
return res

@staticmethod
Expand All @@ -65,8 +67,7 @@ def updateCardTemplate(templateId: int, payload: dict):
"""Method to update card template"""
try:
res = ApiResponse("Error while updating card template")
connectionTypeId = int(payload.get("connectionTypeId"))
connectionType = ConnectionType.objects.get(id=connectionTypeId)

renderType = payload.get("renderType", "table")
templateName = payload.get("templateName", "")
title = payload.get("title", "")
Expand All @@ -80,8 +81,20 @@ def updateCardTemplate(templateId: int, payload: dict):
templateObj.title = title
templateObj.templateName = templateName
templateObj.renderType = renderType
templateObj.connectionType = connectionType
templateObj.save()

oldConnTypeId = templateObj.connectionType.values()

for item in oldConnTypeId:
connId = item['id']
templateObj.connectionType.remove(connId,None)

connectionTypeId = list(payload.get("connectionTypeId"))
for id in connectionTypeId:
value = ConnectionType.objects.get(id=id)
templateObj.connectionType.add(value)


res.update(True, "Successfully updated template")
except Exception as ex:
logging.error("Error %s", str(ex))
Expand All @@ -102,7 +115,7 @@ def publishedCardTemplate(payload: dict):
res.update(True, "Card Template published successfully")
except Exception as ex:
logging.error("Error %s", str(ex))
res.update(False, "Error occurred while publishing Card Template")
res.update(False, "Error occured while publishing Card Template")
return res

def deleteCardTemplate(templateId: int):
Expand All @@ -114,7 +127,7 @@ def deleteCardTemplate(templateId: int):
res.update(True, "Card template deleted successfully")
except Exception as ex:
logging.error("Error while deleting %s", str(ex))
res.update(False, "Error occurred while deleting card template")
res.update(False, "Error occured while deleting card template")
return res

@staticmethod
Expand All @@ -127,23 +140,22 @@ def getCardTemplateById(templateId: int):
res.update(True, "Fetched card templates", data)
except Exception as ex:
logging.error("Error while get card template by Id %s", str(ex))
res.update(False, "Error occurred while getting template by id")
res.update(False, "Error occured while getting template by id")
return res

@staticmethod
def verifyCardTemplate(payload: dict):
res = ApiResponse()
try:
sampleParams = json.loads(json.dumps(SAMPLE_PARAMS))
param = {
**sampleParams,
"templateTitle": payload["templateTitle"],
"templateText": payload["templateText"],
"templateSql": payload["templateSql"],
}
response = SearchCardTemplateServices.renderTemplatesUnsafe(param)
res.update(True, "Template rendered successfully")
except Exception as ex:
logger.error("Error in rendering templates: %s", str(ex))
res.update(False, "Error occurred during rendering", str(ex))
return res
sampleParams = json.loads(json.dumps(SAMPLE_PARAMS))
param = {
"templateTitle": payload['templateTitle'],
"templateText": payload['templateText'],
"templateSql": payload['templateSql'],
"param": sampleParams,
}
response = SearchCardTemplateServices.renderTemplates(param)
if len(response) == 0:
res.update(False,"Error occur during rendering")
else:
res.update(True,"Template rendered successfully")
return res
12 changes: 6 additions & 6 deletions api/seeddata/searchCardTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"bodyText": "This table displays raw data for dataset <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{dataset}}</span> with filter <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter}}</span> ",
"sql": "SELECT * FROM ({{ datasetSql|safe }}) WHERE {{filter|safe}} limit 500",
"renderType": "table",
"connectionType": 6
"connectionType": [6]
}
},
{
Expand All @@ -22,7 +22,7 @@
"bodyText": "{% load event_tags %} {% for filterDim in filterDimensions %} {% conditionalCount searchResults 'dimension' filterDim as dimCount %} {% if dimCount > 1 %} {% for metricName in metrics %} This chart displays filtered values on dimension <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filterDim}}</span> along with other filters applied i.e. <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter|safe}}</span> for metric <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{metricName}}</span> on dataset <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{dataset}}</span> +-; {% endfor %} {% endif %} {% endfor %}",
"sql": "{% load event_tags %} {% for filterDim in filterDimensions %} {% conditionalCount searchResults 'dimension' filterDim as dimCount %} {% if dimCount > 1 %} {% for metricName in metrics %} SELECT ({{ timestampColumn }}), {{ filterDim }}, SUM({{ metricName }}) as {{metricName}} FROM ({{ datasetSql|safe }}) WHERE {{filter|safe}} GROUP BY 1, 2 limit 500 +-; {% endfor %} {% endif %} {% endfor %}",
"renderType": "line",
"connectionType": 6
"connectionType": [6]
}
},
{
Expand All @@ -35,7 +35,7 @@
"bodyText": " {% for metric in metrics %} For <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter}}</span> +-; {% endfor %}",
"sql": " {% for metric in metrics %} SELECT ({{ timestampColumn }}), SUM({{ metric }}) as {{ metric }} FROM ({{ datasetSql|safe }}) WHERE {{filter|safe}} GROUP BY 1 limit 500 +-; {% endfor %}",
"renderType": "line",
"connectionType": 6
"connectionType": [6]
}
},
{
Expand All @@ -48,7 +48,7 @@
"bodyText": "This table displays raw data for dataset <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{dataset}}</span> with filter <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter}}</span> ",
"sql": "SELECT * FROM ({{ datasetSql|safe }}) AS templatetable WHERE {% for orResults in groupedResultsForFilter %} {% for orResult in orResults %} \"templatetable\".\"{{ orResult.dimension }}\" = '{{ orResult.value }}' OR {% endfor %} True AND {% endfor %} True limit 500",
"renderType": "table",
"connectionType": 1
"connectionType": [1]
}
},
{
Expand All @@ -61,7 +61,7 @@
"bodyText": "{% load event_tags %} {% for filterDim in filterDimensions %} {% conditionalCount searchResults 'dimension' filterDim as dimCount %} {% if dimCount > 1 %} {% for metricName in metrics %} This chart displays filtered values on dimension <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filterDim}}</span> along with other filters applied i.e. <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter|safe}}</span> for metric <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{metricName}}</span> on dataset <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{dataset}}</span> +-; {% endfor %} {% endif %} {% endfor %}",
"sql": "{% load event_tags %} {% for filterDim in filterDimensions %} {% conditionalCount searchResults 'dimension' filterDim as dimCount %} {% if dimCount > 1 %} {% for metricName in metrics %} SELECT \"templatetable\".\"{{ timestampColumn }}\", \"templatetable\".\"{{ filterDim }}\", SUM(\"templatetable\".\"{{ metricName }}\") as {{metricName}} FROM ({{ datasetSql|safe }}) AS templatetable WHERE {% for orResults in groupedResultsForFilter %} {% for orResult in orResults %} \"templatetable\".\"{{ orResult.dimension }}\" = '{{ orResult.value }}' OR {% endfor %} True AND {% endfor %} True GROUP BY 1, 2 limit 500 +-; {% endfor %} {% endif %} {% endfor %}",
"renderType": "line",
"connectionType": 1
"connectionType": [1]
}
},
{
Expand All @@ -74,7 +74,7 @@
"bodyText": " {% for metric in metrics %} For <span style=\"background:#eee; padding: 0 4px; border-radius: 4px;\">{{filter}}</span> +-; {% endfor %}",
"sql": " {% for metric in metrics %} SELECT \"templatetable\".\"{{ timestampColumn }}\", SUM(\"templatetable\".\"{{ metric }}\") as {{ metric }} FROM ({{ datasetSql|safe }}) AS templatetable WHERE {% for orResults in groupedResultsForFilter %} {% for orResult in orResults %} \"templatetable\".\"{{ orResult.dimension }}\" = '{{ orResult.value }}' OR {% endfor %} True AND {% endfor %} True GROUP BY 1 limit 500 +-; {% endfor %}",
"renderType": "line",
"connectionType": 1
"connectionType": [1]
}
},
{
Expand Down
5 changes: 4 additions & 1 deletion ui/src/components/Search/CardTemplates/AddCardTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { Option } = Select;
export default function AddCardTemplates(props) {
const [form] = Form.useForm();
const [renderType, setRenderType] = useState("table");
const [connectionType, setConnectionType] = useState();
const [connectionType, setConnectionType] = useState([]);
useEffect(() => {
getConnectionType();
}, []);
Expand All @@ -32,6 +32,7 @@ export default function AddCardTemplates(props) {
const addCardTemplateFormSubmit = async (values) => {
let payload = {};
let connType = values["connectionType"].split(".");
console.log(connType);
payload["connectionTypeId"] = connType[0];
payload["connectionTypeName"] = connType[1];
payload["templateName"] = values["templateName"];
Expand Down Expand Up @@ -166,6 +167,8 @@ export default function AddCardTemplates(props) {
]}
>
<Select
showSearch
mode="tags"
style={{ width: "100%" }}
placeholder="Card Template Connection Type"
onChange={onSelectConnectionTypeChange}
Expand Down
2 changes: 2 additions & 0 deletions ui/src/components/Search/CardTemplates/EditCardTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export default function EditCardTemplate(props) {
]}
>
<Select
showSearch
mode="tags"
style={{ width: "100%" }}
placeholder="Card Template Connection Type"
onChange={onSelectConnectionTypeChange}
Expand Down