Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Nov 28, 2023
0 parents commit c3bde46
Show file tree
Hide file tree
Showing 25 changed files with 3,747 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/eslint");
11 changes: 11 additions & 0 deletions .github/workflows/discourse-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Discourse Plugin

on:
push:
branches:
- main
pull_request:

jobs:
ci:
uses: discourse/.github/.github/workflows/discourse-plugin.yml@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
/gems
/auto_generated
1 change: 1 addition & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/prettier");
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_gem:
rubocop-discourse: stree-compat.yml
2 changes: 2 additions & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--print-width=100
--plugins=plugin/trailing_comma,plugin/disable_auto_ternary
1 change: 1 addition & 0 deletions .template-lintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/template-lint");
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

group :development do
gem "rubocop-discourse"
gem "syntax_tree"
end
60 changes: 60 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
base64 (0.1.1)
json (2.6.3)
language_server-protocol (3.17.0.3)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
racc (1.7.1)
rainbow (3.1.1)
regexp_parser (2.8.2)
rexml (3.2.6)
rubocop (1.57.1)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.19.0)
rubocop (~> 1.41)
rubocop-discourse (3.4.0)
rubocop (>= 1.1.0)
rubocop-rspec (>= 2.0.0)
rubocop-factory_bot (2.24.0)
rubocop (~> 1.33)
rubocop-rspec (2.24.1)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-20
ruby
x86_64-darwin-18
x86_64-darwin-19
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
rubocop-discourse
syntax_tree

BUNDLED WITH
2.3.10
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Civilized Discourse Construction Kit, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# **discourse-deprecation-collector** Plugin

This plugin will report JS deprecations to the server and increment prometheus metrics. It's designed to help hosting providers roll out breaking changes.

Requires the discourse-prometheus plugin to be installed.
55 changes: 55 additions & 0 deletions app/controllers/deprecation_collector/collector_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

module ::DeprecationCollector
class CollectorController < ::ApplicationController
requires_plugin PLUGIN_NAME
skip_before_action :check_xhr, :verify_authenticity_token

LOGS_PER_10_SECONDS = 2

def log
RateLimiter.new(
nil,
"deprecation_collector_report_#{current_user&.id || request.client_ip}",
LOGS_PER_10_SECONDS,
10,
).performed!

begin
reported_data = JSON.parse(params.require("data"))
rescue JSON::ParserError
raise Discourse::InvalidParameters.new("Cannot parse JSON")
end

if reported_data.count > 20
raise Discourse::InvalidParameters.new("Too many deprecations reported")
end

reported_data.each do |key, value|
next if !value.is_a?(Integer)

if !DeprecationCollector::List.include?(key)
# We only collect data for a bound set of deprecation-ids so that clients
# can't inflate our prometheus metrics by sending bogus data.
key = "_other"
end

add_to_counter(key, 1)
end

render json: success_json
end

private

def add_to_counter(name, value)
metric = DiscoursePrometheus::InternalMetric::Custom.new
metric.type = "Counter"
metric.labels = { db: RailsMultisite::ConnectionManagement.current_db, deprecation_id: name }
metric.name = "js_deprecation_count"
metric.description = "js deprecations reported by clients"
metric.value = value
$prometheus_client.send_json(metric.to_h)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("0.8", (api) => {
api.container.lookup("service:deprecation-collector");
});
84 changes: 84 additions & 0 deletions assets/javascripts/discourse/services/deprecation-collector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { registerDeprecationHandler } from "@ember/debug";
import { cancel } from "@ember/runloop";
import Service from "@ember/service";
import discourseDebounce from "discourse-common/lib/debounce";
import { registerDeprecationHandler as registerDiscourseDeprecationHandler } from "discourse-common/lib/deprecated";
import getURL from "discourse-common/lib/get-url";
import { bind } from "discourse-common/utils/decorators";

// Deprecation handling APIs don't have any way to unregister handlers, so we set up permenant
// handlers and link them up to the application lifecycle using module-local state.
let handler;
registerDeprecationHandler((message, opts, next) => {
handler?.(message, opts);
return next(message, opts);
});
registerDiscourseDeprecationHandler((message, opts) =>
handler?.(message, opts)
);

export default class DeprecationCollector extends Service {
#configById = new Map();
#counts = new Map();
#reportDebounce;

constructor() {
super(...arguments);
handler = this.track;

const workflowConfig = window.deprecationWorkflow?.config?.workflow || {};
for (const c of workflowConfig) {
this.#configById.set(c.matchId, c.handler);
}

document.addEventListener("visibilitychange", this.handleVisibilityChanged);
}

willDestroy() {
handler = null;
window.removeEventListener(
"visibilitychange",
this.handleVisibilityChanged
);
cancel(this.#reportDebounce);
super.willDestroy();
}

@bind
handleVisibilityChanged() {
// Tab is going to background, or we're navigating away. Make the report immediately.
if (document.visibilityState !== "visible") {
this.report();
}
}

@bind
track(message, options) {
if (this.#configById.get(options.id) === "silence") {
return;
}

let count = this.#counts.get(options.id) || 0;
count += 1;
this.#counts.set(options.id, count);

this.#reportDebounce = discourseDebounce(this.report, 10_000);
}

@bind
report() {
cancel(this.#reportDebounce);

if (this.#counts.size === 0) {
return;
}

const data = Object.fromEntries(this.#counts.entries());
this.#counts.clear();

const body = new FormData();
body.append("data", JSON.stringify(data));

navigator.sendBeacon(getURL("/deprecation-collector/log"), body);
}
}
6 changes: 6 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
admin_js:
admin:
site_settings:
categories:
deprecation_collector: "Deprecation Collector"
3 changes: 3 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en:
site_settings:
deprecation_collector_enabled: "Enable deprecation collector"
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

DeprecationCollector::Engine.routes.draw do
post "log" => "collector#log"
get "log" => "collector#log"
end

Discourse::Application.routes.draw do
mount ::DeprecationCollector::Engine, at: "deprecation-collector"
end
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
deprecation_collector:
deprecation_collector_enabled:
default: false
9 changes: 9 additions & 0 deletions lib/deprecation_collector/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module ::DeprecationCollector
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DeprecationCollector
config.autoload_paths << File.join(config.root, "lib")
end
end
Loading

0 comments on commit c3bde46

Please sign in to comment.