Commit a351b224 authored by Scott's avatar Scott
Browse files

Bug 1671670 - Update story sort to consider priority prop. r=gvn

parent 594daa7b
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -962,6 +962,36 @@ this.DiscoveryStreamFeed = class DiscoveryStreamFeed {
    }
  }

  /*
   * This function is used to sort any type of story, both spocs and recs.
   * This uses hierarchical sorting, first sorting by priority, then by score within a priority.
   * This function could be sorting an array of spocs or an array of recs.
   * A rec would have priority undefined, and a spoc would probably have a priority set.
   * Priority is sorted ascending, so low numbers are the highest priority.
   * Score is sorted descending, so high numbers are the highest score.
   * Undefined priority values are considered the lowest priority.
   * A negative priority is considered the same as undefined, lowest priority.
   * A negative priority is unlikely and not currently supported or expected.
   * A negative score is a possible use case.
   */
  sortItem(a, b) {
    // If the priorities are the same, sort based on score.
    // If both item priorities are undefined,
    // we can safely sort via score.
    if (a.priority === b.priority) {
      return b.score - a.score;
    } else if (!a.priority || a.priority <= 0) {
      // If priority is undefined or an unexpected value,
      // consider it lowest priority.
      return 1;
    } else if (!b.priority || b.priority <= 0) {
      // Also consider this case lowest priority.
      return -1;
    }
    // Our primary sort for items with priority.
    return a.priority - b.priority;
  }

  async scoreItems(items, type) {
    const filtered = [];
    const scoreStart = Cu.now();
@@ -988,7 +1018,7 @@ this.DiscoveryStreamFeed = class DiscoveryStreamFeed {
        return false;
      })
      // Sort by highest scores.
      .sort((a, b) => b.score - a.score);
      .sort(this.sortItem);

    if (this.personalized && personalizedByType) {
      this.providerSwitcher.dispatchRelevanceScoreDuration(scoreStart);
+48 −0
Original line number Diff line number Diff line
@@ -1200,6 +1200,54 @@ describe("DiscoveryStreamFeed", () => {
      ]);
    });

    it("should sort based on priority", async () => {
      const { data: result } = await feed.scoreItems([
        { id: 6, flight_id: 6, priority: 2, item_score: 0.7, min_score: 0.1 },
        { id: 2, flight_id: 3, priority: 1, item_score: 0.2, min_score: 0.1 },
        { id: 4, flight_id: 4, item_score: 0.6, min_score: 0.1 },
        { id: 5, flight_id: 5, priority: 2, item_score: 0.8, min_score: 0.1 },
        { id: 3, flight_id: 3, item_score: 0.8, min_score: 0.1 },
        { id: 1, flight_id: 1, priority: 1, item_score: 0.3, min_score: 0.1 },
      ]);

      assert.deepEqual(result, [
        {
          id: 1,
          flight_id: 1,
          priority: 1,
          score: 0.3,
          item_score: 0.3,
          min_score: 0.1,
        },
        {
          id: 2,
          flight_id: 3,
          priority: 1,
          score: 0.2,
          item_score: 0.2,
          min_score: 0.1,
        },
        {
          id: 5,
          flight_id: 5,
          priority: 2,
          score: 0.8,
          item_score: 0.8,
          min_score: 0.1,
        },
        {
          id: 6,
          flight_id: 6,
          priority: 2,
          score: 0.7,
          item_score: 0.7,
          min_score: 0.1,
        },
        { id: 3, flight_id: 3, item_score: 0.8, score: 0.8, min_score: 0.1 },
        { id: 4, flight_id: 4, item_score: 0.6, score: 0.6, min_score: 0.1 },
      ]);
    });

    it("should remove items with scores lower than min_score", async () => {
      const { data: result, filtered } = await feed.scoreItems([
        { id: 2, flight_id: 2, item_score: 0.8, min_score: 0.9 },