From 0054c9fc966163bca392cae03ff6998ce11183e8 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 10 Jun 2021 10:25:19 +0300 Subject: [PATCH 1/6] Add api keys to CRM --- config/config.exs | 18 ----------- config/runtime.exs | 3 +- lib/plausible/auth/api_key.ex | 24 ++++++++++++--- lib/plausible/auth/api_key_admin.ex | 46 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 lib/plausible/auth/api_key_admin.ex diff --git a/config/config.exs b/config/config.exs index 14ef29942..f57503a5c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -41,22 +41,4 @@ config :plausible, :user_agent_cache, limit: 1000, stats: false -config :kaffy, - otp_app: :plausible, - ecto_repo: Plausible.Repo, - router: PlausibleWeb.Router, - admin_title: "Plausible Admin", - resources: [ - auth: [ - resources: [ - user: [schema: Plausible.Auth.User, admin: Plausible.Auth.UserAdmin] - ] - ], - sites: [ - resources: [ - site: [schema: Plausible.Site, admin: Plausible.SiteAdmin] - ] - ] - ] - import_config "#{config_env()}.exs" diff --git a/config/runtime.exs b/config/runtime.exs index b04472112..330da9d50 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -279,7 +279,8 @@ config :kaffy, resources: [ auth: [ resources: [ - user: [schema: Plausible.Auth.User, admin: Plausible.Auth.UserAdmin] + user: [schema: Plausible.Auth.User, admin: Plausible.Auth.UserAdmin], + api_key: [schema: Plausible.Auth.ApiKey, admin: Plausible.Auth.ApiKeyAdmin] ] ], sites: [ diff --git a/lib/plausible/auth/api_key.ex b/lib/plausible/auth/api_key.ex index 594d70cb5..2b3527c02 100644 --- a/lib/plausible/auth/api_key.ex +++ b/lib/plausible/auth/api_key.ex @@ -2,12 +2,12 @@ defmodule Plausible.Auth.ApiKey do use Ecto.Schema import Ecto.Changeset - @required [:user_id, :key, :name] - @optional [:scopes, :hourly_request_limit] + @required [:user_id, :name] + @optional [:key, :scopes, :hourly_request_limit] schema "api_keys" do field :name, :string field :scopes, {:array, :string}, default: ["stats:read:*"] - field :hourly_request_limit, :integer + field :hourly_request_limit, :integer, default: 1000 field :key, :string, virtual: true field :key_hash, :string @@ -22,7 +22,14 @@ defmodule Plausible.Auth.ApiKey do schema |> cast(attrs, @required ++ @optional) |> validate_required(@required) - |> process_key + |> generate_key() + |> process_key() + end + + def update(schema, attrs \\ %{}) do + schema + |> cast(attrs, [:name, :user_id, :scopes, :hourly_request_limit]) + |> validate_required([:user_id, :name]) end def do_hash(key) do @@ -42,6 +49,15 @@ defmodule Plausible.Auth.ApiKey do def process_key(changeset), do: changeset + defp generate_key(changeset) do + if !changeset.changes[:key] do + key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64) + change(changeset, key: key) + else + changeset + end + end + defp secret_key_base() do Application.get_env(:plausible, PlausibleWeb.Endpoint) |> Keyword.fetch!(:secret_key_base) diff --git a/lib/plausible/auth/api_key_admin.ex b/lib/plausible/auth/api_key_admin.ex new file mode 100644 index 000000000..8886aab11 --- /dev/null +++ b/lib/plausible/auth/api_key_admin.ex @@ -0,0 +1,46 @@ +defmodule Plausible.Auth.ApiKeyAdmin do + use Plausible.Repo + + def search_fields(_schema) do + [ + :name, + user: [:name, :email] + ] + end + + def custom_index_query(_conn, _schema, query) do + from(r in query, preload: [:user]) + end + + def create_changeset(schema, attrs) do + scopes = [attrs["scope"]] + Plausible.Auth.ApiKey.changeset(schema, Map.merge(%{"scopes" => scopes}, attrs)) + end + + def update_changeset(schema, attrs) do + Plausible.Auth.ApiKey.update(schema, attrs) |> IO.inspect() + end + + @plaintext_key_help """ + The value of the API key is sensitive data like a password. Once created, the value of they will never be revealed again. Make sure to copy/paste this into a secure place before hitting 'save'. When sending the key to a customer, use a secure E2EE system that destructs the message after a certain period like https://bitwarden.com/products/send + """ + def form_fields(_) do + [ + name: nil, + key: %{create: :readonly, update: :hidden, help_text: @plaintext_key_help}, + key_prefix: %{create: :hidden, update: :readonly}, + hourly_request_limit: %{default: 1000}, + scope: %{choices: [{"Stats API", ["stats:read:*"]}, {"Sites API", ["sites:provision:*"]}]}, + user_id: nil + ] + end + + def index(_) do + [ + key_prefix: nil, + name: nil, + scopes: nil, + owner: %{value: & &1.user.email} + ] + end +end From 39287ab1769ea44d8d833ce6498d12a60c4efd4e Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 10 Jun 2021 13:40:42 +0300 Subject: [PATCH 2/6] Remove IO.inspect --- lib/plausible/auth/api_key_admin.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plausible/auth/api_key_admin.ex b/lib/plausible/auth/api_key_admin.ex index 8886aab11..aa9dc7be3 100644 --- a/lib/plausible/auth/api_key_admin.ex +++ b/lib/plausible/auth/api_key_admin.ex @@ -18,7 +18,7 @@ defmodule Plausible.Auth.ApiKeyAdmin do end def update_changeset(schema, attrs) do - Plausible.Auth.ApiKey.update(schema, attrs) |> IO.inspect() + Plausible.Auth.ApiKey.update(schema, attrs) end @plaintext_key_help """ From 645069271a53ba77100c82916202a6070323df0a Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Mon, 14 Jun 2021 08:44:46 -0500 Subject: [PATCH 3/6] Fixes React errors+crash when system theme changes on loaded dashboard (#1123) * Fixes crash on theme change * Changelog --- CHANGELOG.md | 1 + assets/js/dashboard/stats/countries.js | 4 ++-- assets/js/dashboard/stats/visitor-graph.js | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9bb9a9cc..dd770b4f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Fix weekly report time range plausible/analytics#951 - Make sure embedded dashboards can run when user has blocked third-party cookies plausible/analytics#971 - Sites listing page will paginate if the user has a lot of sites plausible/analytics#994 +- Crash when changing theme on a loaded dashboard plausible/analytics#1123 ### Removed - Removes AppSignal monitoring package diff --git a/assets/js/dashboard/stats/countries.js b/assets/js/dashboard/stats/countries.js index 20dd363d0..b5f247686 100644 --- a/assets/js/dashboard/stats/countries.js +++ b/assets/js/dashboard/stats/countries.js @@ -34,7 +34,7 @@ class Countries extends React.Component { componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, countries: null}) - this.fetchCountries().then(this.drawMap.bind(this)) + this.fetchCountries().then(this.drawMap) } if (this.props.darkTheme !== prevProps.darkTheme) { @@ -42,7 +42,7 @@ class Countries extends React.Component { document.getElementById('map-container').removeChild(document.querySelector('.datamaps-hoverover')); document.getElementById('map-container').removeChild(document.querySelector('.datamap')); } - this.drawMap(); + this.fetchCountries().then(this.drawMap) } } diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js index 48f0a6863..40ae56550 100644 --- a/assets/js/dashboard/stats/visitor-graph.js +++ b/assets/js/dashboard/stats/visitor-graph.js @@ -202,6 +202,7 @@ class LineGraph extends React.Component { } if (prevProps.darkTheme !== this.props.darkTheme) { + this.chart.destroy(); this.chart = this.regenerateChart(); this.chart.update(); } From c95d37583952cbad2e25c2cf9dda9a7b20b155f6 Mon Sep 17 00:00:00 2001 From: Ru Singh Date: Tue, 15 Jun 2021 13:04:43 +0530 Subject: [PATCH 4/6] Fix for details button misbehaving on mobile (#1114) * chore(make): convenience commands for installation & dev server * docs: easier development env instructions * docs: add note about docker volumes * docs: detail pre-commit configuration * style: eslint and prettier changes * Allow for passing custom classes to fade-in * style: eslint & prettier for the details button component * style: react lifecycle methods to come first * docs: add instructions to disable pre-commit * style: devices components * Move render methods to the last (together) in the order list * Remove unused component import * React lifecycle to come first before our own methods * General styling and eslint changes * Cleaner renderContent method using switch/case (fixes an eslint error as well!) * Cleaner renderPill method with proper spacing + removing uncessary else * style: more eslint/prettier for pages components * Use newer Fragment syntax * Remove unnecessary else statement * Use backtick strings for concatenating strings * Remove unnecessary space * Remove unused imports and variable declarations * Bunch render methods together as last in the order list * fix: details button to drop to the bottom naturally on smaller screens This _mostly_ fixes one of the issues being tracked on #972, titled "Details button issue on Firefox specifically" * refactor: reduce usage of our CSS class in favor of tailwind's util classes * refactor: remove our css classes in favor of Tailwind's util classes --- .tool-versions | 1 + CONTRIBUTING.md | 21 ++- Makefile | 9 ++ assets/css/app.css | 9 +- assets/js/dashboard/fade-in.js | 12 +- assets/js/dashboard/lazy-loader.js | 12 +- assets/js/dashboard/stats/countries.js | 4 +- assets/js/dashboard/stats/devices/browsers.js | 14 +- assets/js/dashboard/stats/devices/index.js | 129 ++++++++++++------ .../stats/devices/operating-systems.js | 4 +- assets/js/dashboard/stats/more-link.js | 23 +++- .../js/dashboard/stats/pages/entry-pages.js | 23 ++-- assets/js/dashboard/stats/pages/exit-pages.js | 4 +- assets/js/dashboard/stats/pages/index.js | 65 +++++---- assets/js/dashboard/stats/pages/pages.js | 4 +- .../dashboard/stats/sources/referrer-list.js | 31 +++-- .../dashboard/stats/sources/search-terms.js | 6 +- .../js/dashboard/stats/sources/source-list.js | 16 ++- 18 files changed, 261 insertions(+), 126 deletions(-) diff --git a/.tool-versions b/.tool-versions index 675cf7009..12b580032 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,4 @@ elixir 1.11.3-otp-23 erlang 23.2.1 nodejs 15.3.0 +python 3.9.4 \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1435daf2b..9d4d618d4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,12 +9,13 @@ Make sure Docker, Elixir, Erlang and Node.js are all installed on your developme ### Start the environment: 1. Run both `make postgres` and `make clickhouse`. -2. Run `mix deps.get`. This will download the required Elixir dependencies. -2. Run `mix ecto.create`. This will create the required databases in both Postgres and Clickhouse. -3. Run `mix ecto.migrate` to build the database schema. -4. Run `npm ci --prefix assets` to install the required node dependencies. -5. Run `mix phx.server` to start the Phoenix server. -6. The system is now available on `localhost:8000`. +2. You can then get set up with the following bits in one go with `make install`. + 1. Run `mix deps.get`. This will download the required Elixir dependencies. + 2. Run `mix ecto.create`. This will create the required databases in both Postgres and Clickhouse. + 3. Run `mix ecto.migrate` to build the database schema. + 4. Run `npm ci --prefix assets` to install the required node dependencies. +3. Run `make server` or `mix phx.server` to start the Phoenix server. +4. The system is now available on `localhost:8000`. ### Creating an account @@ -29,4 +30,10 @@ Make sure Docker, Elixir, Erlang and Node.js are all installed on your developme 1. Stop and remove the Postgres container with `make postgres-stop`. 2. Stop and remove the Clickhouse container with `make clickhouse-stop`. -Volumes are preserved. You'll find that the Postgres and Clickhouse state are retained when you bring them up again the next time: no need to re-register and so on. \ No newline at end of file +Volumes are preserved. You'll find that the Postgres and Clickhouse state are retained when you bring them up again the next time: no need to re-register and so on. + +Note: Since we are deleting the containers, be careful when deleting volumes with `docker volume prune`. You might accidentally delete the database and would have to go through re-registration process. + +### Pre-commit hooks + +`pre-commit` requires Python to be available locally and covers JavaScript and CSS. Set up with `pip install --user pre-commit` followed by `pre-commit install`. Conversely, if the prompts are far too bothersome, remove with `pre-commit uninstall`. \ No newline at end of file diff --git a/Makefile b/Makefile index 5e7c5c94e..fee3c1096 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,12 @@ +install: + mix deps.get + mix ecto.create + mix ecto.migrate + npm ci --prefix assets + +server: + mix phx.server + clickhouse: docker run --detach -p 8123:8123 --ulimit nofile=262144:262144 --volume=$$PWD/.clickhouse_db_vol:/var/lib/clickhouse --name plausible_clickhouse yandex/clickhouse-server:21.3.2.5 diff --git a/assets/css/app.css b/assets/css/app.css index 58376b8b2..e3e0d7660 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -247,8 +247,7 @@ blockquote { } .stats-item { - @apply mt-6; - width: 100%; + min-height: 436px; } @screen md { @@ -257,6 +256,12 @@ blockquote { margin-right: 6px; width: calc(50% - 6px); position: relative; + min-height: initial; + height: 436px; + } + + .stats-item__header { + height: inherit; } } diff --git a/assets/js/dashboard/fade-in.js b/assets/js/dashboard/fade-in.js index 57d8539f7..6cbe19052 100644 --- a/assets/js/dashboard/fade-in.js +++ b/assets/js/dashboard/fade-in.js @@ -1,8 +1,12 @@ import React from 'react'; -export default function FadeIn({show, children}) { - const className = show ? "fade-enter-active" : "fade-enter" - - return
{children}
+export default function FadeIn({className, show, children}) { + return ( +
+ {children} +
+) } diff --git a/assets/js/dashboard/lazy-loader.js b/assets/js/dashboard/lazy-loader.js index 4a2222fca..3824ef943 100644 --- a/assets/js/dashboard/lazy-loader.js +++ b/assets/js/dashboard/lazy-loader.js @@ -1,10 +1,6 @@ import React from 'react'; export default class extends React.Component { - constructor(props) { - super(props) - } - componentDidMount() { this.observer = new IntersectionObserver((entries) => { if (entries[0].isIntersecting) { @@ -24,7 +20,13 @@ export default class extends React.Component { render() { return ( -
this.element = el} className={this.props.className} style={this.props.style}>{this.props.children}
+
{ this.element = el }} + className={this.props.className} + style={this.props.style} + > + {this.props.children} +
); } } diff --git a/assets/js/dashboard/stats/countries.js b/assets/js/dashboard/stats/countries.js index b5f247686..96b02e525 100644 --- a/assets/js/dashboard/stats/countries.js +++ b/assets/js/dashboard/stats/countries.js @@ -148,7 +148,9 @@ class Countries extends React.Component { render() { return ( -
+
{ this.state.loading &&
} diff --git a/assets/js/dashboard/stats/devices/browsers.js b/assets/js/dashboard/stats/devices/browsers.js index 11150ea3c..57862dbf7 100644 --- a/assets/js/dashboard/stats/devices/browsers.js +++ b/assets/js/dashboard/stats/devices/browsers.js @@ -14,17 +14,17 @@ export default class Browsers extends React.Component { this.onVisible = this.onVisible.bind(this) } - onVisible() { - this.fetchBrowsers() - if (this.props.timer) this.props.timer.onTick(this.fetchBrowsers.bind(this)) - } - componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, browsers: null}) this.fetchBrowsers() } } + + onVisible() { + this.fetchBrowsers() + if (this.props.timer) this.props.timer.onTick(this.fetchBrowsers.bind(this)) + } fetchBrowsers() { if (this.props.query.filters.browser) { @@ -84,9 +84,9 @@ export default class Browsers extends React.Component { render() { return ( - + { this.state.loading &&
} - + { this.renderList() }
diff --git a/assets/js/dashboard/stats/devices/index.js b/assets/js/dashboard/stats/devices/index.js index a8d455322..a07bac301 100644 --- a/assets/js/dashboard/stats/devices/index.js +++ b/assets/js/dashboard/stats/devices/index.js @@ -8,7 +8,6 @@ import OperatingSystems from './operating-systems' import FadeIn from '../../fade-in' import numberFormatter from '../../number-formatter' import Bar from '../bar' -import MoreLink from '../more-link' import * as api from '../../api' @@ -46,11 +45,6 @@ class ScreenSizes extends React.Component { this.onVisible = this.onVisible.bind(this) } - onVisible() { - this.fetchScreenSizes() - if (this.props.timer) this.props.timer.onTick(this.fetchScreenSizes.bind(this)) - } - componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, sizes: null}) @@ -58,11 +52,24 @@ class ScreenSizes extends React.Component { } } + onVisible() { + this.fetchScreenSizes() + if (this.props.timer) this.props.timer.onTick(this.fetchScreenSizes.bind(this)) + } + + fetchScreenSizes() { - api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/screen-sizes`, this.props.query) + api.get( + `/api/stats/${encodeURIComponent(this.props.site.domain)}/screen-sizes`, + this.props.query + ) .then((res) => this.setState({loading: false, sizes: res})) } + label() { + return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' + } + renderScreenSize(size) { const query = new URLSearchParams(window.location.search) query.set('screen', size.name) @@ -70,43 +77,59 @@ class ScreenSizes extends React.Component { return (
- - + + {iconFor(size.name)} {size.name}
- {numberFormatter(size.count)} ({size.percentage}%) + + {numberFormatter(size.count)} + ({size.percentage}%) +
) } - label() { - return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' - } - renderList() { if (this.state.sizes && this.state.sizes.length > 0) { return ( -
+
Screen size { this.label() }
{ this.state.sizes && this.state.sizes.map(this.renderScreenSize.bind(this)) } ) - } else { - return
No data yet
} + return ( +
+ No data yet +
+ ) } render() { return ( - + { this.state.loading &&
} - + { this.renderList() }
@@ -117,57 +140,85 @@ class ScreenSizes extends React.Component { export default class Devices extends React.Component { constructor(props) { super(props) - this.tabKey = 'deviceTab__' + props.site.domain + this.tabKey = `deviceTab__${ props.site.domain}` const storedTab = storage.getItem(this.tabKey) this.state = { mode: storedTab || 'size' } } - renderContent() { - if (this.state.mode === 'size') { - return - } else if (this.state.mode === 'browser') { - return - } else if (this.state.mode === 'os') { - return - } - } - + setMode(mode) { return () => { storage.setItem(this.tabKey, mode) this.setState({mode}) } } + + renderContent() { + switch (this.state.mode) { + case 'browser': + return + case 'os': + return ( + + ) + case 'size': + default: + return ( + + ) + } + } renderPill(name, mode) { const isActive = this.state.mode === mode if (isActive) { - return
  • {name}
  • - } else { - return
  • {name}
  • + return ( +
  • + {name} +
  • + ) } + + return ( +
  • + {name} +
  • + ) } render() { return ( -
    -
    - +
    +

    Devices

    -
      { this.renderPill('Size', 'size') } { this.renderPill('Browser', 'browser') } { this.renderPill('OS', 'os') }
    - { this.renderContent() } -
    ) diff --git a/assets/js/dashboard/stats/devices/operating-systems.js b/assets/js/dashboard/stats/devices/operating-systems.js index 7e6011bc5..1b8e1abc0 100644 --- a/assets/js/dashboard/stats/devices/operating-systems.js +++ b/assets/js/dashboard/stats/devices/operating-systems.js @@ -83,9 +83,9 @@ export default class OperatingSystems extends React.Component { render() { return ( - + { this.state.loading &&
    } - + { this.renderList() }
    diff --git a/assets/js/dashboard/stats/more-link.js b/assets/js/dashboard/stats/more-link.js index 6878693bc..c0f23cf51 100644 --- a/assets/js/dashboard/stats/more-link.js +++ b/assets/js/dashboard/stats/more-link.js @@ -4,9 +4,26 @@ import { Link } from 'react-router-dom' export default function MoreLink({site, list, endpoint}) { if (list.length > 0) { return ( -
    - - +
    + + + {/* eslint-disable-next-line max-len */} + + DETAILS
    diff --git a/assets/js/dashboard/stats/pages/entry-pages.js b/assets/js/dashboard/stats/pages/entry-pages.js index 88c69ea6d..cb6323d77 100644 --- a/assets/js/dashboard/stats/pages/entry-pages.js +++ b/assets/js/dashboard/stats/pages/entry-pages.js @@ -42,9 +42,9 @@ export default class EntryPages extends React.Component {
    - + {page.name} - + @@ -57,7 +57,7 @@ export default class EntryPages extends React.Component { renderList() { if (this.state.pages && this.state.pages.length > 0) { return ( - + <>
    Page url Unique Entrances @@ -66,19 +66,24 @@ export default class EntryPages extends React.Component { { this.state.pages.map(this.renderPage.bind(this)) } - + ) - } else { - return
    No data yet
    - } + } + return ( +
    + No data yet +
    + ) } render() { const { loading } = this.state; return ( - + { loading &&
    } - + { this.renderList() } {!loading && } diff --git a/assets/js/dashboard/stats/pages/exit-pages.js b/assets/js/dashboard/stats/pages/exit-pages.js index 397b3142e..5397165b6 100644 --- a/assets/js/dashboard/stats/pages/exit-pages.js +++ b/assets/js/dashboard/stats/pages/exit-pages.js @@ -76,9 +76,9 @@ export default class ExitPages extends React.Component { render() { const { loading } = this.state; return ( - + { loading &&
    } - + { this.renderList() } {!loading && } diff --git a/assets/js/dashboard/stats/pages/index.js b/assets/js/dashboard/stats/pages/index.js index 08a4ab244..fb6b45ccf 100644 --- a/assets/js/dashboard/stats/pages/index.js +++ b/assets/js/dashboard/stats/pages/index.js @@ -1,11 +1,9 @@ import React from 'react'; -import { Link } from 'react-router-dom' import * as storage from '../../storage' import Visits from './pages' import EntryPages from './entry-pages' import ExitPages from './exit-pages' -import FadeIn from '../../fade-in' const labelFor = { 'pages': 'Top Pages', @@ -16,23 +14,13 @@ const labelFor = { export default class Pages extends React.Component { constructor(props) { super(props) - this.tabKey = 'pageTab__' + props.site.domain + this.tabKey = `pageTab__${ props.site.domain}` const storedTab = storage.getItem(this.tabKey) this.state = { mode: storedTab || 'pages' } } - renderContent() { - if (this.state.mode === 'pages') { - return - } else if (this.state.mode === 'entry-pages') { - return - } else if (this.state.mode === 'exit-pages') { - return - } - } - setMode(mode) { return () => { storage.setItem(this.tabKey, mode) @@ -40,34 +28,63 @@ export default class Pages extends React.Component { } } + renderContent() { + switch(this.state.mode) { + case "entry-pages": + return + case "exit-pages": + return + case "pages": + default: + return + } + } + + renderPill(name, mode) { const isActive = this.state.mode === mode if (isActive) { - return
  • {name}
  • - } else { - return
  • {name}
  • + return ( +
  • + {name} +
  • + ) } + + return ( +
  • + {name} +
  • + ) } render() { - const filters = this.props.query.filters return ( -
    -
    - +
    +
    + {/* Header Container */}
    -

    {labelFor[this.state.mode] || 'Page Visits'}

    - +

    + {labelFor[this.state.mode] || 'Page Visits'} +

      { this.renderPill('Top Pages', 'pages') } { this.renderPill('Entry Pages', 'entry-pages') } { this.renderPill('Exit Pages', 'exit-pages') }
    - + {/* Main Contents */} { this.renderContent() } -
    ) diff --git a/assets/js/dashboard/stats/pages/pages.js b/assets/js/dashboard/stats/pages/pages.js index 4ad1d7765..493c3c68f 100644 --- a/assets/js/dashboard/stats/pages/pages.js +++ b/assets/js/dashboard/stats/pages/pages.js @@ -87,9 +87,9 @@ export default class Visits extends React.Component { render() { const { loading } = this.state; return ( - + { loading &&
    } - + { this.renderList() } {!loading && } diff --git a/assets/js/dashboard/stats/sources/referrer-list.js b/assets/js/dashboard/stats/sources/referrer-list.js index 071b1967d..c122fef81 100644 --- a/assets/js/dashboard/stats/sources/referrer-list.js +++ b/assets/js/dashboard/stats/sources/referrer-list.js @@ -94,20 +94,27 @@ export default class Referrers extends React.Component { renderList() { if (this.state.referrers.length > 0) { return ( - -
    +
    +
    Referrer { this.label() }
    - + {this.state.referrers.map(this.renderReferrer.bind(this))} - +
    ) - } else { - return
    No data yet
    } + return ( +
    + No data yet +
    + ) } renderContent() { @@ -123,13 +130,15 @@ export default class Referrers extends React.Component { render() { return ( -
    - +
    +

    Top Referrers

    { this.state.loading &&
    } - - { this.renderContent() } - + + { this.renderContent() } +
    ) diff --git a/assets/js/dashboard/stats/sources/search-terms.js b/assets/js/dashboard/stats/sources/search-terms.js index f453126a1..65018f8cd 100644 --- a/assets/js/dashboard/stats/sources/search-terms.js +++ b/assets/js/dashboard/stats/sources/search-terms.js @@ -107,9 +107,11 @@ export default class SearchTerms extends React.Component { render() { return ( -
    +
    { this.state.loading &&
    } - + { this.renderContent() }
    diff --git a/assets/js/dashboard/stats/sources/source-list.js b/assets/js/dashboard/stats/sources/source-list.js index 1bd378d50..40ca9003a 100644 --- a/assets/js/dashboard/stats/sources/source-list.js +++ b/assets/js/dashboard/stats/sources/source-list.js @@ -99,7 +99,9 @@ class AllSources extends React.Component { render() { return ( -
    +
    { this.renderContent() }
    ) @@ -166,17 +168,17 @@ class UTMSources extends React.Component { renderList() { if (this.state.referrers && this.state.referrers.length > 0) { return ( - +
    {UTM_TAGS[this.props.tab].label} {this.label()}
    - + {this.state.referrers.map(this.renderReferrer.bind(this))} - +
    ) } else { return
    No data yet
    @@ -191,7 +193,7 @@ class UTMSources extends React.Component { { this.props.renderTabs() }
    { this.state.loading &&
    } - + { this.renderList() } @@ -200,7 +202,9 @@ class UTMSources extends React.Component { render() { return ( -
    +
    { this.renderContent() }
    ) From 3fe8bd6681cac141bf267b0e2e6c57070142d9fc Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 16 Jun 2021 12:03:01 +0300 Subject: [PATCH 5/6] Pr/1117 formatted (#1129) * runtime.exs: get var from file * Format pr/1117 Co-authored-by: happysalada --- config/runtime.exs | 199 +++++++++++++++++++++----------- lib/plausible/helpers/config.ex | 11 ++ 2 files changed, 143 insertions(+), 67 deletions(-) create mode 100644 lib/plausible/helpers/config.ex diff --git a/config/runtime.exs b/config/runtime.exs index 330da9d50..0ce9c8565 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -1,12 +1,16 @@ import Config +import Plausible.ConfigHelpers if config_env() in [:dev, :test] do Envy.auto_load() end -port = System.get_env("PORT") || 8000 +config_dir = System.get_env("CONFIG_DIR", "/run/secrets") -base_url = System.get_env("BASE_URL") +# System.get_env does not accept a non string default +port = get_var_from_path_or_env(config_dir, "PORT") || 8000 + +base_url = get_var_from_path_or_env(config_dir, "BASE_URL") if !base_url do raise "BASE_URL configuration option is required. See https://plausible.io/docs/self-hosting-configuration#server" @@ -20,79 +24,137 @@ if base_url.scheme not in ["http", "https"] do }`" end -secret_key_base = - case System.get_env("SECRET_KEY_BASE") do - nil -> - raise "SECRET_KEY_BASE configuration option is required. See https://plausible.io/docs/self-hosting-configuration#server" +secret_key_base = get_var_from_path_or_env(config_dir, "SECRET_KEY_BASE", nil) - key when byte_size(key) < 64 -> - raise "SECRET_KEY_BASE must be at least 64 bytes long. See https://plausible.io/docs/self-hosting-configuration#server" +case secret_key_base do + nil -> + raise "SECRET_KEY_BASE configuration option is required. See https://plausible.io/docs/self-hosting-configuration#server" - key -> - key - end + key when byte_size(key) < 64 -> + raise "SECRET_KEY_BASE must be at least 64 bytes long. See https://plausible.io/docs/self-hosting-configuration#server" + + _ -> + nil +end db_url = - System.get_env( + get_var_from_path_or_env( + config_dir, "DATABASE_URL", "postgres://postgres:postgres@plausible_db:5432/plausible_db" ) -admin_user = System.get_env("ADMIN_USER_NAME") -admin_email = System.get_env("ADMIN_USER_EMAIL") -admin_emails = System.get_env("ADMIN_EMAILS", "") |> String.split(",") -admin_pwd = System.get_env("ADMIN_USER_PWD") -env = System.get_env("ENVIRONMENT", "prod") -mailer_adapter = System.get_env("MAILER_ADAPTER", "Bamboo.SMTPAdapter") -mailer_email = System.get_env("MAILER_EMAIL", "hello@plausible.local") -app_version = System.get_env("APP_VERSION", "0.0.1") +db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR") + +admin_user = get_var_from_path_or_env(config_dir, "ADMIN_USER_NAME") +admin_email = get_var_from_path_or_env(config_dir, "ADMIN_USER_EMAIL") + +admin_emails = + get_var_from_path_or_env(config_dir, "ADMIN_EMAILS", "") + |> String.split(",") + +admin_pwd = get_var_from_path_or_env(config_dir, "ADMIN_USER_PWD") +env = get_var_from_path_or_env(config_dir, "ENVIRONMENT", "prod") +mailer_adapter = get_var_from_path_or_env(config_dir, "MAILER_ADAPTER", "Bamboo.SMTPAdapter") +mailer_email = get_var_from_path_or_env(config_dir, "MAILER_EMAIL", "hello@plausible.local") +app_version = get_var_from_path_or_env(config_dir, "APP_VERSION", "0.0.1") ch_db_url = - System.get_env("CLICKHOUSE_DATABASE_URL", "http://plausible_events_db:8123/plausible_events_db") + get_var_from_path_or_env( + config_dir, + "CLICKHOUSE_DATABASE_URL", + "http://plausible_events_db:8123/plausible_events_db" + ) -{ch_flush_interval_ms, ""} = Integer.parse(System.get_env("CLICKHOUSE_FLUSH_INTERVAL_MS", "5000")) -{ch_max_buffer_size, ""} = Integer.parse(System.get_env("CLICKHOUSE_MAX_BUFFER_SIZE", "10000")) +{ch_flush_interval_ms, ""} = + config_dir + |> get_var_from_path_or_env("CLICKHOUSE_FLUSH_INTERVAL_MS", "5000") + |> Integer.parse() + +{ch_max_buffer_size, ""} = + config_dir + |> get_var_from_path_or_env("CLICKHOUSE_MAX_BUFFER_SIZE", "10000") + |> Integer.parse() ### Mandatory params End -sentry_dsn = System.get_env("SENTRY_DSN") -paddle_auth_code = System.get_env("PADDLE_VENDOR_AUTH_CODE") -google_cid = System.get_env("GOOGLE_CLIENT_ID") -google_secret = System.get_env("GOOGLE_CLIENT_SECRET") -slack_hook_url = System.get_env("SLACK_WEBHOOK") -twitter_consumer_key = System.get_env("TWITTER_CONSUMER_KEY") -twitter_consumer_secret = System.get_env("TWITTER_CONSUMER_SECRET") -twitter_token = System.get_env("TWITTER_ACCESS_TOKEN") -twitter_token_secret = System.get_env("TWITTER_ACCESS_TOKEN_SECRET") -postmark_api_key = System.get_env("POSTMARK_API_KEY") -cron_enabled = String.to_existing_atom(System.get_env("CRON_ENABLED", "false")) -custom_domain_server_ip = System.get_env("CUSTOM_DOMAIN_SERVER_IP") -custom_domain_server_user = System.get_env("CUSTOM_DOMAIN_SERVER_USER") -custom_domain_server_password = System.get_env("CUSTOM_DOMAIN_SERVER_PASSWORD") +sentry_dsn = get_var_from_path_or_env(config_dir, "SENTRY_DSN") +paddle_auth_code = get_var_from_path_or_env(config_dir, "PADDLE_VENDOR_AUTH_CODE") +google_cid = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_ID") +google_secret = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_SECRET") +slack_hook_url = get_var_from_path_or_env(config_dir, "SLACK_WEBHOOK") +twitter_consumer_key = get_var_from_path_or_env(config_dir, "TWITTER_CONSUMER_KEY") +twitter_consumer_secret = get_var_from_path_or_env(config_dir, "TWITTER_CONSUMER_SECRET") +twitter_token = get_var_from_path_or_env(config_dir, "TWITTER_ACCESS_TOKEN") +twitter_token_secret = get_var_from_path_or_env(config_dir, "TWITTER_ACCESS_TOKEN_SECRET") +postmark_api_key = get_var_from_path_or_env(config_dir, "POSTMARK_API_KEY") + +cron_enabled = + config_dir + |> get_var_from_path_or_env("CRON_ENABLED", "false") + |> String.to_existing_atom() + +custom_domain_server_ip = get_var_from_path_or_env(config_dir, "CUSTOM_DOMAIN_SERVER_IP") +custom_domain_server_user = get_var_from_path_or_env(config_dir, "CUSTOM_DOMAIN_SERVER_USER") + +custom_domain_server_password = + get_var_from_path_or_env(config_dir, "CUSTOM_DOMAIN_SERVER_PASSWORD") geolite2_country_db = - System.get_env( + get_var_from_path_or_env( + config_dir, "GEOLITE2_COUNTRY_DB", Application.app_dir(:plausible) <> "/priv/geodb/dbip-country.mmdb" ) -disable_auth = String.to_existing_atom(System.get_env("DISABLE_AUTH", "false")) -disable_registration = String.to_existing_atom(System.get_env("DISABLE_REGISTRATION", "false")) -hcaptcha_sitekey = System.get_env("HCAPTCHA_SITEKEY") -hcaptcha_secret = System.get_env("HCAPTCHA_SECRET") -log_level = String.to_existing_atom(System.get_env("LOG_LEVEL", "warn")) -is_selfhost = String.to_existing_atom(System.get_env("SELFHOST", "true")) -{site_limit, ""} = Integer.parse(System.get_env("SITE_LIMIT", "20")) +disable_auth = + config_dir + |> get_var_from_path_or_env("DISABLE_AUTH", "false") + |> String.to_existing_atom() + +disable_registration = + config_dir + |> get_var_from_path_or_env("DISABLE_REGISTRATION", "false") + |> String.to_existing_atom() + +hcaptcha_sitekey = get_var_from_path_or_env(config_dir, "HCAPTCHA_SITEKEY") +hcaptcha_secret = get_var_from_path_or_env(config_dir, "HCAPTCHA_SECRET") + +log_level = + config_dir + |> get_var_from_path_or_env("LOG_LEVEL", "warn") + |> String.to_existing_atom() + +is_selfhost = + config_dir + |> get_var_from_path_or_env("SELFHOST", "true") + |> String.to_existing_atom() + +{site_limit, ""} = + config_dir + |> get_var_from_path_or_env("SITE_LIMIT", "20") + |> Integer.parse() site_limit_exempt = - System.get_env("SITE_LIMIT_EXEMPT", "") |> String.split(",") |> Enum.map(&String.trim/1) + config_dir + |> get_var_from_path_or_env("SITE_LIMIT_EXEMPT", "") + |> String.split(",") + |> Enum.map(&String.trim/1) -disable_cron = String.to_existing_atom(System.get_env("DISABLE_CRON", "false")) +disable_cron = + config_dir + |> get_var_from_path_or_env("DISABLE_CRON", "false") + |> String.to_existing_atom() -{user_agent_cache_limit, ""} = Integer.parse(System.get_env("USER_AGENT_CACHE_LIMIT", "1000")) +{user_agent_cache_limit, ""} = + config_dir + |> get_var_from_path_or_env("USER_AGENT_CACHE_LIMIT", "1000") + |> Integer.parse() user_agent_cache_stats = - String.to_existing_atom(System.get_env("USER_AGENT_CACHE_STATS", "false")) + config_dir + |> get_var_from_path_or_env("USER_AGENT_CACHE_STATS", "false") + |> String.to_existing_atom() config :plausible, admin_user: admin_user, @@ -114,14 +176,12 @@ config :plausible, PlausibleWeb.Endpoint, http: [port: port], secret_key_base: secret_key_base -case System.get_env("DATABASE_SOCKET_DIR", "") do - "" -> - config :plausible, Plausible.Repo, url: db_url - - x -> - config :plausible, Plausible.Repo, - socket_dir: x, - database: System.get_env("DATABASE_NAME") +if is_nil(db_socket_dir) do + config :plausible, Plausible.Repo, url: db_url +else + config :plausible, Plausible.Repo, + socket_dir: db_socket_dir, + database: get_var_from_path_or_env(config_dir, "DATABASE_NAME", "plausible") end config :sentry, @@ -154,21 +214,21 @@ case mailer_adapter do config :plausible, Plausible.Mailer, adapter: :"Elixir.#{mailer_adapter}", request_options: [recv_timeout: 10_000], - api_key: System.get_env("POSTMARK_API_KEY") + api_key: get_var_from_path_or_env(config_dir, "POSTMARK_API_KEY") "Bamboo.SMTPAdapter" -> config :plausible, Plausible.Mailer, adapter: :"Elixir.#{mailer_adapter}", - server: System.get_env("SMTP_HOST_ADDR", "mail"), + server: get_var_from_path_or_env(config_dir, "SMTP_HOST_ADDR", "mail"), hostname: base_url.host, - port: System.get_env("SMTP_HOST_PORT", "25"), - username: System.get_env("SMTP_USER_NAME"), - password: System.get_env("SMTP_USER_PWD"), + port: get_var_from_path_or_env(config_dir, "SMTP_HOST_PORT", "25"), + username: get_var_from_path_or_env(config_dir, "SMTP_USER_NAME"), + password: get_var_from_path_or_env(config_dir, "SMTP_USER_PWD"), tls: :if_available, allowed_tls_versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"], - ssl: System.get_env("SMTP_HOST_SSL_ENABLED") || false, - retries: System.get_env("SMTP_RETRIES") || 2, - no_mx_lookups: System.get_env("SMTP_MX_LOOKUPS_ENABLED") || true + ssl: get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED", false), + retries: get_var_from_path_or_env(config_dir, "SMTP_RETRIES", 2), + no_mx_lookups: get_var_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED") || true "Bamboo.LocalAdapter" -> config :plausible, Plausible.Mailer, adapter: Bamboo.LocalAdapter @@ -192,7 +252,10 @@ config :plausible, :custom_domain_server, ip: custom_domain_server_ip config :plausible, PlausibleWeb.Firewall, - blocklist: System.get_env("IP_BLOCKLIST", "") |> String.split(",") |> Enum.map(&String.trim/1) + blocklist: + get_var_from_path_or_env(config_dir, "IP_BLOCKLIST", "") + |> String.split(",") + |> Enum.map(&String.trim/1) if config_env() == :prod && !disable_cron do base_cron = [ @@ -310,4 +373,6 @@ config :logger, Sentry.LoggerBackend, level: :error, excluded_domains: [] -config :tzdata, :data_dir, System.get_env("STORAGE_DIR", Application.app_dir(:tzdata, "priv")) +config :tzdata, + :data_dir, + get_var_from_path_or_env(config_dir, "STORAGE_DIR", Application.app_dir(:tzdata, "priv")) diff --git a/lib/plausible/helpers/config.ex b/lib/plausible/helpers/config.ex new file mode 100644 index 000000000..7b73a713b --- /dev/null +++ b/lib/plausible/helpers/config.ex @@ -0,0 +1,11 @@ +defmodule Plausible.ConfigHelpers do + def get_var_from_path_or_env(config_dir, var_name, default \\ nil) do + var_path = Path.join(config_dir, var_name) + + if File.exists?(var_path) do + File.read(var_path) + else + System.get_env(var_name, default) + end + end +end From 7e229140ed372c0ea04fa6e977875dcf4b96348d Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 16 Jun 2021 12:16:11 +0300 Subject: [PATCH 6/6] Move config files to config directory --- .env.dev => config/.env.dev | 0 .env.test => config/.env.test | 0 config/runtime.exs | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename .env.dev => config/.env.dev (100%) rename .env.test => config/.env.test (100%) diff --git a/.env.dev b/config/.env.dev similarity index 100% rename from .env.dev rename to config/.env.dev diff --git a/.env.test b/config/.env.test similarity index 100% rename from .env.test rename to config/.env.test diff --git a/config/runtime.exs b/config/runtime.exs index 0ce9c8565..b03608247 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -2,7 +2,7 @@ import Config import Plausible.ConfigHelpers if config_env() in [:dev, :test] do - Envy.auto_load() + Envy.load(["config/.env.#{config_env()}"]) end config_dir = System.get_env("CONFIG_DIR", "/run/secrets")