Skip to content

Commit

Permalink
fixing json controller and returing string id's instead of nums
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Grato committed Nov 28, 2023
1 parent df15585 commit 651f216
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 121 deletions.
236 changes: 120 additions & 116 deletions client/pages/analytics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ type SandboxPropsT = {
analytics: {};
};

/**
* This modal is used to sandbox code. feel free to play, this will
* not show up on prod
*/
type TierStatsT = {
p0: string[];
p1: string[];
b0: string[];
};

const initTiers = {
p0: [],
p1: [],
b0: [],
};

const Sandbox = ({ analytics }: SandboxPropsT) => {
// Fromat Date Util
const getFormattedDate = () => {
const today = new Date();
const year = today.getFullYear();
Expand All @@ -22,28 +31,17 @@ const Sandbox = ({ analytics }: SandboxPropsT) => {
return `${year}-${month}-${date}`;
};

const [showReadout, setShowReadout] = useState(false);

// Form Date Data
const [firstStartDate, setFirstStartDate] = useState(getFormattedDate());
const [firstEndDate, setFirstEndDate] = useState(getFormattedDate());

const [secondStartDate, setSecondStartDate] = useState(getFormattedDate());
const [secondEndDate, setSecondEndDate] = useState(getFormattedDate());

type TierStatsT = {
p0: number[];
p1: number[];
b0: number[];
};

const initTiers = {
p0: [],
p1: [],
b0: [],
};

// Hubs Compare Data
const [tiers, setTiers] = useState<TierStatsT>(initTiers);

const [compareTiers, setCompareTiers] = useState<TierStatsT>(initTiers);

const [analyzedData, setAnalyzedData] = useState({
p0: {
persistent: 0,
Expand All @@ -70,7 +68,7 @@ const Sandbox = ({ analytics }: SandboxPropsT) => {
};

hubs.forEach((hub) => {
data[hub.tier].push(hub.hub_id);
data[hub.tier].push(String(hub.hub_id));
});

return data;
Expand Down Expand Up @@ -125,6 +123,7 @@ const Sandbox = ({ analytics }: SandboxPropsT) => {
analyzeData(filteredHub, compareFilteredHub);
setTiers(filteredHub);
setCompareTiers(compareFilteredHub);
setShowReadout(true);
} catch (error) {
console.log(error);
}
Expand Down Expand Up @@ -207,102 +206,107 @@ const Sandbox = ({ analytics }: SandboxPropsT) => {
</div>
</section>

<hr className="my-20" />
<section>
<h3 className="mb-12">Active Hubs</h3>
<div className="mb-20">
<p className="mb-20">
From: <b>{firstStartDate}</b> to <b>{firstEndDate}</b>
</p>
<div className="flex">
<Pill
classProp="mr-12"
title={`P0: ${tiers.p0.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`P1: ${tiers.p1.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`B0: ${tiers.b0.length}`}
category="cool"
/>
</div>
</div>
<div className="mb-40">
<p className="mb-20">
From: <b>{secondStartDate}</b> to <b>{secondEndDate}</b>
</p>
<div className="flex">
<Pill
classProp="mr-12"
title={`P0: ${compareTiers.p0.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`P1: ${compareTiers.p1.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`B0: ${compareTiers.b0.length}`}
category="cool"
/>
</div>
</div>
<h3 className="mb-12">Hub Behaviour</h3>
<p className="paragraph mb-24">
The follow data comes form the HubStat table and the Hubs table.
If a hub is active it is logged to the HubStat table. This query
leverages the HubStat table to see what hubs are active on
specific dates. <b>Persistent</b> is how many hubs remained in
the two date ranges, <b>Dropped</b> is how many hub id&apos;s
where in the first date range but not in the second.
<b> Gained</b> are active Hubs in the second date range but not
in the first.
</p>
{showReadout && (
<>
<hr className="my-20" />
<section>
<h3 className="mb-12">Active Hubs</h3>
<div className="mb-20">
<p className="mb-20">
From: <b>{firstStartDate}</b> to <b>{firstEndDate}</b>
</p>
<div className="flex">
<Pill
classProp="mr-12"
title={`P0: ${tiers.p0.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`P1: ${tiers.p1.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`B0: ${tiers.b0.length}`}
category="cool"
/>
</div>
</div>
<div className="mb-40">
<p className="mb-20">
From: <b>{secondStartDate}</b> to <b>{secondEndDate}</b>
</p>
<div className="flex">
<Pill
classProp="mr-12"
title={`P0: ${compareTiers.p0.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`P1: ${compareTiers.p1.length}`}
category="cool"
/>
<Pill
classProp="mr-12"
title={`B0: ${compareTiers.b0.length}`}
category="cool"
/>
</div>
</div>
<h3 className="mb-12">Hub Behaviour</h3>
<p className="paragraph mb-24">
The follow data comes form the HubStat table and the Hubs
table. If a hub is active it is logged to the HubStat table.
This query leverages the HubStat table to see what hubs are
active on specific dates. <b>Persistent</b> is how many hubs
remained in the two date ranges, <b>Dropped</b> is how many
hub id&apos;s where in the first date range but not in the
second.
<b> Gained</b> are active Hubs in the second date range but
not in the first.
</p>

<table className={styles.data_table}>
<thead>
<tr>
<th>Tier</th>
<th>Persistent</th>
<th>Dropped</th>
<th>Gained</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<Pill title="P0" category="rainbow" />
</td>
<td>{analyzedData.p0.persistent}</td>
<td>{analyzedData.p0.dropped}</td>
<td>{analyzedData.p0.gained}</td>
</tr>
<tr>
<td>
<Pill title="P1" category="rainbow" />
</td>
<td>{analyzedData.p1.persistent}</td>
<td>{analyzedData.p1.dropped}</td>
<td>{analyzedData.p1.gained}</td>
</tr>
<tr>
<td>
<Pill title="B0" category="rainbow" />
</td>
<td>{analyzedData.b0.persistent}</td>
<td>{analyzedData.b0.dropped}</td>
<td>{analyzedData.b0.gained}</td>
</tr>
</tbody>
</table>
</section>
<table className={styles.data_table}>
<thead>
<tr>
<th>Tier</th>
<th>Persistent</th>
<th>Dropped</th>
<th>Gained</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<Pill title="P0" category="rainbow" />
</td>
<td>{analyzedData.p0.persistent}</td>
<td>{analyzedData.p0.dropped}</td>
<td>{analyzedData.p0.gained}</td>
</tr>
<tr>
<td>
<Pill title="P1" category="rainbow" />
</td>
<td>{analyzedData.p1.persistent}</td>
<td>{analyzedData.p1.dropped}</td>
<td>{analyzedData.p1.gained}</td>
</tr>
<tr>
<td>
<Pill title="B0" category="rainbow" />
</td>
<td>{analyzedData.b0.persistent}</td>
<td>{analyzedData.b0.dropped}</td>
<td>{analyzedData.b0.gained}</td>
</tr>
</tbody>
</table>
</section>
</>
)}
</Card>
</div>
</main>
Expand All @@ -313,7 +317,7 @@ const Sandbox = ({ analytics }: SandboxPropsT) => {
export default Sandbox;

export async function getStaticProps() {
if (process.env.ENV === 'prod') {
if (process.env.ENV === 'production') {
return { notFound: true };
}

Expand Down
4 changes: 2 additions & 2 deletions client/services/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type HubStat = {

export const getAnalytics = async (startDate: string, endDate: string) => {
const path = `/?start_date=${startDate}T00:00:00Z&end_date=${endDate}T00:00:00Z`;
console.log('https://dashboard.dev.myhubs.net');

return axios
.get(`https://dashboard.dev.myhubs.net${API_PATH}${path}`, {
.get(`${PUBLIC_API_SERVER}${API_PATH}${path}`, {
withCredentials: true,
})
.then((response: AxiosResponse) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/dash/hub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Dash.Hub do

@type id :: pos_integer
@type t :: %__MODULE__{account_id: id}
@derive {Jason.Encoder, only: [:tier, :hub_id]}
# @derive {Jason.Encoder, only: [:tier, :hub_id]}
@personal_ccu_limit 20
@personal_storage_limit_mb 2_000

Expand Down
3 changes: 1 addition & 2 deletions lib/dash_web/controllers/api/v1/analytics_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule DashWeb.Api.V1.AnalyticsController do
def show(conn, %{"start_date" => start_date, "end_date" => end_date} = _params) do
hubs = Dash.get_hubs_by_date(start_date, end_date)

conn
|> json(%{hubs: hubs})
render(conn, :show, hubs: hubs)
end
end
13 changes: 13 additions & 0 deletions lib/dash_web/controllers/api/v1/analytics_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule DashWeb.Api.V1.AnalyticsJSON do
def show(%{hubs: hubs}) do
list =
for h <- hubs do
%{
hubId: Integer.to_string(h.hub_id),
tier: h.tier
}
end

%{hubs: list}
end
end

0 comments on commit 651f216

Please sign in to comment.