Skip to content

Commit

Permalink
chore: modify new analytics utils (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeevaRamu0104 authored Oct 3, 2024
1 parent 62acb61 commit 4760207
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 105 deletions.
26 changes: 16 additions & 10 deletions src/screens/NewAnalytics/NewAnalyticsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ let fillMissingDataPoints = (
~granularity: string,
) => {
open LogicUtils
let dataDict = Dict.make()
data->Array.forEach(item => {
let time = item->getDictFromJsonObject->getString(timeKey, "")
dataDict->Dict.set(time, item)
})
let dataPoints = Dict.make()
let startingPoint = startDate->DayJs.getDayJsForString
let endingPoint = endDate->DayJs.getDayJsForString
let gap = granularity->getBucketSize

for x in 1 to endingPoint.diff(startingPoint.toString(), gap) {
for x in 0 to endingPoint.diff(startingPoint.toString(), gap) {
let newDict = defaultValue->getDictFromJsonObject->Dict.copy
let timeVal = startingPoint.add(x, gap).endOf(gap).format("YYYY-MM-DD 00:00:00")
newDict->Dict.set(timeKey, timeVal->JSON.Encode.string)
dataPoints->Dict.set(timeVal, newDict->JSON.Encode.object)
switch dataDict->Dict.get(timeVal) {
| Some(val) => {
newDict->Dict.set(timeKey, timeVal->JSON.Encode.string)
dataPoints->Dict.set(timeVal, val)
}
| None => {
newDict->Dict.set(timeKey, timeVal->JSON.Encode.string)
dataPoints->Dict.set(timeVal, newDict->JSON.Encode.object)
}
}
}

data->Array.forEach(value => {
let dataDict = value->getDictFromJsonObject
dataPoints->Dict.set(dataDict->getString(timeKey, ""), value)
})

dataPoints->Dict.valuesToArray
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ let make = () => {
<PaymentsProcessed
entity={paymentsProcessedEntity} chartEntity={paymentsProcessedChartEntity}
/>
// <PaymentsSuccessRate
// entity={paymentsSuccessRateEntity} chartEntity={paymentsSuccessRateChartEntity}
// />
// <SuccessfulPaymentsDistribution
// entity={successfulPaymentsDistributionEntity}
// chartEntity={successfulPaymentsDistributionChartEntity}
// />
<PaymentsSuccessRate
entity={paymentsSuccessRateEntity} chartEntity={paymentsSuccessRateChartEntity}
/>
<SuccessfulPaymentsDistribution
entity={successfulPaymentsDistributionEntity}
chartEntity={successfulPaymentsDistributionChartEntity}
/>
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,57 @@ let getMonthName = month => {
}
}

let getCategories = (json: JSON.t, key: string): array<string> => {
json
->getArrayFromJson([])
->Array.flatMap(item => {
item
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->Array.map(item => {
let value = item->getDictFromJsonObject->getString(key, "")
let getCategories = (data: array<JSON.t>, key: string) => {
data->Array.map(item => {
let value = item->getDictFromJsonObject->getString(key, "")

if value->LogicUtils.isNonEmptyString && key == "time_bucket" {
let dateObj = value->DayJs.getDayJsForString
`${dateObj.month()->getMonthName} ${dateObj.format("DD")}`
} else {
value
}
})
if value->isNonEmptyString && key == "time_bucket" {
let dateObj = value->DayJs.getDayJsForString
`${dateObj.month()->getMonthName} ${dateObj.format("DD")}`
} else {
value
}
})
}

let getColor = index => {
["#1059C1B2", "#0EB025B2"]->Array.get(index)->Option.getOr("#1059C1B2")
}

let getLineGraphData = (json: JSON.t, key: string): LineGraphTypes.data => {
json
->getArrayFromJson([])
->Array.mapWithIndex((item, index) => {
let data =
item
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->Array.map(item => {
item->getDictFromJsonObject->getInt(key, 0)
})
let getLineGraphObj = (
~array: array<JSON.t>,
~key: string,
~name: string,
~color,
): LineGraphTypes.dataObj => {
let data = array->Array.map(item => {
item->getDictFromJsonObject->getInt(key, 0)
})
let dataObj: LineGraphTypes.dataObj = {
showInLegend: false,
name,
data,
color,
}
dataObj
}

let dataObj: LineGraphTypes.dataObj = {
showInLegend: false,
name: `Series ${(index + 1)->Int.toString}`,
data,
color: index->getColor,
}
dataObj
let getBarGraphObj = (
~array: array<JSON.t>,
~key: string,
~name: string,
~color,
): BarGraphTypes.dataObj => {
let data = array->Array.map(item => {
item->getDictFromJsonObject->getInt(key, 0)
})
let dataObj: BarGraphTypes.dataObj = {
showInLegend: false,
name,
data,
color,
}
dataObj
}

let getBarGraphData = (json: JSON.t, key: string): BarGraphTypes.data => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ module TableModule = {
let tableBorderClass = "border-collapse border border-jp-gray-940 border-solid border-2 border-opacity-30 dark:border-jp-gray-dark_table_border_color dark:border-opacity-30"

let paymentsProcessed = switch data->getArrayFromJson([])->Array.get(0) {
| Some(val) => {
let valueDict = val->getDictFromJsonObject
valueDict
->getArrayFromDict("queryData", [])
->Array.map(getDictFromJsonObject)
->Array.map(tableItemToObjMapper)
}

| Some(val) => val->getArrayDataFromJson(tableItemToObjMapper)
| _ => []
}->Array.map(Nullable.make)

Expand Down Expand Up @@ -108,7 +101,12 @@ let make = (
let updateDetails = useUpdateMethod()
let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading)
let {filterValueJson} = React.useContext(FilterContext.filterContext)
let (paymentsProcessed, setpaymentsProcessed) = React.useState(_ => JSON.Encode.array([]))

let (paymentsProcessedData, setPaymentsProcessedData) = React.useState(_ => JSON.Encode.array([]))
let (paymentsProcessedMetaData, setPaymentsProcessedMetaData) = React.useState(_ =>
JSON.Encode.array([])
)

let (selectedMetric, setSelectedMetric) = React.useState(_ => defaultMetric)
let (granularity, setGranularity) = React.useState(_ => defaulGranularity)
let (viewType, setViewType) = React.useState(_ => Graph)
Expand All @@ -124,7 +122,7 @@ let make = (
~id=Some((entity.domain: domain :> string)),
)

let body = NewAnalyticsUtils.requestBody(
let primaryBody = NewAnalyticsUtils.requestBody(
~dimensions=[],
~startTime=startTimeVal,
~endTime=endTimeVal,
Expand All @@ -135,26 +133,54 @@ let make = (
~applyFilterFor=entity.requestBodyConfig.applyFilterFor,
~granularity=granularity.value->Some,
)
let secondaryBody = NewAnalyticsUtils.requestBody(
~dimensions=[],
~startTime="2024-08-11T18:30:00Z", // use compare by function
~endTime="2024-08-18T18:30:00Z", // use compare by function
~delta=entity.requestBodyConfig.delta,
~filters=entity.requestBodyConfig.filters,
~metrics=entity.requestBodyConfig.metrics,
~customFilter=entity.requestBodyConfig.customFilter,
~applyFilterFor=entity.requestBodyConfig.applyFilterFor,
~granularity=granularity.value->Some,
)

let responses = await PromiseUtils.allSettledPolyfill([
updateDetails(url, body, Post),
updateDetails(url, body, Post),
])
let data = NewPaymentAnalyticsUtils.modifyDataWithMissingPoints(
~data=responses,
~key="queryData",
~startDate=startTimeVal,
~endDate=endTimeVal,
~defaultValue={
"count": 0,
"amount": 0,
"time_bucket": startTimeVal,
}->Identity.genericTypeToJson,
~timeKey="time_bucket",
~granularity=granularity.value,
)->Identity.genericTypeToJson
setpaymentsProcessed(_ => data)
setScreenState(_ => PageLoaderWrapper.Success)
let primaryResponse = await updateDetails(url, primaryBody, Post)
let secondaryResponse = await updateDetails(url, secondaryBody, Post)
let primaryData = primaryResponse->getDictFromJsonObject->getArrayFromDict("queryData", [])
let primaryMetaData = primaryResponse->getDictFromJsonObject->getArrayFromDict("metaData", [])

let secondaryData =
secondaryResponse->getDictFromJsonObject->getArrayFromDict("queryData", [])
let secondaryMetaData =
primaryResponse->getDictFromJsonObject->getArrayFromDict("metaData", [])

if primaryData->Array.length > 0 {
let modifiedData =
[primaryData, secondaryData]
->Array.map(data => {
NewAnalyticsUtils.fillMissingDataPoints(
~data,
~startDate=startTimeVal,
~endDate=endTimeVal,
~timeKey="time_bucket",
~defaultValue={
"payment_count": 0,
"payment_processed_amount": 0,
"time_bucket": startTimeVal,
}->Identity.genericTypeToJson,
~granularity=granularity.value,
)
})
->Identity.genericTypeToJson
setPaymentsProcessedData(_ => modifiedData)
setPaymentsProcessedMetaData(_ =>
primaryMetaData->Array.concat(secondaryMetaData)->Identity.genericTypeToJson
)
setScreenState(_ => PageLoaderWrapper.Success)
} else {
setScreenState(_ => PageLoaderWrapper.Custom)
}
} catch {
| _ => setScreenState(_ => PageLoaderWrapper.Custom)
}
Expand All @@ -171,8 +197,9 @@ let make = (
<Card>
<PageLoaderWrapper
screenState customLoader={<Shimmer layoutId=entity.title />} customUI={<NoData />}>
// Need to modify
<PaymentsProcessedHeader
title={paymentsProcessed->graphTitle}
title={paymentsProcessedMetaData->graphTitle}
viewType
setViewType
selectedMetric
Expand All @@ -186,13 +213,13 @@ let make = (
<LineGraph
entity={chartEntity}
data={chartEntity.getObjects(
~data=paymentsProcessed,
~data=paymentsProcessedData,
~xKey=selectedMetric.value,
~yKey=(#time_bucket: metrics :> string),
)}
className="mr-3"
/>
| Table => <TableModule data={paymentsProcessed} className="mx-7" />
| Table => <TableModule data={paymentsProcessedData} className="mx-7" />
}}
</div>
</PageLoaderWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ let paymentsProcessedMapper = (
~yKey: string,
): LineGraphTypes.lineGraphPayload => {
open LineGraphTypes
let categories = getCategories(data, yKey)
let data = getLineGraphData(data, xKey)
let categories =
data
->getArrayFromJson([])
->getValueFromArray(0, []->JSON.Encode.array)
->getArrayFromJson([])
->getCategories(yKey)

let lineGraphData =
data
->getArrayFromJson([])
->Array.mapWithIndex((item, index) => {
let name = `Series ${(index + 1)->Int.toString}`
let color = index->getColor
getLineGraphObj(~array=item->getArrayFromJson([]), ~key=xKey, ~name, ~color)
})
let title = {
text: "USD",
}
{categories, data, title}
{categories, data: lineGraphData, title}
}

// Need to modify
let getMetaData = json =>
json
->getArrayFromJson([])
Expand All @@ -28,7 +41,6 @@ let getMetaData = json =>
let graphTitle = json => {
let totalAmount = getMetaData(json)->getInt("amount", 0)
let currency = getMetaData(json)->getString("currency", "")

totalAmount->Int.toString ++ " " ++ currency
}

Expand Down
Loading

0 comments on commit 4760207

Please sign in to comment.