Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit 838e9d9

Browse files
author
Eric Berry
committed
Add charts to dashboard for impressions and clicks over time
Fixes #14 Fixes #15
1 parent 3d69ad3 commit 838e9d9

File tree

15 files changed

+450
-728
lines changed

15 files changed

+450
-728
lines changed

assets/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"jquery-validation": "1.17.0",
4444
"jquery.maskedinput": "1.4.1",
4545
"ladda": "1.0.5",
46+
"lodash": "^4.17.5",
4647
"moment": "2.20.1",
4748
"node-sass": "^4.7.2",
4849
"pace-progress": "1.0.2",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { Controller } from "stimulus";
2+
import _ from "lodash";
3+
import Chart from "chart.js";
4+
5+
export default class extends Controller {
6+
static get targets() {
7+
return ["trafficImpressionsChart", "trafficClicksChart"];
8+
}
9+
10+
connect() {
11+
const impressionsByDay = JSON.parse(this.element.dataset.impressionsByDay);
12+
const clicksByDay = JSON.parse(this.element.dataset.clicksByDay);
13+
this.loadTrafficImpressionsChart(impressionsByDay);
14+
this.loadTrafficClicksChart(clicksByDay);
15+
16+
console.log("Loaded dashboard");
17+
}
18+
19+
loadTrafficImpressionsChart(impressionsByDay) {
20+
const ctx = this.trafficImpressionsChartTarget.getContext("2d");
21+
22+
const options = {
23+
responsive: true,
24+
scales: {
25+
xAxes: [
26+
{
27+
time: {
28+
unit: "day"
29+
}
30+
}
31+
]
32+
}
33+
};
34+
35+
const data = {
36+
labels: _.keys(impressionsByDay),
37+
datasets: [
38+
{
39+
label: "Impressions",
40+
backgroundColor: "rgba(220,220,220,0.2)",
41+
borderColor: "rgba(220,220,220,1)",
42+
pointBackgroundColor: "rgba(220,220,220,1)",
43+
pointBorderColor: "#fff",
44+
data: _.values(impressionsByDay)
45+
}
46+
]
47+
};
48+
49+
return new Chart(ctx, { type: "line", data, options });
50+
}
51+
52+
loadTrafficClicksChart(clicksByDay) {
53+
const ctx = this.trafficClicksChartTarget.getContext("2d");
54+
55+
const options = {
56+
responsive: true,
57+
scales: {
58+
xAxes: [
59+
{
60+
time: {
61+
unit: "day"
62+
}
63+
}
64+
]
65+
}
66+
};
67+
68+
const data = {
69+
labels: _.keys(clicksByDay),
70+
datasets: [
71+
{
72+
label: "Clicks",
73+
backgroundColor: "rgba(151,187,205,0.2)",
74+
borderColor: "rgba(151,187,205,1)",
75+
pointBackgroundColor: "rgba(151,187,205,1)",
76+
pointBorderColor: "#fff",
77+
data: _.values(clicksByDay)
78+
}
79+
]
80+
};
81+
82+
return new Chart(ctx, { type: "line", data, options });
83+
}
84+
}

bin/copy_prod_to_local

100644100755
File mode changed.

config/configs/rollbar.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ use Mix.Config
22

33
config :rollbax,
44
enabled: false,
5-
access_token: System.get_env("ROLLBAR_ACCESS_TOKEN")
5+
access_token: System.get_env("ROLLBAR_ACCESS_TOKEN") || "None"

lib/code_sponsor/sponsorships/sponsorship.ex

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule CodeSponsor.Sponsorships.Sponsorship do
1010
schema "sponsorships" do
1111
has_many :impressions, CodeSponsor.Impressions.Impression
1212
has_many :clicks, CodeSponsor.Clicks.Click
13-
has_many :sponsorships, CodeSponsor.Sponsorships.Sponsorship
1413
belongs_to :property, CodeSponsor.Properties.Property
1514
belongs_to :campaign, CodeSponsor.Campaigns.Campaign
1615

