Skip to content

Instantly share code, notes, and snippets.

@thrawn01
Created November 2, 2016 20:09
Show Gist options
  • Save thrawn01/9ed280975b6f586eb5f69181d484de60 to your computer and use it in GitHub Desktop.
Save thrawn01/9ed280975b6f586eb5f69181d484de60 to your computer and use it in GitHub Desktop.
TAG Aggregation Implementation

TAG Aggregation

  1. scout.Watcher will be notified whenever a tag is used by an event and track each tag in a map[].
  2. Periodically (every 10 seconds) scout.Watcher will iterate over the map[] and call the method WorkerPool.UpdateTags() which will run a job inside a go routine that runs the following
func (t *tagAggregate) Update(id types.LevelID, tag string) {

	// Retrieve the tag entry
	item, err := t.tagger.GetItem(id, tag)
	if err != nil {
		log.Warningf("Update: GetItem(%s, %s) error - %v", id, tag, err)
		return
	}

	// If first-seen is not set
	if item.FirstSeen.IsZero() {
		// Calculate the first seen
		item.FirstSeen, err = t.findFirstSeen(id, tag)
		if err != nil {
			log.Warningf("findFirstSeen(%s, %s) error - %v", id, tag, err)
			return
		}
	}

	// Update the last and last_month entries
	item.LastSeen = time.Now().UTC()

	// Calculate the total count this tag has been seen for the last 30 days
	item.MonthTotal, err = t.calculate30Days(id, tag)
	if err != nil {
		log.Warningf("calculateMonthUsage(%s, %s) error - %v", id, tag, err)
		return
	}

	if err = t.tagger.UpdateItem(id, item); err != nil {
		log.Warningf("UpdateItem(%s, %s) error - %v", id, tag, err)
	}
}
@anton-efimenko
Copy link

anton-efimenko commented Nov 2, 2016

func (t *tagAggregate) Update(id types.LevelID, tag string) error {

    // Retrieve the tag entry
    item, ok := t.tagCache.Get(id, tag);
    if !ok {
        item, err := t.tagger.GetItem(id, tag);
        if err != nil {
            log.Warningf("Update: GetItem(%s, %s) error - %v", id, tag, err);
            return
        }
    }

    // If first-seen is not set
    if item.FirstSeen.IsZero() {
        // Calculate the first seen
        item.FirstSeen, err = t.findFirstSeen(id, tag);
        if err != nil {
            log.Warningf("findFirstSeen(%s, %s) error - %v", id, tag, err);
            // skip, don't need to return here
        }
    }

    // Update the last and last_month entries
    item.LastSeen = time.Now().UTC();

    err = t.tagger.UpdateItem(id, item);
    if err != nil {
        return err
    }
    t.tagCache.Set(id, tag, item);
    return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment