Compare commits
2 Commits
master
...
reapply-ca
| Author | SHA1 | Date |
|---|---|---|
|
|
6019ebe146 | 3 years ago |
|
|
317cb8aa85 | 3 years ago |
@ -1,28 +0,0 @@
|
||||
name: Validate migrations
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- 'priv/repo/migrations/**'
|
||||
- 'priv/ingest_repo/migrations/**'
|
||||
|
||||
jobs:
|
||||
validate:
|
||||
name: App code does not change at the same time
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: dorny/paths-filter@v3
|
||||
id: changes
|
||||
with:
|
||||
list-files: json
|
||||
filters: |
|
||||
lib:
|
||||
- 'lib/**'
|
||||
- 'extra/**'
|
||||
- 'config/**'
|
||||
|
||||
- if: steps.changes.outputs.lib == 'true'
|
||||
run: |
|
||||
echo "::error file=${{ fromJSON(steps.changes.outputs.lib_files)[0] }}::Code and migrations shouldn't be changed at the same time"
|
||||
exit 1
|
||||
@ -1,247 +0,0 @@
|
||||
import React from 'react';
|
||||
import { withRouter } from 'react-router-dom'
|
||||
import Chart from 'chart.js/auto';
|
||||
import { navigateToQuery } from '../../query'
|
||||
import GraphTooltip from './graph-tooltip'
|
||||
import { buildDataSet, METRIC_LABELS, METRIC_FORMATTER } from './graph-util'
|
||||
import dateFormatter from './date-formatter';
|
||||
import FadeIn from '../../fade-in';
|
||||
import classNames from 'classnames';
|
||||
|
||||
const calculateMaximumY = function(dataset) {
|
||||
const yAxisValues = dataset
|
||||
.flatMap((item) => item.data)
|
||||
.map((item) => item || 0)
|
||||
|
||||
if (yAxisValues) {
|
||||
return Math.max(...yAxisValues)
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
class LineGraph extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.regenerateChart = this.regenerateChart.bind(this);
|
||||
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
|
||||
}
|
||||
|
||||
getGraphMetric() {
|
||||
let metric = this.props.graphData.metric
|
||||
|
||||
if (metric == 'visitors' && this.props.query.filters.goal) {
|
||||
return 'conversions'
|
||||
} else {
|
||||
return metric
|
||||
}
|
||||
}
|
||||
|
||||
regenerateChart() {
|
||||
const { graphData, query } = this.props
|
||||
const metric = this.getGraphMetric()
|
||||
const graphEl = document.getElementById("main-graph-canvas")
|
||||
this.ctx = graphEl.getContext('2d');
|
||||
const dataSet = buildDataSet(graphData.plot, graphData.comparison_plot, graphData.present_index, this.ctx, METRIC_LABELS[metric])
|
||||
|
||||
return new Chart(this.ctx, {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: graphData.labels,
|
||||
datasets: dataSet
|
||||
},
|
||||
options: {
|
||||
animation: false,
|
||||
plugins: {
|
||||
legend: { display: false },
|
||||
tooltip: {
|
||||
enabled: false,
|
||||
mode: 'index',
|
||||
intersect: false,
|
||||
position: 'average',
|
||||
external: GraphTooltip(graphData, metric, query)
|
||||
},
|
||||
},
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
onResize: this.updateWindowDimensions,
|
||||
elements: { line: { tension: 0 }, point: { radius: 0 } },
|
||||
onClick: this.maybeHopToHoveredPeriod.bind(this),
|
||||
scale: {
|
||||
ticks: { precision: 0, maxTicksLimit: 8 }
|
||||
},
|
||||
scales: {
|
||||
y: {
|
||||
min: 0,
|
||||
suggestedMax: calculateMaximumY(dataSet),
|
||||
ticks: {
|
||||
callback: METRIC_FORMATTER[metric],
|
||||
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
||||
},
|
||||
grid: {
|
||||
zeroLineColor: 'transparent',
|
||||
drawBorder: false,
|
||||
}
|
||||
},
|
||||
yComparison: {
|
||||
min: 0,
|
||||
suggestedMax: calculateMaximumY(dataSet),
|
||||
display: false,
|
||||
grid: { display: false },
|
||||
},
|
||||
x: {
|
||||
grid: { display: false },
|
||||
ticks: {
|
||||
callback: function(val, _index, _ticks) {
|
||||
if (this.getLabelForValue(val) == "__blank__") return ""
|
||||
|
||||
const hasMultipleYears =
|
||||
graphData.labels
|
||||
.filter((date) => typeof date === 'string')
|
||||
.map(date => date.split('-')[0])
|
||||
.filter((value, index, list) => list.indexOf(value) === index)
|
||||
.length > 1
|
||||
|
||||
if (graphData.interval === 'hour' && query.period !== 'day') {
|
||||
const date = dateFormatter({
|
||||
interval: "date",
|
||||
longForm: false,
|
||||
period: query.period,
|
||||
shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
|
||||
const hour = dateFormatter({
|
||||
interval: graphData.interval,
|
||||
longForm: false,
|
||||
period: query.period,
|
||||
shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
|
||||
// Returns a combination of date and hour. This is because
|
||||
// small intervals like hour may return multiple days
|
||||
// depending on the query period.
|
||||
return `${date}, ${hour}`
|
||||
}
|
||||
|
||||
if (graphData.interval === 'minute' && query.period !== 'realtime') {
|
||||
return dateFormatter({
|
||||
interval: "hour", longForm: false, period: query.period,
|
||||
})(this.getLabelForValue(val))
|
||||
}
|
||||
|
||||
return dateFormatter({
|
||||
interval: graphData.interval, longForm: false, period: query.period, shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
},
|
||||
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
||||
}
|
||||
}
|
||||
},
|
||||
interaction: {
|
||||
mode: 'index',
|
||||
intersect: false,
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
repositionTooltip(e) {
|
||||
const tooltipEl = document.getElementById('chartjs-tooltip');
|
||||
if (tooltipEl && window.innerWidth >= 768) {
|
||||
if (e.clientX > 0.66 * window.innerWidth) {
|
||||
tooltipEl.style.right = (window.innerWidth - e.clientX) + window.pageXOffset + 'px'
|
||||
tooltipEl.style.left = null;
|
||||
} else {
|
||||
tooltipEl.style.right = null;
|
||||
tooltipEl.style.left = e.clientX + window.pageXOffset + 'px'
|
||||
}
|
||||
tooltipEl.style.top = e.clientY + window.pageYOffset + 'px'
|
||||
tooltipEl.style.opacity = 1;
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.graphData) {
|
||||
this.chart = this.regenerateChart();
|
||||
}
|
||||
window.addEventListener('mousemove', this.repositionTooltip);
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
const { graphData, darkTheme } = this.props;
|
||||
const tooltip = document.getElementById('chartjs-tooltip');
|
||||
|
||||
if (
|
||||
graphData !== prevProps.graphData ||
|
||||
darkTheme !== prevProps.darkTheme
|
||||
) {
|
||||
|
||||
if (graphData) {
|
||||
if (this.chart) {
|
||||
this.chart.destroy();
|
||||
}
|
||||
this.chart = this.regenerateChart();
|
||||
this.chart.update();
|
||||
}
|
||||
|
||||
if (tooltip) {
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
if (!graphData) {
|
||||
if (this.chart) {
|
||||
this.chart.destroy();
|
||||
}
|
||||
|
||||
if (tooltip) {
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
// Ensure that the tooltip doesn't hang around when we are loading more data
|
||||
const tooltip = document.getElementById('chartjs-tooltip');
|
||||
if (tooltip) {
|
||||
tooltip.style.opacity = 0;
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
window.removeEventListener('mousemove', this.repositionTooltip)
|
||||
}
|
||||
|
||||
/**
|
||||
* The current ticks' limits are set to treat iPad (regular/Mini/Pro) as a regular screen.
|
||||
* @param {*} chart - The chart instance.
|
||||
* @param {*} dimensions - An object containing the new dimensions *of the chart.*
|
||||
*/
|
||||
updateWindowDimensions(chart, dimensions) {
|
||||
chart.options.scales.x.ticks.maxTicksLimit = dimensions.width < 720 ? 5 : 8
|
||||
}
|
||||
|
||||
maybeHopToHoveredPeriod(e) {
|
||||
const element = this.chart.getElementsAtEventForMode(e, 'index', { intersect: false })[0]
|
||||
const date = this.props.graphData.labels[element.index] || this.props.graphData.comparison_labels[element.index]
|
||||
|
||||
if (this.props.graphData.interval === 'month') {
|
||||
navigateToQuery(this.props.history, this.props.query, { period: 'month', date })
|
||||
} else if (this.props.graphData.interval === 'date') {
|
||||
navigateToQuery(this.props.history, this.props.query, { period: 'day', date })
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { graphData } = this.props
|
||||
const canvasClass = classNames('mt-4 select-none', { 'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval) })
|
||||
|
||||
return (
|
||||
<FadeIn show={graphData}>
|
||||
<div className="relative h-96 print:h-auto print:pb-8 w-full z-0">
|
||||
<canvas id="main-graph-canvas" className={canvasClass}></canvas>
|
||||
</div>
|
||||
</FadeIn>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default withRouter(LineGraph)
|
||||
@ -1,17 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
export default function SamplingNotice({topStatData}) {
|
||||
const samplePercent = topStatData?.samplePercent
|
||||
|
||||
if (samplePercent && samplePercent < 100) {
|
||||
return (
|
||||
<div tooltip={`Stats based on a ${samplePercent}% sample of all visitors`} className="cursor-pointer w-4 h-4 mx-2">
|
||||
<svg className="absolute w-4 h-4 dark:text-gray-300 text-gray-700" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||
</svg>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
import React, { useState } from "react"
|
||||
import * as api from '../../api'
|
||||
import { getCurrentInterval } from "./interval-picker"
|
||||
|
||||
export default function StatsExport({site, query}) {
|
||||
const [exporting, setExporting] = useState(false)
|
||||
|
||||
function startExport() {
|
||||
setExporting(true)
|
||||
document.cookie = "exporting="
|
||||
pollExportReady()
|
||||
}
|
||||
|
||||
function pollExportReady() {
|
||||
if (document.cookie.includes('exporting')) {
|
||||
setTimeout(pollExportReady, 1000)
|
||||
} else {
|
||||
setExporting(false)
|
||||
}
|
||||
}
|
||||
|
||||
function renderLoading() {
|
||||
return (
|
||||
<svg className="animate-spin h-4 w-4 text-indigo-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
function renderExportLink() {
|
||||
const interval = getCurrentInterval(site, query)
|
||||
const queryParams = api.serializeQuery(query, [{ interval }])
|
||||
const endpoint = `/${encodeURIComponent(site.domain)}/export${queryParams}`
|
||||
|
||||
return (
|
||||
<a href={endpoint} download onClick={startExport}>
|
||||
<svg className="absolute text-gray-700 feather dark:text-gray-300 hover:text-indigo-600 dark:hover:text-indigo-600" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
|
||||
<polyline points="7 10 12 15 17 10"></polyline>
|
||||
<line x1="12" y1="15" x2="12" y2="3"></line>
|
||||
</svg>
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-4 h-4 mx-2">
|
||||
{exporting && renderLoading()}
|
||||
{!exporting && renderExportLink()}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
import React from "react"
|
||||
import { parseNaiveDate, isBefore } from '../../util/date'
|
||||
import { Link } from 'react-router-dom'
|
||||
import * as url from '../../util/url'
|
||||
import { BarsArrowUpIcon } from '@heroicons/react/20/solid'
|
||||
|
||||
export default function WithImportedSwitch({site, topStatData}) {
|
||||
if (!topStatData?.imports_exist) {
|
||||
return null
|
||||
}
|
||||
|
||||
function isBeforeNativeStats(date) {
|
||||
if (!date) return false
|
||||
|
||||
const nativeStatsBegin = parseNaiveDate(site.nativeStatsBegin)
|
||||
const parsedDate = parseNaiveDate(date)
|
||||
|
||||
return isBefore(parsedDate, nativeStatsBegin, "day")
|
||||
}
|
||||
|
||||
const isQueryingImportedPeriod = isBeforeNativeStats(topStatData.from)
|
||||
const isComparingImportedPeriod = isBeforeNativeStats(topStatData.comparing_from)
|
||||
|
||||
if (isQueryingImportedPeriod || isComparingImportedPeriod) {
|
||||
const withImported = topStatData.with_imported;
|
||||
const toggleColor = withImported ? " dark:text-gray-300 text-gray-700" : " dark:text-gray-500 text-gray-400"
|
||||
const target = url.setQuery('with_imported', !withImported)
|
||||
const tip = withImported ? "" : "do not ";
|
||||
|
||||
return (
|
||||
<Link to={target} className="w-4 h-4 mx-2">
|
||||
<div tooltip={`Stats ${tip}include imported data.`} className="cursor-pointer w-4 h-4">
|
||||
<BarsArrowUpIcon className={"absolute " + toggleColor} />
|
||||
</div>
|
||||
</Link>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"kind": "analyticsData#batchRunReports",
|
||||
"reports": [
|
||||
{
|
||||
"dimensionHeaders": [
|
||||
{
|
||||
"name": "date"
|
||||
}
|
||||
],
|
||||
"kind": "analyticsData#runReport",
|
||||
"metadata": {
|
||||
"currencyCode": "USD",
|
||||
"timeZone": "Europe/Warsaw"
|
||||
},
|
||||
"metricHeaders": [
|
||||
{
|
||||
"name": "screenPageViews",
|
||||
"type": "TYPE_INTEGER"
|
||||
}
|
||||
],
|
||||
"rowCount": 3,
|
||||
"rows": [
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240302"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
{
|
||||
"account": "accounts/28425555",
|
||||
"createTime": "2024-02-22T10:50:15.462Z",
|
||||
"currencyCode": "USD",
|
||||
"displayName": "account.one - GA4",
|
||||
"name": "properties/428685444",
|
||||
"parent": "accounts/28425555",
|
||||
"propertyType": "PROPERTY_TYPE_ORDINARY",
|
||||
"serviceLevel": "GOOGLE_ANALYTICS_STANDARD",
|
||||
"timeZone": "Europe/Warsaw",
|
||||
"updateTime": "2024-02-22T10:50:15.462Z"
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20200421"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20120118"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20170118"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,188 +0,0 @@
|
||||
defmodule Plausible.DataMigration.SiteImports do
|
||||
@moduledoc """
|
||||
Site imports migration backfilling SiteImport entries for old imports
|
||||
and alters import end dates to match actual end date of respective import stats.
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Plausible.ClickhouseRepo
|
||||
alias Plausible.Imported
|
||||
alias Plausible.Repo
|
||||
alias Plausible.Site
|
||||
|
||||
def run(opts \\ []) do
|
||||
dry_run? = Keyword.get(opts, :dry_run?, true)
|
||||
|
||||
site_import_query =
|
||||
from(i in Imported.SiteImport, where: i.site_id == parent_as(:site).id, select: 1)
|
||||
|
||||
sites_with_imports =
|
||||
from(s in Site, as: :site, where: not is_nil(s.imported_data) or exists(site_import_query))
|
||||
|> Repo.all(log: false)
|
||||
|
||||
sites_count = length(sites_with_imports)
|
||||
|
||||
IO.puts("Processing #{sites_count} sites with imports (DRY RUN: #{dry_run?})...")
|
||||
|
||||
for {site, idx} <- Enum.with_index(sites_with_imports) do
|
||||
site_imports =
|
||||
from(i in Imported.SiteImport, where: i.site_id == ^site.id)
|
||||
|> Repo.all(log: false)
|
||||
|
||||
IO.puts(
|
||||
"Processing site ID #{site.id} (#{idx + 1} / #{sites_count}) (imported_data: #{is_struct(site.imported_data)}, site_imports: #{length(site_imports)})"
|
||||
)
|
||||
|
||||
site_imports =
|
||||
if site.imported_data && not Enum.any?(site_imports, & &1.legacy) do
|
||||
IO.puts("Creating legacy site import entry for site ID #{site.id}")
|
||||
|
||||
# create legacy entry if there's not one yet
|
||||
params =
|
||||
site.imported_data
|
||||
|> Imported.SiteImport.from_legacy()
|
||||
|> Map.put(:site_id, site.id)
|
||||
|> Map.take([:legacy, :start_date, :end_date, :source, :status, :site_id])
|
||||
|
||||
legacy_site_import =
|
||||
%Imported.SiteImport{}
|
||||
|> Ecto.Changeset.change(params)
|
||||
|> insert!(dry_run?)
|
||||
|
||||
[legacy_site_import | site_imports]
|
||||
else
|
||||
IO.puts("Legacy site import entry for site ID #{site.id} already exists")
|
||||
|
||||
site_imports
|
||||
end
|
||||
|
||||
# adjust end date for each site import
|
||||
for site_import <- site_imports do
|
||||
IO.puts(
|
||||
"Adjusting end date for site import #{site_import.id} (site ID #{site.id}, start date: #{site_import.start_date}, end date: #{site_import.end_date})"
|
||||
)
|
||||
|
||||
import_ids =
|
||||
if site_import.legacy do
|
||||
[0, site_import.id]
|
||||
else
|
||||
[site_import.id]
|
||||
end
|
||||
|
||||
end_date = imported_stats_end_date(site.id, import_ids)
|
||||
|
||||
if !end_date do
|
||||
IO.puts(
|
||||
"Site import #{site_import.id} (site ID #{site.id}) does not have any recorded stats. Removing it."
|
||||
)
|
||||
|
||||
if site_import.legacy do
|
||||
clear_imported_data(site, dry_run?)
|
||||
end
|
||||
|
||||
delete!(site_import, dry_run?)
|
||||
else
|
||||
end_date =
|
||||
if Date.compare(end_date, site_import.end_date) in [:lt, :eq] do
|
||||
end_date
|
||||
else
|
||||
IO.puts(
|
||||
"Site import #{site_import.id} (site ID #{site.id}) computed end date is later than the current one. Skipping."
|
||||
)
|
||||
|
||||
site_import.end_date
|
||||
end
|
||||
|
||||
site_import
|
||||
|> Ecto.Changeset.change(end_date: end_date)
|
||||
|> update!(dry_run?)
|
||||
|
||||
IO.puts(
|
||||
"End date of site import #{site_import.id} (site ID #{site.id}) adjusted to #{end_date}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
IO.puts("Done processing site ID #{site.id}")
|
||||
end
|
||||
|
||||
IO.puts("Finished")
|
||||
end
|
||||
|
||||
# Exposed for testing purposes
|
||||
@doc false
|
||||
def imported_stats_end_date(site_id, import_ids) do
|
||||
[first_schema | schemas] = Imported.schemas()
|
||||
|
||||
query =
|
||||
Enum.reduce(schemas, max_date_query(first_schema, site_id, import_ids), fn schema, query ->
|
||||
from(s in subquery(union_all(query, ^max_date_query(schema, site_id, import_ids))))
|
||||
end)
|
||||
|
||||
dates = ClickhouseRepo.all(from(q in query, select: q.max_date), log: false)
|
||||
|
||||
if dates != [] do
|
||||
case Enum.max(dates, Date) do
|
||||
# no stats for this domain yet
|
||||
~D[1970-01-01] ->
|
||||
nil
|
||||
|
||||
date ->
|
||||
date
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp insert!(changeset, false = _dry_run?) do
|
||||
Repo.insert!(changeset)
|
||||
end
|
||||
|
||||
defp insert!(changeset, true = _dry_run?) do
|
||||
if changeset.valid? do
|
||||
changeset
|
||||
|> Ecto.Changeset.change(id: 0)
|
||||
|> Ecto.Changeset.apply_changes()
|
||||
else
|
||||
raise "Invalid insert: #{inspect(changeset)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp clear_imported_data(site, false = _dry_run?) do
|
||||
Repo.update_all(from(s in Site, where: s.id == ^site.id), set: [imported_data: nil])
|
||||
end
|
||||
|
||||
defp clear_imported_data(site, true = _dry_run?) do
|
||||
%{site | imported_data: nil}
|
||||
end
|
||||
|
||||
defp update!(changeset, false = _dry_run?) do
|
||||
Repo.update!(changeset)
|
||||
end
|
||||
|
||||
defp update!(changeset, true = _dry_run?) do
|
||||
if changeset.valid? do
|
||||
Ecto.Changeset.apply_changes(changeset)
|
||||
else
|
||||
raise "Invalid update: #{inspect(changeset)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp delete!(entity, false = _dry_run?) do
|
||||
Repo.delete!(entity)
|
||||
end
|
||||
|
||||
defp delete!(entity, true = _dry_run?) do
|
||||
entity
|
||||
end
|
||||
|
||||
defp max_date_query(schema, site_id, import_ids) do
|
||||
from(q in schema,
|
||||
where: q.site_id == ^site_id,
|
||||
where: q.import_id in ^import_ids,
|
||||
select: %{max_date: fragment("max(?)", q.date)}
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -1,85 +0,0 @@
|
||||
defmodule CompiledRegex do
|
||||
@moduledoc """
|
||||
Ensures that the regex is compiled on load
|
||||
"""
|
||||
use Ecto.Type
|
||||
|
||||
def type, do: :string
|
||||
|
||||
def cast(val) when is_binary(val), do: {:ok, val}
|
||||
def cast(_), do: :error
|
||||
|
||||
def load(val), do: {:ok, Regex.compile!(val)}
|
||||
def dump(val), do: {:ok, val}
|
||||
end
|
||||
|
||||
defmodule Plausible.Shield.PageRule do
|
||||
@moduledoc """
|
||||
Schema for Pages block list
|
||||
"""
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@type t() :: %__MODULE__{}
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
schema "shield_rules_page" do
|
||||
belongs_to :site, Plausible.Site
|
||||
field :page_path, :string
|
||||
field :page_path_pattern, CompiledRegex
|
||||
field :action, Ecto.Enum, values: [:deny, :allow], default: :deny
|
||||
field :added_by, :string
|
||||
|
||||
# If `from_cache?` is set, the struct might be incomplete - see `Plausible.Site.Shield.Rules.IP.Cache`
|
||||
field :from_cache?, :boolean, virtual: true, default: false
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(rule \\ %__MODULE__{}, attrs) do
|
||||
rule
|
||||
|> cast(attrs, [:site_id, :page_path])
|
||||
|> validate_required([:site_id, :page_path])
|
||||
|> validate_length(:page_path, max: 250)
|
||||
|> validate_change(:page_path, fn :page_path, p ->
|
||||
if not String.starts_with?(p, "/") do
|
||||
[page_path: "must start with /"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end)
|
||||
|> store_regex()
|
||||
|> unique_constraint(:page_path_pattern,
|
||||
name: :shield_rules_page_site_id_page_path_pattern_index,
|
||||
error_key: :page_path,
|
||||
message: "rule already exists"
|
||||
)
|
||||
end
|
||||
|
||||
defp store_regex(changeset) do
|
||||
case get_field(changeset, :page_path) do
|
||||
"/" <> _ = page_path ->
|
||||
regex =
|
||||
page_path
|
||||
|> Regex.escape()
|
||||
|> String.replace("\\*\\*", ".*")
|
||||
|> String.replace("\\*", ".*")
|
||||
|
||||
regex = "^#{regex}$"
|
||||
|
||||
verify_valid_regex(changeset, regex)
|
||||
|
||||
_ ->
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
defp verify_valid_regex(changeset, regex) do
|
||||
case Regex.compile(regex) do
|
||||
{:ok, _} ->
|
||||
put_change(changeset, :page_path_pattern, regex)
|
||||
|
||||
{:error, _} ->
|
||||
add_error(changeset, :page_path, "could not compile regular expression")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,65 +0,0 @@
|
||||
defmodule Plausible.Shield.PageRuleCache do
|
||||
@moduledoc """
|
||||
Allows retrieving Page Rules by domain
|
||||
"""
|
||||
alias Plausible.Shield.PageRule
|
||||
|
||||
import Ecto.Query
|
||||
use Plausible.Cache
|
||||
|
||||
@cache_name :page_blocklist_by_domain
|
||||
|
||||
@cached_schema_fields ~w(
|
||||
id
|
||||
page_path_pattern
|
||||
action
|
||||
)a
|
||||
|
||||
@impl true
|
||||
def name(), do: @cache_name
|
||||
|
||||
@impl true
|
||||
def child_id(), do: :cache_page_blocklist
|
||||
|
||||
@impl true
|
||||
def count_all() do
|
||||
Plausible.Repo.aggregate(PageRule, :count)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def base_db_query() do
|
||||
from rule in PageRule,
|
||||
inner_join: s in assoc(rule, :site),
|
||||
select: {
|
||||
s.domain,
|
||||
s.domain_changed_from,
|
||||
%{struct(rule, ^@cached_schema_fields) | from_cache?: true}
|
||||
}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def get_from_source(domain) do
|
||||
query =
|
||||
base_db_query()
|
||||
|> where([..., site], site.domain == ^domain)
|
||||
|
||||
case Plausible.Repo.one(query) do
|
||||
{_, _, rule} -> %PageRule{rule | from_cache?: false}
|
||||
_any -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def unwrap_cache_keys(items) do
|
||||
Enum.reduce(items, [], fn
|
||||
{domain, nil, object}, acc ->
|
||||
[{domain, object} | acc]
|
||||
|
||||
{domain, domain_changed_from, object}, acc ->
|
||||
[
|
||||
{domain, object},
|
||||
{domain_changed_from, object} | acc
|
||||
]
|
||||
end)
|
||||
end
|
||||
end
|
||||
@ -1,136 +0,0 @@
|
||||
defmodule Plausible.Shields.CountryTest do
|
||||
use Plausible.DataCase
|
||||
import Plausible.Shields
|
||||
|
||||
setup do
|
||||
site = insert(:site)
|
||||
{:ok, %{site: site}}
|
||||
end
|
||||
|
||||
describe "add_country_rule/2" do
|
||||
test "no input", %{site: site} do
|
||||
assert {:error, changeset} = add_country_rule(site, %{})
|
||||
assert changeset.errors == [country_code: {"can't be blank", [validation: :required]}]
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "unsupported country", %{site: site} do
|
||||
assert {:error, changeset} = add_country_rule(site, %{"country_code" => "0X"})
|
||||
assert changeset.errors == [country_code: {"is invalid", []}]
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "incorrect country format", %{site: site} do
|
||||
assert {:error, changeset} = add_country_rule(site, %{"country_code" => "Germany"})
|
||||
|
||||
assert changeset.errors ==
|
||||
[
|
||||
{:country_code, {"is invalid", []}},
|
||||
{:country_code,
|
||||
{"should be %{count} character(s)",
|
||||
[count: 2, validation: :length, kind: :is, type: :string]}}
|
||||
]
|
||||
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "double insert", %{site: site} do
|
||||
assert {:ok, _} = add_country_rule(site, %{"country_code" => "EE"})
|
||||
assert {:error, changeset} = add_country_rule(site, %{"country_code" => "EE"})
|
||||
refute changeset.valid?
|
||||
|
||||
assert changeset.errors == [
|
||||
country_code:
|
||||
{"has already been taken",
|
||||
[
|
||||
{:constraint, :unique},
|
||||
{:constraint_name, "shield_rules_country_site_id_country_code_index"}
|
||||
]}
|
||||
]
|
||||
end
|
||||
|
||||
test "over limit", %{site: site} do
|
||||
country_codes =
|
||||
Location.Country.all()
|
||||
|> Enum.take(Plausible.Shields.maximum_country_rules())
|
||||
|> Enum.map(& &1.alpha_2)
|
||||
|
||||
for cc <- country_codes do
|
||||
assert {:ok, _} =
|
||||
add_country_rule(site, %{"country_code" => cc})
|
||||
end
|
||||
|
||||
assert count_country_rules(site) == maximum_country_rules()
|
||||
|
||||
assert {:error, changeset} =
|
||||
add_country_rule(site, %{"country_code" => "US"})
|
||||
|
||||
refute changeset.valid?
|
||||
assert changeset.errors == [country_code: {"maximum reached", []}]
|
||||
end
|
||||
|
||||
test "with added_by", %{site: site} do
|
||||
assert {:ok, rule} =
|
||||
add_country_rule(site, %{"country_code" => "EE"},
|
||||
added_by: build(:user, name: "Joe", email: "joe@example.com")
|
||||
)
|
||||
|
||||
assert rule.added_by == "Joe <joe@example.com>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "remove_country_rule/2" do
|
||||
test "is idempontent", %{site: site} do
|
||||
{:ok, rule} = add_country_rule(site, %{"country_code" => "EE"})
|
||||
assert remove_country_rule(site, rule.id) == :ok
|
||||
refute Repo.get(Plausible.Shield.CountryRule, rule.id)
|
||||
assert remove_country_rule(site, rule.id) == :ok
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_country_rules/1" do
|
||||
test "empty", %{site: site} do
|
||||
assert(list_country_rules(site) == [])
|
||||
end
|
||||
|
||||
@tag :slow
|
||||
test "many", %{site: site} do
|
||||
{:ok, r1} = add_country_rule(site, %{"country_code" => "EE"})
|
||||
:timer.sleep(1000)
|
||||
{:ok, r2} = add_country_rule(site, %{"country_code" => "PL"})
|
||||
assert [^r2, ^r1] = list_country_rules(site)
|
||||
end
|
||||
end
|
||||
|
||||
describe "count_country_rules/1" do
|
||||
test "counts", %{site: site} do
|
||||
assert count_country_rules(site) == 0
|
||||
{:ok, _} = add_country_rule(site, %{"country_code" => "EE"})
|
||||
assert count_country_rules(site) == 1
|
||||
{:ok, _} = add_country_rule(site, %{"country_code" => "PL"})
|
||||
assert count_country_rules(site) == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "Country Rules" do
|
||||
test "end to end", %{site: site} do
|
||||
site2 = insert(:site)
|
||||
|
||||
assert count_country_rules(site.id) == 0
|
||||
assert list_country_rules(site.id) == []
|
||||
|
||||
assert {:ok, rule} =
|
||||
add_country_rule(site.id, %{"country_code" => "EE"})
|
||||
|
||||
add_country_rule(site2, %{"country_code" => "PL"})
|
||||
|
||||
assert count_country_rules(site) == 1
|
||||
assert [^rule] = list_country_rules(site)
|
||||
assert rule.country_code == "EE"
|
||||
assert rule.action == :deny
|
||||
refute rule.from_cache?
|
||||
assert country_blocked?(site, "ee")
|
||||
refute country_blocked?(site, "xx")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,147 +0,0 @@
|
||||
defmodule Plausible.Shields.IPTest do
|
||||
use Plausible.DataCase
|
||||
import Plausible.Shields
|
||||
|
||||
setup do
|
||||
site = insert(:site)
|
||||
{:ok, %{site: site}}
|
||||
end
|
||||
|
||||
describe "add_ip_rule/2" do
|
||||
test "no input", %{site: site} do
|
||||
assert {:error, changeset} = add_ip_rule(site, %{})
|
||||
assert changeset.errors == [inet: {"can't be blank", [validation: :required]}]
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "unsupported netmask", %{site: site} do
|
||||
assert {:error, changeset} = add_ip_rule(site, %{"inet" => "127.0.0.0/24"})
|
||||
assert changeset.errors == [inet: {"netmask unsupported", []}]
|
||||
refute changeset.valid?
|
||||
assert {:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.0/32"})
|
||||
end
|
||||
|
||||
test "incorrect ip", %{site: site} do
|
||||
assert {:error, changeset} = add_ip_rule(site, %{"inet" => "999.999.999.999"})
|
||||
|
||||
assert changeset.errors == [
|
||||
inet: {"is invalid", [{:type, EctoNetwork.INET}, {:validation, :cast}]}
|
||||
]
|
||||
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "non-strict IPs", %{site: site} do
|
||||
assert {:error, _} = add_ip_rule(site, %{"inet" => "111"})
|
||||
end
|
||||
|
||||
test "double insert", %{site: site} do
|
||||
assert {:ok, _} = add_ip_rule(site, %{"inet" => "0.0.0.111"})
|
||||
assert {:error, changeset} = add_ip_rule(site, %{"inet" => "0.0.0.111"})
|
||||
refute changeset.valid?
|
||||
|
||||
assert changeset.errors == [
|
||||
inet:
|
||||
{"has already been taken",
|
||||
[
|
||||
{:constraint, :unique},
|
||||
{:constraint_name, "shield_rules_ip_site_id_inet_index"}
|
||||
]}
|
||||
]
|
||||
end
|
||||
|
||||
test "ipv6", %{site: site} do
|
||||
assert {:ok, rule} =
|
||||
add_ip_rule(site, %{"inet" => "2001:0000:130F:0000:0000:09C0:876A:130B"})
|
||||
|
||||
assert ^rule = Repo.get(Plausible.Shield.IPRule, rule.id)
|
||||
end
|
||||
|
||||
test "ipv4", %{site: site} do
|
||||
assert {:ok, rule} =
|
||||
add_ip_rule(site, %{"inet" => "1.1.1.1"})
|
||||
|
||||
assert ^rule = Repo.get(Plausible.Shield.IPRule, rule.id)
|
||||
end
|
||||
|
||||
test "over limit", %{site: site} do
|
||||
for i <- 1..maximum_ip_rules() do
|
||||
assert {:ok, _} =
|
||||
add_ip_rule(site, %{"inet" => "1.1.1.#{i}"})
|
||||
end
|
||||
|
||||
assert count_ip_rules(site) == maximum_ip_rules()
|
||||
|
||||
assert {:error, changeset} =
|
||||
add_ip_rule(site, %{"inet" => "1.1.1.31"})
|
||||
|
||||
refute changeset.valid?
|
||||
assert changeset.errors == [inet: {"maximum reached", []}]
|
||||
end
|
||||
|
||||
test "with added_by", %{site: site} do
|
||||
assert {:ok, rule} =
|
||||
add_ip_rule(site, %{"inet" => "1.1.1.1"},
|
||||
added_by: build(:user, name: "Joe", email: "joe@example.com")
|
||||
)
|
||||
|
||||
assert rule.added_by == "Joe <joe@example.com>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "remove_ip_rule/2" do
|
||||
test "is idempontent", %{site: site} do
|
||||
{:ok, rule} = add_ip_rule(site, %{"inet" => "127.0.0.1"})
|
||||
assert remove_ip_rule(site, rule.id) == :ok
|
||||
refute Repo.get(Plausible.Shield.IPRule, rule.id)
|
||||
assert remove_ip_rule(site, rule.id) == :ok
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_ip_rules/1" do
|
||||
test "empty", %{site: site} do
|
||||
assert(list_ip_rules(site) == [])
|
||||
end
|
||||
|
||||
@tag :slow
|
||||
test "many", %{site: site} do
|
||||
{:ok, r1} = add_ip_rule(site, %{"inet" => "127.0.0.1"})
|
||||
:timer.sleep(1000)
|
||||
{:ok, r2} = add_ip_rule(site, %{"inet" => "127.0.0.2"})
|
||||
assert [^r2, ^r1] = list_ip_rules(site)
|
||||
end
|
||||
end
|
||||
|
||||
describe "count_ip_rules/1" do
|
||||
test "counts", %{site: site} do
|
||||
assert count_ip_rules(site) == 0
|
||||
{:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.1"})
|
||||
assert count_ip_rules(site) == 1
|
||||
{:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.2"})
|
||||
assert count_ip_rules(site) == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "IP Rules" do
|
||||
test "end to end", %{site: site} do
|
||||
site2 = insert(:site)
|
||||
|
||||
assert count_ip_rules(site.id) == 0
|
||||
assert list_ip_rules(site.id) == []
|
||||
|
||||
assert {:ok, rule} =
|
||||
add_ip_rule(site.id, %{"inet" => "127.0.0.1", "description" => "Localhost"})
|
||||
|
||||
add_ip_rule(site2, %{"inet" => "127.0.0.1", "description" => "Localhost"})
|
||||
|
||||
assert count_ip_rules(site) == 1
|
||||
assert [^rule] = list_ip_rules(site)
|
||||
assert rule.inet == %Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32}
|
||||
assert rule.description == "Localhost"
|
||||
assert rule.action == :deny
|
||||
refute rule.from_cache?
|
||||
assert ip_blocked?(site, "127.0.0.1")
|
||||
refute ip_blocked?(site, "127.0.0.2")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,174 +0,0 @@
|
||||
defmodule Plausible.Shields.PageTest do
|
||||
use Plausible.DataCase
|
||||
import Plausible.Shields
|
||||
|
||||
setup do
|
||||
site = insert(:site)
|
||||
{:ok, %{site: site}}
|
||||
end
|
||||
|
||||
describe "add_page_rule/2" do
|
||||
test "no input", %{site: site} do
|
||||
assert {:error, changeset} = add_page_rule(site, %{})
|
||||
assert changeset.errors == [page_path: {"can't be blank", [validation: :required]}]
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "no slash", %{site: site} do
|
||||
assert {:error, changeset} = add_page_rule(site, %{"page_path" => "test"})
|
||||
assert changeset.errors == [page_path: {"must start with /", []}]
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "lengthy", %{site: site} do
|
||||
long = "/" <> :binary.copy("a", 251)
|
||||
assert {:error, changeset} = add_page_rule(site, %{"page_path" => long})
|
||||
assert [page_path: {"should be at most %{count} character(s)", _}] = changeset.errors
|
||||
refute changeset.valid?
|
||||
end
|
||||
|
||||
test "double insert", %{site: site} do
|
||||
assert {:ok, _} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
assert {:error, changeset} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
refute changeset.valid?
|
||||
|
||||
assert changeset.errors == [
|
||||
page_path:
|
||||
{"rule already exists",
|
||||
[
|
||||
{:constraint, :unique},
|
||||
{:constraint_name, "shield_rules_page_site_id_page_path_pattern_index"}
|
||||
]}
|
||||
]
|
||||
end
|
||||
|
||||
test "equivalent rules are counted as dupes", %{site: site} do
|
||||
assert {:ok, _} = add_page_rule(site, %{"page_path" => "/test/*"})
|
||||
assert {:error, changeset} = add_page_rule(site, %{"page_path" => "/test/**"})
|
||||
|
||||
assert changeset.errors == [
|
||||
page_path:
|
||||
{"rule already exists",
|
||||
[
|
||||
{:constraint, :unique},
|
||||
{:constraint_name, "shield_rules_page_site_id_page_path_pattern_index"}
|
||||
]}
|
||||
]
|
||||
end
|
||||
|
||||
test "regex storage: wildcard", %{site: site} do
|
||||
assert {:ok, rule} = add_page_rule(site, %{"page_path" => "/test/*"})
|
||||
assert rule.page_path_pattern == "^/test/.*$"
|
||||
end
|
||||
|
||||
test "regex storage: no wildcard", %{site: site} do
|
||||
assert {:ok, rule} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
assert rule.page_path_pattern == "^/test$"
|
||||
end
|
||||
|
||||
test "regex storage: escaping", %{site: site} do
|
||||
assert {:ok, rule} = add_page_rule(site, %{"page_path" => "/test/*/**/|+[0-9]"})
|
||||
assert rule.page_path_pattern == "^/test/.*/.*/\\|\\+\\[0\\-9\\]$"
|
||||
end
|
||||
|
||||
test "over limit", %{site: site} do
|
||||
for i <- 1..maximum_page_rules() do
|
||||
assert {:ok, _} =
|
||||
add_page_rule(site, %{"page_path" => "/test/#{i}"})
|
||||
end
|
||||
|
||||
assert count_page_rules(site) == maximum_page_rules()
|
||||
|
||||
assert {:error, changeset} =
|
||||
add_page_rule(site, %{"page_path" => "/test/31"})
|
||||
|
||||
refute changeset.valid?
|
||||
assert changeset.errors == [page_path: {"maximum reached", []}]
|
||||
end
|
||||
|
||||
test "with added_by", %{site: site} do
|
||||
assert {:ok, rule} =
|
||||
add_page_rule(site, %{"page_path" => "/test"},
|
||||
added_by: build(:user, name: "Joe", email: "joe@example.com")
|
||||
)
|
||||
|
||||
assert rule.added_by == "Joe <joe@example.com>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "page pattern matching" do
|
||||
test "no wildcard", %{site: site} do
|
||||
assert {:ok, _} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
assert page_blocked?(site, "/test")
|
||||
refute page_blocked?(site, "/test/hello")
|
||||
refute page_blocked?(site, "test")
|
||||
end
|
||||
|
||||
test "wildcard", %{site: site} do
|
||||
assert {:ok, _} = add_page_rule(site, %{"page_path" => "/test/*"})
|
||||
refute page_blocked?(site, "/test")
|
||||
assert page_blocked?(site, "/test/")
|
||||
assert page_blocked?(site, "/test/hello")
|
||||
refute page_blocked?(site, "test")
|
||||
refute page_blocked?(site, "/testing")
|
||||
end
|
||||
end
|
||||
|
||||
describe "remove_page_rule/2" do
|
||||
test "is idempontent", %{site: site} do
|
||||
{:ok, rule} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
assert remove_page_rule(site, rule.id) == :ok
|
||||
refute Repo.get(Plausible.Shield.PageRule, rule.id)
|
||||
assert remove_page_rule(site, rule.id) == :ok
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_page_rules/1" do
|
||||
test "empty", %{site: site} do
|
||||
assert(list_page_rules(site) == [])
|
||||
end
|
||||
|
||||
@tag :slow
|
||||
test "many", %{site: site} do
|
||||
{:ok, r1} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
:timer.sleep(1000)
|
||||
{:ok, r2} = add_page_rule(site, %{"page_path" => "/test"})
|
||||
assert [^r2, ^r1] = list_page_rules(site)
|
||||
end
|
||||
end
|
||||
|
||||
describe "count_page_rules/1" do
|
||||
test "counts", %{site: site} do
|
||||
assert count_page_rules(site) == 0
|
||||
{:ok, _} = add_page_rule(site, %{"page_path" => "/test1"})
|
||||
assert count_page_rules(site) == 1
|
||||
{:ok, _} = add_page_rule(site, %{"page_path" => "/test2"})
|
||||
assert count_page_rules(site) == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "Page Rules" do
|
||||
test "end to end", %{site: site} do
|
||||
site2 = insert(:site)
|
||||
|
||||
assert count_page_rules(site.id) == 0
|
||||
assert list_page_rules(site.id) == []
|
||||
|
||||
assert {:ok, rule} =
|
||||
add_page_rule(site.id, %{
|
||||
"page_path" => "/test"
|
||||
})
|
||||
|
||||
add_page_rule(site2, %{"page_path" => "/test"})
|
||||
|
||||
assert count_page_rules(site) == 1
|
||||
assert [%{id: rule_id}] = list_page_rules(site)
|
||||
assert rule.id == rule_id
|
||||
assert rule.page_path == "/test"
|
||||
assert rule.action == :deny
|
||||
refute rule.from_cache?
|
||||
assert page_blocked?(site, "/test")
|
||||
refute page_blocked?(site, "/testing")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,27 +0,0 @@
|
||||
defmodule Plausible.Stats.Metrics do
|
||||
@moduledoc """
|
||||
A module listing all available metrics in Plausible.
|
||||
|
||||
Useful for an explicit string to atom conversion.
|
||||
"""
|
||||
|
||||
use Plausible
|
||||
|
||||
@all_metrics [
|
||||
:visitors,
|
||||
:visits,
|
||||
:pageviews,
|
||||
:views_per_visit,
|
||||
:bounce_rate,
|
||||
:visit_duration,
|
||||
:events,
|
||||
:conversion_rate,
|
||||
:time_on_page
|
||||
] ++ on_full_build(do: Plausible.Stats.Goal.Revenue.revenue_metrics(), else: [])
|
||||
|
||||
@metric_mappings Enum.into(@all_metrics, %{}, fn metric -> {to_string(metric), metric} end)
|
||||
|
||||
def from_string!(str) do
|
||||
Map.fetch!(@metric_mappings, str)
|
||||
end
|
||||
end
|
||||
@ -1,111 +0,0 @@
|
||||
defmodule Plausible.Stats.TableDecider do
|
||||
@moduledoc """
|
||||
This module contains logic for deciding which tables need to be queried given a query
|
||||
and metrics, with the purpose of reducing the number of queries and JOINs needed to perform.
|
||||
"""
|
||||
|
||||
import Enum, only: [empty?: 1]
|
||||
|
||||
alias Plausible.Stats.Query
|
||||
|
||||
def events_join_sessions?(query) do
|
||||
Enum.any?(query.filters, &(filters_partitioner(query, &1) == :session))
|
||||
end
|
||||
|
||||
def partition_metrics(metrics, query, breakdown_property \\ nil) do
|
||||
%{
|
||||
event: event_only_metrics,
|
||||
session: session_only_metrics,
|
||||
either: either_metrics,
|
||||
other: other_metrics,
|
||||
sample_percent: sample_percent
|
||||
} =
|
||||
partition(metrics, query, &metric_partitioner/2)
|
||||
|
||||
# Treat breakdown property as yet another filter
|
||||
filters =
|
||||
if breakdown_property do
|
||||
Map.put(query.filters, breakdown_property, nil)
|
||||
else
|
||||
query.filters
|
||||
end
|
||||
|
||||
%{event: event_only_filters, session: session_only_filters} =
|
||||
partition(filters, query, &filters_partitioner/2)
|
||||
|
||||
cond do
|
||||
# Only one table needs to be queried
|
||||
empty?(event_only_metrics) && empty?(event_only_filters) ->
|
||||
{[], session_only_metrics ++ either_metrics ++ sample_percent, other_metrics}
|
||||
|
||||
empty?(session_only_metrics) && empty?(session_only_filters) ->
|
||||
{event_only_metrics ++ either_metrics ++ sample_percent, [], other_metrics}
|
||||
|
||||
# Filters on both events and sessions, but only one kind of metric
|
||||
empty?(event_only_metrics) ->
|
||||
{[], session_only_metrics ++ either_metrics ++ sample_percent, other_metrics}
|
||||
|
||||
empty?(session_only_metrics) ->
|
||||
{event_only_metrics ++ either_metrics ++ sample_percent, [], other_metrics}
|
||||
|
||||
# Default: prefer sessions
|
||||
true ->
|
||||
{event_only_metrics ++ sample_percent,
|
||||
session_only_metrics ++ either_metrics ++ sample_percent, other_metrics}
|
||||
end
|
||||
end
|
||||
|
||||
defp metric_partitioner(_, :conversion_rate), do: :event
|
||||
defp metric_partitioner(_, :average_revenue), do: :event
|
||||
defp metric_partitioner(_, :total_revenue), do: :event
|
||||
defp metric_partitioner(_, :pageviews), do: :event
|
||||
defp metric_partitioner(_, :events), do: :event
|
||||
defp metric_partitioner(_, :bounce_rate), do: :session
|
||||
defp metric_partitioner(_, :visit_duration), do: :session
|
||||
defp metric_partitioner(_, :views_per_visit), do: :session
|
||||
|
||||
# Metrics which used to only be queried from one table but can be calculated from either
|
||||
defp metric_partitioner(%Query{experimental_reduced_joins?: true}, :visits), do: :either
|
||||
defp metric_partitioner(%Query{experimental_reduced_joins?: true}, :visitors), do: :either
|
||||
|
||||
defp metric_partitioner(_, :visits), do: :session
|
||||
defp metric_partitioner(_, :visitors), do: :event
|
||||
# Calculated metrics - handled on callsite separately from other metrics.
|
||||
defp metric_partitioner(_, :time_on_page), do: :other
|
||||
defp metric_partitioner(_, :total_visitors), do: :other
|
||||
defp metric_partitioner(_, :percentage), do: :other
|
||||
# Sample percentage is included in both tables if queried.
|
||||
defp metric_partitioner(_, :sample_percent), do: :sample_percent
|
||||
|
||||
defp metric_partitioner(%Query{experimental_reduced_joins?: false}, unknown) do
|
||||
raise ArgumentError, "Metric #{unknown} not supported without experimental_reduced_joins?"
|
||||
end
|
||||
|
||||
defp metric_partitioner(_, _), do: :either
|
||||
|
||||
defp filters_partitioner(_, {"event:" <> _, _}), do: :event
|
||||
defp filters_partitioner(_, {"visit:entry_page", _}), do: :session
|
||||
defp filters_partitioner(_, {"visit:entry_page_hostname", _}), do: :session
|
||||
defp filters_partitioner(_, {"visit:exit_page", _}), do: :session
|
||||
defp filters_partitioner(_, {"visit:exit_page_hostname", _}), do: :session
|
||||
|
||||
defp filters_partitioner(%Query{experimental_reduced_joins?: true}, {"visit:" <> _, _}),
|
||||
do: :either
|
||||
|
||||
defp filters_partitioner(_, {"visit:" <> _, _}),
|
||||
do: :session
|
||||
|
||||
defp filters_partitioner(%Query{experimental_reduced_joins?: false}, {unknown, _}) do
|
||||
raise ArgumentError, "Filter #{unknown} not supported without experimental_reduced_joins?"
|
||||
end
|
||||
|
||||
defp filters_partitioner(_, _), do: :either
|
||||
|
||||
@default %{event: [], session: [], either: [], other: [], sample_percent: []}
|
||||
defp partition(values, query, partitioner) do
|
||||
Enum.reduce(values, @default, fn value, acc ->
|
||||
key = partitioner.(query, value)
|
||||
Map.put(acc, key, Map.fetch!(acc, key) ++ [value])
|
||||
end)
|
||||
end
|
||||
end
|
||||
@ -1,50 +0,0 @@
|
||||
defmodule PlausibleWeb.Components.FirstDashboardLaunchBanner do
|
||||
@moduledoc """
|
||||
A banner that appears on the first dashboard launch
|
||||
"""
|
||||
use Phoenix.Component
|
||||
use Phoenix.HTML
|
||||
|
||||
attr(:site, Plausible.Site, required: true)
|
||||
|
||||
def set(assigns) do
|
||||
~H"""
|
||||
<script>
|
||||
sessionStorage.setItem('<%= storage_key(@site) %>', false);
|
||||
</script>
|
||||
"""
|
||||
end
|
||||
|
||||
attr(:site, Plausible.Site, required: true)
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div
|
||||
x-cloak
|
||||
x-data={x_data(@site)}
|
||||
class="w-full px-4 text-sm font-bold text-center text-blue-900 bg-blue-200 rounded transition"
|
||||
style="top: 91px"
|
||||
role="alert"
|
||||
x-bind:class="! show ? 'hidden' : ''"
|
||||
x-init={x_init(@site)}
|
||||
>
|
||||
<%= link("Team members, email reports and GA import. Explore more →",
|
||||
to: "/#{URI.encode_www_form(@site.domain)}/settings/email-reports",
|
||||
class: "py-2 block"
|
||||
) %>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp x_data(site) do
|
||||
"{show: !!sessionStorage.getItem('#{storage_key(site)}')}"
|
||||
end
|
||||
|
||||
defp x_init(site) do
|
||||
"setTimeout(() => sessionStorage.removeItem('#{storage_key(site)}'), 3000)"
|
||||
end
|
||||
|
||||
defp storage_key(site) do
|
||||
"dashboard_seen_#{site.domain}"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,143 @@
|
||||
defmodule PlausibleWeb.GoogleAnalytics4Controller do
|
||||
use PlausibleWeb, :controller
|
||||
|
||||
plug(PlausibleWeb.RequireAccountPlug)
|
||||
|
||||
plug(PlausibleWeb.AuthorizeSiteAccess, [:owner, :admin, :super_admin])
|
||||
|
||||
def property_form(conn, %{
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at
|
||||
}) do
|
||||
redirect_route = Routes.site_path(conn, :settings_imports_exports, conn.assigns.site.domain)
|
||||
|
||||
case Plausible.Google.GA4.API.list_properties(access_token) do
|
||||
{:ok, properties} ->
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("property_form.html",
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
site: conn.assigns.site,
|
||||
properties: properties,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
|
||||
{:error, :authentication_failed} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, _any} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to list your Google Analytics properties. If the problem persists, please contact support for assistance."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
|
||||
def property(conn, %{
|
||||
"property" => property,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
start_date = Plausible.Google.GA4.API.get_analytics_start_date(access_token, property)
|
||||
|
||||
case start_date do
|
||||
{:ok, nil} ->
|
||||
{:ok, properties} = Plausible.Google.GA4.API.list_properties(access_token)
|
||||
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("property_form.html",
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
site: site,
|
||||
properties: properties,
|
||||
selected_property_error: "No data found. Nothing to import",
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
|
||||
{:ok, _date} ->
|
||||
redirect(conn,
|
||||
to:
|
||||
Routes.google_analytics4_path(conn, :confirm, site.domain,
|
||||
property: property,
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def confirm(conn, %{
|
||||
"property" => property,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
|
||||
start_date = Plausible.Google.GA4.API.get_analytics_start_date(access_token, property)
|
||||
|
||||
end_date = Plausible.Sites.native_stats_start_date(site) || Timex.today(site.timezone)
|
||||
|
||||
{:ok, {property_name, property}} =
|
||||
Plausible.Google.GA4.API.get_property(access_token, property)
|
||||
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("confirm.html",
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
site: site,
|
||||
selected_property: property,
|
||||
selected_property_name: property_name,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
|
||||
def import(conn, %{
|
||||
"property" => property,
|
||||
"start_date" => start_date,
|
||||
"end_date" => end_date,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
current_user = conn.assigns.current_user
|
||||
|
||||
redirect_route = Routes.site_path(conn, :settings_imports_exports, site.domain)
|
||||
|
||||
{:ok, _} =
|
||||
Plausible.Imported.GoogleAnalytics4.new_import(
|
||||
site,
|
||||
current_user,
|
||||
property: property,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
token_expires_at: expires_at
|
||||
)
|
||||
|
||||
conn
|
||||
|> put_flash(:success, "Import scheduled. An email will be sent when it completes.")
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
@ -1,336 +0,0 @@
|
||||
defmodule PlausibleWeb.GoogleAnalyticsController do
|
||||
use PlausibleWeb, :controller
|
||||
|
||||
alias Plausible.Google
|
||||
alias Plausible.Imported
|
||||
|
||||
require Plausible.Imported.SiteImport
|
||||
|
||||
plug(PlausibleWeb.RequireAccountPlug)
|
||||
|
||||
plug(PlausibleWeb.AuthorizeSiteAccess, [:owner, :admin, :super_admin])
|
||||
|
||||
def user_metric_notice(conn, %{
|
||||
"property_or_view" => property_or_view,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at,
|
||||
"start_date" => start_date,
|
||||
"end_date" => end_date,
|
||||
"legacy" => legacy
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("user_metric_form.html",
|
||||
site: site,
|
||||
property_or_view: property_or_view,
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
legacy: legacy,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
|
||||
def property_or_view_form(
|
||||
conn,
|
||||
%{
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at,
|
||||
"legacy" => legacy
|
||||
} = params
|
||||
) do
|
||||
site = conn.assigns.site
|
||||
|
||||
redirect_route =
|
||||
if legacy == "true" do
|
||||
Routes.site_path(conn, :settings_integrations, site.domain)
|
||||
else
|
||||
Routes.site_path(conn, :settings_imports_exports, site.domain)
|
||||
end
|
||||
|
||||
result =
|
||||
if legacy == "true" do
|
||||
Google.UA.API.list_views(access_token)
|
||||
else
|
||||
Google.API.list_properties_and_views(access_token)
|
||||
end
|
||||
|
||||
error =
|
||||
case params["error"] do
|
||||
"no_data" ->
|
||||
"No data found. Nothing to import."
|
||||
|
||||
"no_time_window" ->
|
||||
"Imported data time range is completely overlapping with existing data. Nothing to import."
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
|
||||
case result do
|
||||
{:ok, properties_and_views} ->
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("property_or_view_form.html",
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
site: conn.assigns.site,
|
||||
properties_and_views: properties_and_views,
|
||||
selected_property_or_view_error: error,
|
||||
legacy: legacy,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
|
||||
{:error, :authentication_failed} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, :timeout} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics API has timed out. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, _any} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to list your Google Analytics properties and views. If the problem persists, please contact support for assistance."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
|
||||
# see https://stackoverflow.com/a/57416769
|
||||
@universal_analytics_new_user_metric_date ~D[2016-08-24]
|
||||
|
||||
def property_or_view(
|
||||
conn,
|
||||
%{
|
||||
"property_or_view" => property_or_view,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at,
|
||||
"legacy" => legacy
|
||||
} = params
|
||||
) do
|
||||
site = conn.assigns.site
|
||||
|
||||
redirect_route =
|
||||
if legacy == "true" do
|
||||
Routes.site_path(conn, :settings_integrations, site.domain)
|
||||
else
|
||||
Routes.site_path(conn, :settings_imports_exports, site.domain)
|
||||
end
|
||||
|
||||
with {:ok, api_start_date} <-
|
||||
Google.API.get_analytics_start_date(access_token, property_or_view),
|
||||
{:ok, api_end_date} <- Google.API.get_analytics_end_date(access_token, property_or_view),
|
||||
{:ok, start_date, end_date} <- Imported.check_dates(site, api_start_date, api_end_date) do
|
||||
action =
|
||||
if Timex.before?(api_start_date, @universal_analytics_new_user_metric_date) do
|
||||
:user_metric_notice
|
||||
else
|
||||
:confirm
|
||||
end
|
||||
|
||||
redirect(conn,
|
||||
external:
|
||||
Routes.google_analytics_path(conn, action, site.domain,
|
||||
property_or_view: property_or_view,
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
start_date: Date.to_iso8601(start_date),
|
||||
end_date: Date.to_iso8601(end_date),
|
||||
legacy: legacy
|
||||
)
|
||||
)
|
||||
else
|
||||
{:error, error} when error in [:no_data, :no_time_window] ->
|
||||
params =
|
||||
params
|
||||
|> Map.take(["access_token", "refresh_token", "expires_at", "legacy"])
|
||||
|> Map.put("error", Atom.to_string(error))
|
||||
|
||||
property_or_view_form(conn, params)
|
||||
|
||||
{:error, :authentication_failed} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics authentication seems to have expired. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, :timeout} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics API has timed out. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, _any} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to retrieve information from Google Analytics. If the problem persists, please contact support for assistance."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
|
||||
def confirm(conn, %{
|
||||
"property_or_view" => property_or_view,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at,
|
||||
"start_date" => start_date,
|
||||
"end_date" => end_date,
|
||||
"legacy" => legacy
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
|
||||
start_date = Date.from_iso8601!(start_date)
|
||||
end_date = Date.from_iso8601!(end_date)
|
||||
|
||||
redirect_route =
|
||||
if legacy == "true" do
|
||||
Routes.site_path(conn, :settings_integrations, site.domain)
|
||||
else
|
||||
Routes.site_path(conn, :settings_imports_exports, site.domain)
|
||||
end
|
||||
|
||||
case Google.API.get_property_or_view(access_token, property_or_view) do
|
||||
{:ok, %{name: property_or_view_name, id: property_or_view}} ->
|
||||
conn
|
||||
|> assign(:skip_plausible_tracking, true)
|
||||
|> render("confirm.html",
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
expires_at: expires_at,
|
||||
site: site,
|
||||
selected_property_or_view: property_or_view,
|
||||
selected_property_or_view_name: property_or_view_name,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
property?: Google.API.property?(property_or_view),
|
||||
legacy: legacy,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
|
||||
{:error, :authentication_failed} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics authentication seems to have expired. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, :timeout} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics API has timed out. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, :not_found} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Google Analytics property not found. Please try again."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, _any} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to retrieve information from Google Analytics. If the problem persists, please contact support for assistance."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
|
||||
def import(conn, %{
|
||||
"property_or_view" => property_or_view,
|
||||
"start_date" => start_date,
|
||||
"end_date" => end_date,
|
||||
"access_token" => access_token,
|
||||
"refresh_token" => refresh_token,
|
||||
"expires_at" => expires_at,
|
||||
"legacy" => legacy
|
||||
}) do
|
||||
site = conn.assigns.site
|
||||
current_user = conn.assigns.current_user
|
||||
|
||||
start_date = Date.from_iso8601!(start_date)
|
||||
end_date = Date.from_iso8601!(end_date)
|
||||
|
||||
redirect_route =
|
||||
if legacy == "true" do
|
||||
Routes.site_path(conn, :settings_integrations, site.domain)
|
||||
else
|
||||
Routes.site_path(conn, :settings_imports_exports, site.domain)
|
||||
end
|
||||
|
||||
import_opts = [
|
||||
label: property_or_view,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
access_token: access_token,
|
||||
refresh_token: refresh_token,
|
||||
token_expires_at: expires_at,
|
||||
legacy: legacy == "true"
|
||||
]
|
||||
|
||||
with {:ok, start_date, end_date} <- Imported.check_dates(site, start_date, end_date),
|
||||
import_opts = [{:start_date, start_date}, {:end_date, end_date} | import_opts],
|
||||
{:ok, _} <- schedule_job(site, current_user, property_or_view, import_opts) do
|
||||
conn
|
||||
|> put_flash(:success, "Import scheduled. An email will be sent when it completes.")
|
||||
|> redirect(external: redirect_route)
|
||||
else
|
||||
{:error, :no_time_window} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Import failed. No data could be imported because date range overlaps with existing data."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
|
||||
{:error, :import_in_progress} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"There's another import still in progress. Please wait until it's completed or cancel it before starting a new one."
|
||||
)
|
||||
|> redirect(external: redirect_route)
|
||||
end
|
||||
end
|
||||
|
||||
defp schedule_job(site, current_user, property_or_view, opts) do
|
||||
if Google.API.property?(property_or_view) do
|
||||
opts = Keyword.put(opts, :property, property_or_view)
|
||||
Imported.GoogleAnalytics4.new_import(site, current_user, opts)
|
||||
else
|
||||
opts = Keyword.put(opts, :view_id, property_or_view)
|
||||
Imported.UniversalAnalytics.new_import(site, current_user, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue