Skip to content

Commit

Permalink
add feedback chart
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Aug 21, 2024
1 parent 9bbb1c5 commit dfe28d3
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 11 deletions.
23 changes: 23 additions & 0 deletions core/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as Components from "../svelte/**/*.svelte";

import { format } from "timeago.js";
import ClipboardJS from "clipboard";
import Chart from "chart.js/auto";

import "@getcanary/web/components/canary-root.js";
import "@getcanary/web/components/canary-provider-cloud.js";
Expand All @@ -47,6 +48,28 @@ let csrfToken = document

let hooks = {
...getHooks(Components),
ChartJS: {
mounted() {
new Chart(
this.el,
{
type: "bar",
data: {
labels: JSON.parse(this.el.dataset.labels),
datasets: [
{
data: JSON.parse(this.el.dataset.points),
label: 'score',
},
],
},
},
{
responsive: true,
},
);
},
},
LocalTime: {
mounted() {
this.fn();
Expand Down
25 changes: 21 additions & 4 deletions core/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@getcanary/web": "^0.0.83",
"@getcanary/web": "^0.0.84",
"chart.js": "^4.4.4",
"clipboard": "^2.0.11",
"clsx": "^2.1.1",
"live_svelte": "file:../deps/live_svelte",
Expand Down
13 changes: 13 additions & 0 deletions core/lib/canary_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ defmodule CanaryWeb.Layouts do
</.link>
</li>
<li>
<.link class={if @active_tab == :feedback, do: "active"} navigate={~p"/feedback"}>
<span class={[
"h-4 w-4",
if(@active_tab == :feedback,
do: "hero-hand-thumb-up-solid",
else: "hero-hand-thumb-up"
)
]} />
<span>Feedback</span>
</.link>
</li>
<%!-- <li>
<.link class={if @active_tab == :editor, do: "active"} navigate={~p"/editor"}>
<span class={[
Expand Down
24 changes: 21 additions & 3 deletions core/lib/canary_web/live/feedback_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ defmodule CanaryWeb.FeedbackLive do

def render(assigns) do
~H"""
<pre><%= @data %></pre>
<div>
<canvas
id="feedback-page-breakdown"
phx-hook="ChartJS"
data-labels={Jason.encode!(@labels)}
data-points={Jason.encode!(@points)}
>
</canvas>
</div>
"""
end

def mount(_params, _session, socket) do
{:ok, data} = Canary.Analytics.feedback_page_breakdown(socket.assigns.current_account.id)
{:ok, socket |> assign(data: Jason.encode!(data))}
account_id = socket.assigns.current_account.id
{:ok, data} = Canary.Analytics.feedback_page_breakdown(account_id)

labels = data |> Enum.map(fn %{"path" => path} -> path end)
points = data |> Enum.map(fn %{"mean_score" => score} -> score end)

socket =
socket
|> assign(:labels, labels)
|> assign(:points, points)

{:ok, socket}
end
end
1 change: 1 addition & 0 deletions core/lib/canary_web/live/nav_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule CanaryWeb.NavLive do
case socket.view do
CanaryWeb.HomeLive -> :home
CanaryWeb.SourceLive -> :source
CanaryWeb.FeedbackLive -> :feedback
CanaryWeb.SettingsLive -> :settings
_ -> nil
end
Expand Down
6 changes: 6 additions & 0 deletions core/lib/canary_web/operations_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ defmodule CanaryWeb.OperationsController do
%URI{host: host, path: path} = URI.parse(url)
path = path |> remove_extension() |> ensure_trailing_slash()

if host == "localhost" do
conn
|> send_resp(200, "")
|> halt()
end

data = %Canary.Analytics.FeedbackPage{
host: host,
path: path,
Expand Down
5 changes: 4 additions & 1 deletion js/packages/web/src/components/canary-feedback-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export class CanaryFeedbackPage extends LitElement {
apiKey = "";

private _task = new Task(this, {
task: async ([endpoint, key, url, score]: [string, string, string, number], { signal }) => {
task: async (
[endpoint, key, url, score]: [string, string, string, number],
{ signal },
) => {
const response = await fetch(`${endpoint}/api/v1/feedback/page`, {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand Down
4 changes: 3 additions & 1 deletion js/packages/web/src/components/canary-feedback.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default {
parameters: { sourceLink: "components/canary-feedback.stories.ts" },
render: ({ type }: any) => {
if (type === "page") {
return html` <canary-feedback-page api-key="123"></canary-feedback-page> `;
return html`
<canary-feedback-page api-key="123"></canary-feedback-page>
`;
}

if (type === "modal") {
Expand Down
2 changes: 1 addition & 1 deletion js/packages/web/src/components/canary-provider-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const NAME = "canary-provider-cloud";
export class CanaryProviderCloud extends LitElement {
@property({ type: String, attribute: "api-base" })
apiBase = "";

@property({ type: String, attribute: "api-key" })
apiKey = "";

Expand Down

0 comments on commit dfe28d3

Please sign in to comment.