diff --git a/src/Pages/OpenTelemetry.js b/src/Pages/OpenTelemetry.js index 44a73ac..100a711 100644 --- a/src/Pages/OpenTelemetry.js +++ b/src/Pages/OpenTelemetry.js @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { Box, Grid, @@ -31,50 +31,47 @@ const granularities = [ { key: "month", label: "Month" } ]; +const groupes = [ + { key: "date", label: "Date" }, + { key: "country", label: "country" } +]; + const OpenTelemetry = () => { const [startDate, setStartDate] = useState(""); const [endDate, setEndDate] = useState(""); const [country, setCountry] = useState(""); const [category, setCategory] = useState("summary"); const [granularity, setGranularity] = useState("month"); + const [group_by, setGroup_by] = useState("month"); const [data, setData] = useState([]); const [page, setPage] = useState(1); const [rowsPerPage, setRowsPerPage] = useState(5); const [error, setError] = useState(null); + const [loading, setLoading] = useState(false); const tableData = data?.[category]?.data || []; const [totalUsers, setTotalUsers] = useState(0); - const [totalRetained, setTotalRetained] = useState(0); - const [signupCountries, setSignupCountries] = useState(0); - const [retainedCountries, setRetainedCountries] = useState(0); + const [totalSignupCountries, setTotalSignupCountries] = useState(0); const countryNames = Object.entries(countries.getNames("en")); + const [userswithToken, setUsers_with_Tokens] = useState(0); - // Apply filters and set the filtered data const filteredData = country ? tableData.filter((row) => countries.getName(row.country_code, "en") === country) : tableData; - // Calculate totals for filtered data - const countryTotals = filteredData?.reduce( - (totals, row) => { - totals.signup_users += row.signup_users || 0; - totals.retained_users += row.retained_users || 0; - return totals; - }, - { signup_users: 0, retained_users: 0 } - ) || { signup_users: 0, retained_users: 0 }; - const applyFilters = async () => { if (!startDate || !endDate) { setError("Please select both start and end dates."); return; } + setLoading(true); + try { const formattedStartDate = dayjs(startDate).format("YYYY-MM-DD"); const formattedEndDate = dayjs(endDate).format("YYYY-MM-DD"); const response = await fetch( - `https://api.telemetry.smswithoutborders.com/v1/${category}?start_date=${formattedStartDate}&end_date=${formattedEndDate}&granularity=${granularity}&group_by=country&page=1&page_size=100` + `https://api.telemetry.smswithoutborders.com/v1/${category}?start_date=${formattedStartDate}&end_date=${formattedEndDate}&granularity=${granularity}&group_by=${group_by}` ); if (!response.ok) { @@ -83,41 +80,71 @@ const OpenTelemetry = () => { const apiData = await response.json(); setData(apiData); + console.log("apiData", apiData); setError(null); - // Update stats based on category if (category === "summary") { setTotalUsers(apiData[category]?.total_signup_users ?? 0); - setTotalRetained(apiData[category]?.total_retained_users ?? 0); - setSignupCountries(apiData[category]?.total_signup_countries ?? 0); - setRetainedCountries(apiData[category]?.total_retained_countries ?? 0); + setUsers_with_Tokens(apiData[category]?.users_withToken ?? 0); + setTotalSignupCountries(apiData[category]?.total_signup_countries ?? 0); } else if (category === "signup") { setTotalUsers(apiData[category]?.total_signup_users ?? 0); - setSignupCountries(apiData[category]?.total_countries ?? 0); - setTotalRetained(0); - setRetainedCountries(0); + setTotalSignupCountries(apiData[category]?.total_countries ?? 0); + setUsers_with_Tokens(0); } else if (category === "retained") { - setTotalRetained(apiData[category]?.total_retained_users ?? 0); - setRetainedCountries(apiData[category]?.total_countries ?? 0); + setUsers_with_Tokens(apiData[category]?.total_retained_users ?? 0); setTotalUsers(0); - setSignupCountries(0); + setTotalSignupCountries(0); } } catch (error) { console.error("Error fetching data:", error); - setError("Failed to fetch data. Please try again later."); + setError("Failed to fetch data. Please try again later"); + } finally { + setLoading(false); } }; - // Reset Filters Function + const fetchSummaryData = async () => { + setLoading(true); + try { + const today = new Date(); + const formattedToday = today.toISOString().split("T")[0]; + + const response = await fetch( + `https://api.telemetry.smswithoutborders.com/v1/summary?start_date=2021-01-10&end_date=${formattedToday}&granularity=day&group_by=date&page=1&page_size=100` + ); + + const data = await response.json(); + console.log("data", data); + + if (data && data.summary) { + const { total_signup_users, users_withToken, total_signup_countries } = data.summary; + + setTotalUsers(total_signup_users); + setUsers_with_Tokens(users_withToken); + setTotalSignupCountries(total_signup_countries); + } else { + console.error("Invalid data structure received", data); + } + } catch (error) { + console.error("Error fetching summary data:", error); + } finally { + setLoading(false); + } + }; + useEffect(() => { + fetchSummaryData(); + }, []); + const resetFilters = () => { setStartDate(""); setEndDate(""); setCountry(""); setCategory("summary"); + fetchSummaryData(); }; // Define columns for User data Table - const columns = [ { field: "timeframe", headerName: "Timeframe", flex: 1 }, { @@ -128,6 +155,7 @@ const OpenTelemetry = () => { category === "signup" ? params.row.signup_users : params.row.retained_users } ]; + console.log("columns", columns); const countryColumns = [ { field: "country_code", headerName: "Country", flex: 1 }, @@ -139,9 +167,7 @@ const OpenTelemetry = () => { category === "signup" ? params.row.signup_users : params.row.retained_users } ]; - - // ================================== - // Paginate data + console.log("countryColumns", countryColumns); const startIdx = (page - 1) * rowsPerPage; const endIdx = startIdx + rowsPerPage; @@ -153,14 +179,17 @@ const OpenTelemetry = () => { signup_users: row.signup_users, retained_users: row.retained_users })); - - const CountryRows = filteredData.map((row, index) => ({ - id: index + 1, - country_code: countries.getName(row.country_code, "en") || "Unknown", - signup_users: row.signup_users, - retained_users: row.retained_users - })); - + console.log("rows", rows); + + const CountryRows = [ + ...filteredData.map((row, index) => ({ + id: index + 1, + country_code: countries.getName(row.country_code, "en") || "Unknown", + signup_users: row.signup_users, + retained_users: row.retained_users + })) + ]; + console.log("CountryRows", CountryRows); return ( { )} - {/* ============== Data Display section ================ */} - - {/* -=========================================== */} - {/* Display Cards */} - - - Total Users in {country || "All Countries"} - {countryTotals.signup_users} - - - - - Retained Users in {country || "All Countries"} - {countryTotals.retained_users} - - - - - - Signup Users - {totalUsers} - - - - - Retained Users - {totalRetained} - - - - - Signup Countries - {signupCountries} - - - - - Retained Countries - {retainedCountries} - + + {[ + { title: "Signup Users", value: totalUsers, icon: "👥" }, + { title: "Signup Countries", value: totalSignupCountries, icon: "🌍" }, + { title: "Users with stored accounts", value: userswithToken, icon: "💾" } + ].map((item, index) => ( + + + + {item.icon} {item.title} + + + {item.value} + + + + ))} - {/* ================== filter ======================== */} - - {/* Filter Section */} - {/* Category Filter */} - + {/* Category Filters */} + Category - setCategory(e.target.value)} + > {categories.map((cat) => ( {cat.label} @@ -257,14 +282,36 @@ const OpenTelemetry = () => { - {/* Granularity Filter */} - + {/* Granularity Filters */} + Granularity - setGranularity(e.target.value)} + > + {granularities.map((gran) => ( + + {gran.label} + + ))} + + + + {/* ====== country and date filter ============ */} + + + Ground By + + @@ -272,10 +319,8 @@ const OpenTelemetry = () => { {/* Country Filter */} - + - - { @@ -288,16 +333,10 @@ const OpenTelemetry = () => { }} options={countryNames.map(([, name]) => name)} renderInput={(params) => } - renderOption={(props, option) => ( - - {option} - - )} freeSolo /> - {error && ( - + {error} )} @@ -305,123 +344,106 @@ const OpenTelemetry = () => { {/* Date Filters */} - + - - {/* Start Date */} + setStartDate(e.target.value)} value={startDate} - InputLabelProps={{ - shrink: true - }} - fullWidth + onChange={(e) => setStartDate(e.target.value)} + InputLabelProps={{ shrink: true }} /> - {/* End Date */} setEndDate(e.target.value)} value={endDate} - InputLabelProps={{ - shrink: true - }} - fullWidth + onChange={(e) => setEndDate(e.target.value)} + InputLabelProps={{ shrink: true }} /> - {/* Filter Buttons */} - + {/* Buttons */} + - - - - {/* =========== Tables Display ============================== */} - - - - - - User Data + {/* Tables */} + + + +
+ + {category === "signup" + ? "Signup Users by Country" + : "Retained Users by Country"} row.id} autoHeight pagination - pageSize={rowsPerPage} rowCount={tableData.length} paginationMode="server" onPageChange={(newPage) => setPage(newPage + 1)} onPageSizeChange={(newPageSize) => setRowsPerPage(newPageSize)} - components={{ Toolbar: GridToolbar }} /> - - - - - - Country data +
+
+
+ + + +
+ + {category === "signup" ? "Signup Users" : "Retained Users"} row.id} autoHeight pagination - pageSize={rowsPerPage} rowCount={tableData.length} paginationMode="server" onPageChange={(newPage) => setPage(newPage + 1)} onPageSizeChange={(newPageSize) => setRowsPerPage(newPageSize)} - components={{ Toolbar: GridToolbar }} /> - - +
+
-
+