blob: 68b17a9a06eced55a82b10825ed90c0d39faee8a [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001-- Copyright 2016 The Chromium Authors. All Rights Reserved.
2--
3-- Use of this source code is governed by a BSD-style
4-- license that can be found in the LICENSE file or at
5-- https://developers.google.com/open-source/licenses/bsd
6
7
8DROP PROCEDURE IF EXISTS BackfillLastVisitTimestamp;
9
10delimiter //
11
12CREATE PROCEDURE BackfillLastVisitTimestamp(
13 IN in_now_ts INT, IN in_days_ago INT, IN in_num_days INT)
14BEGIN
15
16 DECLARE done INT DEFAULT FALSE;
17
18 DECLARE c_user_id INT;
19 DECLARE c_comment_ts INT;
20
21 DECLARE curs CURSOR FOR
22 SELECT MAX(created), commenter_id FROM Comment
23 WHERE created >= in_now_ts - 60 * 60 * 24 * in_days_ago
24 AND created < in_now_ts - 60 * 60 * 24 * (in_days_ago - in_num_days)
25 GROUP BY commenter_id
26 ORDER BY MAX(created);
27
28 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
29 OPEN curs;
30
31 user_loop: LOOP
32 FETCH curs INTO c_comment_ts, c_user_id;
33 IF done THEN
34 LEAVE user_loop;
35 END IF;
36
37 -- Indicate progress.
38 SELECT c_comment_ts AS 'Processing:';
39
40 -- Set last_visit_timestamp for one user if not already set.
41 UPDATE User
42 SET last_visit_timestamp = IFNULL(last_visit_timestamp, c_comment_ts)
43 WHERE user_id = c_user_id;
44
45 END LOOP;
46
47END;
48
49
50//
51
52
53delimiter ;
54
55
56-- If run locally do all at once:
57-- CALL BackfillLastVisitTimestamp(1476915669, 180, 180);
58
59-- If run on staging or production, consider the last 180 days
60-- in chunks of 30 days at a time:
61-- CALL BackfillLastVisitTimestamp(1476915669, 30, 30);
62-- CALL BackfillLastVisitTimestamp(1476915669, 60, 30);
63-- CALL BackfillLastVisitTimestamp(1476915669, 90, 30);
64-- CALL BackfillLastVisitTimestamp(1476915669, 120, 30);
65-- CALL BackfillLastVisitTimestamp(1476915669, 150, 30);
66-- CALL BackfillLastVisitTimestamp(1476915669, 180, 30);
67