User:Yapperbot/FRS/Documentation/godoc/frslist
Appearance
package frslist // import "yapperbot-frs/src/frslist" VARIABLES var list map[string][]*FRSUser list is the overall list of FRSUsers mapped to their headers. var listHeaders []string listHeaders is just a boring old list of headers, we have a getter for it later. It's used to keep track of which headers we have. var listParserRegex *regexp.Regexp listParserRegex looks at the Feedback Request Service list, and finds each header and its users. var randomGenerator *rand.Rand randomGenerator is our random number generator for this function, separated so we can separately seed it. var sentCount map[string]map[string]uint16 // {header: {user: count sent}} sentCount maps headers down to users, and then users down to the number of messages they've received this month. var sentCountMux sync.Mutex sentCountMux is a simple mutex to make sure that, if we ever add goroutines, we don't start overwriting SentCount simultaneously. var userParserRegex *regexp.Regexp userParserRegex looks over the contents of a FRS list header, and finds each user within the header. FUNCTIONS func FinishRun(w *mwclient.Client) FinishRun for now just calls saveSentCounts, but could do something else too in future func GetListHeaders() []string GetListHeaders is a simple getter for listHeaders func Populate() Populate sets up the FRSList list as appropriate for the start of the program. func calculateMedian(calculatedWeights []float64) (float64, bool) calculateMedian takes a slice of float64s and returns the median if there is one, and a bool indicating if a median could be calculated (i.e. if the given slice has a length greater than zero). func deserializeSentCount(json *jason.Object) (sc map[string]map[string]uint16) deserializeSentCount takes a jason JSON object containing the SentCount.json information, and adds the sent counts into a map, mapping headers to usernames and usernames to numbers of messages sent. It returns this map as a map[string]map[string]uint16. func init() func populateFrsList() string populateFrsList fetches the wikitext of the FRS subscriptions page, and processes the page against the listParserRegex and userParserRegex. Together, those parse the headers in the file, along with the users that are subscribed, turning them into FRSUser objects and storing them in `list`. func populateSentCount() populateSentCount fetches the SentCount page, and checks it's of the right month. If it's a previous month, then it just leaves the `sentCount` map blank; if it's the same month listed on the JSON file, it will parse the JSON and load it into `sentCount`. func saveSentCounts(w *mwclient.Client) saveSentCounts serializes our `sentCount` map into JSON, so we can save it on-wiki and load it again when we need to for the next run. TYPES type FRSUser struct { Username string Header string Limit uint16 Limited bool } An FRSUser is a struct representing a user who has signed up for the FRS. A single username may have multiple FRSUser objects; each corresponds to an individual subscription. func GetUsersFromHeaders(headers []string, allHeader string, n int) (returnedUsers []*FRSUser) GetUsersFromHeaders takes a list of headers and an integer number of users n, and returns a randomly selected portion of the users from the headers, with a total size of maximum n. It won't pick the same user twice, and weights the users based on how far through their limit they are, in an attempt to spread things out a bit. It may pick less than n if there are less users available. func (f FRSUser) ExceedsLimit() bool ExceedsLimit is a simple helper function for checking if a user is limited, and if they are, whether they can be messaged according to their limits. func (f FRSUser) GetCount() uint16 GetCount takes a header and gets the number of messages sent for that header this month. func (f FRSUser) MarkMessageSent() MarkMessageSent takes a header and increases the number of messages sent for that header by one. type frsWeightedUser struct { *FRSUser // weight will represent our probability for this user to be selected weight float64 // _hasAllHeaderChecked is a simple boolean check to make sure we don't halve the probability twice _hasAllHeaderChecked bool } frsWeightedUser extends FRSUser to add a weighting component. It's only used within frslist. func (u *frsWeightedUser) checkWeightForAllHeader(allHeader string) checkWeightForAllHeader takes a string representation of the "All [type]s" header, and checks whether the frsWeightedUser is contained within that header. If it is, it will halve the user's weighting, to encourage users subscribed to specific categories to be selected.