lib/code_sponsor/stats/clicks.ex

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
defmodule CodeSponsor.Stats.Clicks do
2+
import Filtrex.Type.Config
3+
import Ecto.Query, warn: false
4+
alias CodeSponsor.Repo
5+
alias CodeSponsor.Properties.Property
6+
alias CodeSponsor.Campaigns.Campaign
7+
alias CodeSponsor.Sponsorships.Sponsorship
8+
alias CodeSponsor.Clicks.Click
9+
alias CodeSponsor.Coherence.User
10+
11+
def count(start_date, end_date) when start_date <= end_date do
12+
Repo.one(
13+
from c in Click,
14+
where: fragment("?::date", c.inserted_at) >= ^start_date,
15+
where: fragment("?::date", c.inserted_at) <= ^end_date,
16+
select: fragment("count(*)")
17+
)
18+
end
19+
20+
def count(%Property{} = property, start_date, end_date) when start_date <= end_date do
21+
Repo.one(
22+
from c in Click,
23+
where: c.property_id == ^property.id,
24+
where: fragment("?::date", c.inserted_at) >= ^start_date,
25+
where: fragment("?::date", c.inserted_at) <= ^end_date,
26+
select: fragment("count(*)")
27+
)
28+
end
29+
30+
def count(%Sponsorship{} = sponsorship, start_date, end_date) when start_date <= end_date do
31+
Repo.one(
32+
from c in Click,
33+
where: c.sponsorship_id == ^sponsorship.id,
34+
where: fragment("?::date", c.inserted_at) >= ^start_date,
35+
where: fragment("?::date", c.inserted_at) <= ^end_date,
36+
select: fragment("count(*)")
37+
)
38+
end
39+
40+
def count(%Campaign{} = campaign, start_date, end_date) when start_date <= end_date do
41+
Repo.one(
42+
from c in Click,
43+
where: c.campaign_id == ^campaign.id,
44+
where: fragment("?::date", c.inserted_at) >= ^start_date,
45+
where: fragment("?::date", c.inserted_at) <= ^end_date,
46+
select: fragment("count(*)")
47+
)
48+
end
49+
50+
def count(%User{} = user, start_date, end_date) when start_date <= end_date do
51+
campaign_ids =
52+
Repo.all(from c in Campaign, where: c.user_id == ^user.id)
53+
|> Enum.map(fn (c) -> c.id end)
54+
55+
property_ids =
56+
Repo.all(from p in Property, where: p.user_id == ^user.id)
57+
|> Enum.map(fn (c) -> c.id end)
58+
59+
Repo.one(
60+
from c in Click,
61+
where: c.campaign_id in ^campaign_ids or c.property_id in ^property_ids,
62+
where: fragment("?::date", c.inserted_at) >= ^start_date,
63+
where: fragment("?::date", c.inserted_at) <= ^end_date,
64+
select: fragment("count(*)")
65+
)
66+
end
67+
68+
def count_by_day(start_date, end_date) when start_date <= end_date do
69+
(
70+
from c in Click,
71+
where: fragment("?::date", c.inserted_at) >= ^start_date,
72+
where: fragment("?::date", c.inserted_at) <= ^end_date,
73+
group_by: fragment("date_trunc('day', ?)", (field(c, ^:inserted_at))),
74+
select: [(fragment("date_trunc('day', ?)", (field(c, ^:inserted_at)))), count("*")]
75+
) |> to_date_map()
76+
end
77+
78+
def count_by_day(%Property{} = property, start_date, end_date) when start_date <= end_date do
79+
(
80+
from c in Click,
81+
where: c.property_id == ^property.id,
82+
where: fragment("?::date", c.inserted_at) >= ^start_date,
83+
where: fragment("?::date", c.inserted_at) <= ^end_date,
84+
group_by: fragment("date_trunc('day', ?)", (field(c, ^:inserted_at))),
85+
select: [(fragment("date_trunc('day', ?)", (field(c, ^:inserted_at)))), count("*")]
86+
) |> to_date_map()
87+
end
88+
89+
def count_by_day(%Sponsorship{} = sponsorship, start_date, end_date) when start_date <= end_date do
90+
(
91+
from c in Click,
92+
where: c.sponsorship_id == ^sponsorship.id,
93+
where: fragment("?::date", c.inserted_at) >= ^start_date,
94+
where: fragment("?::date", c.inserted_at) <= ^end_date,
95+
group_by: fragment("date_trunc('day', ?)", (field(c, ^:inserted_at))),
96+
select: [(fragment("date_trunc('day', ?)", (field(c, ^:inserted_at)))), count("*")]
97+
) |> to_date_map()
98+
end
99+
100+
def count_by_day(%Campaign{} = campaign, start_date, end_date) when start_date <= end_date do
101+
(
102+
from c in Click,
103+
where: c.campaign_id == ^campaign.id,
104+
where: fragment("?::date", c.inserted_at) >= ^start_date,
105+
where: fragment("?::date", c.inserted_at) <= ^end_date,
106+
group_by: fragment("date_trunc('day', ?)", (field(c, ^:inserted_at))),
107+
select: [(fragment("date_trunc('day', ?)", (field(c, ^:inserted_at)))), count("*")]
108+
) |> to_date_map()
109+
end
110+
111+
def count_by_day(%Campaign{} = campaign, start_date, end_date) when start_date <= end_date do
112+
(
113+
from i in Impression,
114+
where: i.campaign_id == ^campaign.id,
115+
where: fragment("?::date", i.inserted_at) >= ^start_date,
116+
where: fragment("?::date", i.inserted_at) <= ^end_date,
117+
group_by: fragment("date_trunc('day', ?)", (field(i, ^:inserted_at))),
118+
select: [(fragment("date_trunc('day', ?)", (field(i, ^:inserted_at)))), count("*")]
119+
) |> to_date_map()
120+
end
121+
122+
def count_by_day(%User{} = user, start_date, end_date) when start_date <= end_date do
123+
campaign_ids =
124+
Repo.all(from c in Campaign, where: c.user_id == ^user.id)
125+
|> Enum.map(fn (c) -> c.id end)
126+
127+
property_ids =
128+
Repo.all(from p in Property, where: p.user_id == ^user.id)
129+
|> Enum.map(fn (c) -> c.id end)
130+
131+
(
132+
from c in Click,
133+
where: c.campaign_id in ^campaign_ids or c.property_id in ^property_ids,
134+
where: fragment("?::date", c.inserted_at) >= ^start_date,
135+
where: fragment("?::date", c.inserted_at) <= ^end_date,
136+
group_by: fragment("date_trunc('day', ?)", (field(c, ^:inserted_at))),
137+
select: [(fragment("date_trunc('day', ?)", (field(c, ^:inserted_at)))), count("*")]
138+
) |> to_date_map()
139+
end
140+
141+
defp to_date_map(query) do
142+
query
143+
|> Repo.all
144+
|> Enum.map(fn([day, count]) ->
145+
{:ok, date} =
146+
day
147+
|> Tuple.delete_at(1)
148+
|> Tuple.insert_at(1, {0, 0, 0})
149+
|> NaiveDateTime.from_erl()
150+
151+
formatted_date = Timex.format!(date, "%F", :strftime)
152+
{formatted_date, count}
153+
end)
154+
|> Enum.into(%{})
155+
end
156+
end

0 commit comments

Comments
 (0)