This is an automated email from the ASF dual-hosted git repository.

rshah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 47596b0131 Removed TryIfModifiedSinceQuery from servicecategories.go 
and reused from ims.go (#7623)
47596b0131 is described below

commit 47596b01310e8a3b64d68365e438ba390629823c
Author: Jagan Parthiban <[email protected]>
AuthorDate: Mon Jul 10 20:30:06 2023 +0530

    Removed TryIfModifiedSinceQuery from servicecategories.go and reused from 
ims.go (#7623)
    
    * Removed TryIfModifiedSinceQuery from servicecategories.go and used the 
one from ims.go
    
    * Updated CHANGELOG.md
---
 CHANGELOG.md                                       |  1 +
 .../servicecategory/servicecategories.go           | 66 ++++-----------------
 .../servicecategory/servicecategories_test.go      | 67 ----------------------
 3 files changed, 11 insertions(+), 123 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6abfae4288..01b48f0492 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -64,6 +64,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7600](https://github.com/apache/trafficcontrol/pull/7600) *t3c* changed 
default go-direct command line arg to be old to avoid unexpected config changes 
upon upgrade.
 
 ### Fixed
+- [#7623] (https://github.com/apache/trafficcontrol/pull/7623) *Traffic Ops* 
Removed TryIfModifiedSinceQuery from servicecategories.go and reused from ims.go
 - [#7608](https://github.com/apache/trafficcontrol/pull/7608) *Traffic 
Monitor* Use stats_over_http(plugin.system_stats.timestamp_ms) timestamp field 
to calculate bandwidth for TM's caches.
 - [#6318](https://github.com/apache/trafficcontrol/issues/6318) *Docs* 
Included docs for POST, PUT, DELETE (v3,v4,v5) for statuses and statuses{id}
 - [#7561](https://github.com/apache/trafficcontrol/pull/7561) *Traffic Ops* 
*Traffic Ops* Fixed `ASN` V5 apis to respond with `RFC3339` date/time Format.
diff --git 
a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go 
b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
index 2559b7e881..348efb728e 100644
--- a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
+++ b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
@@ -28,12 +28,12 @@ import (
        "time"
 
        "github.com/apache/trafficcontrol/lib/go-log"
-       "github.com/apache/trafficcontrol/lib/go-rfc"
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/lib/go-tc/tovalidate"
        "github.com/apache/trafficcontrol/lib/go-util"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
        
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/ims"
 
        validation "github.com/go-ozzo/ozzo-validation"
        "github.com/jmoiron/sqlx"
@@ -264,7 +264,7 @@ func GetServiceCategory(tx *sqlx.Tx, params 
map[string]string, useIMS bool, head
        }
 
        if useIMS {
-               runSecond, maxTime = TryIfModifiedSinceQuery(header, tx, where, 
queryValues)
+               runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, header, 
queryValues, SelectMaxLastUpdatedQuery(where))
                if !runSecond {
                        log.Debugln("IMS HIT")
                        return scList, maxTime, http.StatusNotModified, nil, nil
@@ -291,60 +291,6 @@ func GetServiceCategory(tx *sqlx.Tx, params 
map[string]string, useIMS bool, head
        return scList, maxTime, http.StatusOK, nil, nil
 }
 
-// TryIfModifiedSinceQuery [Version : V5] function receives transactions and 
header from GetServiceCategory function and returns bool value if status is not 
modified.
-func TryIfModifiedSinceQuery(header http.Header, tx *sqlx.Tx, where string, 
queryValues map[string]interface{}) (bool, time.Time) {
-       var max time.Time
-       var imsDate time.Time
-       var ok bool
-       imsDateHeader := []string{}
-       runSecond := true
-       dontRunSecond := false
-
-       if header == nil {
-               return runSecond, max
-       }
-
-       imsDateHeader = header[rfc.IfModifiedSince]
-       if len(imsDateHeader) == 0 {
-               return runSecond, max
-       }
-
-       if imsDate, ok = rfc.ParseHTTPDate(imsDateHeader[0]); !ok {
-               log.Warnf("IMS request header date '%s' not parsable", 
imsDateHeader[0])
-               return runSecond, max
-       }
-
-       imsQuery := `SELECT max(last_updated) as t from service_category sc`
-       query := imsQuery + where
-       rows, err := tx.NamedQuery(query, queryValues)
-
-       if errors.Is(err, sql.ErrNoRows) {
-               return dontRunSecond, max
-       }
-
-       if err != nil {
-               log.Warnf("Couldn't get the max last updated time: %v", err)
-               return runSecond, max
-       }
-
-       defer rows.Close()
-       // This should only ever contain one row
-       if rows.Next() {
-               v := time.Time{}
-               if err = rows.Scan(&v); err != nil {
-                       log.Warnf("Failed to parse the max time stamp into a 
struct %v", err)
-                       return runSecond, max
-               }
-
-               max = v
-               // The request IMS time is later than the max of (lastUpdated, 
deleted_time)
-               if imsDate.After(v) {
-                       return dontRunSecond, max
-               }
-       }
-       return runSecond, max
-}
-
 // CreateServiceCategory [Version : V5] function creates the service category 
with the passed name.
 func CreateServiceCategory(w http.ResponseWriter, r *http.Request) {
        inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
@@ -506,3 +452,11 @@ func readAndValidateJsonStruct(r *http.Request) 
(tc.ServiceCategoryV5, error) {
        }
        return sc, nil
 }
+
+// SelectMaxLastUpdatedQuery used for TryIfModifiedSinceQuery()
+func SelectMaxLastUpdatedQuery(where string) string {
+       return `SELECT max(t) from (
+               SELECT max(last_updated) as t from service_category sc ` + 
where +
+               ` UNION ALL
+       select max(last_updated) as t from last_deleted l where 
l.table_name='service_category') as res`
+}
diff --git 
a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories_test.go 
b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories_test.go
index 889457be2f..aa842142c0 100644
--- a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories_test.go
+++ b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories_test.go
@@ -30,73 +30,6 @@ import (
        "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
-func TestTryIfModifiedSinceQuery(t *testing.T) {
-
-       type testStruct struct {
-               ifModifiedSince  time.Time
-               setHeader        bool
-               setImsDateHeader bool
-               expected         bool
-       }
-
-       var testData = []testStruct{
-
-               // When header is not set, runSecond must be true
-               {time.Time{}, false, false, true},
-
-               // When header set but header[If-Modified-Since] is not set, 
runSecond must be true
-               {time.Time{}, true, false, true},
-
-               // When header set and header[If-Modified-Since] is set, but 
incorrect time is given then runSecond must be true
-               {time.Time{}, true, true, true},
-
-               // When header set and header[If-Modified-Since] is set with 
correct time, and If-Modified_since < Max(last_Updated) then runSecond must be 
false
-               {time.Now().AddDate(0, 00, 01), true, true, false},
-
-               // When header set and header[If-Modified-Since] is set with 
correct time, and If-Modified_since > Max(last_Updated) then runSecond must be 
true
-               {time.Now().AddDate(0, 00, -01), true, true, true},
-       }
-
-       var header http.Header
-       lastUpdated := time.Now()
-       for i, _ := range testData {
-
-               mockDB, mock, err := sqlmock.New()
-               if err != nil {
-                       t.Fatalf("an error '%v' was not expected when opening a 
stub database connection", err)
-               }
-               defer mockDB.Close()
-
-               db := sqlx.NewDb(mockDB, "sqlmock")
-               defer db.Close()
-
-               if testData[i].setHeader {
-                       header = make(http.Header)
-               }
-
-               if testData[i].setImsDateHeader {
-                       timeValue := testData[i].ifModifiedSince.Format("Mon, 
02 Jan 2006 15:04:05 MST")
-                       header.Set(rfc.IfModifiedSince, timeValue)
-               }
-
-               mock.ExpectBegin()
-               rows := sqlmock.NewRows([]string{"t"})
-               rows.AddRow(lastUpdated)
-               mock.ExpectQuery("SELECT").WithArgs().WillReturnRows(rows)
-
-               where := ""
-               queryValues := map[string]interface{}{}
-
-               runSecond, _ := TryIfModifiedSinceQuery(header, db.MustBegin(), 
where, queryValues)
-
-               if testData[i].expected != runSecond {
-                       t.Errorf("Expected runSecond result doesn't match, got: 
%t; expected: %t", runSecond, testData[i].expected)
-               }
-
-       }
-
-}
-
 func TestGetServiceCategory(t *testing.T) {
 
        type testStruct struct {

Reply via email to