Skip to content

Commit

Permalink
lint: Dc > DC
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen McQuay committed Sep 25, 2017
1 parent e4ae355 commit 4a24fd7
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 158 deletions.
30 changes: 15 additions & 15 deletions api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func getQueryFromRequest(r *http.Request) (*eventmaster.Query, error) {
if err := decoder.Decode(&q); err != nil {
query := r.URL.Query()
q.ParentEventId = query["parent_event_id"]
q.Dc = query["dc"]
q.DC = query["dc"]
q.Host = query["host"]
q.TargetHostSet = query["target_host_set"]
q.User = query["user"]
Expand Down Expand Up @@ -124,7 +124,7 @@ type EventResult struct {
EventID string `json:"event_id"`
ParentEventID string `json:"parent_event_id"`
EventTime int64 `json:"event_time"`
Dc string `json:"dc"`
DC string `json:"dc"`
TopicName string `json:"topic_name"`
Tags []string `json:"tag_set"`
Host string `json:"host"`
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *Server) handleGetEvent(w http.ResponseWriter, r *http.Request, _ httpro
EventID: ev.EventID,
ParentEventID: ev.ParentEventID,
EventTime: ev.EventTime,
Dc: s.store.getDcName(ev.DcID),
DC: s.store.getDCName(ev.DCID),
TopicName: s.store.getTopicName(ev.TopicID),
Tags: ev.Tags,
Host: ev.Host,
Expand Down Expand Up @@ -188,7 +188,7 @@ func (s *Server) handleGetEventByID(w http.ResponseWriter, r *http.Request, ps h
EventID: ev.EventID,
ParentEventID: ev.ParentEventID,
EventTime: ev.EventTime,
Dc: s.store.getDcName(ev.DcID),
DC: s.store.getDCName(ev.DCID),
TopicName: s.store.getTopicName(ev.TopicID),
Tags: ev.Tags,
Host: ev.Host,
Expand Down Expand Up @@ -292,8 +292,8 @@ func (s *Server) handleDeleteTopic(w http.ResponseWriter, r *http.Request, ps ht
s.sendResp(w, "", "", r.URL.Path)
}

func (s *Server) handleAddDc(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var dd Dc
func (s *Server) handleAddDC(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var dd DC
defer r.Body.Close()
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -305,8 +305,8 @@ func (s *Server) handleAddDc(w http.ResponseWriter, r *http.Request, _ httproute
s.sendError(w, http.StatusBadRequest, err, "Error JSON decoding body of request", r.URL.Path)
return
}
id, err := s.store.AddDc(&eventmaster.Dc{
DcName: dd.Name,
id, err := s.store.AddDC(&eventmaster.DC{
DCName: dd.Name,
})
if err != nil {
s.sendError(w, http.StatusBadRequest, err, "Error adding dc", r.URL.Path)
Expand All @@ -315,8 +315,8 @@ func (s *Server) handleAddDc(w http.ResponseWriter, r *http.Request, _ httproute
s.sendResp(w, "dc_id", id, r.URL.Path)
}

func (s *Server) handleUpdateDc(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var dd Dc
func (s *Server) handleUpdateDC(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var dd DC
defer r.Body.Close()
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -333,7 +333,7 @@ func (s *Server) handleUpdateDc(w http.ResponseWriter, r *http.Request, ps httpr
s.sendError(w, http.StatusBadRequest, err, "Error updating topic, no topic name provided", r.URL.Path)
return
}
id, err := s.store.UpdateDc(&eventmaster.UpdateDcRequest{
id, err := s.store.UpdateDC(&eventmaster.UpdateDCRequest{
OldName: dcName,
NewName: dd.Name,
})
Expand All @@ -344,9 +344,9 @@ func (s *Server) handleUpdateDc(w http.ResponseWriter, r *http.Request, ps httpr
s.sendResp(w, "dc_id", id, r.URL.Path)
}

func (s *Server) handleGetDc(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
dcSet := make(map[string][]Dc)
dcs, err := s.store.GetDcs()
func (s *Server) handleGetDC(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
dcSet := make(map[string][]DC)
dcs, err := s.store.GetDCs()
if err != nil {
s.sendError(w, http.StatusInternalServerError, err, "Error getting dcs from store", r.URL.Path)
return
Expand Down Expand Up @@ -375,7 +375,7 @@ func (s *Server) handleGitHubEvent(w http.ResponseWriter, r *http.Request, _ htt
}

id, err := s.store.AddEvent(&UnaddedEvent{
Dc: "github",
DC: "github",
Host: "github",
TopicName: "github",
Data: info,
Expand Down
21 changes: 11 additions & 10 deletions data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type DataStore interface {
AddTopic(RawTopic) error
UpdateTopic(RawTopic) error
DeleteTopic(string) error
GetDcs() ([]Dc, error)
AddDc(Dc) error
UpdateDc(string, string) error
GetDCs() ([]DC, error)
AddDC(DC) error
UpdateDC(string, string) error
CloseSession()
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (event *Event) toCassandra() (string, error) {
VALUES (%[1]s, %[5]s, %[8]d, %[12]s);
INSERT INTO event_by_date(event_id, event_time, date)
VALUES (%[1]s, %[8]d, %[12]s);`,
stringify(event.EventID), stringify(event.ParentEventID), stringifyUUID(event.DcID), stringifyUUID(event.TopicID),
stringify(event.EventID), stringify(event.ParentEventID), stringifyUUID(event.DCID), stringifyUUID(event.TopicID),
stringify(strings.ToLower(event.Host)), stringifyArr(event.TargetHosts), stringify(strings.ToLower(event.User)), event.EventTime,
stringifyArr(event.Tags), data, event.ReceivedTime, stringify(date))
userField := ""
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *CassandraStore) FindByID(id string, includeData bool) (*Event, error) {
EventID: eventID,
ParentEventID: parentEventID,
EventTime: eventTime / 1000,
DcID: dcID.String(),
DCID: dcID.String(),
TopicID: topicID.String(),
Tags: tagSet,
Host: host,
Expand Down Expand Up @@ -496,14 +496,14 @@ func (c *CassandraStore) DeleteTopic(id string) error {
id))
}

func (c *CassandraStore) GetDcs() ([]Dc, error) {
func (c *CassandraStore) GetDCs() ([]DC, error) {
scanIter, closeIter := c.session.ExecIterQuery("SELECT dc_id, dc FROM event_dc;")
var id gocql.UUID
var dc string
var dcs []Dc
var dcs []DC
for true {
if scanIter(&id, &dc) {
dcs = append(dcs, Dc{
dcs = append(dcs, DC{
ID: id.String(),
Name: dc,
})
Expand All @@ -517,7 +517,8 @@ func (c *CassandraStore) GetDcs() ([]Dc, error) {
return dcs, nil
}

func (c *CassandraStore) AddDc(dc Dc) error {
// AddDC inserts dc into the event_dc table.
func (c *CassandraStore) AddDC(dc DC) error {
queryStr := fmt.Sprintf(`INSERT INTO event_dc
(dc_id, dc)
VALUES (%[1]s, %[2]s);`,
Expand All @@ -526,7 +527,7 @@ func (c *CassandraStore) AddDc(dc Dc) error {
return c.session.ExecQuery(queryStr)
}

func (c *CassandraStore) UpdateDc(id string, newName string) error {
func (c *CassandraStore) UpdateDC(id string, newName string) error {
queryStr := fmt.Sprintf(`UPDATE event_dc SET dc=%s WHERE dc_id=%s;`,
stringify(newName), id)
return c.session.ExecQuery(queryStr)
Expand Down
70 changes: 35 additions & 35 deletions event_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Event struct {
EventID string `json:"event_id"`
ParentEventID string `json:"parent_event_id"`
EventTime int64 `json:"event_time"`
DcID string `json:"dc_id"`
DCID string `json:"dc_id"`
TopicID string `json:"topic_id"`
Tags []string `json:"tag_set"`
Host string `json:"host"`
Expand All @@ -47,7 +47,7 @@ func (evts Events) Swap(i, j int) {
type UnaddedEvent struct {
ParentEventID string `json:"parent_event_id"`
EventTime int64 `json:"event_time"`
Dc string `json:"dc"`
DC string `json:"dc"`
TopicName string `json:"topic_name"`
Tags []string `json:"tag_set"`
Host string `json:"host"`
Expand All @@ -70,7 +70,7 @@ type Topic struct {
Schema map[string]interface{} `json:"data_schema"`
}

type Dc struct {
type DC struct {
ID string `json:"dc_id"`
Name string `json:"dc_name"`
}
Expand Down Expand Up @@ -139,14 +139,14 @@ func (es *EventStore) getTopicSchemaProperties(id string) map[string]interface{}
return schema
}

func (es *EventStore) getDcId(dc string) string {
func (es *EventStore) getDCID(dc string) string {
es.dcMutex.RLock()
id := es.dcNameToID[strings.ToLower(dc)]
es.dcMutex.RUnlock()
return id
}

func (es *EventStore) getDcName(id string) string {
func (es *EventStore) getDCName(id string) string {
es.dcMutex.RLock()
name := es.dcIDToName[id]
es.dcMutex.RUnlock()
Expand All @@ -170,7 +170,7 @@ func (es *EventStore) insertDefaults(s map[string]interface{}, m map[string]inte

func (es *EventStore) augmentEvent(event *UnaddedEvent) (*Event, error) {
// validate Event
if event.Dc == "" {
if event.DC == "" {
return nil, errors.New("Event missing dc")
} else if event.Host == "" {
return nil, errors.New("Event missing host")
Expand All @@ -182,9 +182,9 @@ func (es *EventStore) augmentEvent(event *UnaddedEvent) (*Event, error) {
event.EventTime = time.Now().Unix()
}

dcID := es.getDcId(strings.ToLower(event.Dc))
dcID := es.getDCID(strings.ToLower(event.DC))
if dcID == "" {
return nil, errors.New(fmt.Sprintf("Dc '%s' does not exist in dc table", strings.ToLower(event.Dc)))
return nil, fmt.Errorf("DC '%s' does not exist in dc table", strings.ToLower(event.DC))
}
topicID := es.getTopicID(strings.ToLower(event.TopicName))
if topicID == "" {
Expand Down Expand Up @@ -224,7 +224,7 @@ func (es *EventStore) augmentEvent(event *UnaddedEvent) (*Event, error) {
EventID: eventID.String(),
ParentEventID: event.ParentEventID,
EventTime: event.EventTime * 1000,
DcID: dcID,
DCID: dcID,
TopicID: topicID,
Tags: event.Tags,
Host: event.Host,
Expand All @@ -247,8 +247,8 @@ func (es *EventStore) Find(q *eventmaster.Query) (Events, error) {
for _, topic := range q.TopicName {
topicIDs = append(topicIDs, es.getTopicID(topic))
}
for _, dc := range q.Dc {
dcIds = append(dcIds, es.getDcId(dc))
for _, dc := range q.DC {
dcIDs = append(dcIDs, es.getDCID(dc))
}
evts, err := es.ds.Find(q, topicIDs, dcIDs)
if err != nil {
Expand Down Expand Up @@ -327,13 +327,13 @@ func (es *EventStore) GetTopics() ([]Topic, error) {
return topics, nil
}

func (es *EventStore) GetDcs() ([]Dc, error) {
func (es *EventStore) GetDCs() ([]DC, error) {
start := time.Now()
defer func() {
eventStoreTimer.WithLabelValues("GetDcs").Observe(trackTime(start))
eventStoreTimer.WithLabelValues("GetDCs").Observe(trackTime(start))
}()

dcs, err := es.ds.GetDcs()
dcs, err := es.ds.GetDCs()
if err != nil {
eventStoreDbErrCounter.WithLabelValues("cassandra", "read").Inc()
return nil, errors.Wrap(err, "Error deleting topic from data source")
Expand Down Expand Up @@ -483,23 +483,23 @@ func (es *EventStore) DeleteTopic(deleteReq *eventmaster.DeleteTopicRequest) err
return nil
}

func (es *EventStore) AddDc(dc *eventmaster.Dc) (string, error) {
func (es *EventStore) AddDC(dc *eventmaster.DC) (string, error) {
start := time.Now()
defer func() {
eventStoreTimer.WithLabelValues("AddDc").Observe(trackTime(start))
eventStoreTimer.WithLabelValues("AddDC").Observe(trackTime(start))
}()

name := strings.ToLower(dc.DcName)
name := strings.ToLower(dc.DCName)
if name == "" {
return "", errors.New("Error adding dc - dc name is empty")
}
id := es.getDcId(name)
id := es.getDCID(name)
if id != "" {
return "", errors.New(fmt.Sprintf("Error adding dc - dc with name %s already exists", dc))
return "", fmt.Errorf("Error adding dc - dc with name %s already exists", dc)
}

id = uuid.NewV4().String()
if err := es.ds.AddDc(Dc{
if err := es.ds.AddDC(DC{
ID: id,
Name: name,
}); err != nil {
Expand All @@ -515,31 +515,31 @@ func (es *EventStore) AddDc(dc *eventmaster.Dc) (string, error) {
return id, nil
}

func (es *EventStore) UpdateDc(updateReq *eventmaster.UpdateDcRequest) (string, error) {
func (es *EventStore) UpdateDC(updateReq *eventmaster.UpdateDCRequest) (string, error) {
start := time.Now()
defer func() {
eventStoreTimer.WithLabelValues("UpdateDc").Observe(trackTime(start))
eventStoreTimer.WithLabelValues("UpdateDC").Observe(trackTime(start))
}()

oldName := updateReq.OldName
newName := updateReq.NewName

if newName == "" {
return "", errors.New("Dc name cannot be empty")
return "", errors.New("DC name cannot be empty")
}
if oldName == newName {
return "", errors.New("There are no changes to be made")
}

id := es.getDcId(newName)
id := es.getDCID(newName)
if id != "" {
return "", errors.New(fmt.Sprintf("Error updating dc - dc with name %s already exists", newName))
}
id = es.getDcId(oldName)
id = es.getDCID(oldName)
if id == "" {
return "", errors.New(fmt.Sprintf("Error updating dc - dc with name %s doesn't exist", oldName))
}
if err := es.ds.UpdateDc(id, newName); err != nil {
if err := es.ds.UpdateDC(id, newName); err != nil {
eventStoreDbErrCounter.WithLabelValues("cassandra", "write").Inc()
return "", errors.Wrap(err, "Error executing update query in data source")
}
Expand All @@ -561,22 +561,22 @@ func (es *EventStore) Update() error {
eventStoreTimer.WithLabelValues("Update").Observe(trackTime(start))
}()

// Update Dc maps
newDcNameToId := make(map[string]string)
newDcIdToName := make(map[string]string)
dcs, err := es.ds.GetDcs()
// Update DC maps
newDCNameToID := make(map[string]string)
newDCIDToName := make(map[string]string)
dcs, err := es.ds.GetDCs()
if err != nil {
eventStoreDbErrCounter.WithLabelValues("cassandra", "read").Inc()
return errors.Wrap(err, "Error closing dc iter")
}
for _, dc := range dcs {
newDcNameToId[dc.Name] = dc.ID
newDcIdToName[dc.ID] = dc.Name
newDCNameToID[dc.Name] = dc.ID
newDCIDToName[dc.ID] = dc.Name
}
if newDcNameToId != nil {
if newDCNameToID != nil {
es.dcMutex.Lock()
es.dcNameToId = newDcNameToId
es.dcIdToName = newDcIdToName
es.dcNameToID = newDCNameToID
es.dcIDToName = newDCIDToName
es.dcMutex.Unlock()
}

Expand Down
Loading

0 comments on commit 4a24fd7

Please sign in to comment.