Skip to content

Commit

Permalink
Some fixes and add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-martinez committed Jul 13, 2024
1 parent 7bde21e commit d511c45
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions apps/lenra/lib/lenra/apps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ defmodule Lenra.Apps do
end

def deploy_in_main_env(%Build{} = build) do
Lenra.Telemetry.start(:app_deployment, build.application_id)
Lenra.Monitor.ApplicationDeploymentMonitor.monitor(build.application_id, %{})

Check warning on line 337 in apps/lenra/lib/lenra/apps.ex

View workflow job for this annotation

GitHub Actions / server checks

Nested modules could be aliased at the top of the invoking module.

with loaded_build <- Repo.preload(build, :application),
loaded_app <- Repo.preload(loaded_build.application, main_env: [:environment]),
Expand Down Expand Up @@ -407,7 +407,7 @@ defmodule Lenra.Apps do
|> Repo.transaction()

ApplicationServices.stop_app("#{OpenfaasServices.get_function_name(service_name, build_number)}")
Lenra.Telemetry.stop(:app_deployment, deployment.application_id)
Lenra.Monitor.ApplicationDeploymentMonitor.stop(deployment.application_id, %{})

Check warning on line 410 in apps/lenra/lib/lenra/apps.ex

View workflow job for this annotation

GitHub Actions / server checks

Nested modules could be aliased at the top of the invoking module.
transaction

# Function not found in openfaas, 30 retry (15s),
Expand Down
8 changes: 5 additions & 3 deletions apps/lenra/lib/monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Lenra.Monitor do
def setup do
events = [
[:lenra, :app_deployment, :start],
[:lenra, :app_deployment, :stop],
[:lenra, :app_deployment, :stop]
]

:telemetry.attach_many(
Expand All @@ -32,14 +32,16 @@ defmodule Lenra.Monitor do

def handle_event([:lenra, :app_deployment, :start], measurements, metadata, _config) do
application_id = Map.get(metadata, :application_id)
build_id = Map.get(metadata, :build_id)

Repo.insert(ApplicationDeploymentMeasurement.new(application_id, measurements))
Repo.insert(ApplicationDeploymentMeasurement.new(application_id, build_id, measurements))
end

def handle_event([:lenra, :app_deployment, :stop], measurements, metadata, _config) do
application_id = Map.get(metadata, :application_id)
build_id = Map.get(metadata, :build_id)

Repo.get_by!(ApplicationDeploymentMeasurement, application_id: application_id)
Repo.get_by!(ApplicationDeploymentMeasurement, %{application_id: application_id, build_id: build_id})
|> ApplicationDeploymentMeasurement.update(measurements)
|> Repo.update()
end
Expand Down
11 changes: 6 additions & 5 deletions apps/lenra/lib/monitor/application_deployment_measurement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ defmodule Lenra.Monitor.ApplicationDeploymentMeasurement do
use Ecto.Schema
import Ecto.Changeset

alias Lenra.Apps.App
alias Lenra.Apps.{App, Build}

schema "session_measurement" do
schema "application_deployment_measurement" do
belongs_to(:application, App)
belongs_to(:build, Build)

field(:start_time, :utc_datetime)
field(:end_time, :utc_datetime)
Expand All @@ -21,12 +22,12 @@ defmodule Lenra.Monitor.ApplicationDeploymentMeasurement do
def changeset(application_deployment_measurement, params \\ %{}) do
application_deployment_measurement
|> cast(params, [:start_time, :end_time, :duration])
|> validate_required([:start_time, :application_id])
|> validate_required([:start_time, :application_id, :build_id])
|> foreign_key_constraint(:application_id)
end

def new(application_id, params \\ %{}) do
%__MODULE__{application_id: application_id}
def new(application_id, build_id, params \\ %{}) do
%__MODULE__{application_id: application_id, build_id: build_id}
|> __MODULE__.changeset(params)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Lenra.Repo.Migrations.ApplicationDeploymentMeasurement do
use Ecto.Migration

def change do
create table(:application_deployment_measurement) do
add(:user_id, references(:users), null: false)
add(:build_id, references(:builds), null: false)

add(:start_time, :timestamp, null: false)
add(:end_time, :timestamp)

add(:duration, :integer)

timestamps()
end
end
end

0 comments on commit d511c45

Please sign in to comment.