Yicong-Huang commented on code in PR #5849:
URL: https://github.com/apache/texera/pull/5849#discussion_r3463897668


##########
frontend/src/app/common/service/deployment-version/deployment-version.service.spec.ts:
##########
@@ -0,0 +1,231 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpClientTestingModule, HttpTestingController } from 
"@angular/common/http/testing";
+import { TestBed, fakeAsync, tick } from "@angular/core/testing";
+import { NzNotificationDataOptions } from "ng-zorro-antd/notification";
+import { NotificationService } from "../notification/notification.service";
+import { DeploymentVersionService, VERSION_MANIFEST_URL, 
VERSION_POLL_INTERVAL_MS } from "./deployment-version.service";
+
+// Records blank() calls so the prompt is observable without a spy framework.
+class FakeNotificationService {
+  public blankCalls: { title: string; content: string; options: 
NzNotificationDataOptions }[] = [];
+  blank(title: string, content: string, options: NzNotificationDataOptions = 
{}): void {
+    this.blankCalls.push({ title, content, options });
+  }
+}

Review Comment:
   let's call it a mock, not "fake"



##########
frontend/src/app/common/service/deployment-version/deployment-version.service.ts:
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpClient } from "@angular/common/http";
+import { Injectable } from "@angular/core";
+import { Observable, Subscription, of, timer } from "rxjs";
+import { catchError, filter, map, switchMap, take } from "rxjs/operators";
+import { Version } from "../../../../environments/version";
+import { NotificationService } from "../notification/notification.service";
+
+export const VERSION_MANIFEST_URL = "assets/version.json";
+export const VERSION_POLL_INTERVAL_MS = 5 * 60 * 1000;
+
+@Injectable({
+  providedIn: "root",
+})
+export class DeploymentVersionService {
+  private polling?: Subscription;
+
+  constructor(
+    private http: HttpClient,
+    private notification: NotificationService
+  ) {}
+
+  // True when the deployed build's buildNumber differs from the running one.
+  checkForUpdate(): Observable<boolean> {
+    return this.http.get<{ buildNumber?: string }>(VERSION_MANIFEST_URL, { 
params: { t: Date.now().toString() } }).pipe(
+      map(manifest => {
+        const deployed = manifest?.buildNumber;
+        return typeof deployed === "string" && deployed.length > 0 && deployed 
!== Version.buildNumber;
+      }),
+      catchError(() => of(false))
+    );
+  }
+
+  // Poll until a new deployment is detected, then prompt once. Idempotent:
+  // a second call while already polling returns the in-flight subscription
+  // rather than stacking a duplicate poller (and a duplicate prompt).
+  start(intervalMs: number = VERSION_POLL_INTERVAL_MS): Subscription {
+    if (this.polling && !this.polling.closed) {
+      return this.polling;
+    }
+    this.polling = timer(intervalMs, intervalMs)
+      .pipe(
+        switchMap(() => this.checkForUpdate()),
+        filter(updated => updated),
+        take(1)
+      )
+      .subscribe(() => this.promptReload());
+    return this.polling;
+  }
+
+  promptReload(): void {
+    this.notification.blank(
+      "New version available",
+      "A new version of Texera is available. Refresh the page to update.",
+      { nzDuration: 0 }
+    );
+  }

Review Comment:
   not sure if notification can add this:
   
   have a button to trigger page refresh?
   
   just for convenience? 



##########
frontend/src/app/app.component.ts:
##########
@@ -49,6 +54,13 @@ export class AppComponent {
     } catch {
       this.configLoaded = false;
     }
+
+    // Poll for new deployments only when the config opts in (off by default),
+    // config actually loaded, and this isn't the "dev" placeholder build where
+    // no deployments occur.
+    if (this.configLoaded && this.config.env.deploymentVersionCheckEnabled && 
Version.buildNumber !== "dev") {
+      this.deploymentVersion.start();

Review Comment:
   you cannot start a version. let's make the naming better



##########
frontend/src/app/app.component.spec.ts:
##########
@@ -0,0 +1,158 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { CommonModule } from "@angular/common";
+import { ComponentFixture, TestBed } from "@angular/core/testing";
+import { RouterTestingModule } from "@angular/router/testing";
+import { AppComponent } from "./app.component";
+import { GuiConfigService } from "./common/service/gui-config.service";
+import { DeploymentVersionService } from 
"./common/service/deployment-version/deployment-version.service";
+import { Version } from "../environments/version";
+
+// GuiConfigService stub whose env getter either returns a value or throws,
+// mirroring "config loaded" vs "config failed to load by APP_INITIALIZER".
+class StubGuiConfigService {
+  shouldThrow = false;
+  deploymentVersionCheckEnabled = true;
+  get env(): unknown {
+    if (this.shouldThrow) {
+      throw new Error("config not loaded");
+    }
+    return { deploymentVersionCheckEnabled: this.deploymentVersionCheckEnabled 
};
+  }
+}
+
+// Records start() invocations without a spy framework.
+class FakeDeploymentVersionService {

Review Comment:
   create a spy around the real service?
   
   vitest should have framework to spy a function?



##########
frontend/src/app/app.component.ts:
##########
@@ -40,7 +42,10 @@ import { UntilDestroy } from "@ngneat/until-destroy";
 export class AppComponent {
   configLoaded = false;
 
-  constructor(private config: GuiConfigService) {
+  constructor(
+    private config: GuiConfigService,
+    private deploymentVersion: DeploymentVersionService

Review Comment:
   `configService` and `deploymentVersionService`



##########
frontend/src/app/common/service/deployment-version/deployment-version.service.spec.ts:
##########
@@ -0,0 +1,231 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpClientTestingModule, HttpTestingController } from 
"@angular/common/http/testing";
+import { TestBed, fakeAsync, tick } from "@angular/core/testing";
+import { NzNotificationDataOptions } from "ng-zorro-antd/notification";
+import { NotificationService } from "../notification/notification.service";
+import { DeploymentVersionService, VERSION_MANIFEST_URL, 
VERSION_POLL_INTERVAL_MS } from "./deployment-version.service";
+
+// Records blank() calls so the prompt is observable without a spy framework.
+class FakeNotificationService {
+  public blankCalls: { title: string; content: string; options: 
NzNotificationDataOptions }[] = [];
+  blank(title: string, content: string, options: NzNotificationDataOptions = 
{}): void {
+    this.blankCalls.push({ title, content, options });
+  }
+}

Review Comment:
   why don't we test against the real notification service?



##########
frontend/build-version.js:
##########
@@ -23,19 +23,32 @@
 // Dev builds (`yarn start`) keep the static "dev" string in version.ts.
 
 const { generate } = require("build-number-generator");
-const { version } = require("./package.json");
 const { resolve } = require("path");
 const { writeFileSync } = require("fs");
 
-const buildNumber = generate(version);
-const out = resolve(__dirname, "src", "environments", "version.prod.ts");
-writeFileSync(
-  out,
-  `// AUTO-GENERATED by build-version.js — do not edit or commit.
+// Returns the contents of version.prod.ts and version.json for a version.
+function renderVersionArtifacts(version, buildNumber = generate(version)) {
+  const prodTs = `// AUTO-GENERATED by build-version.js — do not edit or 
commit.
 export const Version = {
   buildNumber: ${JSON.stringify(buildNumber)},
   version: ${JSON.stringify(version)},
 };
-`,
-);
-console.log(`build-version: ${buildNumber}`);
+`;
+  const manifestJson = JSON.stringify({ buildNumber, version }) + "\n";
+  return { buildNumber, prodTs, manifestJson };
+}
+
+function main() {
+  const { version } = require("./package.json");
+  const { buildNumber, prodTs, manifestJson } = 
renderVersionArtifacts(version);
+  writeFileSync(resolve(__dirname, "src", "environments", "version.prod.ts"), 
prodTs);
+  writeFileSync(resolve(__dirname, "src", "assets", "version.json"), 
manifestJson);
+  console.log(`build-version: ${buildNumber}`);
+}

Review Comment:
   is it the only way to write a file? how about use an env variable? 
   
   either way we have to take care of the file life cycle: who is going to 
delete this file/env variable?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to