-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PrecalculateBrowserFeatureSupportEvents method
Depends on: #865 This method is the entrypoint to calculate the events stored in the BrowserFeatureSupportEvents table introduced in #865. It reads all of the web features, browser releases and feature availabilities. With that, it builds the event log. In order to read all that data, small helper methods were created to assist. This PrecalculateBrowserFeatureSupportEvents method does not take in any input and just uses existing table data. Fixes #834
- Loading branch information
1 parent
e459623
commit 087bdda
Showing
5 changed files
with
590 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed 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. | ||
|
||
package gcpspanner | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"cloud.google.com/go/spanner" | ||
) | ||
|
||
const browserFeatureSupportEventsTable = "BrowserFeatureSupportEvents" | ||
|
||
type BrowserFeatureSupportStatus string | ||
|
||
const ( | ||
UnsupportedFeatureSupport BrowserFeatureSupportStatus = "unsupported" | ||
SupportedFeatureSupport BrowserFeatureSupportStatus = "supported" | ||
) | ||
|
||
type BrowserFeatureSupportEvent struct { | ||
TargetBrowserName string `spanner:"TargetBrowserName"` | ||
EventBrowserName string `spanner:"EventBrowserName"` | ||
EventReleaseDate time.Time `spanner:"EventReleaseDate"` | ||
WebFeatureID string `spanner:"WebFeatureID"` | ||
SupportStatus BrowserFeatureSupportStatus `spanner:"SupportStatus"` | ||
} | ||
|
||
func buildAvailabilityMap( | ||
releases []spannerBrowserRelease, | ||
availabilities []spannerBrowserFeatureAvailability) map[string]map[string]time.Time { | ||
// Create a map for efficient lookup of browser releases | ||
releaseMap := make(map[string]map[string]time.Time) // map[browserName]map[browserVersion]releaseDate | ||
for _, release := range releases { | ||
if _, ok := releaseMap[release.BrowserName]; !ok { | ||
releaseMap[release.BrowserName] = make(map[string]time.Time) | ||
} | ||
releaseMap[release.BrowserName][release.BrowserVersion] = release.ReleaseDate | ||
} | ||
|
||
// Create a map for efficient lookup of feature availability with release dates | ||
availabilityMap := make(map[string]map[string]time.Time) // map[browserName]map[featureID]time.Time | ||
for _, availability := range availabilities { | ||
if _, ok := availabilityMap[availability.BrowserName]; !ok { | ||
availabilityMap[availability.BrowserName] = make(map[string]time.Time) | ||
} | ||
// Use releaseMap to get the release date for this availability | ||
if releaseDate, ok := releaseMap[availability.BrowserName][availability.BrowserVersion]; ok { | ||
availabilityMap[availability.BrowserName][availability.WebFeatureID] = releaseDate | ||
} | ||
} | ||
|
||
return availabilityMap | ||
} | ||
|
||
func calculateBrowserSupportEvents( | ||
availabilityMap map[string]map[string]time.Time, | ||
releases []spannerBrowserRelease, | ||
ids []string) []BrowserFeatureSupportEvent { | ||
var supportEvents []BrowserFeatureSupportEvent | ||
for _, targetBrowser := range releases { | ||
for _, eventBrowser := range releases { | ||
for _, id := range ids { | ||
supportStatus := UnsupportedFeatureSupport // Default to unsupported | ||
if _, ok := availabilityMap[targetBrowser.BrowserName]; ok { | ||
availabilityTime, supported := availabilityMap[targetBrowser.BrowserName][id] | ||
if supported && (availabilityTime.Equal(eventBrowser.ReleaseDate) || | ||
eventBrowser.ReleaseDate.After(availabilityTime)) { | ||
supportStatus = SupportedFeatureSupport | ||
} | ||
} | ||
supportEvents = append(supportEvents, BrowserFeatureSupportEvent{ | ||
TargetBrowserName: targetBrowser.BrowserName, | ||
EventBrowserName: eventBrowser.BrowserName, | ||
EventReleaseDate: eventBrowser.ReleaseDate, | ||
WebFeatureID: id, | ||
SupportStatus: supportStatus, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return supportEvents | ||
} | ||
|
||
// PrecalculateBrowserFeatureSupportEvents populates the BrowserFeatureSupportEvents table with pre-calculated data. | ||
func (c *Client) PrecalculateBrowserFeatureSupportEvents(ctx context.Context) error { | ||
_, err := c.Client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error { | ||
// 1. Fetch all BrowserFeatureAvailabilities | ||
availabilities, err := c.fetchAllBrowserAvailabilitiesWithTransaction(ctx, txn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2. Fetch all BrowserReleases | ||
releases, err := c.fetchAllBrowserReleasesWithTransaction(ctx, txn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 3. Fetch all WebFeatures | ||
ids, err := c.fetchAllWebFeatureIDsWithTransaction(ctx, txn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 4. Create maps for quick look ups | ||
availabilityMap := buildAvailabilityMap(releases, availabilities) | ||
|
||
// 4. Generate BrowserFeatureSupportEvents entries (including SupportStatus) | ||
supportEvents := calculateBrowserSupportEvents(availabilityMap, releases, ids) | ||
|
||
// 5. Insert the new entries into BrowserFeatureSupportEvents | ||
var mutations []*spanner.Mutation | ||
for _, entry := range supportEvents { | ||
m, err := spanner.InsertOrUpdateStruct(browserFeatureSupportEventsTable, entry) | ||
if err != nil { | ||
return errors.Join(err, ErrInternalQueryFailure) | ||
} | ||
mutations = append(mutations, m) | ||
} | ||
|
||
return txn.BufferWrite(mutations) | ||
|
||
}) | ||
|
||
return err | ||
} |
Oops, something went wrong.