Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: now check for deployment status #78

Merged
merged 6 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"chrome",
"--web-port",
"10000",
"--dart-define=LENRA_SERVER_URL=http://localhost:4000"
"--dart-define=LENRA_SERVER_URL=http://localhost:4000",
"--dart-define=SENTRY_CLIENT_DSN=test",
]
},
{
Expand All @@ -29,6 +30,7 @@
"10000",
"--dart-define=LENRA_SERVER_URL=${env:LENRA_SERVER_URL}",
"--dart-define=LENRA_BASIC_AUTH=${env:LENRA_BASIC_AUTH}",
"--dart-define=SENTRY_CLIENT_DSN=test",
jonas-martinez marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:client_common/config/config.dart';
import 'package:client_common/models/auth_model.dart';
import 'package:client_common/models/build_model.dart';
import 'package:client_common/models/cgu_model.dart';
import 'package:client_common/models/deployment_model.dart';
import 'package:client_common/models/store_model.dart';
import 'package:client_common/models/user_application_model.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -43,6 +44,7 @@ class Backoffice extends StatelessWidget {
providers: [
ChangeNotifierProvider<AuthModel>(create: (context) => AuthModel()),
ChangeNotifierProvider<BuildModel>(create: (context) => BuildModel()),
ChangeNotifierProvider<DeploymentModel>(create: (context) => DeploymentModel()),
ChangeNotifierProvider<UserApplicationModel>(create: (context) => UserApplicationModel()),
ChangeNotifierProvider<StoreModel>(create: (context) => StoreModel()),
ChangeNotifierProvider<CguModel>(create: (context) => CguModel()),
Expand Down
75 changes: 49 additions & 26 deletions lib/views/overview_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'package:client_backoffice/navigation/backoffice_navigator.dart';
import 'package:client_backoffice/views/backoffice_page.dart';
import 'package:client_common/api/response_models/app_response.dart';
import 'package:client_common/api/response_models/build_response.dart';
import 'package:client_common/api/response_models/deployment_response.dart';
import 'package:client_common/config/config.dart';
import 'package:client_common/models/build_model.dart';
import 'package:client_common/models/deployment_model.dart';
import 'package:client_common/models/user_application_model.dart';
import 'package:client_common/navigator/common_navigator.dart';
import 'package:flutter/material.dart';
Expand All @@ -29,6 +31,8 @@ class _OverviewPageState extends State<OverviewPage> {
@override
void initState() {
var buildModel = context.read<BuildModel>();
var deploymentModel = context.read<DeploymentModel>();

UserApplicationModel userApplicationModel = context.read<UserApplicationModel>();

WidgetsBinding.instance.addPostFrameCallback((_) {
Expand All @@ -41,6 +45,7 @@ class _OverviewPageState extends State<OverviewPage> {
CommonNavigator.go(context, BackofficeNavigator.selectProject);
} else {
buildModel.fetchBuilds(widget.appId);
deploymentModel.fetchDeployments(widget.appId);
}
},
);
Expand All @@ -57,49 +62,59 @@ class _OverviewPageState extends State<OverviewPage> {
@override
Widget build(BuildContext context) {
var buildModel = context.read<BuildModel>();
var deploymentModel = context.read<DeploymentModel>();

// A bit dirty
if (app == null) return Center(child: CircularProgressIndicator());

List<BuildResponse> builds =
context.select<BuildModel, List<BuildResponse>>((buildModel) => buildModel.buildsForApp(app!.id));
List<DeploymentResponse> deployments = context.select<DeploymentModel, List<DeploymentResponse>>(
(deploymentModel) => deploymentModel.deploymentsForApp(app!.id));
print(deployments);

var hasPendingBuild = false;
var hasPublishedBuild = false;
var hasPendingDeployment = false;
var hasPublishedDeployment = false;

if (builds.isNotEmpty) {
builds.sort((a, b) => a.buildNumber.compareTo(b.buildNumber));

// Check if there is a createBuildStatus that is currently fetching.
var createBuildStatusFetching = buildModel.createBuildStatus[app!.id]?.isFetching() ?? false;

hasPendingBuild = builds.any((build) => build.status == BuildStatus.pending) || createBuildStatusFetching;
hasPendingDeployment = deployments.any((deployment) =>
deployment.status == DeploymentStatus.pending || deployment.status == DeploymentStatus.created) ||
createBuildStatusFetching;

if (hasPendingBuild) {
if (hasPendingDeployment) {
timer = Timer(Duration(seconds: 5), () {
buildModel.fetchBuilds(widget.appId).then((_) {
setState(() {});
deploymentModel.fetchDeployments(widget.appId).then((_) {
buildModel.fetchBuilds(widget.appId).then((_) {
setState(() {});
});
});
});
}

hasPublishedBuild = builds.any((build) => build.status == BuildStatus.success);
hasPublishedDeployment = deployments.any((deployment) => deployment.status == DeploymentStatus.success);
}

return BackofficePage(
key: ValueKey("overview"),
title: "Overview",
actionWidget: LenraButton(
text: "Publish my application",
disabled: hasPendingBuild,
disabled: hasPendingDeployment,
onPressed: () => buildModel.createBuild(app!.id).then((_) {
setState(() {});
}),
),
child: buildPage(context, hasPublishedBuild, builds),
child: buildPage(context, hasPublishedDeployment, deployments, builds),
);
}

Widget buildPage(BuildContext context, bool hasPublishedBuild, List<BuildResponse> builds) {
Widget buildPage(
BuildContext context, bool hasPublishedBuild, List<DeploymentResponse> deployments, List<BuildResponse> builds) {
var theme = LenraTheme.of(context);

return LenraFlex(
Expand Down Expand Up @@ -137,12 +152,14 @@ class _OverviewPageState extends State<OverviewPage> {
child: Text("Build status"),
),
]),
if (builds.isNotEmpty) buildRow(context, builds.last),
if (builds.length >= 2 && builds.last.status == BuildStatus.pending)
buildRow(context, builds.reversed.elementAt(1)),
if (deployments.isNotEmpty)
buildRow(
context, deployments.last, builds.firstWhere((element) => element.id == deployments.last.buildId)),
if (deployments.length >= 2 && deployments.last.status == DeploymentStatus.pending)
buildRow(context, deployments.reversed.elementAt(1), builds.reversed.elementAt(1)),
],
),
if (builds.isEmpty)
if (deployments.isEmpty)
Text(
"Your application has not been built yet.\nClick “Publish my application” to create your first build.",
style: theme.lenraTextThemeData.disabledBodyText,
Expand All @@ -152,46 +169,52 @@ class _OverviewPageState extends State<OverviewPage> {
);
}

Color colorFromStatus(BuildStatus status) {
Color colorFromStatus(DeploymentStatus status) {
switch (status) {
case BuildStatus.success:
case DeploymentStatus.success:
return LenraColorThemeData.lenraFunGreenPulse;

case BuildStatus.pending:
case DeploymentStatus.pending:
return LenraColorThemeData.lenraFunYellowPulse;

case BuildStatus.failure:
case DeploymentStatus.created:
return LenraColorThemeData.lenraGreyText;

case DeploymentStatus.failure:
return LenraColorThemeData.lenraFunRedPulse;

default:
return LenraColorThemeData.lenraFunRedPulse;
}
}

String textFromStatus(BuildStatus status) {
String textFromStatus(DeploymentStatus status) {
switch (status) {
case BuildStatus.success:
case DeploymentStatus.success:
return "Published";

case BuildStatus.pending:
case DeploymentStatus.pending:
return "Building...";

case BuildStatus.failure:
case DeploymentStatus.created:
return "Created";

case DeploymentStatus.failure:
return "Failure";

default:
return "Failure";
}
}

TableRow buildRow(BuildContext context, BuildResponse buildResponse) {
TableRow buildRow(BuildContext context, DeploymentResponse deploymentResponse, BuildResponse buildResponse) {
var theme = LenraTheme.of(context);
return TableRow(children: [
LenraTableCell(
child: Text("#${buildResponse.buildNumber}"),
),
LenraTableCell(
child: Text(DateFormat.yMMMMd().add_jm().format(buildResponse.insertedAt)),
child: Text(DateFormat.yMMMMd().add_jm().format(deploymentResponse.insertedAt)),
),
LenraTableCell(
child: LenraFlex(
Expand All @@ -200,10 +223,10 @@ class _OverviewPageState extends State<OverviewPage> {
children: [
Icon(
Icons.circle,
color: colorFromStatus(buildResponse.status),
color: colorFromStatus(deploymentResponse.status),
size: theme.baseSize,
),
Text(textFromStatus(buildResponse.status)),
Text(textFromStatus(deploymentResponse.status)),
],
),
),
Expand Down
Loading