Skip to content

Commit

Permalink
Add PrecalculateBrowserFeatureSupportEvents method
Browse files Browse the repository at this point in the history
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
jcscottiii committed Nov 5, 2024
1 parent e459623 commit d7263ce
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/gcpspanner/browser_availabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,28 @@ func (c *Client) InsertBrowserFeatureAvailability(

return newEntityWriter[browserFeatureAvailabilityMapper](c).upsert(ctx, featureAvailability)
}

func (c *Client) fetchAllBrowserAvailabilitiesWithTransaction(
ctx context.Context, txn *spanner.ReadWriteTransaction) ([]spannerBrowserFeatureAvailability, error) {
var availabilities []spannerBrowserFeatureAvailability
iter := txn.Read(ctx, browserFeatureAvailabilitiesTable, spanner.AllKeys(), []string{
"BrowserName",
"BrowserVersion",
"WebFeatureID",
})
defer iter.Stop()
err := iter.Do(func(row *spanner.Row) error {
var entry spannerBrowserFeatureAvailability
if err := row.ToStruct(&entry); err != nil {
return err
}
availabilities = append(availabilities, entry)

return nil
})
if err != nil {
return nil, err
}

return availabilities, nil
}
141 changes: 141 additions & 0 deletions lib/gcpspanner/browser_feature_support_event.go
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
}
Loading

0 comments on commit d7263ce

Please sign in to comment.