From 476020779802fee0f3d7b3ac30f25366ba0c7050 Mon Sep 17 00:00:00 2001 From: Jeeva Ramachandran <120017870+JeevaRamu0104@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:09:21 +0530 Subject: [PATCH] chore: modify new analytics utils (#1531) --- .../NewAnalytics/NewAnalyticsUtils.res | 26 ++++-- .../PaymentAnalytics/NewPaymentAnalytics.res | 14 +-- .../NewPaymentAnalyticsUtils.res | 75 ++++++++------- .../PaymentsProcessed/PaymentsProcessed.res | 91 ++++++++++++------- .../PaymentsProcessedUtils.res | 22 ++++- .../PaymentsSuccessRate.res | 61 +++++++++++-- .../PaymentsSuccessRateUtils.res | 19 +++- .../SuccessfulPaymentsDistribution.res | 4 +- .../SuccessfulPaymentsDistributionUtils.res | 15 ++- 9 files changed, 222 insertions(+), 105 deletions(-) diff --git a/src/screens/NewAnalytics/NewAnalyticsUtils.res b/src/screens/NewAnalytics/NewAnalyticsUtils.res index 326d678a0..e60754a3d 100644 --- a/src/screens/NewAnalytics/NewAnalyticsUtils.res +++ b/src/screens/NewAnalytics/NewAnalyticsUtils.res @@ -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 } diff --git a/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalytics.res b/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalytics.res index 319eddf54..b09e1b7ae 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalytics.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalytics.res @@ -9,12 +9,12 @@ let make = () => { - // - // + + } diff --git a/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalyticsUtils.res b/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalyticsUtils.res index 8532ac419..8a1cee534 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalyticsUtils.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/NewPaymentAnalyticsUtils.res @@ -18,23 +18,16 @@ let getMonthName = month => { } } -let getCategories = (json: JSON.t, key: string): array => { - json - ->getArrayFromJson([]) - ->Array.flatMap(item => { - item - ->getDictFromJsonObject - ->getArrayFromDict("queryData", []) - ->Array.map(item => { - let value = item->getDictFromJsonObject->getString(key, "") +let getCategories = (data: array, 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 + } }) } @@ -42,26 +35,40 @@ 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, + ~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, + ~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 => { diff --git a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessed.res b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessed.res index 5649edb09..b0a11e7cc 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessed.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessed.res @@ -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) @@ -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) @@ -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, @@ -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) } @@ -171,8 +197,9 @@ let make = ( } customUI={}> + // Need to modify graphTitle} + title={paymentsProcessedMetaData->graphTitle} viewType setViewType selectedMetric @@ -186,13 +213,13 @@ let make = ( string), )} className="mr-3" /> - | Table => + | Table => }} diff --git a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessedUtils.res b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessedUtils.res index 62eb00c6a..584e5200c 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessedUtils.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsProcessed/PaymentsProcessedUtils.res @@ -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([]) @@ -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 } diff --git a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRate.res b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRate.res index c92af94c5..8709ac3e5 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRate.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRate.res @@ -37,7 +37,14 @@ let make = ( let getURL = useGetURL() let updateDetails = useUpdateMethod() let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading) - let (paymentsSuccessRate, setpaymentsSuccessRate) = React.useState(_ => JSON.Encode.array([])) + + let (paymentsSuccessRateData, setPaymentsSuccessRateData) = React.useState(_ => + JSON.Encode.array([]) + ) + let (_paymentsSuccessRateMetaData, setpaymentsProcessedMetaData) = React.useState(_ => + JSON.Encode.array([]) + ) + let (granularity, setGranularity) = React.useState(_ => defaulGranularity) let {filterValueJson} = React.useContext(FilterContext.filterContext) let startTimeVal = filterValueJson->getString("startTime", "") @@ -52,7 +59,7 @@ let make = ( ~id=Some((entity.domain: domain :> string)), ) - let body = NewAnalyticsUtils.requestBody( + let primaryBody = NewAnalyticsUtils.requestBody( ~dimensions=[], ~startTime=startTimeVal, ~endTime=endTimeVal, @@ -63,15 +70,49 @@ 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 response = await updateDetails(url, body, Post) - let arr = - response - ->getDictFromJsonObject - ->getArrayFromDict("queryData", []) + 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", []) - if arr->Array.length > 0 { - setpaymentsSuccessRate(_ => [response]->JSON.Encode.array) + 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 + setPaymentsSuccessRateData(_ => modifiedData) + setpaymentsProcessedMetaData(_ => + primaryMetaData->Array.concat(secondaryMetaData)->Identity.genericTypeToJson + ) setScreenState(_ => PageLoaderWrapper.Success) } else { setScreenState(_ => PageLoaderWrapper.Custom) @@ -98,7 +139,7 @@ let make = ( string), ~yKey=(#time_bucket: metrics :> string), )} diff --git a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRateUtils.res b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRateUtils.res index 761afdbe9..32df32822 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRateUtils.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/PaymentsSuccessRate/PaymentsSuccessRateUtils.res @@ -19,12 +19,25 @@ let paymentsSuccessRateMapper = ( ~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: "Payments Success Rate", } - {categories, data, title} + {categories, data: lineGraphData, title} } open NewAnalyticsTypes diff --git a/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistribution.res b/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistribution.res index e24e0ecf4..13a0b330a 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistribution.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistribution.res @@ -69,6 +69,7 @@ let make = (~entity: moduleEntity, ~chartEntity: chartEntity PageLoaderWrapper.Loading) let (paymentsDistribution, setpaymentsDistribution) = React.useState(_ => JSON.Encode.array([])) + let (viewType, setViewType) = React.useState(_ => Graph) let (groupBy, setGroupBy) = React.useState(_ => defaulGroupBy) let startTimeVal = filterValueJson->getString("startTime", "") @@ -96,13 +97,14 @@ let make = (~entity: moduleEntity, ~chartEntity: chartEntitygetDictFromJsonObject->getArrayFromDict("queryData", []) let arr = response ->getDictFromJsonObject ->getArrayFromDict("queryData", []) if arr->Array.length > 0 { - setpaymentsDistribution(_ => [response]->JSON.Encode.array) + setpaymentsDistribution(_ => responseData->JSON.Encode.array) setScreenState(_ => PageLoaderWrapper.Success) } else { setScreenState(_ => PageLoaderWrapper.Custom) diff --git a/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistributionUtils.res b/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistributionUtils.res index 86d14a070..c7d1f9770 100644 --- a/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistributionUtils.res +++ b/src/screens/NewAnalytics/PaymentAnalytics/SuccessfulPaymentsDistribution/SuccessfulPaymentsDistributionUtils.res @@ -18,12 +18,21 @@ let successfulPaymentsDistributionMapper = ( ~yKey: string, ): BarGraphTypes.barGraphPayload => { open BarGraphTypes - let categories = getCategories(data, yKey) - let data = getBarGraphData(data, xKey) + let categories = + data + ->getArrayFromJson([]) + ->getCategories(yKey) + + let barGraphData = getBarGraphObj( + ~array=data->getArrayFromJson([]), + ~key=xKey, + ~name=xKey->snakeToTitle, + ~color="#7CC88F", + ) let title = { text: "", } - {categories, data, title} + {categories, data: [barGraphData], title} } open NewAnalyticsTypes